blob: 103976bc12f89126022bf384e1d461a4b8fcac3d [file] [log] [blame]
Chris Lattner2c6fcf52008-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
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
John McCall31168b02011-06-15 23:02:42 +000020#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000021#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000023#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000025using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000026using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000027
John McCall5fca7ea2011-03-02 12:29:23 +000028/// These constants match the enumerated choices of
29/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
30enum {
31 ExpectedFunction,
32 ExpectedUnion,
33 ExpectedVariableOrFunction,
34 ExpectedFunctionOrMethod,
35 ExpectedParameter,
36 ExpectedParameterOrMethod,
37 ExpectedFunctionMethodOrBlock,
38 ExpectedClassOrVirtualMethod,
39 ExpectedFunctionMethodOrParameter,
40 ExpectedClass,
41 ExpectedVirtualMethod,
42 ExpectedClassMember,
43 ExpectedVariable,
44 ExpectedMethod,
45 ExpectedVariableFunctionOrLabel
46};
47
Chris Lattner58418ff2008-06-29 00:16:31 +000048//===----------------------------------------------------------------------===//
49// Helper functions
50//===----------------------------------------------------------------------===//
51
Chandler Carruthff4c4f02011-07-01 23:49:12 +000052static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000053 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000054 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000055 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000056 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000059 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000060 Ty = decl->getUnderlyingType();
61 else
62 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000063
Chris Lattner2c6fcf52008-06-26 18:38:35 +000064 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000065 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000066 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000067 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000068
John McCall9dd450b2009-09-21 23:43:11 +000069 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000070}
71
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000072// FIXME: We should provide an abstraction around a method or function
73// to provide the following bits of information.
74
Nuno Lopes518e3702009-12-20 23:11:08 +000075/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000076/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000077static bool isFunction(const Decl *D) {
78 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000079}
80
81/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000082/// type (function or function-typed variable) or an Objective-C
83/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084static bool isFunctionOrMethod(const Decl *D) {
85 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000086}
87
Fariborz Jahanian4447e172009-05-15 23:15:03 +000088/// isFunctionOrMethodOrBlock - Return true if the given decl has function
89/// type (function or function-typed variable) or an Objective-C
90/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000091static bool isFunctionOrMethodOrBlock(const Decl *D) {
92 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000093 return true;
94 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000095 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000096 QualType Ty = V->getType();
97 return Ty->isBlockPointerType();
98 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +000099 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000100}
101
John McCall3882ace2011-01-05 12:14:39 +0000102/// Return true if the given decl has a declarator that should have
103/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000104static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000105 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000106 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
107 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000108}
109
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000110/// hasFunctionProto - Return true if the given decl has a argument
111/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000112/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000113static bool hasFunctionProto(const Decl *D) {
114 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000115 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000116 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000117 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000118 return true;
119 }
120}
121
122/// getFunctionOrMethodNumArgs - Return number of function or method
123/// arguments. It is an error to call this on a K&R function (use
124/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000125static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000129 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000130 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000131}
132
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000133static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000135 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000137 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000138
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000139 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000140}
141
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000142static QualType getFunctionOrMethodResultType(const Decl *D) {
143 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000144 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000145 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000146}
147
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000148static bool isFunctionOrMethodVariadic(const Decl *D) {
149 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000150 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000151 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000153 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000154 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000156 }
157}
158
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000159static bool isInstanceMethod(const Decl *D) {
160 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000161 return MethodDecl->isInstance();
162 return false;
163}
164
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000165static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000166 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000167 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000168 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000169
John McCall96fa4842010-05-17 21:00:27 +0000170 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
171 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000172 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000173
John McCall96fa4842010-05-17 21:00:27 +0000174 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000175
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000176 // FIXME: Should we walk the chain of classes?
177 return ClsName == &Ctx.Idents.get("NSString") ||
178 ClsName == &Ctx.Idents.get("NSMutableString");
179}
180
Daniel Dunbar980c6692008-09-26 03:32:58 +0000181static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000182 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000183 if (!PT)
184 return false;
185
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000186 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000187 if (!RT)
188 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000189
Daniel Dunbar980c6692008-09-26 03:32:58 +0000190 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000191 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000192 return false;
193
194 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
195}
196
Chris Lattner58418ff2008-06-29 00:16:31 +0000197//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000198// Attribute Implementations
199//===----------------------------------------------------------------------===//
200
Daniel Dunbar032db472008-07-31 22:40:48 +0000201// FIXME: All this manual attribute parsing code is gross. At the
202// least add some helper functions to check most argument patterns (#
203// and types of args).
204
Chandler Carruthedc2c642011-07-02 00:01:44 +0000205static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
206 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000207 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000208 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000209 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000210 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000212
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000213 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000214
215 Expr *sizeExpr;
216
217 // Special case where the argument is a template id.
218 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000219 CXXScopeSpec SS;
220 UnqualifiedId id;
221 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor39c02722011-06-15 16:02:29 +0000222
223 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
224 if (Size.isInvalid())
225 return;
226
227 sizeExpr = Size.get();
Douglas Gregor758a8692009-06-17 21:51:59 +0000228 } else {
229 // check the attribute arguments.
230 if (Attr.getNumArgs() != 1) {
231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
232 return;
233 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000234 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000235 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000236
237 // Instantiate/Install the vector type, and let Sema build the type for us.
238 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000239 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000240 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000241 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000242 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000243
Douglas Gregor758a8692009-06-17 21:51:59 +0000244 // Remember this typedef decl, we will need it later for diagnostics.
245 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000246 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000247}
248
Chandler Carruthedc2c642011-07-02 00:01:44 +0000249static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000250 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000251 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000252 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000253 return;
254 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000255
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000256 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000257 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000258 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000259 // If the alignment is less than or equal to 8 bits, the packed attribute
260 // has no effect.
261 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000262 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000263 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000264 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000265 else
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000266 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000267 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000268 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000269}
270
Chandler Carruthedc2c642011-07-02 00:01:44 +0000271static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000272 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000273 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context));
274 else
275 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
276}
277
Chandler Carruthedc2c642011-07-02 00:01:44 +0000278static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000279 // check the attribute arguments.
280 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000282 return;
283 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000284
Ted Kremenek1f672822010-02-18 03:08:58 +0000285 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000286 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000287 if (MD->isInstanceMethod()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000288 D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000289 return;
290 }
291
Ted Kremenekd68ec812011-02-04 06:54:16 +0000292 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000293}
294
Chandler Carruthedc2c642011-07-02 00:01:44 +0000295static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +0000296 // check the attribute arguments.
297 if (Attr.getNumArgs() > 0) {
298 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
299 return;
300 }
301
302 // The IBOutlet attributes only apply to instance variables of
Ted Kremenek06be9682010-02-17 02:37:45 +0000303 // Objective-C classes.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000304 if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) {
305 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000306 return;
Ted Kremenek06be9682010-02-17 02:37:45 +0000307 }
Ted Kremenek1f672822010-02-18 03:08:58 +0000308
Ted Kremenekd68ec812011-02-04 06:54:16 +0000309 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000310}
311
Chandler Carruthedc2c642011-07-02 00:01:44 +0000312static void handleIBOutletCollection(Sema &S, Decl *D,
313 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000314
315 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000316 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000317 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
318 return;
319 }
320
321 // The IBOutletCollection attributes only apply to instance variables of
322 // Objective-C classes.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000323 if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
Ted Kremenekd68ec812011-02-04 06:54:16 +0000324 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek26bde772010-05-19 17:38:06 +0000325 return;
326 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000327 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000328 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
329 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
330 << VD->getType() << 0;
331 return;
332 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000333 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000334 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
335 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
336 << PD->getType() << 1;
337 return;
338 }
339
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000340 IdentifierInfo *II = Attr.getParameterName();
341 if (!II)
342 II = &S.Context.Idents.get("id");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000343
John McCallba7bf592010-08-24 05:47:05 +0000344 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000345 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000346 if (!TypeRep) {
347 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
348 return;
349 }
John McCallba7bf592010-08-24 05:47:05 +0000350 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000351 // Diagnose use of non-object type in iboutletcollection attribute.
352 // FIXME. Gnu attribute extension ignores use of builtin types in
353 // attributes. So, __attribute__((iboutletcollection(char))) will be
354 // treated as __attribute__((iboutletcollection())).
355 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
356 !QT->isObjCObjectType()) {
357 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
358 return;
359 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000360 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000361 QT));
Ted Kremenek26bde772010-05-19 17:38:06 +0000362}
363
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000364static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000365 if (const RecordType *UT = T->getAsUnionType())
366 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
367 RecordDecl *UD = UT->getDecl();
368 for (RecordDecl::field_iterator it = UD->field_begin(),
369 itend = UD->field_end(); it != itend; ++it) {
370 QualType QT = it->getType();
371 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
372 T = QT;
373 return;
374 }
375 }
376 }
377}
378
Chandler Carruthedc2c642011-07-02 00:01:44 +0000379static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000380 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
381 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000382 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000383 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000384 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000385 return;
386 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000387
Chandler Carruth743682b2010-11-16 08:35:43 +0000388 // In C++ the implicit 'this' function parameter also counts, and they are
389 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000390 bool HasImplicitThisParam = isInstanceMethod(D);
391 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000392
393 // The nonnull attribute only applies to pointers.
394 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000395
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000396 for (AttributeList::arg_iterator I=Attr.arg_begin(),
397 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000398
399
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000400 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000401 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000402 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000403 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
404 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000405 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
406 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000407 return;
408 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000409
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000410 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000411
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000412 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000413 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000414 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000415 return;
416 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000417
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000418 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000419 if (HasImplicitThisParam) {
420 if (x == 0) {
421 S.Diag(Attr.getLoc(),
422 diag::err_attribute_invalid_implicit_this_argument)
423 << "nonnull" << Ex->getSourceRange();
424 return;
425 }
426 --x;
427 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000428
429 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000430 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000431 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000432
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000433 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000434 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000435 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000436 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000437 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000438 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000439
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000440 NonNullArgs.push_back(x);
441 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000442
443 // If no arguments were specified to __attribute__((nonnull)) then all pointer
444 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000445 if (NonNullArgs.empty()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000446 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
447 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000448 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000449 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000450 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000451 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000452
Ted Kremenek22813f42010-10-21 18:49:36 +0000453 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000454 if (NonNullArgs.empty()) {
455 // Warn the trivial case only if attribute is not coming from a
456 // macro instantiation.
457 if (Attr.getLoc().isFileID())
458 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000459 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000460 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000461 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000462
463 unsigned* start = &NonNullArgs[0];
464 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000465 llvm::array_pod_sort(start, start + size);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000466 D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000467 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000468}
469
Chandler Carruthedc2c642011-07-02 00:01:44 +0000470static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000471 // This attribute must be applied to a function declaration.
472 // The first argument to the attribute must be a string,
473 // the name of the resource, for example "malloc".
474 // The following arguments must be argument indexes, the arguments must be
475 // of integer type for Returns, otherwise of pointer type.
476 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000477 // after being held. free() should be __attribute((ownership_takes)), whereas
478 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000479
480 if (!AL.getParameterName()) {
481 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
482 << AL.getName()->getName() << 1;
483 return;
484 }
485 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000486 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000487 switch (AL.getKind()) {
488 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000489 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000490 if (AL.getNumArgs() < 1) {
491 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
492 return;
493 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000494 break;
495 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000496 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000497 if (AL.getNumArgs() < 1) {
498 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
499 return;
500 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000501 break;
502 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000503 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000504 if (AL.getNumArgs() > 1) {
505 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
506 << AL.getNumArgs() + 1;
507 return;
508 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000509 break;
510 default:
511 // This should never happen given how we are called.
512 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000513 }
514
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000515 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +0000516 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
517 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000518 return;
519 }
520
Chandler Carruth743682b2010-11-16 08:35:43 +0000521 // In C++ the implicit 'this' function parameter also counts, and they are
522 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000523 bool HasImplicitThisParam = isInstanceMethod(D);
524 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000525
526 llvm::StringRef Module = AL.getParameterName()->getName();
527
528 // Normalize the argument, __foo__ becomes foo.
529 if (Module.startswith("__") && Module.endswith("__"))
530 Module = Module.substr(2, Module.size() - 4);
531
532 llvm::SmallVector<unsigned, 10> OwnershipArgs;
533
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000534 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
535 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000536
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000537 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000538 llvm::APSInt ArgNum(32);
539 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
540 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
541 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
542 << AL.getName()->getName() << IdxExpr->getSourceRange();
543 continue;
544 }
545
546 unsigned x = (unsigned) ArgNum.getZExtValue();
547
548 if (x > NumArgs || x < 1) {
549 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
550 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
551 continue;
552 }
553 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000554 if (HasImplicitThisParam) {
555 if (x == 0) {
556 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
557 << "ownership" << IdxExpr->getSourceRange();
558 return;
559 }
560 --x;
561 }
562
Ted Kremenekd21139a2010-07-31 01:52:11 +0000563 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000564 case OwnershipAttr::Takes:
565 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000566 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000567 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +0000568 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
569 // FIXME: Should also highlight argument in decl.
570 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000571 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +0000572 << "pointer"
573 << IdxExpr->getSourceRange();
574 continue;
575 }
576 break;
577 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000578 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000579 if (AL.getNumArgs() > 1) {
580 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000581 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +0000582 llvm::APSInt ArgNum(32);
583 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
584 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
585 S.Diag(AL.getLoc(), diag::err_ownership_type)
586 << "ownership_returns" << "integer"
587 << IdxExpr->getSourceRange();
588 return;
589 }
590 }
591 break;
592 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000593 default:
594 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000595 } // switch
596
597 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000598 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000599 i = D->specific_attr_begin<OwnershipAttr>(),
600 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000601 i != e; ++i) {
602 if ((*i)->getOwnKind() != K) {
603 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
604 I!=E; ++I) {
605 if (x == *I) {
606 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
607 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +0000608 }
609 }
610 }
611 }
612 OwnershipArgs.push_back(x);
613 }
614
615 unsigned* start = OwnershipArgs.data();
616 unsigned size = OwnershipArgs.size();
617 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000618
619 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
620 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
621 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000622 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000623
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000624 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000625 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +0000626}
627
John McCall7a198ce2011-02-08 22:35:49 +0000628/// Whether this declaration has internal linkage for the purposes of
629/// things that want to complain about things not have internal linkage.
630static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
631 switch (D->getLinkage()) {
632 case NoLinkage:
633 case InternalLinkage:
634 return true;
635
636 // Template instantiations that go from external to unique-external
637 // shouldn't get diagnosed.
638 case UniqueExternalLinkage:
639 return true;
640
641 case ExternalLinkage:
642 return false;
643 }
644 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +0000645 return false;
646}
647
Chandler Carruthedc2c642011-07-02 00:01:44 +0000648static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +0000649 // Check the attribute arguments.
650 if (Attr.getNumArgs() > 1) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
652 return;
653 }
654
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000655 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +0000656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000657 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +0000658 return;
659 }
660
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000661 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +0000662
Rafael Espindolac18086a2010-02-23 22:00:30 +0000663 // gcc rejects
664 // class c {
665 // static int a __attribute__((weakref ("v2")));
666 // static int b() __attribute__((weakref ("f3")));
667 // };
668 // and ignores the attributes of
669 // void f(void) {
670 // static int a __attribute__((weakref ("v2")));
671 // }
672 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000673 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +0000674 if (!Ctx->isFileContext()) {
675 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +0000676 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +0000677 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000678 }
679
680 // The GCC manual says
681 //
682 // At present, a declaration to which `weakref' is attached can only
683 // be `static'.
684 //
685 // It also says
686 //
687 // Without a TARGET,
688 // given as an argument to `weakref' or to `alias', `weakref' is
689 // equivalent to `weak'.
690 //
691 // gcc 4.4.1 will accept
692 // int a7 __attribute__((weakref));
693 // as
694 // int a7 __attribute__((weak));
695 // This looks like a bug in gcc. We reject that for now. We should revisit
696 // it if this behaviour is actually used.
697
John McCall7a198ce2011-02-08 22:35:49 +0000698 if (!hasEffectivelyInternalLinkage(nd)) {
699 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000700 return;
701 }
702
703 // GCC rejects
704 // static ((alias ("y"), weakref)).
705 // Should we? How to check that weakref is before or after alias?
706
707 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000708 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000709 Arg = Arg->IgnoreParenCasts();
710 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
711
712 if (Str == 0 || Str->isWide()) {
713 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
714 << "weakref" << 1;
715 return;
716 }
717 // GCC will accept anything as the argument of weakref. Should we
718 // check for an existing decl?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000719 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +0000720 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000721 }
722
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000723 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000724}
725
Chandler Carruthedc2c642011-07-02 00:01:44 +0000726static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000727 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000728 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000729 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000730 return;
731 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000732
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000733 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000734 Arg = Arg->IgnoreParenCasts();
735 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000736
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000737 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000738 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000739 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000740 return;
741 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000742
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000743 if (S.Context.Target.getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +0000744 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
745 return;
746 }
747
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000748 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000749
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000750 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +0000751 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000752}
753
Chandler Carruthedc2c642011-07-02 00:01:44 +0000754static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000755 // Check the attribute arguments.
Daniel Dunbar03a38442008-10-28 00:17:57 +0000756 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000757 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000758 return;
759 }
Anders Carlsson88097122009-02-19 19:16:48 +0000760
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000761 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000762 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000763 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000764 return;
765 }
766
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000767 D->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000768}
769
Chandler Carruthedc2c642011-07-02 00:01:44 +0000770static void handleAlwaysInlineAttr(Sema &S, Decl *D,
771 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000772 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000773 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000774 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
775 return;
776 }
777
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000778 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000780 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +0000781 return;
782 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000783
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000784 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +0000785}
786
Chandler Carruthedc2c642011-07-02 00:01:44 +0000787static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000788 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000789 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
791 return;
792 }
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000794 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +0000795 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000796 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000797 D->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +0000798 return;
799 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000800 }
801
Ted Kremenek08479ae2009-08-15 00:51:46 +0000802 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000803}
804
Chandler Carruthedc2c642011-07-02 00:01:44 +0000805static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000806 // check the attribute arguments.
807 if (Attr.getNumArgs() != 0) {
808 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
809 return;
810 }
811
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000812 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000813}
814
Chandler Carruthedc2c642011-07-02 00:01:44 +0000815static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher8a2ee392010-12-02 02:45:55 +0000816 assert(Attr.isInvalid() == false);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000817 if (isa<VarDecl>(D))
818 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +0000819 else
820 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000821 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000822}
823
Chandler Carruthedc2c642011-07-02 00:01:44 +0000824static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher8a2ee392010-12-02 02:45:55 +0000825 assert(Attr.isInvalid() == false);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000826 if (isa<VarDecl>(D))
827 D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +0000828 else
829 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000830 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000831}
832
Chandler Carruthedc2c642011-07-02 00:01:44 +0000833static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000834 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +0000835
836 if (S.CheckNoReturnAttr(attr)) return;
837
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000838 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +0000839 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000840 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +0000841 return;
842 }
843
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000844 D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +0000845}
846
847bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +0000848 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +0000849 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
850 attr.setInvalid();
851 return true;
852 }
853
854 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000855}
856
Chandler Carruthedc2c642011-07-02 00:01:44 +0000857static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
858 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +0000859
860 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
861 // because 'analyzer_noreturn' does not impact the type.
862
863 if (Attr.getNumArgs() != 0) {
864 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
865 return;
866 }
867
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000868 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
869 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +0000870 if (VD == 0 || (!VD->getType()->isBlockPointerType()
871 && !VD->getType()->isFunctionPointerType())) {
872 S.Diag(Attr.getLoc(),
873 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
874 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000875 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +0000876 return;
877 }
878 }
879
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000880 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000881}
882
John Thompsoncdb847ba2010-08-09 21:53:52 +0000883// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +0000884static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000885/*
886 Returning a Vector Class in Registers
887
Eric Christopherbc638a82010-12-01 22:13:54 +0000888 According to the PPU ABI specifications, a class with a single member of
889 vector type is returned in memory when used as the return value of a function.
890 This results in inefficient code when implementing vector classes. To return
891 the value in a single vector register, add the vecreturn attribute to the
892 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +0000893
894 Example:
895
896 struct Vector
897 {
898 __vector float xyzw;
899 } __attribute__((vecreturn));
900
901 Vector Add(Vector lhs, Vector rhs)
902 {
903 Vector result;
904 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
905 return result; // This will be returned in a register
906 }
907*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000908 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000909 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000910 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +0000911 return;
912 }
913
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000914 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000915 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
916 return;
917 }
918
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000919 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +0000920 int count = 0;
921
922 if (!isa<CXXRecordDecl>(record)) {
923 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
924 return;
925 }
926
927 if (!cast<CXXRecordDecl>(record)->isPOD()) {
928 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
929 return;
930 }
931
Eric Christopherbc638a82010-12-01 22:13:54 +0000932 for (RecordDecl::field_iterator iter = record->field_begin();
933 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +0000934 if ((count == 1) || !iter->getType()->isVectorType()) {
935 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
936 return;
937 }
938 count++;
939 }
940
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000941 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +0000942}
943
Chandler Carruthedc2c642011-07-02 00:01:44 +0000944static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000945 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000947 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000948 return;
949 }
950 // FIXME: Actually store the attribute on the declaration
951}
952
Chandler Carruthedc2c642011-07-02 00:01:44 +0000953static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +0000954 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000955 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000956 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000957 return;
958 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000959
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000960 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
961 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000962 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000963 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000964 return;
965 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000966
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000967 D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +0000968}
969
Chandler Carruthedc2c642011-07-02 00:01:44 +0000970static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000971 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000972 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000973 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
974 return;
975 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000976
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000977 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000978 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000979 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
980 return;
981 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000982 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000983 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000984 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000985 return;
986 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000987
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000988 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000989}
990
Chandler Carruthedc2c642011-07-02 00:01:44 +0000991static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +0000992 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +0000993 if (Attr.getNumArgs() > 1) {
994 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +0000995 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000996 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000997
998 int priority = 65535; // FIXME: Do not hardcode such constants.
999 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001000 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001001 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001002 if (E->isTypeDependent() || E->isValueDependent() ||
1003 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001004 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001005 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001006 return;
1007 }
1008 priority = Idx.getZExtValue();
1009 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001010
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001011 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001012 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001013 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001014 return;
1015 }
1016
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001017 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001018 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001019}
1020
Chandler Carruthedc2c642011-07-02 00:01:44 +00001021static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001022 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001023 if (Attr.getNumArgs() > 1) {
1024 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001025 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001026 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001027
1028 int priority = 65535; // FIXME: Do not hardcode such constants.
1029 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001030 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001031 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001032 if (E->isTypeDependent() || E->isValueDependent() ||
1033 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001034 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001035 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001036 return;
1037 }
1038 priority = Idx.getZExtValue();
1039 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001040
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001041 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001042 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001043 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001044 return;
1045 }
1046
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001047 D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001048 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001049}
1050
Chandler Carruthedc2c642011-07-02 00:01:44 +00001051static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001052 unsigned NumArgs = Attr.getNumArgs();
1053 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001054 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001055 return;
1056 }
Chris Lattner190aa102011-02-24 05:42:24 +00001057
Fariborz Jahanian551063102010-10-06 21:18:44 +00001058 // Handle the case where deprecated attribute has a text message.
Chris Lattner190aa102011-02-24 05:42:24 +00001059 llvm::StringRef Str;
1060 if (NumArgs == 1) {
1061 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001062 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001063 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1064 << "deprecated";
Fariborz Jahanian551063102010-10-06 21:18:44 +00001065 return;
1066 }
Chris Lattner190aa102011-02-24 05:42:24 +00001067 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001068 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001069
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001070 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001071}
1072
Chandler Carruthedc2c642011-07-02 00:01:44 +00001073static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001074 unsigned NumArgs = Attr.getNumArgs();
1075 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001076 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001077 return;
1078 }
Chris Lattner190aa102011-02-24 05:42:24 +00001079
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001080 // Handle the case where unavailable attribute has a text message.
Chris Lattner190aa102011-02-24 05:42:24 +00001081 llvm::StringRef Str;
1082 if (NumArgs == 1) {
1083 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001084 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001085 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001086 diag::err_attribute_not_string) << "unavailable";
1087 return;
1088 }
Chris Lattner190aa102011-02-24 05:42:24 +00001089 Str = SE->getString();
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001090 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001091 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001092}
1093
Chandler Carruthedc2c642011-07-02 00:01:44 +00001094static void handleAvailabilityAttr(Sema &S, Decl *D,
1095 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001096 IdentifierInfo *Platform = Attr.getParameterName();
1097 SourceLocation PlatformLoc = Attr.getParameterLoc();
1098
1099 llvm::StringRef PlatformName
1100 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1101 if (PlatformName.empty()) {
1102 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1103 << Platform;
1104
1105 PlatformName = Platform->getName();
1106 }
1107
1108 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1109 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1110 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001111 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001112
1113 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1114 // of these steps are needed).
1115 if (Introduced.isValid() && Deprecated.isValid() &&
1116 !(Introduced.Version < Deprecated.Version)) {
1117 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1118 << 1 << PlatformName << Deprecated.Version.getAsString()
1119 << 0 << Introduced.Version.getAsString();
1120 return;
1121 }
1122
1123 if (Introduced.isValid() && Obsoleted.isValid() &&
1124 !(Introduced.Version < Obsoleted.Version)) {
1125 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1126 << 2 << PlatformName << Obsoleted.Version.getAsString()
1127 << 0 << Introduced.Version.getAsString();
1128 return;
1129 }
1130
1131 if (Deprecated.isValid() && Obsoleted.isValid() &&
1132 !(Deprecated.Version < Obsoleted.Version)) {
1133 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1134 << 2 << PlatformName << Obsoleted.Version.getAsString()
1135 << 1 << Deprecated.Version.getAsString();
1136 return;
1137 }
1138
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001139 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001140 Platform,
1141 Introduced.Version,
1142 Deprecated.Version,
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001143 Obsoleted.Version,
1144 IsUnavailable));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001145}
1146
Chandler Carruthedc2c642011-07-02 00:01:44 +00001147static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001148 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001149 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001150 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001151 return;
1152 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001153
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001154 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001155 Arg = Arg->IgnoreParenCasts();
1156 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001157
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001158 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001159 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001160 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001161 return;
1162 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001163
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001164 llvm::StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001165 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001166
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001167 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001168 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001169 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001170 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001171 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001172 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001173 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001174 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001175 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001176 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001177 return;
1178 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001179
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001180 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001181}
1182
Chandler Carruthedc2c642011-07-02 00:01:44 +00001183static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1184 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00001185 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1186 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001187 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001188 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001189 return;
1190 }
1191
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001192 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1193 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1194 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00001195 << "objc_method_family" << 1;
1196 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001197 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00001198 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001199 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00001200 return;
1201 }
1202
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001203 llvm::StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00001204 ObjCMethodFamilyAttr::FamilyKind family;
1205 if (param == "none")
1206 family = ObjCMethodFamilyAttr::OMF_None;
1207 else if (param == "alloc")
1208 family = ObjCMethodFamilyAttr::OMF_alloc;
1209 else if (param == "copy")
1210 family = ObjCMethodFamilyAttr::OMF_copy;
1211 else if (param == "init")
1212 family = ObjCMethodFamilyAttr::OMF_init;
1213 else if (param == "mutableCopy")
1214 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1215 else if (param == "new")
1216 family = ObjCMethodFamilyAttr::OMF_new;
1217 else {
1218 // Just warn and ignore it. This is future-proof against new
1219 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001220 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00001221 return;
1222 }
1223
John McCall31168b02011-06-15 23:02:42 +00001224 if (family == ObjCMethodFamilyAttr::OMF_init &&
1225 !method->getResultType()->isObjCObjectPointerType()) {
1226 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1227 << method->getResultType();
1228 // Ignore the attribute.
1229 return;
1230 }
1231
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001232 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(),
John McCall31168b02011-06-15 23:02:42 +00001233 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00001234}
1235
Chandler Carruthedc2c642011-07-02 00:01:44 +00001236static void handleObjCExceptionAttr(Sema &S, Decl *D,
1237 const AttributeList &Attr) {
Chris Lattner677a3582009-02-14 08:09:34 +00001238 if (Attr.getNumArgs() != 0) {
1239 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1240 return;
1241 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001242
Chris Lattner677a3582009-02-14 08:09:34 +00001243 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1244 if (OCI == 0) {
1245 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1246 return;
1247 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001248
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001249 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001250}
1251
Chandler Carruthedc2c642011-07-02 00:01:44 +00001252static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001253 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001254 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001255 return;
1256 }
Richard Smithdda56e42011-04-15 14:24:37 +00001257 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001258 QualType T = TD->getUnderlyingType();
1259 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001260 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001261 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1262 return;
1263 }
1264 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001265 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001266}
1267
Mike Stumpd3bb5572009-07-24 19:02:52 +00001268static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00001269handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001270 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001271 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001272 return;
1273 }
1274
1275 if (!isa<FunctionDecl>(D)) {
1276 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1277 return;
1278 }
1279
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001280 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001281}
1282
Chandler Carruthedc2c642011-07-02 00:01:44 +00001283static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001284 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001285 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001286 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001287 return;
1288 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001289
Steve Naroff3405a732008-09-18 16:44:58 +00001290 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001291 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001292 return;
1293 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001294
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001295 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001296 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001297 type = BlocksAttr::ByRef;
1298 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001299 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001300 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001301 return;
1302 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001303
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001304 D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001305}
1306
Chandler Carruthedc2c642011-07-02 00:01:44 +00001307static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001308 // check the attribute arguments.
1309 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00001310 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00001311 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001312 }
1313
Anders Carlssonc181b012008-10-05 18:05:59 +00001314 int sentinel = 0;
1315 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001316 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001317 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001318 if (E->isTypeDependent() || E->isValueDependent() ||
1319 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001320 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001321 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001322 return;
1323 }
1324 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001325
Anders Carlssonc181b012008-10-05 18:05:59 +00001326 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001327 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1328 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001329 return;
1330 }
1331 }
1332
1333 int nullPos = 0;
1334 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001335 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001336 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001337 if (E->isTypeDependent() || E->isValueDependent() ||
1338 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001339 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001340 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001341 return;
1342 }
1343 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001344
Anders Carlssonc181b012008-10-05 18:05:59 +00001345 if (nullPos > 1 || nullPos < 0) {
1346 // FIXME: This error message could be improved, it would be nice
1347 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001348 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1349 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001350 return;
1351 }
1352 }
1353
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001354 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall9dd450b2009-09-21 23:43:11 +00001355 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001356 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001357
Chris Lattner9363e312009-03-17 23:03:47 +00001358 if (isa<FunctionNoProtoType>(FT)) {
1359 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1360 return;
1361 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001362
Chris Lattner9363e312009-03-17 23:03:47 +00001363 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001364 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001365 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001366 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001367 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001368 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001369 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001370 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001371 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001372 } else if (isa<BlockDecl>(D)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001373 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1374 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001375 ;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001376 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001377 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001378 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001379 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00001380 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001381 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001382 int m = Ty->isFunctionPointerType() ? 0 : 1;
1383 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001384 return;
1385 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001386 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001387 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001388 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001389 return;
1390 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001391 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001392 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001393 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00001394 return;
1395 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001396 D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00001397 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001398}
1399
Chandler Carruthedc2c642011-07-02 00:01:44 +00001400static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00001401 // check the attribute arguments.
1402 if (Attr.getNumArgs() != 0) {
1403 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1404 return;
1405 }
1406
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001407 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001408 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001409 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00001410 return;
1411 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001412
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001413 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1414 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1415 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001416 return;
1417 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001418 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1419 if (MD->getResultType()->isVoidType()) {
1420 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1421 << Attr.getName() << 1;
1422 return;
1423 }
1424
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001425 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001426}
1427
Chandler Carruthedc2c642011-07-02 00:01:44 +00001428static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001429 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001430 if (Attr.hasParameterOrArguments()) {
1431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001432 return;
1433 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001434
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001435 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1436 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1437 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001438 return;
1439 }
1440
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001441 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001442
1443 // 'weak' only applies to declarations with external linkage.
1444 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001445 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001446 return;
1447 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001448
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001449 nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001450}
1451
Chandler Carruthedc2c642011-07-02 00:01:44 +00001452static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001453 // check the attribute arguments.
1454 if (Attr.getNumArgs() != 0) {
1455 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1456 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001457 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001458
1459 // weak_import only applies to variable & function declarations.
1460 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001461 if (!D->canBeWeakImported(isDef)) {
1462 if (isDef)
1463 S.Diag(Attr.getLoc(),
1464 diag::warn_attribute_weak_import_invalid_on_definition)
1465 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00001466 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00001467 (S.Context.Target.getTriple().isOSDarwin() &&
Douglas Gregord71149a2011-03-23 13:27:51 +00001468 isa<ObjCInterfaceDecl>(D))) {
1469 // Nothing to warn about here.
1470 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001472 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001473
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001474 return;
1475 }
1476
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001477 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001478}
1479
Chandler Carruthedc2c642011-07-02 00:01:44 +00001480static void handleReqdWorkGroupSize(Sema &S, Decl *D,
1481 const AttributeList &Attr) {
Nate Begemanf2758702009-06-26 06:32:41 +00001482 // Attribute has 3 arguments.
1483 if (Attr.getNumArgs() != 3) {
Chandler Carruthc91f09d2011-06-30 08:14:54 +00001484 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Nate Begemanf2758702009-06-26 06:32:41 +00001485 return;
1486 }
1487
1488 unsigned WGSize[3];
1489 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001490 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00001491 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001492 if (E->isTypeDependent() || E->isValueDependent() ||
1493 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001494 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1495 << "reqd_work_group_size" << E->getSourceRange();
1496 return;
1497 }
1498 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1499 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001500 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1501 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001502 WGSize[2]));
1503}
1504
Chandler Carruthedc2c642011-07-02 00:01:44 +00001505static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001506 // Attribute has no arguments.
1507 if (Attr.getNumArgs() != 1) {
1508 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1509 return;
1510 }
1511
1512 // Make sure that there is a string literal as the sections's single
1513 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001514 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001515 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001516 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001517 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001518 return;
1519 }
Mike Stump11289f42009-09-09 15:08:12 +00001520
Chris Lattner30ba6742009-08-10 19:03:04 +00001521 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001522 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001523 if (!Error.empty()) {
1524 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1525 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001526 return;
1527 }
Mike Stump11289f42009-09-09 15:08:12 +00001528
Chris Lattner20aee9b2010-01-12 20:58:53 +00001529 // This attribute cannot be applied to local variables.
1530 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1531 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1532 return;
1533 }
1534
Eric Christopherbc638a82010-12-01 22:13:54 +00001535 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1536 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001537}
1538
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001539
Chandler Carruthedc2c642011-07-02 00:01:44 +00001540static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001541 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001542 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001543 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001544 return;
1545 }
Douglas Gregor88336832011-06-15 05:45:11 +00001546
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001547 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00001548 if (Existing->getLocation().isInvalid())
1549 Existing->setLocation(Attr.getLoc());
1550 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001551 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00001552 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001553}
1554
Chandler Carruthedc2c642011-07-02 00:01:44 +00001555static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00001556 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001557 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001558 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001559 return;
1560 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001561
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001562 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00001563 if (Existing->getLocation().isInvalid())
1564 Existing->setLocation(Attr.getLoc());
1565 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001566 D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00001567 }
Anders Carlssonb8316282008-10-05 23:32:53 +00001568}
1569
Chandler Carruthedc2c642011-07-02 00:01:44 +00001570static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00001571 // check the attribute arguments.
1572 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001573 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001574 return;
1575 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001576
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001577 D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001578}
1579
Chandler Carruthedc2c642011-07-02 00:01:44 +00001580static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001581 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1583 return;
1584 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001585
Anders Carlssond277d792009-01-31 01:16:18 +00001586 if (Attr.getNumArgs() != 0) {
1587 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1588 return;
1589 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001590
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001591 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001592
Anders Carlssond277d792009-01-31 01:16:18 +00001593 if (!VD || !VD->hasLocalStorage()) {
1594 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1595 return;
1596 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001597
Anders Carlssond277d792009-01-31 01:16:18 +00001598 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001599 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00001600 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001601 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1602 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001603 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001604 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001605 Attr.getParameterName();
1606 return;
1607 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001608
Anders Carlssond277d792009-01-31 01:16:18 +00001609 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1610 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001611 S.Diag(Attr.getParameterLoc(),
1612 diag::err_attribute_cleanup_arg_not_function)
1613 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001614 return;
1615 }
1616
Anders Carlssond277d792009-01-31 01:16:18 +00001617 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001618 S.Diag(Attr.getParameterLoc(),
1619 diag::err_attribute_cleanup_func_must_take_one_arg)
1620 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001621 return;
1622 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001623
Anders Carlsson723f55d2009-02-07 23:16:50 +00001624 // We're currently more strict than GCC about what function types we accept.
1625 // If this ever proves to be a problem it should be easy to fix.
1626 QualType Ty = S.Context.getPointerType(VD->getType());
1627 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00001628 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1629 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001630 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001631 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1632 Attr.getParameterName() << ParamTy << Ty;
1633 return;
1634 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001635
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001636 D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidise88168a2010-12-06 17:51:53 +00001637 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00001638}
1639
Mike Stumpd3bb5572009-07-24 19:02:52 +00001640/// Handle __attribute__((format_arg((idx)))) attribute based on
1641/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00001642static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001643 if (Attr.getNumArgs() != 1) {
1644 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1645 return;
1646 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001647 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001648 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001649 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001650 return;
1651 }
Chandler Carruth743682b2010-11-16 08:35:43 +00001652
1653 // In C++ the implicit 'this' function parameter also counts, and they are
1654 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001655 bool HasImplicitThisParam = isInstanceMethod(D);
1656 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001657 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00001658
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001659 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001660 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001661 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001662 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1663 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001664 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1665 << "format" << 2 << IdxExpr->getSourceRange();
1666 return;
1667 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001668
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001669 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1670 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1671 << "format" << 2 << IdxExpr->getSourceRange();
1672 return;
1673 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001674
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001675 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001676
Chandler Carruth743682b2010-11-16 08:35:43 +00001677 if (HasImplicitThisParam) {
1678 if (ArgIdx == 0) {
1679 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1680 << "format_arg" << IdxExpr->getSourceRange();
1681 return;
1682 }
1683 ArgIdx--;
1684 }
1685
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001686 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001687 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001688
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001689 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1690 if (not_nsstring_type &&
1691 !isCFStringType(Ty, S.Context) &&
1692 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001693 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001694 // FIXME: Should highlight the actual expression that has the wrong type.
1695 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001696 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001697 << IdxExpr->getSourceRange();
1698 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001699 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001700 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001701 if (!isNSStringType(Ty, S.Context) &&
1702 !isCFStringType(Ty, S.Context) &&
1703 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001704 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001705 // FIXME: Should highlight the actual expression that has the wrong type.
1706 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001707 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001708 << IdxExpr->getSourceRange();
1709 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001710 }
1711
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001712 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00001713 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001714}
1715
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001716enum FormatAttrKind {
1717 CFStringFormat,
1718 NSStringFormat,
1719 StrftimeFormat,
1720 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001721 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001722 InvalidFormat
1723};
1724
1725/// getFormatAttrKind - Map from format attribute names to supported format
1726/// types.
1727static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1728 // Check for formats that get handled specially.
1729 if (Format == "NSString")
1730 return NSStringFormat;
1731 if (Format == "CFString")
1732 return CFStringFormat;
1733 if (Format == "strftime")
1734 return StrftimeFormat;
1735
1736 // Otherwise, check for supported formats.
1737 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1738 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1739 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00001740 Format == "zcmn_err" ||
1741 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001742 return SupportedFormat;
1743
Duncan Sandsde4fe352010-03-23 14:44:19 +00001744 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1745 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001746 return IgnoredFormat;
1747
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001748 return InvalidFormat;
1749}
1750
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001751/// Handle __attribute__((init_priority(priority))) attributes based on
1752/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00001753static void handleInitPriorityAttr(Sema &S, Decl *D,
1754 const AttributeList &Attr) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001755 if (!S.getLangOptions().CPlusPlus) {
1756 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1757 return;
1758 }
1759
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001760 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001761 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1762 Attr.setInvalid();
1763 return;
1764 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001765 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001766 if (S.Context.getAsArrayType(T))
1767 T = S.Context.getBaseElementType(T);
1768 if (!T->getAs<RecordType>()) {
1769 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1770 Attr.setInvalid();
1771 return;
1772 }
1773
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001774 if (Attr.getNumArgs() != 1) {
1775 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1776 Attr.setInvalid();
1777 return;
1778 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001779 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001780
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001781 llvm::APSInt priority(32);
1782 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1783 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1784 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1785 << "init_priority" << priorityExpr->getSourceRange();
1786 Attr.setInvalid();
1787 return;
1788 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001789 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001790 if (prioritynum < 101 || prioritynum > 65535) {
1791 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1792 << priorityExpr->getSourceRange();
1793 Attr.setInvalid();
1794 return;
1795 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001796 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001797 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001798}
1799
Mike Stumpd3bb5572009-07-24 19:02:52 +00001800/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1801/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00001802static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001803
Chris Lattner4a927cb2008-06-28 23:36:30 +00001804 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001805 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001806 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001807 return;
1808 }
1809
Chris Lattner4a927cb2008-06-28 23:36:30 +00001810 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001811 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001812 return;
1813 }
1814
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001815 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001816 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001817 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001818 return;
1819 }
1820
Chandler Carruth743682b2010-11-16 08:35:43 +00001821 // In C++ the implicit 'this' function parameter also counts, and they are
1822 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001823 bool HasImplicitThisParam = isInstanceMethod(D);
1824 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001825 unsigned FirstIdx = 1;
1826
Daniel Dunbar07d07852009-10-18 21:17:35 +00001827 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001828
1829 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001830 if (Format.startswith("__") && Format.endswith("__"))
1831 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001832
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001833 // Check for supported formats.
1834 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001835
1836 if (Kind == IgnoredFormat)
1837 return;
1838
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001839 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001840 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001841 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001842 return;
1843 }
1844
1845 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001846 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001847 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001848 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1849 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001850 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001851 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001852 return;
1853 }
1854
1855 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001856 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001857 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001858 return;
1859 }
1860
1861 // FIXME: Do we need to bounds check?
1862 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001863
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001864 if (HasImplicitThisParam) {
1865 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00001866 S.Diag(Attr.getLoc(),
1867 diag::err_format_attribute_implicit_this_format_string)
1868 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001869 return;
1870 }
1871 ArgIdx--;
1872 }
Mike Stump11289f42009-09-09 15:08:12 +00001873
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001874 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001875 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001876
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001877 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001878 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001879 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1880 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001881 return;
1882 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001883 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001884 // FIXME: do we need to check if the type is NSString*? What are the
1885 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001886 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001887 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001888 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1889 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001890 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001891 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001892 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001893 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001894 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001895 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1896 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001897 return;
1898 }
1899
1900 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001901 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001902 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001903 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1904 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001905 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001906 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001907 return;
1908 }
1909
1910 // check if the function is variadic if the 3rd argument non-zero
1911 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001912 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001913 ++NumArgs; // +1 for ...
1914 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001915 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001916 return;
1917 }
1918 }
1919
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001920 // strftime requires FirstArg to be 0 because it doesn't read from any
1921 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001922 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001923 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001924 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1925 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001926 return;
1927 }
1928 // if 0 it disables parameter checking (to use with e.g. va_list)
1929 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001930 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001931 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001932 return;
1933 }
1934
Douglas Gregor88336832011-06-15 05:45:11 +00001935 // Check whether we already have an equivalent format attribute.
1936 for (specific_attr_iterator<FormatAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001937 i = D->specific_attr_begin<FormatAttr>(),
1938 e = D->specific_attr_end<FormatAttr>();
Douglas Gregor88336832011-06-15 05:45:11 +00001939 i != e ; ++i) {
1940 FormatAttr *f = *i;
1941 if (f->getType() == Format &&
1942 f->getFormatIdx() == (int)Idx.getZExtValue() &&
1943 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
1944 // If we don't have a valid location for this attribute, adopt the
1945 // location.
1946 if (f->getLocation().isInvalid())
1947 f->setLocation(Attr.getLoc());
1948 return;
1949 }
1950 }
1951
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001952 D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001953 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001954 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001955}
1956
Chandler Carruthedc2c642011-07-02 00:01:44 +00001957static void handleTransparentUnionAttr(Sema &S, Decl *D,
1958 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001959 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001960 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001961 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001962 return;
1963 }
1964
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001965 // Try to find the underlying union declaration.
1966 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001967 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001968 if (TD && TD->getUnderlyingType()->isUnionType())
1969 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1970 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001971 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001972
1973 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001974 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001975 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001976 return;
1977 }
1978
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001979 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001980 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001981 diag::warn_transparent_union_attribute_not_definition);
1982 return;
1983 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001984
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001985 RecordDecl::field_iterator Field = RD->field_begin(),
1986 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001987 if (Field == FieldEnd) {
1988 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1989 return;
1990 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001991
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001992 FieldDecl *FirstField = *Field;
1993 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00001994 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001995 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00001996 diag::warn_transparent_union_attribute_floating)
1997 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001998 return;
1999 }
2000
2001 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2002 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2003 for (; Field != FieldEnd; ++Field) {
2004 QualType FieldType = Field->getType();
2005 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2006 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2007 // Warn if we drop the attribute.
2008 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002009 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002010 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002011 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002012 diag::warn_transparent_union_attribute_field_size_align)
2013 << isSize << Field->getDeclName() << FieldBits;
2014 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002015 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002016 diag::note_transparent_union_first_field_size_align)
2017 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002018 return;
2019 }
2020 }
2021
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002022 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002023}
2024
Chandler Carruthedc2c642011-07-02 00:01:44 +00002025static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002026 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002027 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002028 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002029 return;
2030 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002031 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002032 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002033
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002034 // Make sure that there is a string literal as the annotation's single
2035 // argument.
2036 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002037 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002038 return;
2039 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002040 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002041 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002042}
2043
Chandler Carruthedc2c642011-07-02 00:01:44 +00002044static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002045 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002046 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002047 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002048 return;
2049 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002050
2051 //FIXME: The C++0x version of this attribute has more limited applicabilty
2052 // than GNU's, and should error out when it is used to specify a
2053 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002054
Chris Lattner4a927cb2008-06-28 23:36:30 +00002055 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002056 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002057 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002058 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002059
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002060 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002061}
2062
2063void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2064 if (E->isTypeDependent() || E->isValueDependent()) {
2065 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002066 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002067 return;
2068 }
2069
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002070 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002071 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002072 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2073 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2074 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00002075 return;
2076 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002077 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002078 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2079 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002080 return;
2081 }
2082
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002083 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2084}
2085
2086void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2087 // FIXME: Cache the number on the Attr object if non-dependent?
2088 // FIXME: Perform checking of type validity
2089 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2090 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002091}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002092
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002093/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002094/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002095///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002096/// Despite what would be logical, the mode attribute is a decl attribute, not a
2097/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2098/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002099static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002100 // This attribute isn't documented, but glibc uses it. It changes
2101 // the width of an int or unsigned int to the specified size.
2102
2103 // Check that there aren't any arguments
2104 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002105 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002106 return;
2107 }
2108
2109 IdentifierInfo *Name = Attr.getParameterName();
2110 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002111 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002112 return;
2113 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002114
Daniel Dunbar07d07852009-10-18 21:17:35 +00002115 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002116
2117 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002118 if (Str.startswith("__") && Str.endswith("__"))
2119 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002120
2121 unsigned DestWidth = 0;
2122 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002123 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002124 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002125 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002126 switch (Str[0]) {
2127 case 'Q': DestWidth = 8; break;
2128 case 'H': DestWidth = 16; break;
2129 case 'S': DestWidth = 32; break;
2130 case 'D': DestWidth = 64; break;
2131 case 'X': DestWidth = 96; break;
2132 case 'T': DestWidth = 128; break;
2133 }
2134 if (Str[1] == 'F') {
2135 IntegerMode = false;
2136 } else if (Str[1] == 'C') {
2137 IntegerMode = false;
2138 ComplexMode = true;
2139 } else if (Str[1] != 'I') {
2140 DestWidth = 0;
2141 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002142 break;
2143 case 4:
2144 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2145 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002146 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002147 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002148 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002149 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002150 break;
2151 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002152 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002153 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002154 break;
2155 }
2156
2157 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002158 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002159 OldTy = TD->getUnderlyingType();
2160 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2161 OldTy = VD->getType();
2162 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002163 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2164 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00002165 return;
2166 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002167
John McCall9dd450b2009-09-21 23:43:11 +00002168 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002169 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2170 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002171 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002172 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2173 } else if (ComplexMode) {
2174 if (!OldTy->isComplexType())
2175 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2176 } else {
2177 if (!OldTy->isFloatingType())
2178 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2179 }
2180
Mike Stump87c57ac2009-05-16 07:39:55 +00002181 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2182 // and friends, at least with glibc.
2183 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2184 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002185 // FIXME: Make sure floating-point mappings are accurate
2186 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002187 QualType NewTy;
2188 switch (DestWidth) {
2189 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002190 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002191 return;
2192 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002193 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002194 return;
2195 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002196 if (!IntegerMode) {
2197 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2198 return;
2199 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002200 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002201 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002202 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002203 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002204 break;
2205 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002206 if (!IntegerMode) {
2207 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2208 return;
2209 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002210 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002211 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002212 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002213 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002214 break;
2215 case 32:
2216 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002217 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002218 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002219 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002220 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002221 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002222 break;
2223 case 64:
2224 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002225 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002226 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00002227 if (S.Context.Target.getLongWidth() == 64)
2228 NewTy = S.Context.LongTy;
2229 else
2230 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002231 else
Chandler Carruth72343702010-01-26 06:39:24 +00002232 if (S.Context.Target.getLongWidth() == 64)
2233 NewTy = S.Context.UnsignedLongTy;
2234 else
2235 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002236 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002237 case 96:
2238 NewTy = S.Context.LongDoubleTy;
2239 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002240 case 128:
2241 if (!IntegerMode) {
2242 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2243 return;
2244 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002245 if (OldTy->isSignedIntegerType())
2246 NewTy = S.Context.Int128Ty;
2247 else
2248 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002249 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002250 }
2251
Eli Friedman4735374e2009-03-03 06:41:03 +00002252 if (ComplexMode) {
2253 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002254 }
2255
2256 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00002257 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00002258 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002259 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002260 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002261 cast<ValueDecl>(D)->setType(NewTy);
2262}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002263
Chandler Carruthedc2c642011-07-02 00:01:44 +00002264static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002265 // check the attribute arguments.
2266 if (Attr.getNumArgs() > 0) {
2267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2268 return;
2269 }
Anders Carlsson63784f42009-02-13 08:11:52 +00002270
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002271 if (!isFunctionOrMethod(D)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002272 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002273 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00002274 return;
2275 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002276
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002277 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002278}
2279
Chandler Carruthedc2c642011-07-02 00:01:44 +00002280static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00002281 // check the attribute arguments.
2282 if (Attr.getNumArgs() != 0) {
2283 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2284 return;
2285 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002286
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002287 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002288 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002289 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00002290 return;
2291 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002292
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002293 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002294}
2295
Chandler Carruthedc2c642011-07-02 00:01:44 +00002296static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2297 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00002298 // check the attribute arguments.
2299 if (Attr.getNumArgs() != 0) {
2300 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2301 return;
2302 }
2303
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002304 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00002305 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002306 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00002307 return;
2308 }
2309
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002310 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002311 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002312}
2313
Chandler Carruthedc2c642011-07-02 00:01:44 +00002314static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002315 if (S.LangOpts.CUDA) {
2316 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002317 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002318 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2319 return;
2320 }
2321
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002322 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002323 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002324 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002325 return;
2326 }
2327
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002328 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002329 } else {
2330 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2331 }
2332}
2333
Chandler Carruthedc2c642011-07-02 00:01:44 +00002334static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002335 if (S.LangOpts.CUDA) {
2336 // check the attribute arguments.
2337 if (Attr.getNumArgs() != 0) {
2338 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2339 return;
2340 }
2341
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002342 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002343 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002344 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002345 return;
2346 }
2347
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002348 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002349 } else {
2350 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2351 }
2352}
2353
Chandler Carruthedc2c642011-07-02 00:01:44 +00002354static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002355 if (S.LangOpts.CUDA) {
2356 // check the attribute arguments.
2357 if (Attr.getNumArgs() != 0) {
2358 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2359 return;
2360 }
2361
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002362 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002363 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002364 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002365 return;
2366 }
2367
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002368 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002369 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00002370 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002371 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2372 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2373 << FD->getType()
2374 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2375 "void");
2376 } else {
2377 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2378 << FD->getType();
2379 }
2380 return;
2381 }
2382
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002383 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002384 } else {
2385 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2386 }
2387}
2388
Chandler Carruthedc2c642011-07-02 00:01:44 +00002389static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002390 if (S.LangOpts.CUDA) {
2391 // check the attribute arguments.
2392 if (Attr.getNumArgs() != 0) {
2393 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2394 return;
2395 }
2396
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002397 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002398 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002399 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002400 return;
2401 }
2402
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002403 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002404 } else {
2405 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2406 }
2407}
2408
Chandler Carruthedc2c642011-07-02 00:01:44 +00002409static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002410 if (S.LangOpts.CUDA) {
2411 // check the attribute arguments.
2412 if (Attr.getNumArgs() != 0) {
2413 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2414 return;
2415 }
2416
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002417 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002418 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002419 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002420 return;
2421 }
2422
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002423 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002424 } else {
2425 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2426 }
2427}
2428
Chandler Carruthedc2c642011-07-02 00:01:44 +00002429static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002430 // check the attribute arguments.
2431 if (Attr.getNumArgs() != 0) {
2432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2433 return;
2434 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002435
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002436 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00002437 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002438 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002439 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002440 return;
2441 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002442
Douglas Gregor35b57532009-10-27 21:01:01 +00002443 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002444 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002445 return;
2446 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002447
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002448 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002449}
2450
Chandler Carruthedc2c642011-07-02 00:01:44 +00002451static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002452 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002453
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002454 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00002455 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2456 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002457 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00002458 return;
2459
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002460 if (!isa<ObjCMethodDecl>(D)) {
2461 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2462 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002463 return;
2464 }
2465
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002466 switch (Attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002467 case AttributeList::AT_fastcall:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002468 D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002469 return;
2470 case AttributeList::AT_stdcall:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002471 D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002472 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002473 case AttributeList::AT_thiscall:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002474 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002475 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002476 case AttributeList::AT_cdecl:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002477 D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002478 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002479 case AttributeList::AT_pascal:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002480 D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00002481 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002482 case AttributeList::AT_pcs: {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002483 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002484 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2485 if (Str == 0 || Str->isWide()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002486 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002487 << "pcs" << 1;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002488 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002489 return;
2490 }
2491
2492 llvm::StringRef StrRef = Str->getString();
2493 PcsAttr::PCSType PCS;
2494 if (StrRef == "aapcs")
2495 PCS = PcsAttr::AAPCS;
2496 else if (StrRef == "aapcs-vfp")
2497 PCS = PcsAttr::AAPCS_VFP;
2498 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002499 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2500 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002501 return;
2502 }
2503
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002504 D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS));
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002505 }
Abramo Bagnara50099372010-04-30 13:10:51 +00002506 default:
2507 llvm_unreachable("unexpected attribute kind");
2508 return;
2509 }
2510}
2511
Chandler Carruthedc2c642011-07-02 00:01:44 +00002512static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002513 assert(Attr.isInvalid() == false);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002514 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002515}
2516
John McCall3882ace2011-01-05 12:14:39 +00002517bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2518 if (attr.isInvalid())
2519 return true;
2520
Ted Kremenek1551d552011-04-15 05:49:29 +00002521 if ((attr.getNumArgs() != 0 &&
2522 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2523 attr.getParameterName()) {
John McCall3882ace2011-01-05 12:14:39 +00002524 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2525 attr.setInvalid();
2526 return true;
2527 }
2528
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002529 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2530 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00002531 switch (attr.getKind()) {
2532 case AttributeList::AT_cdecl: CC = CC_C; break;
2533 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2534 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2535 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2536 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002537 case AttributeList::AT_pcs: {
2538 Expr *Arg = attr.getArg(0);
2539 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2540 if (Str == 0 || Str->isWide()) {
2541 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2542 << "pcs" << 1;
2543 attr.setInvalid();
2544 return true;
2545 }
2546
2547 llvm::StringRef StrRef = Str->getString();
2548 if (StrRef == "aapcs") {
2549 CC = CC_AAPCS;
2550 break;
2551 } else if (StrRef == "aapcs-vfp") {
2552 CC = CC_AAPCS_VFP;
2553 break;
2554 }
2555 // FALLS THROUGH
2556 }
John McCall3882ace2011-01-05 12:14:39 +00002557 default: llvm_unreachable("unexpected attribute kind"); return true;
2558 }
2559
2560 return false;
2561}
2562
Chandler Carruthedc2c642011-07-02 00:01:44 +00002563static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002564 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00002565
2566 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002567 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00002568 return;
2569
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002570 if (!isa<ObjCMethodDecl>(D)) {
2571 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2572 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002573 return;
2574 }
Eli Friedman7044b762009-03-27 21:06:47 +00002575
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002576 D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00002577}
2578
2579/// Checks a regparm attribute, returning true if it is ill-formed and
2580/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002581bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
2582 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00002583 return true;
2584
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002585 if (Attr.getNumArgs() != 1) {
2586 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2587 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002588 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002589 }
Eli Friedman7044b762009-03-27 21:06:47 +00002590
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002591 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00002592 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002593 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00002594 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002595 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00002596 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002597 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002598 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002599 }
2600
John McCall3882ace2011-01-05 12:14:39 +00002601 if (Context.Target.getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002602 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002603 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002604 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002605 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002606 }
2607
John McCall3882ace2011-01-05 12:14:39 +00002608 numParams = NumParams.getZExtValue();
2609 if (numParams > Context.Target.getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002610 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
John McCall3882ace2011-01-05 12:14:39 +00002611 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002612 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002613 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002614 }
2615
John McCall3882ace2011-01-05 12:14:39 +00002616 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002617}
2618
Chandler Carruthedc2c642011-07-02 00:01:44 +00002619static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00002620 if (S.LangOpts.CUDA) {
2621 // check the attribute arguments.
2622 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00002623 // FIXME: 0 is not okay.
2624 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002625 return;
2626 }
2627
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002628 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00002629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002630 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002631 return;
2632 }
2633
2634 Expr *MaxThreadsExpr = Attr.getArg(0);
2635 llvm::APSInt MaxThreads(32);
2636 if (MaxThreadsExpr->isTypeDependent() ||
2637 MaxThreadsExpr->isValueDependent() ||
2638 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2639 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2640 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2641 return;
2642 }
2643
2644 llvm::APSInt MinBlocks(32);
2645 if (Attr.getNumArgs() > 1) {
2646 Expr *MinBlocksExpr = Attr.getArg(1);
2647 if (MinBlocksExpr->isTypeDependent() ||
2648 MinBlocksExpr->isValueDependent() ||
2649 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2650 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2651 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2652 return;
2653 }
2654 }
2655
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002656 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00002657 MaxThreads.getZExtValue(),
2658 MinBlocks.getZExtValue()));
2659 } else {
2660 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2661 }
2662}
2663
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002664//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002665// Checker-specific attribute handlers.
2666//===----------------------------------------------------------------------===//
2667
John McCalled433932011-01-25 03:31:58 +00002668static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2669 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2670}
2671static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2672 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2673}
2674
Chandler Carruthedc2c642011-07-02 00:01:44 +00002675static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002676 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00002677 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002678 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2679 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00002680 return;
2681 }
2682
2683 bool typeOK, cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002684 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCalled433932011-01-25 03:31:58 +00002685 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2686 cf = false;
2687 } else {
2688 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2689 cf = true;
2690 }
2691
2692 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002693 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2694 << SourceRange(Attr.getLoc()) << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00002695 return;
2696 }
2697
2698 if (cf)
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002699 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context));
John McCalled433932011-01-25 03:31:58 +00002700 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002701 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context));
John McCalled433932011-01-25 03:31:58 +00002702}
2703
Chandler Carruthedc2c642011-07-02 00:01:44 +00002704static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
2705 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002706 if (!isa<ObjCMethodDecl>(D)) {
2707 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2708 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00002709 return;
2710 }
2711
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002712 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context));
John McCalled433932011-01-25 03:31:58 +00002713}
2714
Chandler Carruthedc2c642011-07-02 00:01:44 +00002715static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
2716 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002717
John McCalled433932011-01-25 03:31:58 +00002718 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002719
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002720 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00002721 returnType = MD->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002722 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002723 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002724 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
2725 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCall31168b02011-06-15 23:02:42 +00002726 return; // ignore: was handled as a type attribute
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002727 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00002728 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00002729 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002730 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2731 << SourceRange(Attr.getLoc()) << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00002732 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002733 return;
2734 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002735
John McCalled433932011-01-25 03:31:58 +00002736 bool typeOK;
2737 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002738 switch (Attr.getKind()) {
John McCalled433932011-01-25 03:31:58 +00002739 default: llvm_unreachable("invalid ownership attribute"); return;
2740 case AttributeList::AT_ns_returns_autoreleased:
2741 case AttributeList::AT_ns_returns_retained:
2742 case AttributeList::AT_ns_returns_not_retained:
2743 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2744 cf = false;
2745 break;
2746
2747 case AttributeList::AT_cf_returns_retained:
2748 case AttributeList::AT_cf_returns_not_retained:
2749 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2750 cf = true;
2751 break;
2752 }
2753
2754 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002755 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2756 << SourceRange(Attr.getLoc())
2757 << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002758 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002759 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002760
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002761 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002762 default:
2763 assert(0 && "invalid ownership attribute");
2764 return;
John McCalled433932011-01-25 03:31:58 +00002765 case AttributeList::AT_ns_returns_autoreleased:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002766 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(),
John McCalled433932011-01-25 03:31:58 +00002767 S.Context));
2768 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002769 case AttributeList::AT_cf_returns_not_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002770 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002771 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002772 return;
2773 case AttributeList::AT_ns_returns_not_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002774 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002775 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002776 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002777 case AttributeList::AT_cf_returns_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002778 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002779 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002780 return;
2781 case AttributeList::AT_ns_returns_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002782 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002783 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002784 return;
2785 };
2786}
2787
Chandler Carruthedc2c642011-07-02 00:01:44 +00002788static void handleObjCOwnershipAttr(Sema &S, Decl *D,
2789 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002790 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00002791
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002792 SourceLocation L = Attr.getLoc();
2793 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
2794 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCall31168b02011-06-15 23:02:42 +00002795}
2796
Chandler Carruthedc2c642011-07-02 00:01:44 +00002797static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
2798 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002799 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
2800 SourceLocation L = Attr.getLoc();
2801 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
2802 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCall31168b02011-06-15 23:02:42 +00002803 return;
2804 }
2805
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002806 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00002807 QualType type = vd->getType();
2808
2809 if (!type->isDependentType() &&
2810 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002811 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00002812 << type;
2813 return;
2814 }
2815
2816 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
2817
2818 // If we have no lifetime yet, check the lifetime we're presumably
2819 // going to infer.
2820 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
2821 lifetime = type->getObjCARCImplicitLifetime();
2822
2823 switch (lifetime) {
2824 case Qualifiers::OCL_None:
2825 assert(type->isDependentType() &&
2826 "didn't infer lifetime for non-dependent type?");
2827 break;
2828
2829 case Qualifiers::OCL_Weak: // meaningful
2830 case Qualifiers::OCL_Strong: // meaningful
2831 break;
2832
2833 case Qualifiers::OCL_ExplicitNone:
2834 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002835 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00002836 << (lifetime == Qualifiers::OCL_Autoreleasing);
2837 break;
2838 }
2839
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002840 D->addAttr(::new (S.Context)
2841 ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00002842}
2843
Charles Davis163855f2010-02-16 18:27:26 +00002844static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2845 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Picheta83957a2010-12-19 06:50:37 +00002846 Attr.getKind() == AttributeList::AT_dllexport ||
2847 Attr.getKind() == AttributeList::AT_uuid;
2848}
2849
2850//===----------------------------------------------------------------------===//
2851// Microsoft specific attribute handlers.
2852//===----------------------------------------------------------------------===//
2853
Chandler Carruthedc2c642011-07-02 00:01:44 +00002854static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Picheta83957a2010-12-19 06:50:37 +00002855 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2856 // check the attribute arguments.
2857 if (Attr.getNumArgs() != 1) {
2858 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2859 return;
2860 }
2861 Expr *Arg = Attr.getArg(0);
2862 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichet7da11662010-12-20 01:41:49 +00002863 if (Str == 0 || Str->isWide()) {
2864 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2865 << "uuid" << 1;
2866 return;
2867 }
2868
2869 llvm::StringRef StrRef = Str->getString();
2870
2871 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2872 StrRef.back() == '}';
2873
2874 // Validate GUID length.
2875 if (IsCurly && StrRef.size() != 38) {
2876 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2877 return;
2878 }
2879 if (!IsCurly && StrRef.size() != 36) {
2880 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2881 return;
2882 }
2883
2884 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2885 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlsson19588aa2011-01-23 21:07:30 +00002886 llvm::StringRef::iterator I = StrRef.begin();
2887 if (IsCurly) // Skip the optional '{'
2888 ++I;
2889
2890 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00002891 if (i == 8 || i == 13 || i == 18 || i == 23) {
2892 if (*I != '-') {
2893 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2894 return;
2895 }
2896 } else if (!isxdigit(*I)) {
2897 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2898 return;
2899 }
2900 I++;
2901 }
Francois Picheta83957a2010-12-19 06:50:37 +00002902
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002903 D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00002904 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00002905 } else
Francois Picheta83957a2010-12-19 06:50:37 +00002906 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00002907}
2908
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002909//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002910// Top Level Sema Entry Points
2911//===----------------------------------------------------------------------===//
2912
Chandler Carruthedc2c642011-07-02 00:01:44 +00002913static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
2914 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00002915 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00002916 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
2917 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
2918 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00002919 default:
2920 break;
2921 }
2922}
Abramo Bagnara50099372010-04-30 13:10:51 +00002923
Chandler Carruthedc2c642011-07-02 00:01:44 +00002924static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
2925 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002926 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00002927 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
2928 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002929 case AttributeList::AT_IBOutletCollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002930 handleIBOutletCollection(S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002931 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002932 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002933 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002934 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00002935 case AttributeList::AT_neon_vector_type:
2936 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002937 // Ignore these, these are type attributes, handled by
2938 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002939 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00002940 case AttributeList::AT_device:
2941 case AttributeList::AT_host:
2942 case AttributeList::AT_overloadable:
2943 // Ignore, this is a non-inheritable attribute, handled
2944 // by ProcessNonInheritableDeclAttr.
2945 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00002946 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
2947 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002948 case AttributeList::AT_always_inline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002949 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002950 case AttributeList::AT_analyzer_noreturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002951 handleAnalyzerNoReturnAttr (S, D, Attr); break;
2952 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
2953 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002954 case AttributeList::AT_carries_dependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002955 handleDependencyAttr (S, D, Attr); break;
2956 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
2957 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
2958 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
2959 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
2960 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002961 case AttributeList::AT_ext_vector_type:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002962 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002963 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00002964 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
2965 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
2966 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
2967 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002968 case AttributeList::AT_launch_bounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002969 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00002970 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00002971 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
2972 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
2973 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
2974 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
2975 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002976 case AttributeList::AT_ownership_returns:
2977 case AttributeList::AT_ownership_takes:
2978 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002979 handleOwnershipAttr (S, D, Attr); break;
2980 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
2981 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
2982 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
2983 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
2984 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002985
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00002986 case AttributeList::AT_objc_ownership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002987 handleObjCOwnershipAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00002988 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002989 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00002990
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002991 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00002992 case AttributeList::AT_cf_consumed:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002993 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00002994 case AttributeList::AT_ns_consumes_self:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002995 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00002996
2997 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00002998 case AttributeList::AT_ns_returns_not_retained:
2999 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003000 case AttributeList::AT_ns_returns_retained:
3001 case AttributeList::AT_cf_returns_retained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003002 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003003
Nate Begemanf2758702009-06-26 06:32:41 +00003004 case AttributeList::AT_reqd_wg_size:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003005 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00003006
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003007 case AttributeList::AT_init_priority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003008 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003009
Chandler Carruthedc2c642011-07-02 00:01:44 +00003010 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3011 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
3012 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3013 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
3014 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
3015 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3016 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3017 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003018 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003019 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3020 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3021 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003022 case AttributeList::AT_transparent_union:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003023 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003024 break;
Chris Lattner677a3582009-02-14 08:09:34 +00003025 case AttributeList::AT_objc_exception:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003026 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00003027 break;
John McCall86bc21f2011-03-02 11:33:24 +00003028 case AttributeList::AT_objc_method_family:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003029 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003030 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003031 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
3032 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3033 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3034 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3035 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3036 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3037 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3038 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3039 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003040 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003041 // Just ignore
3042 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00003043 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003044 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00003045 break;
John McCallab26cfa2010-02-05 21:31:56 +00003046 case AttributeList::AT_stdcall:
3047 case AttributeList::AT_cdecl:
3048 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003049 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003050 case AttributeList::AT_pascal:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003051 case AttributeList::AT_pcs:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003052 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003053 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003054 case AttributeList::AT_opencl_kernel_function:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003055 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003056 break;
Francois Picheta83957a2010-12-19 06:50:37 +00003057 case AttributeList::AT_uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003058 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00003059 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003060 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003061 // Ask target about the attribute.
3062 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3063 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00003064 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3065 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003066 break;
3067 }
3068}
3069
Peter Collingbourneb331b262011-01-21 02:08:45 +00003070/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3071/// the attribute applies to decls. If the attribute is a type attribute, just
3072/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3073/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00003074static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3075 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003076 bool NonInheritable, bool Inheritable) {
3077 if (Attr.isInvalid())
3078 return;
3079
3080 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3081 // FIXME: Try to deal with other __declspec attributes!
3082 return;
3083
3084 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00003085 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00003086
3087 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00003088 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00003089}
3090
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003091/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3092/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00003093void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003094 const AttributeList *AttrList,
3095 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003096 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003097 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindolac18086a2010-02-23 22:00:30 +00003098 }
3099
3100 // GCC accepts
3101 // static int a9 __attribute__((weakref));
3102 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003103 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003104 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00003105 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00003106 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003107 }
3108}
3109
Ryan Flynn7d470f32009-07-30 03:15:39 +00003110/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3111/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00003112NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00003113 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003114 NamedDecl *NewD = 0;
3115 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3116 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003117 FD->getInnerLocStart(),
Ryan Flynn7d470f32009-07-30 03:15:39 +00003118 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00003119 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00003120 if (FD->getQualifier()) {
3121 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00003122 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00003123 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003124 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3125 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003126 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00003127 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00003128 VD->getStorageClass(),
3129 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00003130 if (VD->getQualifier()) {
3131 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00003132 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00003133 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003134 }
3135 return NewD;
3136}
3137
3138/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3139/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00003140void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00003141 if (W.getUsed()) return; // only do this once
3142 W.setUsed(true);
3143 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3144 IdentifierInfo *NDId = ND->getIdentifier();
3145 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003146 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3147 NDId->getName()));
3148 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00003149 WeakTopLevelDecl.push_back(NewD);
3150 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3151 // to insert Decl at TU scope, sorry.
3152 DeclContext *SavedContext = CurContext;
3153 CurContext = Context.getTranslationUnitDecl();
3154 PushOnScopeChains(NewD, S);
3155 CurContext = SavedContext;
3156 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003157 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003158 }
3159}
3160
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003161/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3162/// it, apply them to D. This is a bit tricky because PD can have attributes
3163/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003164void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3165 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00003166 // It's valid to "forward-declare" #pragma weak, in which case we
3167 // have to do this.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003168 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCall6fe02402010-10-27 00:59:00 +00003169 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3170 if (IdentifierInfo *Id = ND->getIdentifier()) {
3171 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3172 = WeakUndeclaredIdentifiers.find(Id);
3173 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3174 WeakInfo W = I->second;
3175 DeclApplyPragmaWeak(S, ND, W);
3176 WeakUndeclaredIdentifiers[Id] = W;
3177 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003178 }
3179 }
3180 }
3181
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003182 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00003183 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003184 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003185
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003186 // Walk the declarator structure, applying decl attributes that were in a type
3187 // position to the decl itself. This handles cases like:
3188 // int *__attr__(x)** D;
3189 // when X is a decl attribute.
3190 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3191 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003192 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003193
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003194 // Finally, apply any attributes on the decl itself.
3195 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003196 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003197}
John McCall28a6aea2009-11-04 02:18:39 +00003198
John McCall31168b02011-06-15 23:02:42 +00003199/// Is the given declaration allowed to use a forbidden type?
3200static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3201 // Private ivars are always okay. Unfortunately, people don't
3202 // always properly make their ivars private, even in system headers.
3203 // Plus we need to make fields okay, too.
3204 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
3205 return false;
3206
3207 // Require it to be declared in a system header.
3208 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3209}
3210
3211/// Handle a delayed forbidden-type diagnostic.
3212static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3213 Decl *decl) {
3214 if (decl && isForbiddenTypeAllowed(S, decl)) {
3215 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3216 "this system declaration uses an unsupported type"));
3217 return;
3218 }
3219
3220 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3221 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3222 diag.Triggered = true;
3223}
3224
John McCallc1465822011-02-14 07:13:47 +00003225// This duplicates a vector push_back but hides the need to know the
3226// size of the type.
3227void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3228 assert(StackSize <= StackCapacity);
3229
3230 // Grow the stack if necessary.
3231 if (StackSize == StackCapacity) {
3232 unsigned newCapacity = 2 * StackCapacity + 2;
3233 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3234 const char *oldBuffer = (const char*) Stack;
3235
3236 if (StackCapacity)
3237 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3238
3239 delete[] oldBuffer;
3240 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3241 StackCapacity = newCapacity;
3242 }
3243
3244 assert(StackSize < StackCapacity);
3245 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall86121512010-01-27 03:50:35 +00003246}
3247
John McCallc1465822011-02-14 07:13:47 +00003248void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3249 Decl *decl) {
3250 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall86121512010-01-27 03:50:35 +00003251
John McCallc1465822011-02-14 07:13:47 +00003252 // Check the invariants.
3253 assert(DD.StackSize >= state.SavedStackSize);
3254 assert(state.SavedStackSize >= DD.ActiveStackBase);
3255 assert(DD.ParsingDepth > 0);
3256
3257 // Drop the parsing depth.
3258 DD.ParsingDepth--;
3259
3260 // If there are no active diagnostics, we're done.
3261 if (DD.StackSize == DD.ActiveStackBase)
John McCall86121512010-01-27 03:50:35 +00003262 return;
3263
John McCall86121512010-01-27 03:50:35 +00003264 // We only want to actually emit delayed diagnostics when we
3265 // successfully parsed a decl.
Argyrios Kyrtzidisd8a27712011-06-24 19:59:27 +00003266 if (decl && !decl->isInvalidDecl()) {
John McCallc1465822011-02-14 07:13:47 +00003267 // We emit all the active diagnostics, not just those starting
3268 // from the saved state. The idea is this: we get one push for a
John McCall86121512010-01-27 03:50:35 +00003269 // decl spec and another for each declarator; in a decl group like:
3270 // deprecated_typedef foo, *bar, baz();
3271 // only the declarator pops will be passed decls. This is correct;
3272 // we really do need to consider delayed diagnostics from the decl spec
3273 // for each of the different declarations.
John McCallc1465822011-02-14 07:13:47 +00003274 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3275 DelayedDiagnostic &diag = DD.Stack[i];
3276 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00003277 continue;
3278
John McCallc1465822011-02-14 07:13:47 +00003279 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00003280 case DelayedDiagnostic::Deprecation:
John McCallc1465822011-02-14 07:13:47 +00003281 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003282 break;
3283
3284 case DelayedDiagnostic::Access:
John McCallc1465822011-02-14 07:13:47 +00003285 S.HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003286 break;
John McCall31168b02011-06-15 23:02:42 +00003287
3288 case DelayedDiagnostic::ForbiddenType:
3289 handleDelayedForbiddenType(S, diag, decl);
3290 break;
John McCall86121512010-01-27 03:50:35 +00003291 }
3292 }
3293 }
3294
John McCall1064d7e2010-03-16 05:22:47 +00003295 // Destroy all the delayed diagnostics we're about to pop off.
John McCallc1465822011-02-14 07:13:47 +00003296 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor899b68f2011-03-23 15:13:44 +00003297 DD.Stack[i].Destroy();
John McCall1064d7e2010-03-16 05:22:47 +00003298
John McCallc1465822011-02-14 07:13:47 +00003299 DD.StackSize = state.SavedStackSize;
John McCall28a6aea2009-11-04 02:18:39 +00003300}
3301
3302static bool isDeclDeprecated(Decl *D) {
3303 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003304 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00003305 return true;
3306 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3307 return false;
3308}
3309
John McCallb45a1e72010-08-26 02:13:20 +00003310void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00003311 Decl *Ctx) {
3312 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00003313 return;
3314
John McCall86121512010-01-27 03:50:35 +00003315 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003316 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003317 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003318 << DD.getDeprecationDecl()->getDeclName()
3319 << DD.getDeprecationMessage();
Fariborz Jahanian551063102010-10-06 21:18:44 +00003320 else
3321 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003322 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00003323}
3324
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003325void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003326 SourceLocation Loc,
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003327 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00003328 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00003329 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3330 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall28a6aea2009-11-04 02:18:39 +00003331 return;
3332 }
3333
3334 // Otherwise, don't warn if our current context is deprecated.
3335 if (isDeclDeprecated(cast<Decl>(CurContext)))
3336 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003337 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003338 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3339 << Message;
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003340 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00003341 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003342 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003343 else {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003344 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003345 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3346 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003347 }
John McCall28a6aea2009-11-04 02:18:39 +00003348}