blob: 1e3405768aebee4eff5bfdbd9030802f122b5129 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregore737f502010-08-12 20:07:10 +000014#include "clang/Sema/Sema.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattnere5c5ee12008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremeneka18d7d82009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000039
Chris Lattner6b6b5372008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000044
John McCall183700f2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000046}
47
Daniel Dunbar35682492008-09-26 04:12:28 +000048// FIXME: We should provide an abstraction around a method or function
49// to provide the following bits of information.
50
Nuno Lopesd20254f2009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000052/// type (function or function-typed variable).
53static bool isFunction(const Decl *d) {
54 return getFunctionType(d, false) != NULL;
55}
56
57/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000062}
63
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000064/// isFunctionOrMethodOrBlock - Return true if the given decl has function
65/// type (function or function-typed variable) or an Objective-C
66/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000068 if (isFunctionOrMethod(d))
69 return true;
70 // check for block is more involved.
71 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
72 QualType Ty = V->getType();
73 return Ty->isBlockPointerType();
74 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076}
77
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078/// hasFunctionProto - Return true if the given decl has a argument
79/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000084 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000086 return true;
87 }
88}
89
90/// getFunctionOrMethodNumArgs - Return number of function or method
91/// arguments. It is an error to call this on a K&R function (use
92/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000099}
100
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000106
Chris Lattner89951a82009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000108}
109
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000111 if (const FunctionType *FnTy = getFunctionType(d))
112 return cast<FunctionProtoType>(FnTy)->getResultType();
113 return cast<ObjCMethodDecl>(d)->getResultType();
114}
115
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000121 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000122 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner6b6b5372008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000131
John McCall506b57e2010-05-17 21:00:27 +0000132 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
133 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000134 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000135
John McCall506b57e2010-05-17 21:00:27 +0000136 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000137
Chris Lattner6b6b5372008-06-26 18:38:35 +0000138 // FIXME: Should we walk the chain of classes?
139 return ClsName == &Ctx.Idents.get("NSString") ||
140 ClsName == &Ctx.Idents.get("NSMutableString");
141}
142
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000151
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000152 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000153 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000154 return false;
155
156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
157}
158
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000163// FIXME: All this manual attribute parsing code is gross. At the
164// least add some helper functions to check most argument patterns (#
165// and types of args).
166
Mike Stumpbf916502009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000172 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000173 }
Mike Stumpbf916502009-07-24 19:02:52 +0000174
Chris Lattner6b6b5372008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000176
177 Expr *sizeExpr;
178
179 // Special case where the argument is a template id.
180 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000181 CXXScopeSpec SS;
182 UnqualifiedId id;
183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000185 } else {
186 // check the attribute arguments.
187 if (Attr.getNumArgs() != 1) {
188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
189 return;
190 }
191 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000192 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000193
194 // Instantiate/Install the vector type, and let Sema build the type for us.
195 // This will run the reguired checks.
196 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
197 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000200
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000203 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000204}
205
Chris Lattner803d0802008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpbf916502009-07-24 19:02:52 +0000212
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlssona860e752009-08-08 18:23:56 +0000214 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
216 // If the alignment is less than or equal to 8 bits, the packed attribute
217 // has no effect.
218 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 else
Anders Carlssona860e752009-08-08 18:23:56 +0000223 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000224 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000226}
227
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000228static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpbf916502009-07-24 19:02:52 +0000234
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000235 // The IBAction attributes only apply to instance methods.
236 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
237 if (MD->isInstanceMethod()) {
238 d->addAttr(::new (S.Context) IBActionAttr());
239 return;
240 }
241
242 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
243}
244
245static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
246 // check the attribute arguments.
247 if (Attr.getNumArgs() > 0) {
248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
249 return;
250 }
251
252 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000253 // Objective-C classes.
254 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000255 d->addAttr(::new (S.Context) IBOutletAttr());
256 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000257 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000258
259 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000260}
261
Ted Kremenek857e9182010-05-19 17:38:06 +0000262static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
263 Sema &S) {
264
265 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000266 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
268 return;
269 }
270
271 // The IBOutletCollection attributes only apply to instance variables of
272 // Objective-C classes.
273 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
274 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
275 return;
276 }
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000277 IdentifierInfo *II = Attr.getParameterName();
278 if (!II)
279 II = &S.Context.Idents.get("id");
280 Sema::TypeTy *TypeRep = S.getTypeName(*II, Attr.getLoc(),
281 S.getScopeForContext(d->getDeclContext()->getParent()));
282 if (!TypeRep) {
283 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
284 return;
285 }
286 QualType QT(QualType::getFromOpaquePtr(TypeRep));
287 // Diagnose use of non-object type in iboutletcollection attribute.
288 // FIXME. Gnu attribute extension ignores use of builtin types in
289 // attributes. So, __attribute__((iboutletcollection(char))) will be
290 // treated as __attribute__((iboutletcollection())).
291 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
292 !QT->isObjCObjectType()) {
293 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
294 return;
295 }
296 d->addAttr(::new (S.Context) IBOutletCollectionAttr(QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000297}
298
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000299static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000300 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
301 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000302 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000303 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000304 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000305 return;
306 }
Mike Stumpbf916502009-07-24 19:02:52 +0000307
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000308 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000309
310 // The nonnull attribute only applies to pointers.
311 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000312
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000313 for (AttributeList::arg_iterator I=Attr.arg_begin(),
314 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000315
316
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000317 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000318 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000319 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000320 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
321 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000322 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
323 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000324 return;
325 }
Mike Stumpbf916502009-07-24 19:02:52 +0000326
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000327 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000328
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000329 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000330 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000331 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000332 return;
333 }
Mike Stumpbf916502009-07-24 19:02:52 +0000334
Ted Kremenek465172f2008-07-21 22:09:15 +0000335 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336
337 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000338 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000339 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000340 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000341 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000342 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000343 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000344 }
Mike Stumpbf916502009-07-24 19:02:52 +0000345
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000346 NonNullArgs.push_back(x);
347 }
Mike Stumpbf916502009-07-24 19:02:52 +0000348
349 // If no arguments were specified to __attribute__((nonnull)) then all pointer
350 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000351 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000352 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
353 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000354 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000355 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000356 }
Mike Stumpbf916502009-07-24 19:02:52 +0000357
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000358 if (NonNullArgs.empty()) {
359 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
360 return;
361 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000362 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000363
364 unsigned* start = &NonNullArgs[0];
365 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000366 llvm::array_pod_sort(start, start + size);
Ted Kremenek59616112010-02-11 07:31:47 +0000367 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000368}
369
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000370static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
371 // This attribute must be applied to a function declaration.
372 // The first argument to the attribute must be a string,
373 // the name of the resource, for example "malloc".
374 // The following arguments must be argument indexes, the arguments must be
375 // of integer type for Returns, otherwise of pointer type.
376 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000377 // after being held. free() should be __attribute((ownership_takes)), whereas
378 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000379
380 if (!AL.getParameterName()) {
381 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
382 << AL.getName()->getName() << 1;
383 return;
384 }
385 // Figure out our Kind, and check arguments while we're at it.
Jordy Rose2a479922010-08-12 08:54:03 +0000386 attr::Kind K;
387 switch (AL.getKind()) {
388 case AttributeList::AT_ownership_takes:
389 K = attr::OwnershipTakes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000390 if (AL.getNumArgs() < 1) {
391 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
392 return;
393 }
Jordy Rose2a479922010-08-12 08:54:03 +0000394 break;
395 case AttributeList::AT_ownership_holds:
396 K = attr::OwnershipHolds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000397 if (AL.getNumArgs() < 1) {
398 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
399 return;
400 }
Jordy Rose2a479922010-08-12 08:54:03 +0000401 break;
402 case AttributeList::AT_ownership_returns:
403 K = attr::OwnershipReturns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000404 if (AL.getNumArgs() > 1) {
405 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
406 << AL.getNumArgs() + 1;
407 return;
408 }
Jordy Rose2a479922010-08-12 08:54:03 +0000409 break;
410 default:
411 // This should never happen given how we are called.
412 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000413 }
414
415 if (!isFunction(d) || !hasFunctionProto(d)) {
416 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
417 << 0 /*function*/;
418 return;
419 }
420
421 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
422
423 llvm::StringRef Module = AL.getParameterName()->getName();
424
425 // Normalize the argument, __foo__ becomes foo.
426 if (Module.startswith("__") && Module.endswith("__"))
427 Module = Module.substr(2, Module.size() - 4);
428
429 llvm::SmallVector<unsigned, 10> OwnershipArgs;
430
Jordy Rose2a479922010-08-12 08:54:03 +0000431 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
432 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000433
434 Expr *IdxExpr = static_cast<Expr *>(*I);
435 llvm::APSInt ArgNum(32);
436 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
437 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
438 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
439 << AL.getName()->getName() << IdxExpr->getSourceRange();
440 continue;
441 }
442
443 unsigned x = (unsigned) ArgNum.getZExtValue();
444
445 if (x > NumArgs || x < 1) {
446 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
447 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
448 continue;
449 }
450 --x;
451 switch (K) {
Jordy Rose2a479922010-08-12 08:54:03 +0000452 case attr::OwnershipTakes:
453 case attr::OwnershipHolds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000454 // Is the function argument a pointer type?
455 QualType T = getFunctionOrMethodArgType(d, x);
456 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
457 // FIXME: Should also highlight argument in decl.
458 S.Diag(AL.getLoc(), diag::err_ownership_type)
Jordy Rose2a479922010-08-12 08:54:03 +0000459 << ((K==attr::OwnershipTakes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000460 << "pointer"
461 << IdxExpr->getSourceRange();
462 continue;
463 }
464 break;
465 }
Jordy Rose2a479922010-08-12 08:54:03 +0000466 case attr::OwnershipReturns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000467 if (AL.getNumArgs() > 1) {
468 // Is the function argument an integer type?
469 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
470 llvm::APSInt ArgNum(32);
471 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
472 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
473 S.Diag(AL.getLoc(), diag::err_ownership_type)
474 << "ownership_returns" << "integer"
475 << IdxExpr->getSourceRange();
476 return;
477 }
478 }
479 break;
480 }
Jordy Rose2a479922010-08-12 08:54:03 +0000481 default:
482 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000483 } // switch
484
485 // Check we don't have a conflict with another ownership attribute.
Jordy Rose2a479922010-08-12 08:54:03 +0000486 if (K != attr::OwnershipReturns && d->hasAttrs()) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000487 for (const Attr *attr = d->getAttrs(); attr; attr = attr->getNext()) {
488 if (const OwnershipAttr* Att = dyn_cast<OwnershipAttr>(attr)) {
489 // Two ownership attributes of the same kind can't conflict,
490 // except returns attributes.
Jordy Rose2a479922010-08-12 08:54:03 +0000491 if (Att->getKind() != K) {
492 for (const unsigned *I = Att->begin(), *E = Att->end(); I!=E; ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000493 if (x == *I) {
494 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
495 << AL.getName()->getName() << "ownership_*";
496 }
497 }
498 }
499 }
500 }
501 }
502 OwnershipArgs.push_back(x);
503 }
504
505 unsigned* start = OwnershipArgs.data();
506 unsigned size = OwnershipArgs.size();
507 llvm::array_pod_sort(start, start + size);
508 switch (K) {
Jordy Rose2a479922010-08-12 08:54:03 +0000509 case attr::OwnershipTakes: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000510 if (OwnershipArgs.empty()) {
511 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
512 return;
513 }
514 d->addAttr(::new (S.Context) OwnershipTakesAttr(S.Context, start, size,
515 Module));
516 break;
517 }
Jordy Rose2a479922010-08-12 08:54:03 +0000518 case attr::OwnershipHolds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000519 if (OwnershipArgs.empty()) {
520 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
521 return;
522 }
523 d->addAttr(::new (S.Context) OwnershipHoldsAttr(S.Context, start, size,
524 Module));
525 break;
526 }
Jordy Rose2a479922010-08-12 08:54:03 +0000527 case attr::OwnershipReturns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000528 d->addAttr(::new (S.Context) OwnershipReturnsAttr(S.Context, start, size,
529 Module));
530 break;
531 }
532 default:
Jordy Rose2a479922010-08-12 08:54:03 +0000533 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000534 }
535}
536
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000537static bool isStaticVarOrStaticFunciton(Decl *D) {
538 if (VarDecl *VD = dyn_cast<VarDecl>(D))
539 return VD->getStorageClass() == VarDecl::Static;
540 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
541 return FD->getStorageClass() == FunctionDecl::Static;
542 return false;
543}
544
545static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
546 // Check the attribute arguments.
547 if (Attr.getNumArgs() > 1) {
548 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
549 return;
550 }
551
552 // gcc rejects
553 // class c {
554 // static int a __attribute__((weakref ("v2")));
555 // static int b() __attribute__((weakref ("f3")));
556 // };
557 // and ignores the attributes of
558 // void f(void) {
559 // static int a __attribute__((weakref ("v2")));
560 // }
561 // we reject them
562 if (const DeclContext *Ctx = d->getDeclContext()) {
563 Ctx = Ctx->getLookupContext();
564 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
565 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000566 dyn_cast<NamedDecl>(d)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000567 return;
568 }
569 }
570
571 // The GCC manual says
572 //
573 // At present, a declaration to which `weakref' is attached can only
574 // be `static'.
575 //
576 // It also says
577 //
578 // Without a TARGET,
579 // given as an argument to `weakref' or to `alias', `weakref' is
580 // equivalent to `weak'.
581 //
582 // gcc 4.4.1 will accept
583 // int a7 __attribute__((weakref));
584 // as
585 // int a7 __attribute__((weak));
586 // This looks like a bug in gcc. We reject that for now. We should revisit
587 // it if this behaviour is actually used.
588
589 if (!isStaticVarOrStaticFunciton(d)) {
590 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
591 dyn_cast<NamedDecl>(d)->getNameAsString();
592 return;
593 }
594
595 // GCC rejects
596 // static ((alias ("y"), weakref)).
597 // Should we? How to check that weakref is before or after alias?
598
599 if (Attr.getNumArgs() == 1) {
600 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
601 Arg = Arg->IgnoreParenCasts();
602 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
603
604 if (Str == 0 || Str->isWide()) {
605 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
606 << "weakref" << 1;
607 return;
608 }
609 // GCC will accept anything as the argument of weakref. Should we
610 // check for an existing decl?
611 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
612 }
613
614 d->addAttr(::new (S.Context) WeakRefAttr());
615}
616
Chris Lattner803d0802008-06-29 00:43:07 +0000617static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000618 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000619 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000621 return;
622 }
Mike Stumpbf916502009-07-24 19:02:52 +0000623
Chris Lattner545dd342008-06-28 23:36:30 +0000624 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000625 Arg = Arg->IgnoreParenCasts();
626 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000627
Chris Lattner6b6b5372008-06-26 18:38:35 +0000628 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000629 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000630 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000631 return;
632 }
Mike Stumpbf916502009-07-24 19:02:52 +0000633
Chris Lattner6b6b5372008-06-26 18:38:35 +0000634 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000635
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000636 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000637}
638
Mike Stumpbf916502009-07-24 19:02:52 +0000639static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000640 Sema &S) {
641 // check the attribute arguments.
642 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000644 return;
645 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000646
Chris Lattnerc5197432009-04-14 17:02:11 +0000647 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000648 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000649 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000650 return;
651 }
Mike Stumpbf916502009-07-24 19:02:52 +0000652
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000653 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000654}
655
Ryan Flynn76168e22009-08-09 20:07:29 +0000656static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
657 // check the attribute arguments.
658 if (Attr.getNumArgs() != 0) {
659 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
660 return;
661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000663 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000664 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000665 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
666 d->addAttr(::new (S.Context) MallocAttr());
667 return;
668 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000669 }
670
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000671 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000672}
673
Ted Kremenekb7252322009-04-10 00:01:14 +0000674static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Abramo Bagnarae215f722010-04-30 13:10:51 +0000675 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000676 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000677 if (Attr.getNumArgs() != 0) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000678 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000679 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000680 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000681
Mike Stump19c30c02009-04-29 19:03:13 +0000682 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
683 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000684 if (VD == 0 || (!VD->getType()->isBlockPointerType()
685 && !VD->getType()->isFunctionPointerType())) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000686 S.Diag(Attr.getLoc(),
687 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
688 : diag::warn_attribute_wrong_decl_type)
689 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000690 return false;
691 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000692 }
Mike Stumpbf916502009-07-24 19:02:52 +0000693
Ted Kremenekb7252322009-04-10 00:01:14 +0000694 return true;
695}
696
697static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000698 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
699 assert(Attr.isInvalid() == false);
700 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000701}
702
703static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
704 Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000705 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000706 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000707}
708
John Thompson35cc9622010-08-09 21:53:52 +0000709// PS3 PPU-specific.
710static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
711 Sema &S) {
712/*
713 Returning a Vector Class in Registers
714
715 According to the PPU ABI specifications, a class with a single member of vector type is returned in
716 memory when used as the return value of a function. This results in inefficient code when implementing
717 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
718 definition. This attribute is also applicable to struct types.
719
720 Example:
721
722 struct Vector
723 {
724 __vector float xyzw;
725 } __attribute__((vecreturn));
726
727 Vector Add(Vector lhs, Vector rhs)
728 {
729 Vector result;
730 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
731 return result; // This will be returned in a register
732 }
733*/
734 if (!isa<CXXRecordDecl>(d)) {
735 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
736 << Attr.getName() << 9 /*class*/;
737 return;
738 }
739
740 if (d->getAttr<VecReturnAttr>()) {
741 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
742 return;
743 }
744
745 d->addAttr(::new (S.Context) VecReturnAttr());
746}
747
Sean Huntbbd37c62009-11-21 08:43:09 +0000748static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
749 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
750 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000751 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000752 return;
753 }
754 // FIXME: Actually store the attribute on the declaration
755}
756
Ted Kremenek73798892008-07-25 04:39:19 +0000757static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
758 // check the attribute arguments.
759 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000760 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000761 return;
762 }
Mike Stumpbf916502009-07-24 19:02:52 +0000763
John McCallaec58602010-03-31 02:47:45 +0000764 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
765 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000766 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000767 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000768 return;
769 }
Mike Stumpbf916502009-07-24 19:02:52 +0000770
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000771 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000772}
773
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000774static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
775 // check the attribute arguments.
776 if (Attr.getNumArgs() != 0) {
777 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
778 return;
779 }
Mike Stumpbf916502009-07-24 19:02:52 +0000780
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000781 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000782 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000783 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
784 return;
785 }
786 } else if (!isFunctionOrMethod(d)) {
787 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000788 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000789 return;
790 }
Mike Stumpbf916502009-07-24 19:02:52 +0000791
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000792 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000793}
794
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000795static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
796 // check the attribute arguments.
797 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000798 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
799 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000800 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000801 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000802
803 int priority = 65535; // FIXME: Do not hardcode such constants.
804 if (Attr.getNumArgs() > 0) {
805 Expr *E = static_cast<Expr *>(Attr.getArg(0));
806 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000807 if (E->isTypeDependent() || E->isValueDependent() ||
808 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000809 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000810 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000811 return;
812 }
813 priority = Idx.getZExtValue();
814 }
Mike Stumpbf916502009-07-24 19:02:52 +0000815
Chris Lattnerc5197432009-04-14 17:02:11 +0000816 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000817 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000818 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000819 return;
820 }
821
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000822 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000823}
824
825static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
826 // check the attribute arguments.
827 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000828 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
829 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000830 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000831 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000832
833 int priority = 65535; // FIXME: Do not hardcode such constants.
834 if (Attr.getNumArgs() > 0) {
835 Expr *E = static_cast<Expr *>(Attr.getArg(0));
836 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000837 if (E->isTypeDependent() || E->isValueDependent() ||
838 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000839 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000840 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000841 return;
842 }
843 priority = Idx.getZExtValue();
844 }
Mike Stumpbf916502009-07-24 19:02:52 +0000845
Anders Carlsson6782fc62008-08-22 22:10:48 +0000846 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000847 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000848 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000849 return;
850 }
851
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000852 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000853}
854
Chris Lattner803d0802008-06-29 00:43:07 +0000855static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000856 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000857 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000858 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000859 return;
860 }
Mike Stumpbf916502009-07-24 19:02:52 +0000861
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000862 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000863}
864
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000865static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
866 // check the attribute arguments.
867 if (Attr.getNumArgs() != 0) {
868 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
869 return;
870 }
Mike Stumpbf916502009-07-24 19:02:52 +0000871
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000872 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000873}
874
Chris Lattner803d0802008-06-29 00:43:07 +0000875static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000876 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000877 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000878 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000879 return;
880 }
Mike Stumpbf916502009-07-24 19:02:52 +0000881
Chris Lattner545dd342008-06-28 23:36:30 +0000882 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000883 Arg = Arg->IgnoreParenCasts();
884 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000885
Chris Lattner6b6b5372008-06-26 18:38:35 +0000886 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000887 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000888 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000889 return;
890 }
Mike Stumpbf916502009-07-24 19:02:52 +0000891
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000892 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000893 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000894
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000895 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000896 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000897 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000898 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000899 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000900 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000901 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000902 type = VisibilityAttr::ProtectedVisibility;
903 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000904 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000905 return;
906 }
Mike Stumpbf916502009-07-24 19:02:52 +0000907
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000908 d->addAttr(::new (S.Context) VisibilityAttr(type, false));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000909}
910
Chris Lattner0db29ec2009-02-14 08:09:34 +0000911static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
912 Sema &S) {
913 if (Attr.getNumArgs() != 0) {
914 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
915 return;
916 }
Mike Stumpbf916502009-07-24 19:02:52 +0000917
Chris Lattner0db29ec2009-02-14 08:09:34 +0000918 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
919 if (OCI == 0) {
920 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
921 return;
922 }
Mike Stumpbf916502009-07-24 19:02:52 +0000923
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000924 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000925}
926
927static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000928 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000929 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000930 return;
931 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000932 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000933 QualType T = TD->getUnderlyingType();
934 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000935 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000936 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
937 return;
938 }
939 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000940 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000941}
942
Mike Stumpbf916502009-07-24 19:02:52 +0000943static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000944HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
945 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000947 return;
948 }
949
950 if (!isa<FunctionDecl>(D)) {
951 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
952 return;
953 }
954
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000955 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000956}
957
Steve Naroff9eae5762008-09-18 16:44:58 +0000958static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000959 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000960 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000961 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000962 return;
963 }
Mike Stumpbf916502009-07-24 19:02:52 +0000964
Steve Naroff9eae5762008-09-18 16:44:58 +0000965 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000966 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000967 return;
968 }
Mike Stumpbf916502009-07-24 19:02:52 +0000969
Steve Naroff9eae5762008-09-18 16:44:58 +0000970 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000971 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000972 type = BlocksAttr::ByRef;
973 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000974 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000975 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000976 return;
977 }
Mike Stumpbf916502009-07-24 19:02:52 +0000978
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000979 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000980}
981
Anders Carlsson77091822008-10-05 18:05:59 +0000982static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
983 // check the attribute arguments.
984 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000985 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
986 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000987 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000988 }
989
Anders Carlsson77091822008-10-05 18:05:59 +0000990 int sentinel = 0;
991 if (Attr.getNumArgs() > 0) {
992 Expr *E = static_cast<Expr *>(Attr.getArg(0));
993 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000994 if (E->isTypeDependent() || E->isValueDependent() ||
995 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000997 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000998 return;
999 }
1000 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001001
Anders Carlsson77091822008-10-05 18:05:59 +00001002 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001003 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1004 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001005 return;
1006 }
1007 }
1008
1009 int nullPos = 0;
1010 if (Attr.getNumArgs() > 1) {
1011 Expr *E = static_cast<Expr *>(Attr.getArg(1));
1012 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001013 if (E->isTypeDependent() || E->isValueDependent() ||
1014 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001015 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001016 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001017 return;
1018 }
1019 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001020
Anders Carlsson77091822008-10-05 18:05:59 +00001021 if (nullPos > 1 || nullPos < 0) {
1022 // FIXME: This error message could be improved, it would be nice
1023 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001024 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1025 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001026 return;
1027 }
1028 }
1029
1030 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001031 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001032 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001033
Chris Lattner897cd902009-03-17 23:03:47 +00001034 if (isa<FunctionNoProtoType>(FT)) {
1035 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1036 return;
1037 }
Mike Stumpbf916502009-07-24 19:02:52 +00001038
Chris Lattner897cd902009-03-17 23:03:47 +00001039 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001040 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001041 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001042 }
Anders Carlsson77091822008-10-05 18:05:59 +00001043 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1044 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001045 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001046 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001047 }
1048 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001049 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1050 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001051 ;
1052 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001053 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001054 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001055 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +00001056 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001057 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001058 int m = Ty->isFunctionPointerType() ? 0 : 1;
1059 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001060 return;
1061 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001062 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001063 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001064 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001065 return;
1066 }
Anders Carlsson77091822008-10-05 18:05:59 +00001067 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001068 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001069 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001070 return;
1071 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001072 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001073}
1074
Chris Lattner026dc962009-02-14 07:37:35 +00001075static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1076 // check the attribute arguments.
1077 if (Attr.getNumArgs() != 0) {
1078 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1079 return;
1080 }
1081
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001082 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001083 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001084 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001085 return;
1086 }
Mike Stumpbf916502009-07-24 19:02:52 +00001087
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001088 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1089 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1090 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001091 return;
1092 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001093 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1094 if (MD->getResultType()->isVoidType()) {
1095 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1096 << Attr.getName() << 1;
1097 return;
1098 }
1099
Nuno Lopesd20254f2009-12-20 23:11:08 +00001100 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +00001101}
1102
1103static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001104 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001105 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001106 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001107 return;
1108 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001109
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001110 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001111 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001112 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1113 dyn_cast<NamedDecl>(D)->getNameAsString();
1114 return;
1115 }
1116
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001117 // TODO: could also be applied to methods?
1118 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1119 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001120 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001121 return;
1122 }
Mike Stumpbf916502009-07-24 19:02:52 +00001123
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001124 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001125}
1126
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001127static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1128 // check the attribute arguments.
1129 if (Attr.getNumArgs() != 0) {
1130 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1131 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001132 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001133
1134 // weak_import only applies to variable & function declarations.
1135 bool isDef = false;
1136 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1137 isDef = (!VD->hasExternalStorage() || VD->getInit());
1138 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001139 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001140 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1141 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001142 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001143 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001144 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001145 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001146 !isa<ObjCInterfaceDecl>(D))
1147 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001148 << Attr.getName() << 2 /*variable and function*/;
1149 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001150 }
1151
1152 // Merge should handle any subsequent violations.
1153 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001154 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001155 diag::warn_attribute_weak_import_invalid_on_definition)
1156 << "weak_import" << 2 /*variable and function*/;
1157 return;
1158 }
1159
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001160 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001161}
1162
Nate Begeman6f3d8382009-06-26 06:32:41 +00001163static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1164 Sema &S) {
1165 // Attribute has 3 arguments.
1166 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001167 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001168 return;
1169 }
1170
1171 unsigned WGSize[3];
1172 for (unsigned i = 0; i < 3; ++i) {
1173 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1174 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001175 if (E->isTypeDependent() || E->isValueDependent() ||
1176 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001177 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1178 << "reqd_work_group_size" << E->getSourceRange();
1179 return;
1180 }
1181 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1182 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001183 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001184 WGSize[2]));
1185}
1186
Chris Lattner026dc962009-02-14 07:37:35 +00001187static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001188 // Attribute has no arguments.
1189 if (Attr.getNumArgs() != 1) {
1190 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1191 return;
1192 }
1193
1194 // Make sure that there is a string literal as the sections's single
1195 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001196 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1197 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001198 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001199 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001200 return;
1201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Chris Lattner797c3c42009-08-10 19:03:04 +00001203 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001204 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001205 if (!Error.empty()) {
1206 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1207 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001208 return;
1209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001211 // This attribute cannot be applied to local variables.
1212 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1213 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1214 return;
1215 }
1216
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001217 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001218}
1219
Chris Lattner6b6b5372008-06-26 18:38:35 +00001220
Chris Lattner803d0802008-06-29 00:43:07 +00001221static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001222 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001223 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001224 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001225 return;
1226 }
Mike Stumpbf916502009-07-24 19:02:52 +00001227
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001228 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001229}
1230
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001231static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1232 // check the attribute arguments.
1233 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001234 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001235 return;
1236 }
Mike Stumpbf916502009-07-24 19:02:52 +00001237
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001238 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001239}
1240
1241static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1242 // check the attribute arguments.
1243 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001244 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001245 return;
1246 }
Mike Stumpbf916502009-07-24 19:02:52 +00001247
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001248 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001249}
1250
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001251static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001252 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001253 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1254 return;
1255 }
Mike Stumpbf916502009-07-24 19:02:52 +00001256
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001257 if (Attr.getNumArgs() != 0) {
1258 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1259 return;
1260 }
Mike Stumpbf916502009-07-24 19:02:52 +00001261
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001262 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001263
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001264 if (!VD || !VD->hasLocalStorage()) {
1265 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1266 return;
1267 }
Mike Stumpbf916502009-07-24 19:02:52 +00001268
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001269 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001270 // FIXME: Lookup probably isn't looking in the right place
1271 // FIXME: The lookup source location should be in the attribute, not the
1272 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001273 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001274 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001275 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001276 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001277 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001278 Attr.getParameterName();
1279 return;
1280 }
Mike Stumpbf916502009-07-24 19:02:52 +00001281
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001282 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1283 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001284 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001285 Attr.getParameterName();
1286 return;
1287 }
1288
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001289 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001290 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001291 Attr.getParameterName();
1292 return;
1293 }
Mike Stumpbf916502009-07-24 19:02:52 +00001294
Anders Carlsson89941c12009-02-07 23:16:50 +00001295 // We're currently more strict than GCC about what function types we accept.
1296 // If this ever proves to be a problem it should be easy to fix.
1297 QualType Ty = S.Context.getPointerType(VD->getType());
1298 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001299 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001300 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001301 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1302 Attr.getParameterName() << ParamTy << Ty;
1303 return;
1304 }
Mike Stumpbf916502009-07-24 19:02:52 +00001305
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001306 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001307}
1308
Mike Stumpbf916502009-07-24 19:02:52 +00001309/// Handle __attribute__((format_arg((idx)))) attribute based on
1310/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1311static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001312 if (Attr.getNumArgs() != 1) {
1313 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1314 return;
1315 }
1316 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1317 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1318 << Attr.getName() << 0 /*function*/;
1319 return;
1320 }
Mike Stumpbf916502009-07-24 19:02:52 +00001321 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1322 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001323 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1324 unsigned FirstIdx = 1;
1325 // checks for the 2nd argument
1326 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1327 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001328 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1329 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001330 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1331 << "format" << 2 << IdxExpr->getSourceRange();
1332 return;
1333 }
Mike Stumpbf916502009-07-24 19:02:52 +00001334
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001335 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1336 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1337 << "format" << 2 << IdxExpr->getSourceRange();
1338 return;
1339 }
Mike Stumpbf916502009-07-24 19:02:52 +00001340
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001341 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001342
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001343 // make sure the format string is really a string
1344 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001345
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001346 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1347 if (not_nsstring_type &&
1348 !isCFStringType(Ty, S.Context) &&
1349 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001350 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001351 // FIXME: Should highlight the actual expression that has the wrong type.
1352 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001353 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001354 << IdxExpr->getSourceRange();
1355 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001356 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001357 Ty = getFunctionOrMethodResultType(d);
1358 if (!isNSStringType(Ty, S.Context) &&
1359 !isCFStringType(Ty, S.Context) &&
1360 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001361 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001362 // FIXME: Should highlight the actual expression that has the wrong type.
1363 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001364 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001365 << IdxExpr->getSourceRange();
1366 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001367 }
1368
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001369 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001370}
1371
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001372enum FormatAttrKind {
1373 CFStringFormat,
1374 NSStringFormat,
1375 StrftimeFormat,
1376 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001377 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001378 InvalidFormat
1379};
1380
1381/// getFormatAttrKind - Map from format attribute names to supported format
1382/// types.
1383static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1384 // Check for formats that get handled specially.
1385 if (Format == "NSString")
1386 return NSStringFormat;
1387 if (Format == "CFString")
1388 return CFStringFormat;
1389 if (Format == "strftime")
1390 return StrftimeFormat;
1391
1392 // Otherwise, check for supported formats.
1393 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1394 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1395 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1396 Format == "zcmn_err")
1397 return SupportedFormat;
1398
Duncan Sandsbc525952010-03-23 14:44:19 +00001399 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1400 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001401 return IgnoredFormat;
1402
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001403 return InvalidFormat;
1404}
1405
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001406/// Handle __attribute__((init_priority(priority))) attributes based on
1407/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1408static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1409 Sema &S) {
1410 if (!S.getLangOptions().CPlusPlus) {
1411 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1412 return;
1413 }
1414
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001415 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1416 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1417 Attr.setInvalid();
1418 return;
1419 }
1420 QualType T = dyn_cast<VarDecl>(d)->getType();
1421 if (S.Context.getAsArrayType(T))
1422 T = S.Context.getBaseElementType(T);
1423 if (!T->getAs<RecordType>()) {
1424 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1425 Attr.setInvalid();
1426 return;
1427 }
1428
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001429 if (Attr.getNumArgs() != 1) {
1430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1431 Attr.setInvalid();
1432 return;
1433 }
1434 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001435
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001436 llvm::APSInt priority(32);
1437 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1438 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1439 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1440 << "init_priority" << priorityExpr->getSourceRange();
1441 Attr.setInvalid();
1442 return;
1443 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001444 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001445 if (prioritynum < 101 || prioritynum > 65535) {
1446 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1447 << priorityExpr->getSourceRange();
1448 Attr.setInvalid();
1449 return;
1450 }
1451 d->addAttr(::new (S.Context) InitPriorityAttr(prioritynum));
1452}
1453
Mike Stumpbf916502009-07-24 19:02:52 +00001454/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1455/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001456static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001457
Chris Lattner545dd342008-06-28 23:36:30 +00001458 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001459 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001460 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001461 return;
1462 }
1463
Chris Lattner545dd342008-06-28 23:36:30 +00001464 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001465 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001466 return;
1467 }
1468
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001469 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001470 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001471 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001472 return;
1473 }
1474
Daniel Dunbar35682492008-09-26 04:12:28 +00001475 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001476 unsigned FirstIdx = 1;
1477
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001478 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001479
1480 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001481 if (Format.startswith("__") && Format.endswith("__"))
1482 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001483
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001484 // Check for supported formats.
1485 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001486
1487 if (Kind == IgnoredFormat)
1488 return;
1489
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001490 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001491 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001492 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001493 return;
1494 }
1495
1496 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001497 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001498 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001499 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1500 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001501 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001502 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001503 return;
1504 }
1505
Anders Carlsson4fb77202009-08-25 14:12:34 +00001506 // FIXME: We should handle the implicit 'this' parameter in a more generic
1507 // way that can be used for other arguments.
1508 bool HasImplicitThisParam = false;
1509 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1510 if (MD->isInstance()) {
1511 HasImplicitThisParam = true;
1512 NumArgs++;
1513 }
1514 }
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Chris Lattner6b6b5372008-06-26 18:38:35 +00001516 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001517 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001518 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001519 return;
1520 }
1521
1522 // FIXME: Do we need to bounds check?
1523 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001524
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001525 if (HasImplicitThisParam) {
1526 if (ArgIdx == 0) {
1527 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1528 << "a string type" << IdxExpr->getSourceRange();
1529 return;
1530 }
1531 ArgIdx--;
1532 }
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Chris Lattner6b6b5372008-06-26 18:38:35 +00001534 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001535 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001536
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001537 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001538 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001539 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1540 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001541 return;
1542 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001543 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001544 // FIXME: do we need to check if the type is NSString*? What are the
1545 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001546 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001547 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001548 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1549 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001550 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001551 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001552 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001553 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001554 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001555 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1556 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001557 return;
1558 }
1559
1560 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001561 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001562 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001563 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1564 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001565 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001566 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001567 return;
1568 }
1569
1570 // check if the function is variadic if the 3rd argument non-zero
1571 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001572 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001573 ++NumArgs; // +1 for ...
1574 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001575 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001576 return;
1577 }
1578 }
1579
Chris Lattner3c73c412008-11-19 08:23:25 +00001580 // strftime requires FirstArg to be 0 because it doesn't read from any
1581 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001582 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001583 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001584 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1585 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001586 return;
1587 }
1588 // if 0 it disables parameter checking (to use with e.g. va_list)
1589 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001590 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001591 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001592 return;
1593 }
1594
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001595 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001596 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001597}
1598
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001599static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1600 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001601 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001602 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001603 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001604 return;
1605 }
1606
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001607 // Try to find the underlying union declaration.
1608 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001609 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001610 if (TD && TD->getUnderlyingType()->isUnionType())
1611 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1612 else
1613 RD = dyn_cast<RecordDecl>(d);
1614
1615 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001616 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001617 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001618 return;
1619 }
1620
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001621 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001622 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001623 diag::warn_transparent_union_attribute_not_definition);
1624 return;
1625 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001626
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001627 RecordDecl::field_iterator Field = RD->field_begin(),
1628 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001629 if (Field == FieldEnd) {
1630 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1631 return;
1632 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001633
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001634 FieldDecl *FirstField = *Field;
1635 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001636 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001637 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001638 diag::warn_transparent_union_attribute_floating)
1639 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001640 return;
1641 }
1642
1643 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1644 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1645 for (; Field != FieldEnd; ++Field) {
1646 QualType FieldType = Field->getType();
1647 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1648 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1649 // Warn if we drop the attribute.
1650 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001651 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001652 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001653 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001654 diag::warn_transparent_union_attribute_field_size_align)
1655 << isSize << Field->getDeclName() << FieldBits;
1656 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001657 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001658 diag::note_transparent_union_first_field_size_align)
1659 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001660 return;
1661 }
1662 }
1663
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001664 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001665}
1666
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001667static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001668 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001669 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001670 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001671 return;
1672 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001673 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1674 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001675
Chris Lattner6b6b5372008-06-26 18:38:35 +00001676 // Make sure that there is a string literal as the annotation's single
1677 // argument.
1678 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001679 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001680 return;
1681 }
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001682 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001683}
1684
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001685static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001686 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001687 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001688 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001689 return;
1690 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001691
1692 //FIXME: The C++0x version of this attribute has more limited applicabilty
1693 // than GNU's, and should error out when it is used to specify a
1694 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001695
Chris Lattner545dd342008-06-28 23:36:30 +00001696 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001697 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001698 // (For now we just use 128 bits which is the maximum on X86).
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001699 D->addAttr(::new (S.Context) AlignedAttr(128));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001700 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001701 }
Mike Stumpbf916502009-07-24 19:02:52 +00001702
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001703 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1704}
1705
1706void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1707 if (E->isTypeDependent() || E->isValueDependent()) {
1708 // Save dependent expressions in the AST to be instantiated.
1709 D->addAttr(::new (Context) AlignedAttr(E));
1710 return;
1711 }
1712
Chris Lattner49e2d342008-06-28 23:50:44 +00001713 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001714 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1715 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1716 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001717 return;
1718 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001719 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001720 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1721 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001722 return;
1723 }
1724
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001725 D->addAttr(::new (Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001726}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001727
Mike Stumpbf916502009-07-24 19:02:52 +00001728/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1729/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001730///
Mike Stumpbf916502009-07-24 19:02:52 +00001731/// Despite what would be logical, the mode attribute is a decl attribute, not a
1732/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1733/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001734static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001735 // This attribute isn't documented, but glibc uses it. It changes
1736 // the width of an int or unsigned int to the specified size.
1737
1738 // Check that there aren't any arguments
1739 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001741 return;
1742 }
1743
1744 IdentifierInfo *Name = Attr.getParameterName();
1745 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001746 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001747 return;
1748 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001749
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001750 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001751
1752 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001753 if (Str.startswith("__") && Str.endswith("__"))
1754 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001755
1756 unsigned DestWidth = 0;
1757 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001758 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001759 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001760 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001761 switch (Str[0]) {
1762 case 'Q': DestWidth = 8; break;
1763 case 'H': DestWidth = 16; break;
1764 case 'S': DestWidth = 32; break;
1765 case 'D': DestWidth = 64; break;
1766 case 'X': DestWidth = 96; break;
1767 case 'T': DestWidth = 128; break;
1768 }
1769 if (Str[1] == 'F') {
1770 IntegerMode = false;
1771 } else if (Str[1] == 'C') {
1772 IntegerMode = false;
1773 ComplexMode = true;
1774 } else if (Str[1] != 'I') {
1775 DestWidth = 0;
1776 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001777 break;
1778 case 4:
1779 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1780 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001781 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001782 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001783 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001784 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001785 break;
1786 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001787 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001788 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001789 break;
1790 }
1791
1792 QualType OldTy;
1793 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1794 OldTy = TD->getUnderlyingType();
1795 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1796 OldTy = VD->getType();
1797 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001798 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1799 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001800 return;
1801 }
Eli Friedman73397492009-03-03 06:41:03 +00001802
John McCall183700f2009-09-21 23:43:11 +00001803 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001804 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1805 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001806 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001807 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1808 } else if (ComplexMode) {
1809 if (!OldTy->isComplexType())
1810 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1811 } else {
1812 if (!OldTy->isFloatingType())
1813 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1814 }
1815
Mike Stump390b4cc2009-05-16 07:39:55 +00001816 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1817 // and friends, at least with glibc.
1818 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1819 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001820 // FIXME: Make sure floating-point mappings are accurate
1821 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001822 QualType NewTy;
1823 switch (DestWidth) {
1824 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001825 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001826 return;
1827 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001828 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001829 return;
1830 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001831 if (!IntegerMode) {
1832 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1833 return;
1834 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001835 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001836 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001837 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001838 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001839 break;
1840 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001841 if (!IntegerMode) {
1842 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1843 return;
1844 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001845 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001846 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001847 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001848 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001849 break;
1850 case 32:
1851 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001852 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001853 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001854 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001855 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001856 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001857 break;
1858 case 64:
1859 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001860 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001861 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001862 if (S.Context.Target.getLongWidth() == 64)
1863 NewTy = S.Context.LongTy;
1864 else
1865 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001866 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001867 if (S.Context.Target.getLongWidth() == 64)
1868 NewTy = S.Context.UnsignedLongTy;
1869 else
1870 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001871 break;
Eli Friedman73397492009-03-03 06:41:03 +00001872 case 96:
1873 NewTy = S.Context.LongDoubleTy;
1874 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001875 case 128:
1876 if (!IntegerMode) {
1877 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1878 return;
1879 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001880 if (OldTy->isSignedIntegerType())
1881 NewTy = S.Context.Int128Ty;
1882 else
1883 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001884 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001885 }
1886
Eli Friedman73397492009-03-03 06:41:03 +00001887 if (ComplexMode) {
1888 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001889 }
1890
1891 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001892 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1893 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001894 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001895 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001896 cast<ValueDecl>(D)->setType(NewTy);
1897}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001898
Mike Stump1feade82009-08-26 22:31:08 +00001899static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001900 // check the attribute arguments.
1901 if (Attr.getNumArgs() > 0) {
1902 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1903 return;
1904 }
Anders Carlssone896d982009-02-13 08:11:52 +00001905
Anders Carlsson5bab7882009-02-19 19:16:48 +00001906 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001907 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001908 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001909 return;
1910 }
Mike Stumpbf916502009-07-24 19:02:52 +00001911
Mike Stump1feade82009-08-26 22:31:08 +00001912 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001913}
1914
Mike Stump1feade82009-08-26 22:31:08 +00001915static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001916 // check the attribute arguments.
1917 if (Attr.getNumArgs() != 0) {
1918 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1919 return;
1920 }
Mike Stumpbf916502009-07-24 19:02:52 +00001921
Chris Lattnerc5197432009-04-14 17:02:11 +00001922 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001923 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001924 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001925 return;
1926 }
Mike Stumpbf916502009-07-24 19:02:52 +00001927
Mike Stump1feade82009-08-26 22:31:08 +00001928 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001929}
1930
Chris Lattner7255a2d2010-06-22 00:03:40 +00001931static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1932 Sema &S) {
1933 // check the attribute arguments.
1934 if (Attr.getNumArgs() != 0) {
1935 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1936 return;
1937 }
1938
1939 if (!isa<FunctionDecl>(d)) {
1940 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1941 << Attr.getName() << 0 /*function*/;
1942 return;
1943 }
1944
1945 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr());
1946}
1947
Chris Lattnercf2a7212009-04-20 19:12:28 +00001948static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001949 // check the attribute arguments.
1950 if (Attr.getNumArgs() != 0) {
1951 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1952 return;
1953 }
Mike Stumpbf916502009-07-24 19:02:52 +00001954
Chris Lattnerc5197432009-04-14 17:02:11 +00001955 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1956 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001957 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001958 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001959 return;
1960 }
Mike Stumpbf916502009-07-24 19:02:52 +00001961
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001962 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001963 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001964 return;
1965 }
Mike Stumpbf916502009-07-24 19:02:52 +00001966
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001967 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001968}
1969
Abramo Bagnarae215f722010-04-30 13:10:51 +00001970static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1971 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
1972 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
1973 assert(Attr.isInvalid() == false);
1974
1975 switch (Attr.getKind()) {
1976 case AttributeList::AT_fastcall:
1977 d->addAttr(::new (S.Context) FastCallAttr());
1978 return;
1979 case AttributeList::AT_stdcall:
1980 d->addAttr(::new (S.Context) StdCallAttr());
1981 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001982 case AttributeList::AT_thiscall:
1983 d->addAttr(::new (S.Context) ThisCallAttr());
Abramo Bagnarae215f722010-04-30 13:10:51 +00001984 case AttributeList::AT_cdecl:
1985 d->addAttr(::new (S.Context) CDeclAttr());
1986 return;
1987 default:
1988 llvm_unreachable("unexpected attribute kind");
1989 return;
1990 }
1991}
1992
Fariborz Jahanianee760332009-03-27 18:38:55 +00001993static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1994 // check the attribute arguments.
1995 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001996 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001997 return;
1998 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001999
Fariborz Jahanianee760332009-03-27 18:38:55 +00002000 if (!isFunctionOrMethod(d)) {
2001 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002002 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002003 return;
2004 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002005
2006 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
2007 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002008 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2009 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002010 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2011 << "regparm" << NumParamsExpr->getSourceRange();
2012 return;
2013 }
2014
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002015 if (S.Context.Target.getRegParmMax() == 0) {
2016 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002017 << NumParamsExpr->getSourceRange();
2018 return;
2019 }
2020
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002021 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002022 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2023 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002024 return;
2025 }
2026
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002027 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002028}
2029
Sean Huntbbd37c62009-11-21 08:43:09 +00002030static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2031 // check the attribute arguments.
2032 if (Attr.getNumArgs() != 0) {
2033 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2034 return;
2035 }
2036
2037 if (!isa<CXXRecordDecl>(d)
2038 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2039 S.Diag(Attr.getLoc(),
2040 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2041 : diag::warn_attribute_wrong_decl_type)
2042 << Attr.getName() << 7 /*virtual method or class*/;
2043 return;
2044 }
Sean Hunt7725e672009-11-25 04:20:27 +00002045
2046 // FIXME: Conform to C++0x redeclaration rules.
2047
2048 if (d->getAttr<FinalAttr>()) {
2049 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2050 return;
2051 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002052
2053 d->addAttr(::new (S.Context) FinalAttr());
2054}
2055
Chris Lattner0744e5f2008-06-29 00:23:49 +00002056//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002057// C++0x member checking attributes
2058//===----------------------------------------------------------------------===//
2059
2060static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2061 if (Attr.getNumArgs() != 0) {
2062 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2063 return;
2064 }
2065
2066 if (!isa<CXXRecordDecl>(d)) {
2067 S.Diag(Attr.getLoc(),
2068 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2069 : diag::warn_attribute_wrong_decl_type)
2070 << Attr.getName() << 9 /*class*/;
2071 return;
2072 }
2073
2074 if (d->getAttr<BaseCheckAttr>()) {
2075 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2076 return;
2077 }
2078
2079 d->addAttr(::new (S.Context) BaseCheckAttr());
2080}
2081
2082static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2083 if (Attr.getNumArgs() != 0) {
2084 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2085 return;
2086 }
2087
2088 if (!isa<RecordDecl>(d->getDeclContext())) {
2089 // FIXME: It's not the type that's the problem
2090 S.Diag(Attr.getLoc(),
2091 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2092 : diag::warn_attribute_wrong_decl_type)
2093 << Attr.getName() << 11 /*member*/;
2094 return;
2095 }
2096
2097 // FIXME: Conform to C++0x redeclaration rules.
2098
2099 if (d->getAttr<HidingAttr>()) {
2100 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2101 return;
2102 }
2103
2104 d->addAttr(::new (S.Context) HidingAttr());
2105}
2106
2107static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2108 if (Attr.getNumArgs() != 0) {
2109 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2110 return;
2111 }
2112
2113 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2114 // FIXME: It's not the type that's the problem
2115 S.Diag(Attr.getLoc(),
2116 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2117 : diag::warn_attribute_wrong_decl_type)
2118 << Attr.getName() << 10 /*virtual method*/;
2119 return;
2120 }
2121
2122 // FIXME: Conform to C++0x redeclaration rules.
2123
2124 if (d->getAttr<OverrideAttr>()) {
2125 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2126 return;
2127 }
2128
2129 d->addAttr(::new (S.Context) OverrideAttr());
2130}
2131
2132//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002133// Checker-specific attribute handlers.
2134//===----------------------------------------------------------------------===//
2135
2136static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2137 Sema &S) {
2138
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002139 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002140
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002141 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2142 RetTy = MD->getResultType();
2143 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2144 RetTy = FD->getResultType();
2145 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002146 SourceLocation L = Attr.getLoc();
2147 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2148 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002149 return;
2150 }
Mike Stumpbf916502009-07-24 19:02:52 +00002151
Ted Kremenek6217b802009-07-29 21:53:49 +00002152 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002153 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002154 SourceLocation L = Attr.getLoc();
2155 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2156 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002157 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002158 }
Mike Stumpbf916502009-07-24 19:02:52 +00002159
Ted Kremenekb71368d2009-05-09 02:44:38 +00002160 switch (Attr.getKind()) {
2161 default:
2162 assert(0 && "invalid ownership attribute");
2163 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002164 case AttributeList::AT_cf_returns_not_retained:
2165 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
2166 return;
2167 case AttributeList::AT_ns_returns_not_retained:
2168 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
2169 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002170 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002171 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00002172 return;
2173 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002174 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00002175 return;
2176 };
2177}
2178
Charles Davisf0122fe2010-02-16 18:27:26 +00002179static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2180 return Attr.getKind() == AttributeList::AT_dllimport ||
2181 Attr.getKind() == AttributeList::AT_dllexport;
2182}
2183
Ted Kremenekb71368d2009-05-09 02:44:38 +00002184//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002185// Top Level Sema Entry Points
2186//===----------------------------------------------------------------------===//
2187
Sebastian Redla89d82c2008-12-21 19:24:58 +00002188/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002189/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002190/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2191/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002192static void ProcessDeclAttribute(Scope *scope, Decl *D,
2193 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002194 if (Attr.isInvalid())
2195 return;
2196
Charles Davisf0122fe2010-02-16 18:27:26 +00002197 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2198 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002199 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002200 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002201 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002202 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2203 case AttributeList::AT_IBOutletCollection:
2204 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002205 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002206 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002207 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00002208 // Ignore these, these are type attributes, handled by
2209 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002210 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002211 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2212 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002213 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002214 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002215 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002216 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002217 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2218 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002219 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002220 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002221 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2222 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2223 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002224 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002225 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002226 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002227 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2228 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2229 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2230 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2231 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2232 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2233 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2234 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002235 case AttributeList::AT_ownership_returns:
2236 case AttributeList::AT_ownership_takes:
2237 case AttributeList::AT_ownership_holds:
2238 HandleOwnershipAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002239 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2240 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2241 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002242 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002243
2244 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002245 case AttributeList::AT_ns_returns_not_retained:
2246 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002247 case AttributeList::AT_ns_returns_retained:
2248 case AttributeList::AT_cf_returns_retained:
2249 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2250
Nate Begeman6f3d8382009-06-26 06:32:41 +00002251 case AttributeList::AT_reqd_wg_size:
2252 HandleReqdWorkGroupSize(D, Attr, S); break;
2253
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002254 case AttributeList::AT_init_priority:
2255 HandleInitPriorityAttr(D, Attr, S); break;
2256
Sean Hunt7725e672009-11-25 04:20:27 +00002257 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2258 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002259 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2260 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2261 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002262 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002263 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2264 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002265 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002266 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002267 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002268 case AttributeList::AT_transparent_union:
2269 HandleTransparentUnionAttr(D, Attr, S);
2270 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002271 case AttributeList::AT_objc_exception:
2272 HandleObjCExceptionAttr(D, Attr, S);
2273 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002274 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002275 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2276 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2277 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2278 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2279 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2280 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2281 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2282 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2283 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002284 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002285 // Just ignore
2286 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002287 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2288 HandleNoInstrumentFunctionAttr(D, Attr, S);
2289 break;
John McCall04a67a62010-02-05 21:31:56 +00002290 case AttributeList::AT_stdcall:
2291 case AttributeList::AT_cdecl:
2292 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002293 case AttributeList::AT_thiscall:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002294 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002295 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002296 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002297 // Ask target about the attribute.
2298 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2299 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002300 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2301 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002302 break;
2303 }
2304}
2305
2306/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2307/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002308void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002309 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2310 ProcessDeclAttribute(S, D, *l, *this);
2311 }
2312
2313 // GCC accepts
2314 // static int a9 __attribute__((weakref));
2315 // but that looks really pointless. We reject it.
2316 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2317 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002318 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002319 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002320 }
2321}
2322
Ryan Flynne25ff832009-07-30 03:15:39 +00002323/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2324/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002325NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002326 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002327 NamedDecl *NewD = 0;
2328 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2329 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2330 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002331 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002332 if (FD->getQualifier()) {
2333 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2334 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2335 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002336 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2337 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2338 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002339 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002340 VD->getStorageClass(),
2341 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002342 if (VD->getQualifier()) {
2343 VarDecl *NewVD = cast<VarDecl>(NewD);
2344 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2345 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002346 }
2347 return NewD;
2348}
2349
2350/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2351/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002352void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002353 if (W.getUsed()) return; // only do this once
2354 W.setUsed(true);
2355 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2356 IdentifierInfo *NDId = ND->getIdentifier();
2357 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00002358 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002359 NewD->addAttr(::new (Context) WeakAttr());
2360 WeakTopLevelDecl.push_back(NewD);
2361 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2362 // to insert Decl at TU scope, sorry.
2363 DeclContext *SavedContext = CurContext;
2364 CurContext = Context.getTranslationUnitDecl();
2365 PushOnScopeChains(NewD, S);
2366 CurContext = SavedContext;
2367 } else { // just add weak to existing
2368 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002369 }
2370}
2371
Chris Lattner0744e5f2008-06-29 00:23:49 +00002372/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2373/// it, apply them to D. This is a bit tricky because PD can have attributes
2374/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002375void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002376 // Handle #pragma weak
2377 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2378 if (ND->hasLinkage()) {
2379 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2380 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002381 // Identifier referenced by #pragma weak before it was declared
2382 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002383 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2384 }
2385 }
2386 }
2387
Chris Lattner0744e5f2008-06-29 00:23:49 +00002388 // Apply decl attributes from the DeclSpec if present.
2389 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002390 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002391
Chris Lattner0744e5f2008-06-29 00:23:49 +00002392 // Walk the declarator structure, applying decl attributes that were in a type
2393 // position to the decl itself. This handles cases like:
2394 // int *__attr__(x)** D;
2395 // when X is a decl attribute.
2396 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2397 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002398 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002399
Chris Lattner0744e5f2008-06-29 00:23:49 +00002400 // Finally, apply any attributes on the decl itself.
2401 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002402 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002403}
John McCall54abf7d2009-11-04 02:18:39 +00002404
2405/// PushParsingDeclaration - Enter a new "scope" of deprecation
2406/// warnings.
2407///
2408/// The state token we use is the start index of this scope
2409/// on the warning stack.
2410Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2411 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002412 return (ParsingDeclStackState) DelayedDiagnostics.size();
2413}
2414
2415void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2416 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2417 ParsingDeclDepth--;
2418
2419 if (DelayedDiagnostics.empty())
2420 return;
2421
2422 unsigned SavedIndex = (unsigned) S;
2423 assert(SavedIndex <= DelayedDiagnostics.size() &&
2424 "saved index is out of bounds");
2425
John McCall58e6f342010-03-16 05:22:47 +00002426 unsigned E = DelayedDiagnostics.size();
2427
John McCall2f514482010-01-27 03:50:35 +00002428 // We only want to actually emit delayed diagnostics when we
2429 // successfully parsed a decl.
2430 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2431 if (D) {
2432 // We really do want to start with 0 here. We get one push for a
2433 // decl spec and another for each declarator; in a decl group like:
2434 // deprecated_typedef foo, *bar, baz();
2435 // only the declarator pops will be passed decls. This is correct;
2436 // we really do need to consider delayed diagnostics from the decl spec
2437 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002438 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002439 if (DelayedDiagnostics[I].Triggered)
2440 continue;
2441
2442 switch (DelayedDiagnostics[I].Kind) {
2443 case DelayedDiagnostic::Deprecation:
2444 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2445 break;
2446
2447 case DelayedDiagnostic::Access:
2448 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2449 break;
2450 }
2451 }
2452 }
2453
John McCall58e6f342010-03-16 05:22:47 +00002454 // Destroy all the delayed diagnostics we're about to pop off.
2455 for (unsigned I = SavedIndex; I != E; ++I)
2456 DelayedDiagnostics[I].destroy();
2457
John McCall2f514482010-01-27 03:50:35 +00002458 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002459}
2460
2461static bool isDeclDeprecated(Decl *D) {
2462 do {
2463 if (D->hasAttr<DeprecatedAttr>())
2464 return true;
2465 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2466 return false;
2467}
2468
John McCall2f514482010-01-27 03:50:35 +00002469void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2470 Decl *Ctx) {
2471 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002472 return;
2473
John McCall2f514482010-01-27 03:50:35 +00002474 DD.Triggered = true;
2475 Diag(DD.Loc, diag::warn_deprecated)
2476 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002477}
2478
2479void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2480 // Delay if we're currently parsing a declaration.
2481 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002482 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002483 return;
2484 }
2485
2486 // Otherwise, don't warn if our current context is deprecated.
2487 if (isDeclDeprecated(cast<Decl>(CurContext)))
2488 return;
2489
2490 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2491}