blob: 75268b8cc5a2f0621e03dd7cd75889caaf50f187 [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"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
John McCall31168b02011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000023#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000025#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000026#include "clang/Sema/Lookup.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000028using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000029using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000030
John McCall5fca7ea2011-03-02 12:29:23 +000031/// These constants match the enumerated choices of
32/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000033enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000034 ExpectedFunction,
35 ExpectedUnion,
36 ExpectedVariableOrFunction,
37 ExpectedFunctionOrMethod,
38 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000040 ExpectedFunctionMethodOrClass,
John McCall5fca7ea2011-03-02 12:29:23 +000041 ExpectedFunctionMethodOrParameter,
42 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000043 ExpectedVariable,
44 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000045 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000046 ExpectedFieldOrGlobalVar,
Hans Wennborgd3b01bc2012-06-23 11:51:46 +000047 ExpectedStruct,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000048 ExpectedVariableFunctionOrTag,
Hans Wennborgd3b01bc2012-06-23 11:51:46 +000049 ExpectedTLSVar
John McCall5fca7ea2011-03-02 12:29:23 +000050};
51
Chris Lattner58418ff2008-06-29 00:16:31 +000052//===----------------------------------------------------------------------===//
53// Helper functions
54//===----------------------------------------------------------------------===//
55
Chandler Carruthff4c4f02011-07-01 23:49:12 +000056static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000057 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000058 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000059 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000060 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000061 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000062 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000063 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000064 Ty = decl->getUnderlyingType();
65 else
66 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000067
Chris Lattner2c6fcf52008-06-26 18:38:35 +000068 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000069 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000070 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000071 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000072
John McCall9dd450b2009-09-21 23:43:11 +000073 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000074}
75
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000076// FIXME: We should provide an abstraction around a method or function
77// to provide the following bits of information.
78
Nuno Lopes518e3702009-12-20 23:11:08 +000079/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000080/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000081static bool isFunction(const Decl *D) {
82 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000083}
84
85/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000086/// type (function or function-typed variable) or an Objective-C
87/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000088static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000089 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000090}
91
Fariborz Jahanian4447e172009-05-15 23:15:03 +000092/// isFunctionOrMethodOrBlock - Return true if the given decl has function
93/// type (function or function-typed variable) or an Objective-C
94/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000095static bool isFunctionOrMethodOrBlock(const Decl *D) {
96 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000097 return true;
98 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000099 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000100 QualType Ty = V->getType();
101 return Ty->isBlockPointerType();
102 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000103 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000104}
105
John McCall3882ace2011-01-05 12:14:39 +0000106/// Return true if the given decl has a declarator that should have
107/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000108static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000109 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000110 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
111 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000112}
113
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000114/// hasFunctionProto - Return true if the given decl has a argument
115/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000116/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000117static bool hasFunctionProto(const Decl *D) {
118 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000119 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000120 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000121 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000122 return true;
123 }
124}
125
126/// getFunctionOrMethodNumArgs - Return number of function or method
127/// arguments. It is an error to call this on a K&R function (use
128/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000129static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
130 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000131 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000132 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000133 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000134 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000135}
136
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
138 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000139 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000140 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000141 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000142
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000143 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000144}
145
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000146static QualType getFunctionOrMethodResultType(const Decl *D) {
147 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000148 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000149 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000150}
151
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152static bool isFunctionOrMethodVariadic(const Decl *D) {
153 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000154 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000155 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000156 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000157 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000158 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000159 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000160 }
161}
162
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000163static bool isInstanceMethod(const Decl *D) {
164 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000165 return MethodDecl->isInstance();
166 return false;
167}
168
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000169static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000170 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000171 if (!PT)
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 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
175 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000176 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000177
John McCall96fa4842010-05-17 21:00:27 +0000178 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000179
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000180 // FIXME: Should we walk the chain of classes?
181 return ClsName == &Ctx.Idents.get("NSString") ||
182 ClsName == &Ctx.Idents.get("NSMutableString");
183}
184
Daniel Dunbar980c6692008-09-26 03:32:58 +0000185static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000186 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000187 if (!PT)
188 return false;
189
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000190 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000191 if (!RT)
192 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000193
Daniel Dunbar980c6692008-09-26 03:32:58 +0000194 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000195 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000196 return false;
197
198 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
199}
200
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000201/// \brief Check if the attribute has exactly as many args as Num. May
202/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000203static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
204 unsigned int Num) {
205 if (Attr.getNumArgs() != Num) {
206 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
207 return false;
208 }
209
210 return true;
211}
212
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000213
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000214/// \brief Check if the attribute has at least as many args as Num. May
215/// output an error.
216static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
217 unsigned int Num) {
218 if (Attr.getNumArgs() < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000219 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
220 return false;
221 }
222
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000223 return true;
224}
225
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000226/// \brief Check if IdxExpr is a valid argument index for a function or
227/// instance method D. May output an error.
228///
229/// \returns true if IdxExpr is a valid index.
230static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
231 StringRef AttrName,
232 SourceLocation AttrLoc,
233 unsigned AttrArgNum,
234 const Expr *IdxExpr,
235 uint64_t &Idx)
236{
237 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
238
239 // In C++ the implicit 'this' function parameter also counts.
240 // Parameters are counted from one.
241 const bool HasImplicitThisParam = isInstanceMethod(D);
242 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
243 const unsigned FirstIdx = 1;
244
245 llvm::APSInt IdxInt;
246 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
247 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
248 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
249 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
250 return false;
251 }
252
253 Idx = IdxInt.getLimitedValue();
254 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
255 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
256 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
257 return false;
258 }
259 Idx--; // Convert to zero-based.
260 if (HasImplicitThisParam) {
261 if (Idx == 0) {
262 S.Diag(AttrLoc,
263 diag::err_attribute_invalid_implicit_this_argument)
264 << AttrName << IdxExpr->getSourceRange();
265 return false;
266 }
267 --Idx;
268 }
269
270 return true;
271}
272
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000273///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000274/// \brief Check if passed in Decl is a field or potentially shared global var
275/// \return true if the Decl is a field or potentially shared global variable
276///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000277static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000278 if (isa<FieldDecl>(D))
279 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000280 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000281 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
282
283 return false;
284}
285
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000286/// \brief Check if the passed-in expression is of type int or bool.
287static bool isIntOrBool(Expr *Exp) {
288 QualType QT = Exp->getType();
289 return QT->isBooleanType() || QT->isIntegerType();
290}
291
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000292
293// Check to see if the type is a smart pointer of some kind. We assume
294// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000295static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
296 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
297 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000298 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000299 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000300
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000301 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
302 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000303 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000304 return false;
305
306 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000307}
308
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000309/// \brief Check if passed in Decl is a pointer type.
310/// Note that this function may produce an error message.
311/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000312static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
313 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000314 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000315 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000316 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000317 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000318
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000319 if (const RecordType *RT = QT->getAs<RecordType>()) {
320 // If it's an incomplete type, it could be a smart pointer; skip it.
321 // (We don't want to force template instantiation if we can avoid it,
322 // since that would alter the order in which templates are instantiated.)
323 if (RT->isIncompleteType())
324 return true;
325
326 if (threadSafetyCheckIsSmartPointer(S, RT))
327 return true;
328 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000329
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000330 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000331 << Attr.getName()->getName() << QT;
332 } else {
333 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
334 << Attr.getName();
335 }
336 return false;
337}
338
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000339/// \brief Checks that the passed in QualType either is of RecordType or points
340/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000341static const RecordType *getRecordType(QualType QT) {
342 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000343 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000344
345 // Now check if we point to record type.
346 if (const PointerType *PT = QT->getAs<PointerType>())
347 return PT->getPointeeType()->getAs<RecordType>();
348
349 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000350}
351
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000352
Jordy Rose740b0c22012-05-08 03:27:22 +0000353static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
354 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000355 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
356 if (RT->getDecl()->getAttr<LockableAttr>())
357 return true;
358 return false;
359}
360
361
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000362/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000363/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000364static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
365 QualType Ty) {
366 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000367
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000368 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000369 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000370 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000371 << Attr.getName() << Ty.getAsString();
372 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000373 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000374
Michael Hana9171bc2012-08-03 17:40:43 +0000375 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000376 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000377 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000378
379 // Allow smart pointers to be used as lockable objects.
380 // FIXME -- Check the type that the smart pointer points to.
381 if (threadSafetyCheckIsSmartPointer(S, RT))
382 return;
383
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000384 // Check if the type is lockable.
385 RecordDecl *RD = RT->getDecl();
386 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000387 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000388
389 // Else check if any base classes are lockable.
390 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
391 CXXBasePaths BPaths(false, false);
392 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
393 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000394 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000395
396 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
397 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000398}
399
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000400/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000401/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000402/// \param Sidx The attribute argument index to start checking with.
403/// \param ParamIdxOk Whether an argument can be indexing into a function
404/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000405static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000406 const AttributeList &Attr,
407 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000408 int Sidx = 0,
409 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000410 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000411 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000412
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000413 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000414 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000415 Args.push_back(ArgExp);
416 continue;
417 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000418
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000419 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000420 if (StrLit->getLength() == 0 ||
421 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000422 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000423 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000424 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000425 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000426 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000427
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000428 // We allow constant strings to be used as a placeholder for expressions
429 // that are not valid C++ syntax, but warn that they are ignored.
430 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
431 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000432 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000433 continue;
434 }
435
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000436 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000437
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000438 // A pointer to member expression of the form &MyClass::mu is treated
439 // specially -- we need to look at the type of the member.
440 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
441 if (UOp->getOpcode() == UO_AddrOf)
442 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
443 if (DRE->getDecl()->isCXXInstanceMember())
444 ArgTy = DRE->getDecl()->getType();
445
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000446 // First see if we can just cast to record type, or point to record type.
447 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000448
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000449 // Now check if we index into a record type function param.
450 if(!RT && ParamIdxOk) {
451 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000452 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
453 if(FD && IL) {
454 unsigned int NumParams = FD->getNumParams();
455 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000456 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
457 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
458 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000459 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
460 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000461 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000462 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000464 }
465 }
466
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000467 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000468
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000469 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000470 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000471}
472
Chris Lattner58418ff2008-06-29 00:16:31 +0000473//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000474// Attribute Implementations
475//===----------------------------------------------------------------------===//
476
Daniel Dunbar032db472008-07-31 22:40:48 +0000477// FIXME: All this manual attribute parsing code is gross. At the
478// least add some helper functions to check most argument patterns (#
479// and types of args).
480
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000481enum ThreadAttributeDeclKind {
482 ThreadExpectedFieldOrGlobalVar,
483 ThreadExpectedFunctionOrMethod,
484 ThreadExpectedClassOrStruct
485};
486
Michael Hana9171bc2012-08-03 17:40:43 +0000487static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000488 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000489 assert(!Attr.isInvalid());
490
491 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000492 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000493
494 // D must be either a member field or global (potentially shared) variable.
495 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000496 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
497 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000498 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000499 }
500
Michael Han3be3b442012-07-23 18:48:41 +0000501 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000502}
503
Michael Han3be3b442012-07-23 18:48:41 +0000504static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
505 if (!checkGuardedVarAttrCommon(S, D, Attr))
506 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000507
Michael Han3be3b442012-07-23 18:48:41 +0000508 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
509}
510
Michael Hana9171bc2012-08-03 17:40:43 +0000511static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000512 const AttributeList &Attr) {
513 if (!checkGuardedVarAttrCommon(S, D, Attr))
514 return;
515
516 if (!threadSafetyCheckIsPointer(S, D, Attr))
517 return;
518
519 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
520}
521
Michael Hana9171bc2012-08-03 17:40:43 +0000522static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
523 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000524 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000525 assert(!Attr.isInvalid());
526
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000528 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000529
530 // D must be either a member field or global (potentially shared) variable.
531 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000532 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
533 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000534 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000535 }
536
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000537 SmallVector<Expr*, 1> Args;
538 // check that all arguments are lockable objects
539 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
540 unsigned Size = Args.size();
541 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000542 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000543
Michael Han3be3b442012-07-23 18:48:41 +0000544 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000545
Michael Han3be3b442012-07-23 18:48:41 +0000546 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000547}
548
Michael Han3be3b442012-07-23 18:48:41 +0000549static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
550 Expr *Arg = 0;
551 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
552 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000553
Michael Han3be3b442012-07-23 18:48:41 +0000554 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
555}
556
Michael Hana9171bc2012-08-03 17:40:43 +0000557static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000558 const AttributeList &Attr) {
559 Expr *Arg = 0;
560 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
561 return;
562
563 if (!threadSafetyCheckIsPointer(S, D, Attr))
564 return;
565
566 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
567 S.Context, Arg));
568}
569
Michael Hana9171bc2012-08-03 17:40:43 +0000570static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000571 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000572 assert(!Attr.isInvalid());
573
574 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000575 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000576
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000577 // FIXME: Lockable structs for C code.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000578 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000579 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
580 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Han3be3b442012-07-23 18:48:41 +0000581 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000582 }
583
Michael Han3be3b442012-07-23 18:48:41 +0000584 return true;
585}
586
587static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
588 if (!checkLockableAttrCommon(S, D, Attr))
589 return;
590
591 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
592}
593
Michael Hana9171bc2012-08-03 17:40:43 +0000594static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000595 const AttributeList &Attr) {
596 if (!checkLockableAttrCommon(S, D, Attr))
597 return;
598
599 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000600}
601
602static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
603 const AttributeList &Attr) {
604 assert(!Attr.isInvalid());
605
606 if (!checkAttributeNumArgs(S, Attr, 0))
607 return;
608
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000609 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000610 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
611 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000612 return;
613 }
614
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000615 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000616 S.Context));
617}
618
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000619static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000620 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000621 assert(!Attr.isInvalid());
622
623 if (!checkAttributeNumArgs(S, Attr, 0))
624 return;
625
626 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
627 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
628 << Attr.getName() << ExpectedFunctionOrMethod;
629 return;
630 }
631
632 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
Nick Lewyckycfb45172012-07-24 01:37:23 +0000633 S.Context));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000634}
635
Michael Hana9171bc2012-08-03 17:40:43 +0000636static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
637 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000638 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000639 assert(!Attr.isInvalid());
640
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000641 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000642 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000643
644 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000645 ValueDecl *VD = dyn_cast<ValueDecl>(D);
646 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000647 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
648 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000649 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000650 }
651
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000652 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000653 QualType QT = VD->getType();
654 if (!QT->isDependentType()) {
655 const RecordType *RT = getRecordType(QT);
656 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000657 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000658 << Attr.getName();
659 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000660 }
661 }
662
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000663 // Check that all arguments are lockable objects.
664 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Han3be3b442012-07-23 18:48:41 +0000665 if (Args.size() == 0)
666 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000667
Michael Han3be3b442012-07-23 18:48:41 +0000668 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000669}
670
Michael Hana9171bc2012-08-03 17:40:43 +0000671static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000672 const AttributeList &Attr) {
673 SmallVector<Expr*, 1> Args;
674 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
675 return;
676
677 Expr **StartArg = &Args[0];
678 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +0000679 StartArg, Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000680}
681
Michael Hana9171bc2012-08-03 17:40:43 +0000682static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000683 const AttributeList &Attr) {
684 SmallVector<Expr*, 1> Args;
685 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
686 return;
687
688 Expr **StartArg = &Args[0];
689 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +0000690 StartArg, Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000691}
692
Michael Hana9171bc2012-08-03 17:40:43 +0000693static bool checkLockFunAttrCommon(Sema &S, Decl *D,
694 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000695 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000696 assert(!Attr.isInvalid());
697
698 // zero or more arguments ok
699
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000700 // check that the attribute is applied to a function
701 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000702 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
703 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000704 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000705 }
706
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000707 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000708 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000709
Michael Han3be3b442012-07-23 18:48:41 +0000710 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000711}
712
Michael Hana9171bc2012-08-03 17:40:43 +0000713static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000714 const AttributeList &Attr) {
715 SmallVector<Expr*, 1> Args;
716 if (!checkLockFunAttrCommon(S, D, Attr, Args))
717 return;
718
719 unsigned Size = Args.size();
720 Expr **StartArg = Size == 0 ? 0 : &Args[0];
721 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000722 S.Context,
Michael Han3be3b442012-07-23 18:48:41 +0000723 StartArg, Size));
724}
725
Michael Hana9171bc2012-08-03 17:40:43 +0000726static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000727 const AttributeList &Attr) {
728 SmallVector<Expr*, 1> Args;
729 if (!checkLockFunAttrCommon(S, D, Attr, Args))
730 return;
731
732 unsigned Size = Args.size();
733 Expr **StartArg = Size == 0 ? 0 : &Args[0];
734 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000735 S.Context,
Michael Han3be3b442012-07-23 18:48:41 +0000736 StartArg, Size));
737}
738
Michael Hana9171bc2012-08-03 17:40:43 +0000739static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
740 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000741 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000742 assert(!Attr.isInvalid());
743
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000744 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000745 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000746
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000747 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000748 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
749 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000750 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000751 }
752
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000753 if (!isIntOrBool(Attr.getArg(0))) {
754 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Han3be3b442012-07-23 18:48:41 +0000755 << Attr.getName();
756 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000757 }
758
759 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000760 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000761
Michael Han3be3b442012-07-23 18:48:41 +0000762 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000763}
764
Michael Hana9171bc2012-08-03 17:40:43 +0000765static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000766 const AttributeList &Attr) {
767 SmallVector<Expr*, 2> Args;
768 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
769 return;
770
771 unsigned Size = Args.size();
772 Expr **StartArg = Size == 0 ? 0 : &Args[0];
773 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000774 S.Context,
775 Attr.getArg(0),
Michael Han3be3b442012-07-23 18:48:41 +0000776 StartArg, Size));
777}
778
Michael Hana9171bc2012-08-03 17:40:43 +0000779static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000780 const AttributeList &Attr) {
781 SmallVector<Expr*, 2> Args;
782 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
783 return;
784
785 unsigned Size = Args.size();
786 Expr **StartArg = Size == 0 ? 0 : &Args[0];
787 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000788 S.Context,
789 Attr.getArg(0),
Michael Han3be3b442012-07-23 18:48:41 +0000790 StartArg, Size));
791}
792
Michael Hana9171bc2012-08-03 17:40:43 +0000793static bool checkLocksRequiredCommon(Sema &S, Decl *D,
794 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000795 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000796 assert(!Attr.isInvalid());
797
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000798 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000799 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000800
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000801 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000802 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
803 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000804 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000805 }
806
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000807 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000808 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Han3be3b442012-07-23 18:48:41 +0000809 if (Args.size() == 0)
810 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000811
Michael Han3be3b442012-07-23 18:48:41 +0000812 return true;
813}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000814
Michael Hana9171bc2012-08-03 17:40:43 +0000815static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000816 const AttributeList &Attr) {
817 SmallVector<Expr*, 1> Args;
818 if (!checkLocksRequiredCommon(S, D, Attr, Args))
819 return;
820
821 Expr **StartArg = &Args[0];
822 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000823 S.Context,
824 StartArg,
Michael Han3be3b442012-07-23 18:48:41 +0000825 Args.size()));
826}
827
Michael Hana9171bc2012-08-03 17:40:43 +0000828static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000829 const AttributeList &Attr) {
830 SmallVector<Expr*, 1> Args;
831 if (!checkLocksRequiredCommon(S, D, Attr, Args))
832 return;
833
834 Expr **StartArg = &Args[0];
835 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000836 S.Context,
837 StartArg,
Michael Han3be3b442012-07-23 18:48:41 +0000838 Args.size()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000839}
840
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000841static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000842 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000843 assert(!Attr.isInvalid());
844
845 // zero or more arguments ok
846
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000847 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000848 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
849 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000850 return;
851 }
852
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000853 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000854 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000855 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000856 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000857 Expr **StartArg = Size == 0 ? 0 : &Args[0];
858
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000859 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000860 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000861}
862
863static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000864 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000865 assert(!Attr.isInvalid());
866
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000867 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000868 return;
869
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000870 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000871 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
872 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000873 return;
874 }
875
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000876 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000877 SmallVector<Expr*, 1> Args;
878 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
879 unsigned Size = Args.size();
880 if (Size == 0)
881 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000882
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000883 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
884 Args[0]));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000885}
886
887static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000888 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000889 assert(!Attr.isInvalid());
890
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000891 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000892 return;
893
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000894 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000895 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
896 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000897 return;
898 }
899
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000900 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000901 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000902 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000903 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000904 if (Size == 0)
905 return;
906 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000907
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000908 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000909 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000910}
911
912
Chandler Carruthedc2c642011-07-02 00:01:44 +0000913static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
914 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000915 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000916 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000917 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000918 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000919 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000920
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000921 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000922
923 Expr *sizeExpr;
924
925 // Special case where the argument is a template id.
926 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000927 CXXScopeSpec SS;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000928 SourceLocation TemplateKWLoc;
John McCalle66edc12009-11-24 19:00:30 +0000929 UnqualifiedId id;
930 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Michael Hana9171bc2012-08-03 17:40:43 +0000931
Abramo Bagnara7945c982012-01-27 09:46:47 +0000932 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
933 false, false);
Douglas Gregor39c02722011-06-15 16:02:29 +0000934 if (Size.isInvalid())
935 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000936
Douglas Gregor39c02722011-06-15 16:02:29 +0000937 sizeExpr = Size.get();
Douglas Gregor758a8692009-06-17 21:51:59 +0000938 } else {
939 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000940 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor758a8692009-06-17 21:51:59 +0000941 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000942
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000943 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000944 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000945
946 // Instantiate/Install the vector type, and let Sema build the type for us.
947 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000948 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000949 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000950 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000951 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000952
Douglas Gregor758a8692009-06-17 21:51:59 +0000953 // Remember this typedef decl, we will need it later for diagnostics.
954 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000955 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000956}
957
Chandler Carruthedc2c642011-07-02 00:01:44 +0000958static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000959 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000960 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000961 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000962
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000963 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000964 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000965 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000966 // If the alignment is less than or equal to 8 bits, the packed attribute
967 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +0000968 if (!FD->getType()->isDependentType() &&
969 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000970 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000971 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000972 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000973 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000974 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000975 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000976 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000977}
978
Chandler Carruthedc2c642011-07-02 00:01:44 +0000979static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +0000980 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
981 RD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000982 else
983 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
984}
985
Chandler Carruthedc2c642011-07-02 00:01:44 +0000986static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000987 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000988 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000989 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000990
Ted Kremenek1f672822010-02-18 03:08:58 +0000991 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000992 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000993 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000994 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000995 return;
996 }
997
Ted Kremenekd68ec812011-02-04 06:54:16 +0000998 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000999}
1000
Ted Kremenek7fd17232011-09-29 07:02:25 +00001001static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1002 // The IBOutlet/IBOutletCollection attributes only apply to instance
1003 // variables or properties of Objective-C classes. The outlet must also
1004 // have an object reference type.
1005 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1006 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001007 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001008 << Attr.getName() << VD->getType() << 0;
1009 return false;
1010 }
1011 }
1012 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1013 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001014 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001015 << Attr.getName() << PD->getType() << 1;
1016 return false;
1017 }
1018 }
1019 else {
1020 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1021 return false;
1022 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001023
Ted Kremenek7fd17232011-09-29 07:02:25 +00001024 return true;
1025}
1026
Chandler Carruthedc2c642011-07-02 00:01:44 +00001027static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001028 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001029 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +00001030 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001031
1032 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001033 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001034
Ted Kremenek7fd17232011-09-29 07:02:25 +00001035 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001036}
1037
Chandler Carruthedc2c642011-07-02 00:01:44 +00001038static void handleIBOutletCollection(Sema &S, Decl *D,
1039 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001040
1041 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001042 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001043 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1044 return;
1045 }
1046
Ted Kremenek7fd17232011-09-29 07:02:25 +00001047 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001048 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001049
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001050 IdentifierInfo *II = Attr.getParameterName();
1051 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001052 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +00001053
John McCallba7bf592010-08-24 05:47:05 +00001054 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001055 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001056 if (!TypeRep) {
1057 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1058 return;
1059 }
John McCallba7bf592010-08-24 05:47:05 +00001060 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001061 // Diagnose use of non-object type in iboutletcollection attribute.
1062 // FIXME. Gnu attribute extension ignores use of builtin types in
1063 // attributes. So, __attribute__((iboutletcollection(char))) will be
1064 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001065 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001066 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1067 return;
1068 }
Argyrios Kyrtzidis8db67df2011-09-13 18:41:59 +00001069 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1070 QT, Attr.getParameterLoc()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001071}
1072
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001073static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001074 if (const RecordType *UT = T->getAsUnionType())
1075 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1076 RecordDecl *UD = UT->getDecl();
1077 for (RecordDecl::field_iterator it = UD->field_begin(),
1078 itend = UD->field_end(); it != itend; ++it) {
1079 QualType QT = it->getType();
1080 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1081 T = QT;
1082 return;
1083 }
1084 }
1085 }
1086}
1087
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001088static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001089 if (!isFunctionOrMethod(D)) {
1090 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1091 << "alloc_size" << ExpectedFunctionOrMethod;
1092 return;
1093 }
1094
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001095 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1096 return;
1097
1098 // In C++ the implicit 'this' function parameter also counts, and they are
1099 // counted from one.
1100 bool HasImplicitThisParam = isInstanceMethod(D);
1101 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1102
1103 SmallVector<unsigned, 8> SizeArgs;
1104
1105 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1106 E = Attr.arg_end(); I!=E; ++I) {
1107 // The argument must be an integer constant expression.
1108 Expr *Ex = *I;
1109 llvm::APSInt ArgNum;
1110 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1111 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1112 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1113 << "alloc_size" << Ex->getSourceRange();
1114 return;
1115 }
1116
1117 uint64_t x = ArgNum.getZExtValue();
1118
1119 if (x < 1 || x > NumArgs) {
1120 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1121 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1122 return;
1123 }
1124
1125 --x;
1126 if (HasImplicitThisParam) {
1127 if (x == 0) {
1128 S.Diag(Attr.getLoc(),
1129 diag::err_attribute_invalid_implicit_this_argument)
1130 << "alloc_size" << Ex->getSourceRange();
1131 return;
1132 }
1133 --x;
1134 }
1135
1136 // check if the function argument is of an integer type
1137 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1138 if (!T->isIntegerType()) {
1139 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1140 << "alloc_size" << Ex->getSourceRange();
1141 return;
1142 }
1143
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001144 SizeArgs.push_back(x);
1145 }
1146
1147 // check if the function returns a pointer
1148 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1149 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1150 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1151 }
1152
Nuno Lopese44e93a2012-06-18 16:27:56 +00001153 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1154 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001155}
1156
Chandler Carruthedc2c642011-07-02 00:01:44 +00001157static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001158 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1159 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001160 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001162 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001163 return;
1164 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001165
Chandler Carruth743682b2010-11-16 08:35:43 +00001166 // In C++ the implicit 'this' function parameter also counts, and they are
1167 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001168 bool HasImplicitThisParam = isInstanceMethod(D);
1169 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001170
1171 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001172 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001173
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001174 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1175 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001176
1177
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001178 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001179 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001180 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001181 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1182 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001183 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1184 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001185 return;
1186 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001187
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001188 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001189
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001190 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001191 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +00001192 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001193 return;
1194 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001195
Ted Kremenek5224e6a2008-07-21 22:09:15 +00001196 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001197 if (HasImplicitThisParam) {
1198 if (x == 0) {
1199 S.Diag(Attr.getLoc(),
1200 diag::err_attribute_invalid_implicit_this_argument)
1201 << "nonnull" << Ex->getSourceRange();
1202 return;
1203 }
1204 --x;
1205 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001206
1207 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001208 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001209 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001210
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001211 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001212 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001213 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001214 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001215 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001216 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001217
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001218 NonNullArgs.push_back(x);
1219 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001220
1221 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1222 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001223 if (NonNullArgs.empty()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001224 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1225 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001226 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001227 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +00001228 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001229 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001230
Ted Kremenek22813f42010-10-21 18:49:36 +00001231 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001232 if (NonNullArgs.empty()) {
1233 // Warn the trivial case only if attribute is not coming from a
1234 // macro instantiation.
1235 if (Attr.getLoc().isFileID())
1236 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001237 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001238 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001239 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001240
1241 unsigned* start = &NonNullArgs[0];
1242 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001243 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001244 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001245 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001246}
1247
Chandler Carruthedc2c642011-07-02 00:01:44 +00001248static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001249 // This attribute must be applied to a function declaration.
1250 // The first argument to the attribute must be a string,
1251 // the name of the resource, for example "malloc".
1252 // The following arguments must be argument indexes, the arguments must be
1253 // of integer type for Returns, otherwise of pointer type.
1254 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001255 // after being held. free() should be __attribute((ownership_takes)), whereas
1256 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001257
1258 if (!AL.getParameterName()) {
1259 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1260 << AL.getName()->getName() << 1;
1261 return;
1262 }
1263 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001264 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001265 switch (AL.getKind()) {
1266 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001267 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001268 if (AL.getNumArgs() < 1) {
1269 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1270 return;
1271 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001272 break;
1273 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001274 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001275 if (AL.getNumArgs() < 1) {
1276 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1277 return;
1278 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001279 break;
1280 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001281 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001282 if (AL.getNumArgs() > 1) {
1283 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1284 << AL.getNumArgs() + 1;
1285 return;
1286 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001287 break;
1288 default:
1289 // This should never happen given how we are called.
1290 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001291 }
1292
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001293 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001294 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1295 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001296 return;
1297 }
1298
Chandler Carruth743682b2010-11-16 08:35:43 +00001299 // In C++ the implicit 'this' function parameter also counts, and they are
1300 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001301 bool HasImplicitThisParam = isInstanceMethod(D);
1302 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001303
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001304 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001305
1306 // Normalize the argument, __foo__ becomes foo.
1307 if (Module.startswith("__") && Module.endswith("__"))
1308 Module = Module.substr(2, Module.size() - 4);
1309
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001310 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001311
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001312 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1313 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001314
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001315 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001316 llvm::APSInt ArgNum(32);
1317 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1318 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1319 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1320 << AL.getName()->getName() << IdxExpr->getSourceRange();
1321 continue;
1322 }
1323
1324 unsigned x = (unsigned) ArgNum.getZExtValue();
1325
1326 if (x > NumArgs || x < 1) {
1327 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1328 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1329 continue;
1330 }
1331 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001332 if (HasImplicitThisParam) {
1333 if (x == 0) {
1334 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1335 << "ownership" << IdxExpr->getSourceRange();
1336 return;
1337 }
1338 --x;
1339 }
1340
Ted Kremenekd21139a2010-07-31 01:52:11 +00001341 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001342 case OwnershipAttr::Takes:
1343 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001344 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001345 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001346 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1347 // FIXME: Should also highlight argument in decl.
1348 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001349 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001350 << "pointer"
1351 << IdxExpr->getSourceRange();
1352 continue;
1353 }
1354 break;
1355 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001356 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001357 if (AL.getNumArgs() > 1) {
1358 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001359 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001360 llvm::APSInt ArgNum(32);
1361 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1362 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1363 S.Diag(AL.getLoc(), diag::err_ownership_type)
1364 << "ownership_returns" << "integer"
1365 << IdxExpr->getSourceRange();
1366 return;
1367 }
1368 }
1369 break;
1370 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001371 } // switch
1372
1373 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001374 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001375 i = D->specific_attr_begin<OwnershipAttr>(),
1376 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001377 i != e; ++i) {
1378 if ((*i)->getOwnKind() != K) {
1379 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1380 I!=E; ++I) {
1381 if (x == *I) {
1382 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1383 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384 }
1385 }
1386 }
1387 }
1388 OwnershipArgs.push_back(x);
1389 }
1390
1391 unsigned* start = OwnershipArgs.data();
1392 unsigned size = OwnershipArgs.size();
1393 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001394
1395 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1396 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1397 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001398 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001399
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001400 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001401 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001402}
1403
John McCall7a198ce2011-02-08 22:35:49 +00001404/// Whether this declaration has internal linkage for the purposes of
1405/// things that want to complain about things not have internal linkage.
1406static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1407 switch (D->getLinkage()) {
1408 case NoLinkage:
1409 case InternalLinkage:
1410 return true;
1411
1412 // Template instantiations that go from external to unique-external
1413 // shouldn't get diagnosed.
1414 case UniqueExternalLinkage:
1415 return true;
1416
1417 case ExternalLinkage:
1418 return false;
1419 }
1420 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +00001421}
1422
Chandler Carruthedc2c642011-07-02 00:01:44 +00001423static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001424 // Check the attribute arguments.
1425 if (Attr.getNumArgs() > 1) {
1426 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1427 return;
1428 }
1429
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001430 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001432 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001433 return;
1434 }
1435
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001436 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001437
Rafael Espindolac18086a2010-02-23 22:00:30 +00001438 // gcc rejects
1439 // class c {
1440 // static int a __attribute__((weakref ("v2")));
1441 // static int b() __attribute__((weakref ("f3")));
1442 // };
1443 // and ignores the attributes of
1444 // void f(void) {
1445 // static int a __attribute__((weakref ("v2")));
1446 // }
1447 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001448 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001449 if (!Ctx->isFileContext()) {
1450 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001451 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001452 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001453 }
1454
1455 // The GCC manual says
1456 //
1457 // At present, a declaration to which `weakref' is attached can only
1458 // be `static'.
1459 //
1460 // It also says
1461 //
1462 // Without a TARGET,
1463 // given as an argument to `weakref' or to `alias', `weakref' is
1464 // equivalent to `weak'.
1465 //
1466 // gcc 4.4.1 will accept
1467 // int a7 __attribute__((weakref));
1468 // as
1469 // int a7 __attribute__((weak));
1470 // This looks like a bug in gcc. We reject that for now. We should revisit
1471 // it if this behaviour is actually used.
1472
John McCall7a198ce2011-02-08 22:35:49 +00001473 if (!hasEffectivelyInternalLinkage(nd)) {
1474 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001475 return;
1476 }
1477
1478 // GCC rejects
1479 // static ((alias ("y"), weakref)).
1480 // Should we? How to check that weakref is before or after alias?
1481
1482 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001483 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001484 Arg = Arg->IgnoreParenCasts();
1485 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1486
Douglas Gregorfb65e592011-07-27 05:40:30 +00001487 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001488 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1489 << "weakref" << 1;
1490 return;
1491 }
1492 // GCC will accept anything as the argument of weakref. Should we
1493 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001494 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001495 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001496 }
1497
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001498 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001499}
1500
Chandler Carruthedc2c642011-07-02 00:01:44 +00001501static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001502 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001503 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001505 return;
1506 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001507
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001508 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001509 Arg = Arg->IgnoreParenCasts();
1510 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001511
Douglas Gregorfb65e592011-07-27 05:40:30 +00001512 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001514 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001515 return;
1516 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001517
Douglas Gregore8bbc122011-09-02 00:18:52 +00001518 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001519 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1520 return;
1521 }
1522
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001523 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001524
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001525 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001526 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001527}
1528
Quentin Colombet4e172062012-11-01 23:55:47 +00001529static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1530 // Check the attribute arguments.
1531 if (!checkAttributeNumArgs(S, Attr, 0))
1532 return;
1533
1534 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1535 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1536 << Attr.getName() << ExpectedFunctionOrMethod;
1537 return;
1538 }
1539
1540 D->addAttr(::new (S.Context) MinSizeAttr(Attr.getRange(), S.Context));
1541}
1542
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001543static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1544 // Check the attribute arguments.
1545 if (!checkAttributeNumArgs(S, Attr, 0))
1546 return;
1547
1548 if (!isa<FunctionDecl>(D)) {
1549 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1550 << Attr.getName() << ExpectedFunction;
1551 return;
1552 }
1553
1554 if (D->hasAttr<HotAttr>()) {
1555 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1556 << Attr.getName() << "hot";
1557 return;
1558 }
1559
1560 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1561}
1562
1563static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1564 // Check the attribute arguments.
1565 if (!checkAttributeNumArgs(S, Attr, 0))
1566 return;
1567
1568 if (!isa<FunctionDecl>(D)) {
1569 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1570 << Attr.getName() << ExpectedFunction;
1571 return;
1572 }
1573
1574 if (D->hasAttr<ColdAttr>()) {
1575 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1576 << Attr.getName() << "cold";
1577 return;
1578 }
1579
1580 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1581}
1582
Chandler Carruthedc2c642011-07-02 00:01:44 +00001583static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001584 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001585 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001586 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001587
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001588 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001589 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001590 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001591 return;
1592 }
1593
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001594 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001595}
1596
Chandler Carruthedc2c642011-07-02 00:01:44 +00001597static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1598 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001599 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001600 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1602 return;
1603 }
1604
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001605 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001606 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001607 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001608 return;
1609 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001610
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001611 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001612}
1613
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001614static void handleTLSModelAttr(Sema &S, Decl *D,
1615 const AttributeList &Attr) {
1616 // Check the attribute arguments.
1617 if (Attr.getNumArgs() != 1) {
1618 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1619 return;
1620 }
1621
1622 Expr *Arg = Attr.getArg(0);
1623 Arg = Arg->IgnoreParenCasts();
1624 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1625
1626 // Check that it is a string.
1627 if (!Str) {
1628 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1629 return;
1630 }
1631
1632 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1633 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1634 << Attr.getName() << ExpectedTLSVar;
1635 return;
1636 }
1637
1638 // Check that the value.
1639 StringRef Model = Str->getString();
1640 if (Model != "global-dynamic" && Model != "local-dynamic"
1641 && Model != "initial-exec" && Model != "local-exec") {
1642 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1643 return;
1644 }
1645
1646 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1647 Model));
1648}
1649
Chandler Carruthedc2c642011-07-02 00:01:44 +00001650static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001651 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001652 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1654 return;
1655 }
Mike Stump11289f42009-09-09 15:08:12 +00001656
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001657 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001658 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001659 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001660 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001661 return;
1662 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001663 }
1664
Ted Kremenek08479ae2009-08-15 00:51:46 +00001665 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001666}
1667
Chandler Carruthedc2c642011-07-02 00:01:44 +00001668static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001669 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001670 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001671 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001672
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001673 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001674}
1675
Chandler Carruthedc2c642011-07-02 00:01:44 +00001676static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001677 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001678 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001679 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001680 else
1681 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001682 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001683}
1684
Chandler Carruthedc2c642011-07-02 00:01:44 +00001685static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001686 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001687 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001688 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001689 else
1690 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001691 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001692}
1693
Chandler Carruthedc2c642011-07-02 00:01:44 +00001694static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001695 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001696
1697 if (S.CheckNoReturnAttr(attr)) return;
1698
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001699 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001700 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001701 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001702 return;
1703 }
1704
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001705 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +00001706}
1707
1708bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001709 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001710 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1711 attr.setInvalid();
1712 return true;
1713 }
1714
1715 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001716}
1717
Chandler Carruthedc2c642011-07-02 00:01:44 +00001718static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1719 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001720
1721 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1722 // because 'analyzer_noreturn' does not impact the type.
1723
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001724 if(!checkAttributeNumArgs(S, Attr, 0))
1725 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001726
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001727 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1728 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001729 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1730 && !VD->getType()->isFunctionPointerType())) {
1731 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001732 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001733 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001734 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001735 return;
1736 }
1737 }
1738
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001739 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001740}
1741
John Thompsoncdb847ba2010-08-09 21:53:52 +00001742// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001743static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001744/*
1745 Returning a Vector Class in Registers
1746
Eric Christopherbc638a82010-12-01 22:13:54 +00001747 According to the PPU ABI specifications, a class with a single member of
1748 vector type is returned in memory when used as the return value of a function.
1749 This results in inefficient code when implementing vector classes. To return
1750 the value in a single vector register, add the vecreturn attribute to the
1751 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001752
1753 Example:
1754
1755 struct Vector
1756 {
1757 __vector float xyzw;
1758 } __attribute__((vecreturn));
1759
1760 Vector Add(Vector lhs, Vector rhs)
1761 {
1762 Vector result;
1763 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1764 return result; // This will be returned in a register
1765 }
1766*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001767 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001768 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001769 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001770 return;
1771 }
1772
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001773 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001774 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1775 return;
1776 }
1777
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001778 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001779 int count = 0;
1780
1781 if (!isa<CXXRecordDecl>(record)) {
1782 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1783 return;
1784 }
1785
1786 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1787 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1788 return;
1789 }
1790
Eric Christopherbc638a82010-12-01 22:13:54 +00001791 for (RecordDecl::field_iterator iter = record->field_begin();
1792 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001793 if ((count == 1) || !iter->getType()->isVectorType()) {
1794 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1795 return;
1796 }
1797 count++;
1798 }
1799
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001800 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001801}
1802
Chandler Carruthedc2c642011-07-02 00:01:44 +00001803static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001804 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001805 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001806 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001807 return;
1808 }
1809 // FIXME: Actually store the attribute on the declaration
1810}
1811
Chandler Carruthedc2c642011-07-02 00:01:44 +00001812static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001813 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001814 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001816 return;
1817 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001818
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001819 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001820 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001821 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001822 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001823 return;
1824 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001825
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001826 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001827}
1828
Rafael Espindola70107f92011-10-03 14:59:42 +00001829static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1830 const AttributeList &Attr) {
1831 // check the attribute arguments.
1832 if (Attr.hasParameterOrArguments()) {
1833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1834 return;
1835 }
1836
1837 if (!isa<FunctionDecl>(D)) {
1838 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1839 << Attr.getName() << ExpectedFunction;
1840 return;
1841 }
1842
1843 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1844}
1845
Chandler Carruthedc2c642011-07-02 00:01:44 +00001846static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001847 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001848 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001849 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1850 return;
1851 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001852
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001853 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001854 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001855 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1856 return;
1857 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001858 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001859 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001860 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001861 return;
1862 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001863
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001864 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001865}
1866
Chandler Carruthedc2c642011-07-02 00:01:44 +00001867static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001868 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001869 if (Attr.getNumArgs() > 1) {
1870 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001871 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001872 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001873
1874 int priority = 65535; // FIXME: Do not hardcode such constants.
1875 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001876 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001877 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001878 if (E->isTypeDependent() || E->isValueDependent() ||
1879 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001880 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001881 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001882 return;
1883 }
1884 priority = Idx.getZExtValue();
1885 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001886
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001887 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001888 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001889 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001890 return;
1891 }
1892
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001893 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001894 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001895}
1896
Chandler Carruthedc2c642011-07-02 00:01:44 +00001897static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001898 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001899 if (Attr.getNumArgs() > 1) {
1900 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001901 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001902 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001903
1904 int priority = 65535; // FIXME: Do not hardcode such constants.
1905 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001906 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001907 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001908 if (E->isTypeDependent() || E->isValueDependent() ||
1909 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001910 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001911 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001912 return;
1913 }
1914 priority = Idx.getZExtValue();
1915 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001916
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001917 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001918 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001919 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001920 return;
1921 }
1922
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001923 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001924 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001925}
1926
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001927template <typename AttrTy>
1928static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1929 const char *Name) {
Chris Lattner190aa102011-02-24 05:42:24 +00001930 unsigned NumArgs = Attr.getNumArgs();
1931 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001932 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001933 return;
1934 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001935
1936 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001937 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001938 if (NumArgs == 1) {
1939 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001940 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001941 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001942 << Name;
Fariborz Jahanian551063102010-10-06 21:18:44 +00001943 return;
1944 }
Chris Lattner190aa102011-02-24 05:42:24 +00001945 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001946 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001947
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001948 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001949}
1950
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001951static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1952 const AttributeList &Attr) {
1953 unsigned NumArgs = Attr.getNumArgs();
1954 if (NumArgs > 0) {
1955 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1956 return;
1957 }
1958
1959 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001960 Attr.getRange(), S.Context));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001961}
1962
Patrick Beardacfbe9e2012-04-06 18:12:22 +00001963static void handleObjCRootClassAttr(Sema &S, Decl *D,
1964 const AttributeList &Attr) {
1965 if (!isa<ObjCInterfaceDecl>(D)) {
1966 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1967 return;
1968 }
1969
1970 unsigned NumArgs = Attr.getNumArgs();
1971 if (NumArgs > 0) {
1972 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1973 return;
1974 }
1975
1976 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1977}
1978
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001979static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001980 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00001981 if (!isa<ObjCInterfaceDecl>(D)) {
1982 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1983 return;
1984 }
1985
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001986 unsigned NumArgs = Attr.getNumArgs();
1987 if (NumArgs > 0) {
1988 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1989 return;
1990 }
1991
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001992 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001993 Attr.getRange(), S.Context));
1994}
1995
Jordy Rose740b0c22012-05-08 03:27:22 +00001996static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1997 IdentifierInfo *Platform,
1998 VersionTuple Introduced,
1999 VersionTuple Deprecated,
2000 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002001 StringRef PlatformName
2002 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2003 if (PlatformName.empty())
2004 PlatformName = Platform->getName();
2005
2006 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2007 // of these steps are needed).
2008 if (!Introduced.empty() && !Deprecated.empty() &&
2009 !(Introduced <= Deprecated)) {
2010 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2011 << 1 << PlatformName << Deprecated.getAsString()
2012 << 0 << Introduced.getAsString();
2013 return true;
2014 }
2015
2016 if (!Introduced.empty() && !Obsoleted.empty() &&
2017 !(Introduced <= Obsoleted)) {
2018 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2019 << 2 << PlatformName << Obsoleted.getAsString()
2020 << 0 << Introduced.getAsString();
2021 return true;
2022 }
2023
2024 if (!Deprecated.empty() && !Obsoleted.empty() &&
2025 !(Deprecated <= Obsoleted)) {
2026 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2027 << 2 << PlatformName << Obsoleted.getAsString()
2028 << 1 << Deprecated.getAsString();
2029 return true;
2030 }
2031
2032 return false;
2033}
2034
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002035AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
2036 IdentifierInfo *Platform,
2037 VersionTuple Introduced,
2038 VersionTuple Deprecated,
2039 VersionTuple Obsoleted,
2040 bool IsUnavailable,
2041 StringRef Message) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002042 VersionTuple MergedIntroduced = Introduced;
2043 VersionTuple MergedDeprecated = Deprecated;
2044 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002045 bool FoundAny = false;
2046
Rafael Espindolac67f2232012-05-10 02:50:16 +00002047 if (D->hasAttrs()) {
2048 AttrVec &Attrs = D->getAttrs();
2049 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2050 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2051 if (!OldAA) {
2052 ++i;
2053 continue;
2054 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002055
Rafael Espindolac67f2232012-05-10 02:50:16 +00002056 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2057 if (OldPlatform != Platform) {
2058 ++i;
2059 continue;
2060 }
2061
2062 FoundAny = true;
2063 VersionTuple OldIntroduced = OldAA->getIntroduced();
2064 VersionTuple OldDeprecated = OldAA->getDeprecated();
2065 VersionTuple OldObsoleted = OldAA->getObsoleted();
2066 bool OldIsUnavailable = OldAA->getUnavailable();
2067 StringRef OldMessage = OldAA->getMessage();
2068
2069 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2070 OldIntroduced != Introduced) ||
2071 (!OldDeprecated.empty() && !Deprecated.empty() &&
2072 OldDeprecated != Deprecated) ||
2073 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2074 OldObsoleted != Obsoleted) ||
2075 (OldIsUnavailable != IsUnavailable) ||
2076 (OldMessage != Message)) {
2077 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2078 Diag(Range.getBegin(), diag::note_previous_attribute);
2079 Attrs.erase(Attrs.begin() + i);
2080 --e;
2081 continue;
2082 }
2083
2084 VersionTuple MergedIntroduced2 = MergedIntroduced;
2085 VersionTuple MergedDeprecated2 = MergedDeprecated;
2086 VersionTuple MergedObsoleted2 = MergedObsoleted;
2087
2088 if (MergedIntroduced2.empty())
2089 MergedIntroduced2 = OldIntroduced;
2090 if (MergedDeprecated2.empty())
2091 MergedDeprecated2 = OldDeprecated;
2092 if (MergedObsoleted2.empty())
2093 MergedObsoleted2 = OldObsoleted;
2094
2095 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2096 MergedIntroduced2, MergedDeprecated2,
2097 MergedObsoleted2)) {
2098 Attrs.erase(Attrs.begin() + i);
2099 --e;
2100 continue;
2101 }
2102
2103 MergedIntroduced = MergedIntroduced2;
2104 MergedDeprecated = MergedDeprecated2;
2105 MergedObsoleted = MergedObsoleted2;
2106 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002107 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002108 }
2109
2110 if (FoundAny &&
2111 MergedIntroduced == Introduced &&
2112 MergedDeprecated == Deprecated &&
2113 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002114 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002115
Rafael Espindolac67f2232012-05-10 02:50:16 +00002116 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002117 MergedDeprecated, MergedObsoleted)) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002118 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2119 Introduced, Deprecated,
2120 Obsoleted, IsUnavailable, Message);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002121 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002122 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002123}
2124
Chandler Carruthedc2c642011-07-02 00:01:44 +00002125static void handleAvailabilityAttr(Sema &S, Decl *D,
2126 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002127 IdentifierInfo *Platform = Attr.getParameterName();
2128 SourceLocation PlatformLoc = Attr.getParameterLoc();
2129
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002130 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002131 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2132 << Platform;
2133
Rafael Espindolac231fab2013-01-08 21:30:32 +00002134 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2135 if (!ND) {
2136 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2137 return;
2138 }
2139
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002140 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2141 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2142 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002143 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002144 StringRef Str;
2145 const StringLiteral *SE =
2146 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2147 if (SE)
2148 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002149
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002150 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
2151 Platform,
2152 Introduced.Version,
2153 Deprecated.Version,
2154 Obsoleted.Version,
2155 IsUnavailable, Str);
Rafael Espindola54606d52012-12-25 07:31:49 +00002156 if (NewAttr) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002157 D->addAttr(NewAttr);
Rafael Espindola54606d52012-12-25 07:31:49 +00002158 ND->ClearLVCache();
2159 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002160}
2161
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002162VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2163 VisibilityAttr::VisibilityType Vis) {
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00002164 if (isa<TypedefNameDecl>(D)) {
2165 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002166 return NULL;
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00002167 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002168 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2169 if (ExistingAttr) {
2170 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2171 if (ExistingVis == Vis)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002172 return NULL;
Rafael Espindolac67f2232012-05-10 02:50:16 +00002173 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2174 Diag(Range.getBegin(), diag::note_previous_attribute);
2175 D->dropAttr<VisibilityAttr>();
Rafael Espindola54606d52012-12-25 07:31:49 +00002176 NamedDecl *ND = cast<NamedDecl>(D);
2177 ND->ClearLVCache();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002178 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002179 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002180}
2181
Chandler Carruthedc2c642011-07-02 00:01:44 +00002182static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002183 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002184 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002185 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002186
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002187 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002188 Arg = Arg->IgnoreParenCasts();
2189 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002190
Douglas Gregorfb65e592011-07-27 05:40:30 +00002191 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002192 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002193 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002194 return;
2195 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002196
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002197 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002198 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002199
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002200 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002201 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002202 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002203 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002204 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002205 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00002206 else if (TypeStr == "protected") {
2207 // Complain about attempts to use protected visibility on targets
2208 // (like Darwin) that don't support it.
2209 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2210 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2211 type = VisibilityAttr::Default;
2212 } else {
2213 type = VisibilityAttr::Protected;
2214 }
2215 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002216 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002217 return;
2218 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002219
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002220 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
Rafael Espindola54606d52012-12-25 07:31:49 +00002221 if (NewAttr) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002222 D->addAttr(NewAttr);
Rafael Espindola54606d52012-12-25 07:31:49 +00002223 NamedDecl *ND = cast<NamedDecl>(D);
2224 ND->ClearLVCache();
2225 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002226}
2227
Chandler Carruthedc2c642011-07-02 00:01:44 +00002228static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2229 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002230 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2231 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002232 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002233 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002234 return;
2235 }
2236
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002237 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2238 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2239 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00002240 << "objc_method_family" << 1;
2241 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002242 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002243 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002244 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002245 return;
2246 }
2247
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002248 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002249 ObjCMethodFamilyAttr::FamilyKind family;
2250 if (param == "none")
2251 family = ObjCMethodFamilyAttr::OMF_None;
2252 else if (param == "alloc")
2253 family = ObjCMethodFamilyAttr::OMF_alloc;
2254 else if (param == "copy")
2255 family = ObjCMethodFamilyAttr::OMF_copy;
2256 else if (param == "init")
2257 family = ObjCMethodFamilyAttr::OMF_init;
2258 else if (param == "mutableCopy")
2259 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2260 else if (param == "new")
2261 family = ObjCMethodFamilyAttr::OMF_new;
2262 else {
2263 // Just warn and ignore it. This is future-proof against new
2264 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002265 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002266 return;
2267 }
2268
John McCall31168b02011-06-15 23:02:42 +00002269 if (family == ObjCMethodFamilyAttr::OMF_init &&
2270 !method->getResultType()->isObjCObjectPointerType()) {
2271 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2272 << method->getResultType();
2273 // Ignore the attribute.
2274 return;
2275 }
2276
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002277 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002278 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002279}
2280
Chandler Carruthedc2c642011-07-02 00:01:44 +00002281static void handleObjCExceptionAttr(Sema &S, Decl *D,
2282 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002283 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002284 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002285
Chris Lattner677a3582009-02-14 08:09:34 +00002286 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2287 if (OCI == 0) {
2288 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2289 return;
2290 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002291
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002292 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00002293}
2294
Chandler Carruthedc2c642011-07-02 00:01:44 +00002295static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002296 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002297 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002298 return;
2299 }
Richard Smithdda56e42011-04-15 14:24:37 +00002300 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002301 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002302 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002303 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2304 return;
2305 }
2306 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002307 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2308 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002309 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002310 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2311 return;
2312 }
2313 }
2314 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002315 // It is okay to include this attribute on properties, e.g.:
2316 //
2317 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2318 //
2319 // In this case it follows tradition and suppresses an error in the above
2320 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002321 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002322 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002323 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002324}
2325
Mike Stumpd3bb5572009-07-24 19:02:52 +00002326static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002327handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002328 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002329 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002330 return;
2331 }
2332
2333 if (!isa<FunctionDecl>(D)) {
2334 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2335 return;
2336 }
2337
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002338 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002339}
2340
Chandler Carruthedc2c642011-07-02 00:01:44 +00002341static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002342 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002343 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002344 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002345 return;
2346 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002347
Steve Naroff3405a732008-09-18 16:44:58 +00002348 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002349 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002350 return;
2351 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002352
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002353 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002354 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002355 type = BlocksAttr::ByRef;
2356 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002357 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002358 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002359 return;
2360 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002361
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002362 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002363}
2364
Chandler Carruthedc2c642011-07-02 00:01:44 +00002365static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002366 // check the attribute arguments.
2367 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002368 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002369 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002370 }
2371
John McCallb46f2872011-09-09 07:56:05 +00002372 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002373 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002374 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002375 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002376 if (E->isTypeDependent() || E->isValueDependent() ||
2377 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002378 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002379 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002380 return;
2381 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002382
John McCallb46f2872011-09-09 07:56:05 +00002383 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002384 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2385 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002386 return;
2387 }
John McCallb46f2872011-09-09 07:56:05 +00002388
2389 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002390 }
2391
John McCallb46f2872011-09-09 07:56:05 +00002392 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002393 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002394 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002395 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002396 if (E->isTypeDependent() || E->isValueDependent() ||
2397 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002398 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002399 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002400 return;
2401 }
2402 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002403
John McCallb46f2872011-09-09 07:56:05 +00002404 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002405 // FIXME: This error message could be improved, it would be nice
2406 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002407 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2408 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002409 return;
2410 }
2411 }
2412
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002413 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002414 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002415 if (isa<FunctionNoProtoType>(FT)) {
2416 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2417 return;
2418 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002419
Chris Lattner9363e312009-03-17 23:03:47 +00002420 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002421 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002422 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002423 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002424 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002425 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002426 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002427 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002428 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002429 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2430 if (!BD->isVariadic()) {
2431 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2432 return;
2433 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002434 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002435 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002436 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002437 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002438 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002439 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002440 int m = Ty->isFunctionPointerType() ? 0 : 1;
2441 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002442 return;
2443 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002444 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002445 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002446 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002447 return;
2448 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002449 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002450 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002451 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002452 return;
2453 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002454 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00002455 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002456}
2457
Chandler Carruthedc2c642011-07-02 00:01:44 +00002458static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002459 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002460 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002461 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002462
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002463 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002464 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002465 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002466 return;
2467 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002468
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002469 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2470 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2471 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002472 return;
2473 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002474 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2475 if (MD->getResultType()->isVoidType()) {
2476 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2477 << Attr.getName() << 1;
2478 return;
2479 }
2480
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002481 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00002482}
2483
Chandler Carruthedc2c642011-07-02 00:01:44 +00002484static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002485 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002486 if (Attr.hasParameterOrArguments()) {
2487 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002488 return;
2489 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002490
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002491 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002492 if (isa<CXXRecordDecl>(D)) {
2493 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2494 return;
2495 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002496 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2497 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002498 return;
2499 }
2500
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002501 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002502
2503 // 'weak' only applies to declarations with external linkage.
2504 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002505 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002506 return;
2507 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002508
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002509 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002510}
2511
Chandler Carruthedc2c642011-07-02 00:01:44 +00002512static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002513 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002514 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002515 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002516
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002517
2518 // weak_import only applies to variable & function declarations.
2519 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002520 if (!D->canBeWeakImported(isDef)) {
2521 if (isDef)
2522 S.Diag(Attr.getLoc(),
2523 diag::warn_attribute_weak_import_invalid_on_definition)
2524 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002525 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002526 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002527 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002528 // Nothing to warn about here.
2529 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002530 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002531 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002532
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002533 return;
2534 }
2535
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002536 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002537}
2538
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002539// Handles reqd_work_group_size and work_group_size_hint.
2540static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002541 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002542 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2543 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2544
Nate Begemanf2758702009-06-26 06:32:41 +00002545 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002546 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002547
2548 unsigned WGSize[3];
2549 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002550 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002551 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002552 if (E->isTypeDependent() || E->isValueDependent() ||
2553 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002554 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002555 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002556 return;
2557 }
2558 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2559 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002560
2561 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2562 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2563 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2564 if (!(A->getXDim() == WGSize[0] &&
2565 A->getYDim() == WGSize[1] &&
2566 A->getZDim() == WGSize[2])) {
2567 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2568 Attr.getName();
2569 }
2570 }
2571
2572 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2573 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2574 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2575 if (!(A->getXDim() == WGSize[0] &&
2576 A->getYDim() == WGSize[1] &&
2577 A->getZDim() == WGSize[2])) {
2578 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2579 Attr.getName();
2580 }
2581 }
2582
2583 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2584 D->addAttr(::new (S.Context)
2585 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2586 WGSize[0], WGSize[1], WGSize[2]));
2587 else
2588 D->addAttr(::new (S.Context)
2589 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2590 WGSize[0], WGSize[1], WGSize[2]));
Nate Begemanf2758702009-06-26 06:32:41 +00002591}
2592
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002593SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2594 StringRef Name) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002595 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2596 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002597 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002598 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2599 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002600 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002601 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002602 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002603}
2604
Chandler Carruthedc2c642011-07-02 00:01:44 +00002605static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002606 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002607 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002608 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002609
2610 // Make sure that there is a string literal as the sections's single
2611 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002612 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002613 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002614 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002615 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002616 return;
2617 }
Mike Stump11289f42009-09-09 15:08:12 +00002618
Chris Lattner30ba6742009-08-10 19:03:04 +00002619 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002620 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002621 if (!Error.empty()) {
2622 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2623 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002624 return;
2625 }
Mike Stump11289f42009-09-09 15:08:12 +00002626
Chris Lattner20aee9b2010-01-12 20:58:53 +00002627 // This attribute cannot be applied to local variables.
2628 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2629 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2630 return;
2631 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002632 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2633 SE->getString());
2634 if (NewAttr)
2635 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002636}
2637
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002638
Chandler Carruthedc2c642011-07-02 00:01:44 +00002639static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002640 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002641 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002642 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002643 return;
2644 }
Douglas Gregor88336832011-06-15 05:45:11 +00002645
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002646 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002647 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002648 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002649 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002650 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002651 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002652}
2653
Chandler Carruthedc2c642011-07-02 00:01:44 +00002654static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002655 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002656 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002658 return;
2659 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002660
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002661 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002662 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002663 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002664 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002665 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002666 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002667}
2668
Chandler Carruthedc2c642011-07-02 00:01:44 +00002669static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002670 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002671 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002672 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002673
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002674 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00002675}
2676
Chandler Carruthedc2c642011-07-02 00:01:44 +00002677static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002678 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002679 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2680 return;
2681 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002682
Anders Carlssond277d792009-01-31 01:16:18 +00002683 if (Attr.getNumArgs() != 0) {
2684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2685 return;
2686 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002687
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002688 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002689
Anders Carlssond277d792009-01-31 01:16:18 +00002690 if (!VD || !VD->hasLocalStorage()) {
2691 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2692 return;
2693 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002694
Anders Carlssond277d792009-01-31 01:16:18 +00002695 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002696 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002697 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002698 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2699 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002700 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002701 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002702 Attr.getParameterName();
2703 return;
2704 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002705
Anders Carlssond277d792009-01-31 01:16:18 +00002706 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2707 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002708 S.Diag(Attr.getParameterLoc(),
2709 diag::err_attribute_cleanup_arg_not_function)
2710 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002711 return;
2712 }
2713
Anders Carlssond277d792009-01-31 01:16:18 +00002714 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002715 S.Diag(Attr.getParameterLoc(),
2716 diag::err_attribute_cleanup_func_must_take_one_arg)
2717 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002718 return;
2719 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002720
Anders Carlsson723f55d2009-02-07 23:16:50 +00002721 // We're currently more strict than GCC about what function types we accept.
2722 // If this ever proves to be a problem it should be easy to fix.
2723 QualType Ty = S.Context.getPointerType(VD->getType());
2724 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002725 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2726 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002727 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002728 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2729 Attr.getParameterName() << ParamTy << Ty;
2730 return;
2731 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002732
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002733 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002734 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00002735}
2736
Mike Stumpd3bb5572009-07-24 19:02:52 +00002737/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002738/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002739static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002740 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002741 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002742
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002743 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002745 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002746 return;
2747 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002748
2749 // In C++ the implicit 'this' function parameter also counts, and they are
2750 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002751 bool HasImplicitThisParam = isInstanceMethod(D);
2752 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002753 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002754
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002755 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002756 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002757 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002758 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2759 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002760 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2761 << "format" << 2 << IdxExpr->getSourceRange();
2762 return;
2763 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002764
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002765 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2766 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2767 << "format" << 2 << IdxExpr->getSourceRange();
2768 return;
2769 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002770
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002771 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002772
Chandler Carruth743682b2010-11-16 08:35:43 +00002773 if (HasImplicitThisParam) {
2774 if (ArgIdx == 0) {
2775 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2776 << "format_arg" << IdxExpr->getSourceRange();
2777 return;
2778 }
2779 ArgIdx--;
2780 }
2781
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002782 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002783 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002784
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002785 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2786 if (not_nsstring_type &&
2787 !isCFStringType(Ty, S.Context) &&
2788 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002789 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002790 // FIXME: Should highlight the actual expression that has the wrong type.
2791 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002792 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002793 << IdxExpr->getSourceRange();
2794 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002795 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002796 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002797 if (!isNSStringType(Ty, S.Context) &&
2798 !isCFStringType(Ty, S.Context) &&
2799 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002800 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002801 // FIXME: Should highlight the actual expression that has the wrong type.
2802 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002803 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002804 << IdxExpr->getSourceRange();
2805 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002806 }
2807
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002808 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00002809 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002810}
2811
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002812enum FormatAttrKind {
2813 CFStringFormat,
2814 NSStringFormat,
2815 StrftimeFormat,
2816 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002817 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002818 InvalidFormat
2819};
2820
2821/// getFormatAttrKind - Map from format attribute names to supported format
2822/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002823static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002824 return llvm::StringSwitch<FormatAttrKind>(Format)
2825 // Check for formats that get handled specially.
2826 .Case("NSString", NSStringFormat)
2827 .Case("CFString", CFStringFormat)
2828 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002829
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002830 // Otherwise, check for supported formats.
2831 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2832 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2833 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002834
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002835 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2836 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002837}
2838
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002839/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002840/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002841static void handleInitPriorityAttr(Sema &S, Decl *D,
2842 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002843 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002844 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2845 return;
2846 }
2847
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002848 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002849 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2850 Attr.setInvalid();
2851 return;
2852 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002853 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002854 if (S.Context.getAsArrayType(T))
2855 T = S.Context.getBaseElementType(T);
2856 if (!T->getAs<RecordType>()) {
2857 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2858 Attr.setInvalid();
2859 return;
2860 }
2861
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002862 if (Attr.getNumArgs() != 1) {
2863 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2864 Attr.setInvalid();
2865 return;
2866 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002867 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002868
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002869 llvm::APSInt priority(32);
2870 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2871 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2872 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2873 << "init_priority" << priorityExpr->getSourceRange();
2874 Attr.setInvalid();
2875 return;
2876 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002877 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002878 if (prioritynum < 101 || prioritynum > 65535) {
2879 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2880 << priorityExpr->getSourceRange();
2881 Attr.setInvalid();
2882 return;
2883 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002884 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002885 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002886}
2887
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002888FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2889 int FormatIdx, int FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002890 // Check whether we already have an equivalent format attribute.
2891 for (specific_attr_iterator<FormatAttr>
2892 i = D->specific_attr_begin<FormatAttr>(),
2893 e = D->specific_attr_end<FormatAttr>();
2894 i != e ; ++i) {
2895 FormatAttr *f = *i;
2896 if (f->getType() == Format &&
2897 f->getFormatIdx() == FormatIdx &&
2898 f->getFirstArg() == FirstArg) {
2899 // If we don't have a valid location for this attribute, adopt the
2900 // location.
2901 if (f->getLocation().isInvalid())
2902 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002903 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002904 }
2905 }
2906
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002907 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2908 FirstArg);
Rafael Espindola92d49452012-05-11 00:36:07 +00002909}
2910
Mike Stumpd3bb5572009-07-24 19:02:52 +00002911/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002912/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002913static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002914
Chris Lattner4a927cb2008-06-28 23:36:30 +00002915 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002916 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002917 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002918 return;
2919 }
2920
Chris Lattner4a927cb2008-06-28 23:36:30 +00002921 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002922 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002923 return;
2924 }
2925
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002926 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002927 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002928 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002929 return;
2930 }
2931
Chandler Carruth743682b2010-11-16 08:35:43 +00002932 // In C++ the implicit 'this' function parameter also counts, and they are
2933 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002934 bool HasImplicitThisParam = isInstanceMethod(D);
2935 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002936 unsigned FirstIdx = 1;
2937
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002938 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002939
2940 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002941 if (Format.startswith("__") && Format.endswith("__"))
2942 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002943
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002944 // Check for supported formats.
2945 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002946
2947 if (Kind == IgnoredFormat)
2948 return;
2949
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002950 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002951 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00002952 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002953 return;
2954 }
2955
2956 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002957 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002958 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002959 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2960 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002961 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002962 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002963 return;
2964 }
2965
2966 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002967 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002968 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002969 return;
2970 }
2971
2972 // FIXME: Do we need to bounds check?
2973 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002974
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002975 if (HasImplicitThisParam) {
2976 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002977 S.Diag(Attr.getLoc(),
2978 diag::err_format_attribute_implicit_this_format_string)
2979 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002980 return;
2981 }
2982 ArgIdx--;
2983 }
Mike Stump11289f42009-09-09 15:08:12 +00002984
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002985 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002986 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002987
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002988 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002989 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002990 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2991 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002992 return;
2993 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002994 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002995 // FIXME: do we need to check if the type is NSString*? What are the
2996 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002997 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002998 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002999 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3000 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003001 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003002 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003003 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003004 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003005 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003006 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3007 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003008 return;
3009 }
3010
3011 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003012 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003013 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003014 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3015 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003016 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003017 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003018 return;
3019 }
3020
3021 // check if the function is variadic if the 3rd argument non-zero
3022 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003023 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003024 ++NumArgs; // +1 for ...
3025 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003026 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003027 return;
3028 }
3029 }
3030
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003031 // strftime requires FirstArg to be 0 because it doesn't read from any
3032 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003033 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003034 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003035 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3036 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003037 return;
3038 }
3039 // if 0 it disables parameter checking (to use with e.g. va_list)
3040 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003041 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003042 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003043 return;
3044 }
3045
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003046 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3047 Idx.getZExtValue(),
3048 FirstArg.getZExtValue());
3049 if (NewAttr)
3050 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003051}
3052
Chandler Carruthedc2c642011-07-02 00:01:44 +00003053static void handleTransparentUnionAttr(Sema &S, Decl *D,
3054 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003055 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003056 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003057 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003058
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003059
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003060 // Try to find the underlying union declaration.
3061 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003062 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003063 if (TD && TD->getUnderlyingType()->isUnionType())
3064 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3065 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003066 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003067
3068 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003069 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003070 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003071 return;
3072 }
3073
John McCallf937c022011-10-07 06:10:15 +00003074 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003075 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003076 diag::warn_transparent_union_attribute_not_definition);
3077 return;
3078 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003079
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003080 RecordDecl::field_iterator Field = RD->field_begin(),
3081 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003082 if (Field == FieldEnd) {
3083 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3084 return;
3085 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003086
David Blaikie40ed2972012-06-06 20:45:41 +00003087 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003088 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003089 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003090 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003091 diag::warn_transparent_union_attribute_floating)
3092 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003093 return;
3094 }
3095
3096 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3097 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3098 for (; Field != FieldEnd; ++Field) {
3099 QualType FieldType = Field->getType();
3100 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3101 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3102 // Warn if we drop the attribute.
3103 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003104 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003105 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003106 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003107 diag::warn_transparent_union_attribute_field_size_align)
3108 << isSize << Field->getDeclName() << FieldBits;
3109 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003110 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003111 diag::note_transparent_union_first_field_size_align)
3112 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003113 return;
3114 }
3115 }
3116
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003117 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003118}
3119
Chandler Carruthedc2c642011-07-02 00:01:44 +00003120static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003121 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003122 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003123 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003124
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003125 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003126 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003127
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003128 // Make sure that there is a string literal as the annotation's single
3129 // argument.
3130 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003131 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003132 return;
3133 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003134
3135 // Don't duplicate annotations that are already set.
3136 for (specific_attr_iterator<AnnotateAttr>
3137 i = D->specific_attr_begin<AnnotateAttr>(),
3138 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3139 if ((*i)->getAnnotation() == SE->getString())
3140 return;
3141 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003142 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00003143 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003144}
3145
Chandler Carruthedc2c642011-07-02 00:01:44 +00003146static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003147 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003148 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003149 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003150 return;
3151 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003152
Alexis Hunt96d5c762009-11-21 08:43:09 +00003153 //FIXME: The C++0x version of this attribute has more limited applicabilty
3154 // than GNU's, and should error out when it is used to specify a
3155 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003156
Chris Lattner4a927cb2008-06-28 23:36:30 +00003157 if (Attr.getNumArgs() == 0) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003158 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3159 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003160 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003161 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003162
Aaron Ballman478faed2012-06-19 22:09:27 +00003163 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3164 Attr.isDeclspecAttribute());
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003165}
3166
Aaron Ballman478faed2012-06-19 22:09:27 +00003167void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3168 bool isDeclSpec) {
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00003169 // FIXME: Handle pack-expansions here.
3170 if (DiagnoseUnexpandedParameterPack(E))
3171 return;
3172
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003173 if (E->isTypeDependent() || E->isValueDependent()) {
3174 // Save dependent expressions in the AST to be instantiated.
Aaron Ballman478faed2012-06-19 22:09:27 +00003175 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3176 isDeclSpec));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003177 return;
3178 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003179
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003180 SourceLocation AttrLoc = AttrRange.getBegin();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003181 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003182 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003183 ExprResult ICE
3184 = VerifyIntegerConstantExpression(E, &Alignment,
3185 diag::err_aligned_attribute_argument_not_int,
3186 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003187 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003188 return;
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003189 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003190 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3191 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003192 return;
3193 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003194 if (isDeclSpec) {
3195 // We've already verified it's a power of 2, now let's make sure it's
3196 // 8192 or less.
3197 if (Alignment.getZExtValue() > 8192) {
3198 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3199 << E->getSourceRange();
3200 return;
3201 }
3202 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003203
Aaron Ballman478faed2012-06-19 22:09:27 +00003204 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3205 isDeclSpec));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003206}
3207
Aaron Ballman478faed2012-06-19 22:09:27 +00003208void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3209 bool isDeclSpec) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003210 // FIXME: Cache the number on the Attr object if non-dependent?
3211 // FIXME: Perform checking of type validity
Aaron Ballman478faed2012-06-19 22:09:27 +00003212 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3213 isDeclSpec));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003214 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003215}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003216
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003217/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003218/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003219///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003220/// Despite what would be logical, the mode attribute is a decl attribute, not a
3221/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3222/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003223static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003224 // This attribute isn't documented, but glibc uses it. It changes
3225 // the width of an int or unsigned int to the specified size.
3226
3227 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003228 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003229 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003230
Chris Lattneracbc2d22008-06-27 22:18:37 +00003231
3232 IdentifierInfo *Name = Attr.getParameterName();
3233 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003234 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003235 return;
3236 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003237
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003238 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003239
3240 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003241 if (Str.startswith("__") && Str.endswith("__"))
3242 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003243
3244 unsigned DestWidth = 0;
3245 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003246 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003247 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003248 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003249 switch (Str[0]) {
3250 case 'Q': DestWidth = 8; break;
3251 case 'H': DestWidth = 16; break;
3252 case 'S': DestWidth = 32; break;
3253 case 'D': DestWidth = 64; break;
3254 case 'X': DestWidth = 96; break;
3255 case 'T': DestWidth = 128; break;
3256 }
3257 if (Str[1] == 'F') {
3258 IntegerMode = false;
3259 } else if (Str[1] == 'C') {
3260 IntegerMode = false;
3261 ComplexMode = true;
3262 } else if (Str[1] != 'I') {
3263 DestWidth = 0;
3264 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003265 break;
3266 case 4:
3267 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3268 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003269 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003270 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003271 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003272 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003273 break;
3274 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003275 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003276 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003277 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003278 case 11:
3279 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003280 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003281 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003282 }
3283
3284 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003285 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003286 OldTy = TD->getUnderlyingType();
3287 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3288 OldTy = VD->getType();
3289 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003290 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003291 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003292 return;
3293 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003294
John McCall9dd450b2009-09-21 23:43:11 +00003295 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003296 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3297 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003298 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003299 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3300 } else if (ComplexMode) {
3301 if (!OldTy->isComplexType())
3302 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3303 } else {
3304 if (!OldTy->isFloatingType())
3305 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3306 }
3307
Mike Stump87c57ac2009-05-16 07:39:55 +00003308 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3309 // and friends, at least with glibc.
3310 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3311 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003312 // FIXME: Make sure floating-point mappings are accurate
3313 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003314 QualType NewTy;
3315 switch (DestWidth) {
3316 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003317 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003318 return;
3319 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003320 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003321 return;
3322 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003323 if (!IntegerMode) {
3324 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3325 return;
3326 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003327 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003328 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003329 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003330 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003331 break;
3332 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003333 if (!IntegerMode) {
3334 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3335 return;
3336 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003337 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003338 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003339 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003340 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003341 break;
3342 case 32:
3343 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003344 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003345 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003346 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003347 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003348 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003349 break;
3350 case 64:
3351 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003352 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003353 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003354 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003355 NewTy = S.Context.LongTy;
3356 else
3357 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003358 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003359 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003360 NewTy = S.Context.UnsignedLongTy;
3361 else
3362 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003363 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003364 case 96:
3365 NewTy = S.Context.LongDoubleTy;
3366 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003367 case 128:
3368 if (!IntegerMode) {
3369 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3370 return;
3371 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003372 if (OldTy->isSignedIntegerType())
3373 NewTy = S.Context.Int128Ty;
3374 else
3375 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003376 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003377 }
3378
Eli Friedman4735374e2009-03-03 06:41:03 +00003379 if (ComplexMode) {
3380 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003381 }
3382
3383 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003384 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003385 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003386 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003387 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003388 cast<ValueDecl>(D)->setType(NewTy);
3389}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003390
Chandler Carruthedc2c642011-07-02 00:01:44 +00003391static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003392 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003393 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003394 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003395
Nick Lewycky08597072012-07-24 01:40:49 +00003396 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3397 if (!VD->hasGlobalStorage())
3398 S.Diag(Attr.getLoc(),
3399 diag::warn_attribute_requires_functions_or_static_globals)
3400 << Attr.getName();
3401 } else if (!isFunctionOrMethod(D)) {
3402 S.Diag(Attr.getLoc(),
3403 diag::warn_attribute_requires_functions_or_static_globals)
3404 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003405 return;
3406 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003407
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003408 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00003409}
3410
Chandler Carruthedc2c642011-07-02 00:01:44 +00003411static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003412 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003413 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003414 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003415
Mike Stumpd3bb5572009-07-24 19:02:52 +00003416
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003417 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003418 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003419 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003420 return;
3421 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003422
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003423 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00003424}
3425
Chandler Carruthedc2c642011-07-02 00:01:44 +00003426static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3427 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003428 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003429 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003430 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003431
Chris Lattner3c77a352010-06-22 00:03:40 +00003432
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003433 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003434 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003435 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003436 return;
3437 }
3438
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003439 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003440 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00003441}
3442
Chandler Carruthedc2c642011-07-02 00:01:44 +00003443static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003444 if (S.LangOpts.CUDA) {
3445 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003446 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3448 return;
3449 }
3450
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003451 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003452 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003453 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003454 return;
3455 }
3456
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003457 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003458 } else {
3459 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3460 }
3461}
3462
Chandler Carruthedc2c642011-07-02 00:01:44 +00003463static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003464 if (S.LangOpts.CUDA) {
3465 // check the attribute arguments.
3466 if (Attr.getNumArgs() != 0) {
3467 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3468 return;
3469 }
3470
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003471 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003472 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003473 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003474 return;
3475 }
3476
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003477 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003478 } else {
3479 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3480 }
3481}
3482
Chandler Carruthedc2c642011-07-02 00:01:44 +00003483static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003484 if (S.LangOpts.CUDA) {
3485 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003486 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003487 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003488
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003489 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003490 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003491 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003492 return;
3493 }
3494
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003495 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003496 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003497 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003498 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3499 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3500 << FD->getType()
3501 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3502 "void");
3503 } else {
3504 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3505 << FD->getType();
3506 }
3507 return;
3508 }
3509
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003510 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003511 } else {
3512 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3513 }
3514}
3515
Chandler Carruthedc2c642011-07-02 00:01:44 +00003516static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003517 if (S.LangOpts.CUDA) {
3518 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003519 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003520 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003521
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003522
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003523 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003524 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003525 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003526 return;
3527 }
3528
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003529 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003530 } else {
3531 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3532 }
3533}
3534
Chandler Carruthedc2c642011-07-02 00:01:44 +00003535static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003536 if (S.LangOpts.CUDA) {
3537 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003538 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003539 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003540
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003541
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003542 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003543 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003544 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003545 return;
3546 }
3547
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003548 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003549 } else {
3550 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3551 }
3552}
3553
Chandler Carruthedc2c642011-07-02 00:01:44 +00003554static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003555 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003556 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003557 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003558
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003559 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003560 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003561 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003562 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003563 return;
3564 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003565
Douglas Gregor35b57532009-10-27 21:01:01 +00003566 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003567 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003568 return;
3569 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003570
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003571 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003572}
3573
Chandler Carruthedc2c642011-07-02 00:01:44 +00003574static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003575 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003576
Aaron Ballman02df2e02012-12-09 17:45:41 +00003577 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003578 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003579 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3580 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003581 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003582 return;
3583
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003584 if (!isa<ObjCMethodDecl>(D)) {
3585 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3586 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003587 return;
3588 }
3589
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003590 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003591 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003592 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003593 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003594 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003595 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003596 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003597 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003598 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003599 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003600 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003601 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003602 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003603 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003604 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003605 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003606 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003607 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003608 switch (CC) {
3609 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003610 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003611 break;
3612 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003613 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003614 break;
3615 default:
3616 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003617 }
3618
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003619 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Derek Schuffa2020962012-10-16 22:30:41 +00003620 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003621 }
Derek Schuffa2020962012-10-16 22:30:41 +00003622 case AttributeList::AT_PnaclCall:
3623 D->addAttr(::new (S.Context) PnaclCallAttr(Attr.getRange(), S.Context));
3624 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003625 case AttributeList::AT_IntelOclBicc:
3626 D->addAttr(::new (S.Context) IntelOclBiccAttr(Attr.getRange(), S.Context));
3627 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003628
Abramo Bagnara50099372010-04-30 13:10:51 +00003629 default:
3630 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003631 }
3632}
3633
Chandler Carruthedc2c642011-07-02 00:01:44 +00003634static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003635 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003636 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003637}
3638
Aaron Ballman02df2e02012-12-09 17:45:41 +00003639bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3640 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003641 if (attr.isInvalid())
3642 return true;
3643
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003644 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3645 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3646 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00003647 attr.setInvalid();
3648 return true;
3649 }
3650
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003651 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3652 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003653 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003654 case AttributeList::AT_CDecl: CC = CC_C; break;
3655 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3656 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3657 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3658 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3659 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003660 Expr *Arg = attr.getArg(0);
3661 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003662 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003663 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3664 << "pcs" << 1;
3665 attr.setInvalid();
3666 return true;
3667 }
3668
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003669 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003670 if (StrRef == "aapcs") {
3671 CC = CC_AAPCS;
3672 break;
3673 } else if (StrRef == "aapcs-vfp") {
3674 CC = CC_AAPCS_VFP;
3675 break;
3676 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003677
3678 attr.setInvalid();
3679 Diag(attr.getLoc(), diag::err_invalid_pcs);
3680 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003681 }
Derek Schuffa2020962012-10-16 22:30:41 +00003682 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003683 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003684 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003685 }
3686
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003687 const TargetInfo &TI = Context.getTargetInfo();
3688 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3689 if (A == TargetInfo::CCCR_Warning) {
3690 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003691
3692 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3693 if (FD)
3694 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3695 TargetInfo::CCMT_NonMember;
3696 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003697 }
3698
John McCall3882ace2011-01-05 12:14:39 +00003699 return false;
3700}
3701
Chandler Carruthedc2c642011-07-02 00:01:44 +00003702static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003703 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003704
3705 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003706 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003707 return;
3708
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003709 if (!isa<ObjCMethodDecl>(D)) {
3710 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3711 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003712 return;
3713 }
Eli Friedman7044b762009-03-27 21:06:47 +00003714
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003715 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00003716}
3717
3718/// Checks a regparm attribute, returning true if it is ill-formed and
3719/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003720bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3721 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003722 return true;
3723
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003724 if (Attr.getNumArgs() != 1) {
3725 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3726 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003727 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003728 }
Eli Friedman7044b762009-03-27 21:06:47 +00003729
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003730 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003731 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003732 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003733 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003734 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00003735 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003736 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003737 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003738 }
3739
Douglas Gregore8bbc122011-09-02 00:18:52 +00003740 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003741 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003742 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003743 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003744 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003745 }
3746
John McCall3882ace2011-01-05 12:14:39 +00003747 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003748 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003749 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003750 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003751 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003752 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003753 }
3754
John McCall3882ace2011-01-05 12:14:39 +00003755 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003756}
3757
Chandler Carruthedc2c642011-07-02 00:01:44 +00003758static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003759 if (S.LangOpts.CUDA) {
3760 // check the attribute arguments.
3761 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003762 // FIXME: 0 is not okay.
3763 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003764 return;
3765 }
3766
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003767 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003768 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003769 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003770 return;
3771 }
3772
3773 Expr *MaxThreadsExpr = Attr.getArg(0);
3774 llvm::APSInt MaxThreads(32);
3775 if (MaxThreadsExpr->isTypeDependent() ||
3776 MaxThreadsExpr->isValueDependent() ||
3777 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3778 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3779 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3780 return;
3781 }
3782
3783 llvm::APSInt MinBlocks(32);
3784 if (Attr.getNumArgs() > 1) {
3785 Expr *MinBlocksExpr = Attr.getArg(1);
3786 if (MinBlocksExpr->isTypeDependent() ||
3787 MinBlocksExpr->isValueDependent() ||
3788 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3789 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3790 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3791 return;
3792 }
3793 }
3794
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003795 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00003796 MaxThreads.getZExtValue(),
3797 MinBlocks.getZExtValue()));
3798 } else {
3799 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3800 }
3801}
3802
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003803static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3804 const AttributeList &Attr) {
3805 StringRef AttrName = Attr.getName()->getName();
3806 if (!Attr.getParameterName()) {
3807 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3808 << Attr.getName() << /* arg num = */ 1;
3809 return;
3810 }
3811
3812 if (Attr.getNumArgs() != 2) {
3813 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3814 << /* required args = */ 3;
3815 return;
3816 }
3817
3818 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3819
3820 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3821 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3822 << Attr.getName() << ExpectedFunctionOrMethod;
3823 return;
3824 }
3825
3826 uint64_t ArgumentIdx;
3827 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3828 Attr.getLoc(), 2,
3829 Attr.getArg(0), ArgumentIdx))
3830 return;
3831
3832 uint64_t TypeTagIdx;
3833 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3834 Attr.getLoc(), 3,
3835 Attr.getArg(1), TypeTagIdx))
3836 return;
3837
3838 bool IsPointer = (AttrName == "pointer_with_type_tag");
3839 if (IsPointer) {
3840 // Ensure that buffer has a pointer type.
3841 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3842 if (!BufferTy->isPointerType()) {
3843 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3844 << AttrName;
3845 }
3846 }
3847
3848 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3849 S.Context,
3850 ArgumentKind,
3851 ArgumentIdx,
3852 TypeTagIdx,
3853 IsPointer));
3854}
3855
3856static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3857 const AttributeList &Attr) {
3858 IdentifierInfo *PointerKind = Attr.getParameterName();
3859 if (!PointerKind) {
3860 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3861 << "type_tag_for_datatype" << 1;
3862 return;
3863 }
3864
3865 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3866
3867 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3868 Attr.getRange(),
3869 S.Context,
3870 PointerKind,
3871 MatchingCType,
3872 Attr.getLayoutCompatible(),
3873 Attr.getMustBeNull()));
3874}
3875
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003876//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003877// Checker-specific attribute handlers.
3878//===----------------------------------------------------------------------===//
3879
John McCalled433932011-01-25 03:31:58 +00003880static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003881 return type->isDependentType() ||
3882 type->isObjCObjectPointerType() ||
3883 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003884}
3885static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003886 return type->isDependentType() ||
3887 type->isPointerType() ||
3888 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003889}
3890
Chandler Carruthedc2c642011-07-02 00:01:44 +00003891static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003892 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003893 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003894 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003895 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00003896 return;
3897 }
3898
3899 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003900 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003901 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3902 cf = false;
3903 } else {
3904 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3905 cf = true;
3906 }
3907
3908 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003909 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003910 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003911 return;
3912 }
3913
3914 if (cf)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003915 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003916 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003917 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003918}
3919
Chandler Carruthedc2c642011-07-02 00:01:44 +00003920static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3921 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003922 if (!isa<ObjCMethodDecl>(D)) {
3923 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003924 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00003925 return;
3926 }
3927
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003928 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003929}
3930
Chandler Carruthedc2c642011-07-02 00:01:44 +00003931static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3932 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003933
John McCalled433932011-01-25 03:31:58 +00003934 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003935
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003936 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003937 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003938 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003939 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003940 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003941 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3942 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003943 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003944 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003945 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003946 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003947 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003948 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003949 return;
3950 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003951
John McCalled433932011-01-25 03:31:58 +00003952 bool typeOK;
3953 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003954 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003955 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003956 case AttributeList::AT_NSReturnsAutoreleased:
3957 case AttributeList::AT_NSReturnsRetained:
3958 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003959 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3960 cf = false;
3961 break;
3962
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003963 case AttributeList::AT_CFReturnsRetained:
3964 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003965 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3966 cf = true;
3967 break;
3968 }
3969
3970 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003971 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003972 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003973 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003974 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003975
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003976 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003977 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003978 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003979 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003980 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCalled433932011-01-25 03:31:58 +00003981 S.Context));
3982 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003983 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003984 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003985 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003986 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003987 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003988 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003989 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003990 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003991 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003992 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003993 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003994 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003995 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003996 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003997 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003998 return;
3999 };
4000}
4001
John McCallcf166702011-07-22 08:53:00 +00004002static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4003 const AttributeList &attr) {
4004 SourceLocation loc = attr.getLoc();
4005
4006 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4007
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004008 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00004009 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004010 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00004011 return;
4012 }
4013
4014 // Check that the method returns a normal pointer.
4015 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004016
4017 if (!resultType->isReferenceType() &&
4018 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00004019 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4020 << SourceRange(loc)
4021 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4022
4023 // Drop the attribute.
4024 return;
4025 }
4026
4027 method->addAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004028 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCallcf166702011-07-22 08:53:00 +00004029}
4030
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004031static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4032 const AttributeList &attr) {
4033 SourceLocation loc = attr.getLoc();
4034 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4035
4036 if (!method) {
4037 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4038 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4039 return;
4040 }
4041 DeclContext *DC = method->getDeclContext();
4042 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4043 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4044 << attr.getName() << 0;
4045 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4046 return;
4047 }
4048 if (method->getMethodFamily() == OMF_dealloc) {
4049 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4050 << attr.getName() << 1;
4051 return;
4052 }
4053
4054 method->addAttr(
4055 ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context));
4056}
4057
John McCall32f5fe12011-09-30 05:12:12 +00004058/// Handle cf_audited_transfer and cf_unknown_transfer.
4059static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4060 if (!isa<FunctionDecl>(D)) {
4061 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004062 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004063 return;
4064 }
4065
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004066 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004067
4068 // Check whether there's a conflicting attribute already present.
4069 Attr *Existing;
4070 if (IsAudited) {
4071 Existing = D->getAttr<CFUnknownTransferAttr>();
4072 } else {
4073 Existing = D->getAttr<CFAuditedTransferAttr>();
4074 }
4075 if (Existing) {
4076 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4077 << A.getName()
4078 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4079 << A.getRange() << Existing->getRange();
4080 return;
4081 }
4082
4083 // All clear; add the attribute.
4084 if (IsAudited) {
4085 D->addAttr(
4086 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4087 } else {
4088 D->addAttr(
4089 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4090 }
4091}
4092
John McCallf1e8b342011-09-29 07:17:38 +00004093static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4094 const AttributeList &Attr) {
4095 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4096 if (!RD || RD->isUnion()) {
4097 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004098 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004099 }
4100
4101 IdentifierInfo *ParmName = Attr.getParameterName();
4102
4103 // In Objective-C, verify that the type names an Objective-C type.
4104 // We don't want to check this outside of ObjC because people sometimes
4105 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004106 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004107 // Check for an existing type with this name.
4108 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4109 Sema::LookupOrdinaryName);
4110 if (S.LookupName(R, Sc)) {
4111 NamedDecl *Target = R.getFoundDecl();
4112 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4113 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4114 S.Diag(Target->getLocStart(), diag::note_declared_at);
4115 }
4116 }
4117 }
4118
4119 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4120 ParmName));
4121}
4122
Chandler Carruthedc2c642011-07-02 00:01:44 +00004123static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4124 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004125 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004126
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004127 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004128 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004129}
4130
Chandler Carruthedc2c642011-07-02 00:01:44 +00004131static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4132 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004133 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004134 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004135 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004136 return;
4137 }
4138
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004139 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004140 QualType type = vd->getType();
4141
4142 if (!type->isDependentType() &&
4143 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004144 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004145 << type;
4146 return;
4147 }
4148
4149 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4150
4151 // If we have no lifetime yet, check the lifetime we're presumably
4152 // going to infer.
4153 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4154 lifetime = type->getObjCARCImplicitLifetime();
4155
4156 switch (lifetime) {
4157 case Qualifiers::OCL_None:
4158 assert(type->isDependentType() &&
4159 "didn't infer lifetime for non-dependent type?");
4160 break;
4161
4162 case Qualifiers::OCL_Weak: // meaningful
4163 case Qualifiers::OCL_Strong: // meaningful
4164 break;
4165
4166 case Qualifiers::OCL_ExplicitNone:
4167 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004168 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004169 << (lifetime == Qualifiers::OCL_Autoreleasing);
4170 break;
4171 }
4172
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004173 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004174 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00004175}
4176
Francois Picheta83957a2010-12-19 06:50:37 +00004177//===----------------------------------------------------------------------===//
4178// Microsoft specific attribute handlers.
4179//===----------------------------------------------------------------------===//
4180
Chandler Carruthedc2c642011-07-02 00:01:44 +00004181static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00004182 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00004183 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004184 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00004185 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004186
Francois Picheta83957a2010-12-19 06:50:37 +00004187 Expr *Arg = Attr.getArg(0);
4188 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004189 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00004190 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4191 << "uuid" << 1;
4192 return;
4193 }
4194
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004195 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00004196
4197 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4198 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004199
Francois Pichet7da11662010-12-20 01:41:49 +00004200 // Validate GUID length.
4201 if (IsCurly && StrRef.size() != 38) {
4202 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4203 return;
4204 }
4205 if (!IsCurly && StrRef.size() != 36) {
4206 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4207 return;
4208 }
4209
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004210 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00004211 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004212 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00004213 if (IsCurly) // Skip the optional '{'
4214 ++I;
4215
4216 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00004217 if (i == 8 || i == 13 || i == 18 || i == 23) {
4218 if (*I != '-') {
4219 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4220 return;
4221 }
4222 } else if (!isxdigit(*I)) {
4223 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4224 return;
4225 }
4226 I++;
4227 }
Francois Picheta83957a2010-12-19 06:50:37 +00004228
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004229 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00004230 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00004231 } else
Francois Picheta83957a2010-12-19 06:50:37 +00004232 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00004233}
4234
John McCall8d32c052012-05-22 21:28:12 +00004235static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weberefa45b22012-11-07 21:31:36 +00004236 if (!S.LangOpts.MicrosoftExt) {
John McCall8d32c052012-05-22 21:28:12 +00004237 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weberefa45b22012-11-07 21:31:36 +00004238 return;
4239 }
4240
4241 AttributeList::Kind Kind = Attr.getKind();
4242 if (Kind == AttributeList::AT_SingleInheritance)
4243 D->addAttr(
4244 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
4245 else if (Kind == AttributeList::AT_MultipleInheritance)
4246 D->addAttr(
4247 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
4248 else if (Kind == AttributeList::AT_VirtualInheritance)
4249 D->addAttr(
4250 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
John McCall8d32c052012-05-22 21:28:12 +00004251}
4252
4253static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4254 if (S.LangOpts.MicrosoftExt) {
4255 AttributeList::Kind Kind = Attr.getKind();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004256 if (Kind == AttributeList::AT_Ptr32)
John McCall8d32c052012-05-22 21:28:12 +00004257 D->addAttr(
4258 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004259 else if (Kind == AttributeList::AT_Ptr64)
John McCall8d32c052012-05-22 21:28:12 +00004260 D->addAttr(
4261 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004262 else if (Kind == AttributeList::AT_Win64)
John McCall8d32c052012-05-22 21:28:12 +00004263 D->addAttr(
4264 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4265 } else
4266 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4267}
4268
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004269static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4270 if (S.LangOpts.MicrosoftExt)
4271 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4272 else
4273 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4274}
4275
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004276//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004277// Top Level Sema Entry Points
4278//===----------------------------------------------------------------------===//
4279
Chandler Carruthedc2c642011-07-02 00:01:44 +00004280static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4281 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004282 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004283 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4284 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4285 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004286 default:
4287 break;
4288 }
4289}
Abramo Bagnara50099372010-04-30 13:10:51 +00004290
Chandler Carruthedc2c642011-07-02 00:01:44 +00004291static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4292 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004293 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004294 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4295 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4296 case AttributeList::AT_IBOutletCollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004297 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004298 case AttributeList::AT_AddressSpace:
4299 case AttributeList::AT_OpenCLImageAccess:
4300 case AttributeList::AT_ObjCGC:
4301 case AttributeList::AT_VectorSize:
4302 case AttributeList::AT_NeonVectorType:
4303 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004304 // Ignore these, these are type attributes, handled by
4305 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004306 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004307 case AttributeList::AT_CUDADevice:
4308 case AttributeList::AT_CUDAHost:
4309 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004310 // Ignore, this is a non-inheritable attribute, handled
4311 // by ProcessNonInheritableDeclAttr.
4312 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004313 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4314 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4315 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4316 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004317 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004318 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004319 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004320 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004321 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4322 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4323 case AttributeList::AT_CarriesDependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004324 handleDependencyAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004325 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4326 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4327 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4328 case AttributeList::AT_Deprecated:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004329 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4330 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004331 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4332 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004333 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004334 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004335 case AttributeList::AT_MinSize:
4336 handleMinSizeAttr(S, D, Attr);
4337 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004338 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4339 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4340 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4341 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4342 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004343 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004344 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004345 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4346 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4347 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4348 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4349 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004350 case AttributeList::AT_ownership_returns:
4351 case AttributeList::AT_ownership_takes:
4352 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004353 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004354 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4355 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4356 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4357 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4358 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4359 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4360 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004361
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004362 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004363 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004364 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004365 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004366
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004367 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004368 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4369
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004370 case AttributeList::AT_ObjCRequiresSuper:
4371 handleObjCRequiresSuperAttr(S, D, Attr); break;
4372
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004373 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004374 handleNSBridgedAttr(S, scope, D, Attr); break;
4375
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004376 case AttributeList::AT_CFAuditedTransfer:
4377 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004378 handleCFTransferAttr(S, D, Attr); break;
4379
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004380 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004381 case AttributeList::AT_CFConsumed:
4382 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4383 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004384 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004385
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004386 case AttributeList::AT_NSReturnsAutoreleased:
4387 case AttributeList::AT_NSReturnsNotRetained:
4388 case AttributeList::AT_CFReturnsNotRetained:
4389 case AttributeList::AT_NSReturnsRetained:
4390 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004391 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004392
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004393 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004394 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004395 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004396
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004397 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004398 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004399
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004400 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4401 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4402 case AttributeList::AT_Unavailable:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004403 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4404 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004405 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004406 handleArcWeakrefUnavailableAttr (S, D, Attr);
4407 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004408 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004409 handleObjCRootClassAttr(S, D, Attr);
4410 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004411 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004412 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004413 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004414 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4415 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004416 handleReturnsTwiceAttr(S, D, Attr);
4417 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004418 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4419 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4420 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004421 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004422 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4423 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4424 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4425 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004426 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004427 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004428 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004429 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004430 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004431 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004432 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004433 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004434 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4435 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4436 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4437 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4438 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4439 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4440 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4441 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4442 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004443 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004444 // Just ignore
4445 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004446 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004447 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004448 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004449 case AttributeList::AT_StdCall:
4450 case AttributeList::AT_CDecl:
4451 case AttributeList::AT_FastCall:
4452 case AttributeList::AT_ThisCall:
4453 case AttributeList::AT_Pascal:
4454 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004455 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004456 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004457 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004458 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004459 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004460 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004461 break;
John McCall8d32c052012-05-22 21:28:12 +00004462
4463 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004464 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004465 handleMsStructAttr(S, D, Attr);
4466 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004467 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004468 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004469 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004470 case AttributeList::AT_SingleInheritance:
4471 case AttributeList::AT_MultipleInheritance:
4472 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004473 handleInheritanceAttr(S, D, Attr);
4474 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004475 case AttributeList::AT_Win64:
4476 case AttributeList::AT_Ptr32:
4477 case AttributeList::AT_Ptr64:
John McCall8d32c052012-05-22 21:28:12 +00004478 handlePortabilityAttr(S, D, Attr);
4479 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004480 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004481 handleForceInlineAttr(S, D, Attr);
4482 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004483
4484 // Thread safety attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004485 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004486 handleGuardedVarAttr(S, D, Attr);
4487 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004488 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004489 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004490 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004491 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004492 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004493 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004494 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004495 handleNoAddressSafetyAttr(S, D, Attr);
4496 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004497 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004498 handleNoThreadSafetyAttr(S, D, Attr);
4499 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004500 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004501 handleLockableAttr(S, D, Attr);
4502 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004503 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004504 handleGuardedByAttr(S, D, Attr);
4505 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004506 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004507 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004508 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004509 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004510 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004511 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004512 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004513 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004514 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004515 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004516 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004517 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004518 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004519 handleLockReturnedAttr(S, D, Attr);
4520 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004521 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004522 handleLocksExcludedAttr(S, D, Attr);
4523 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004524 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004525 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004526 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004527 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004528 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004529 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004530 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004531 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004532 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004533 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004534 handleUnlockFunAttr(S, D, Attr);
4535 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004536 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004537 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004538 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004539 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004540 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004541 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004542
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004543 // Type safety attributes.
4544 case AttributeList::AT_ArgumentWithTypeTag:
4545 handleArgumentWithTypeTagAttr(S, D, Attr);
4546 break;
4547 case AttributeList::AT_TypeTagForDatatype:
4548 handleTypeTagForDatatypeAttr(S, D, Attr);
4549 break;
4550
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004551 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004552 // Ask target about the attribute.
4553 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4554 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004555 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4556 diag::warn_unhandled_ms_attribute_ignored :
4557 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004558 break;
4559 }
4560}
4561
Peter Collingbourneb331b262011-01-21 02:08:45 +00004562/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4563/// the attribute applies to decls. If the attribute is a type attribute, just
4564/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4565/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00004566static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4567 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004568 bool NonInheritable, bool Inheritable) {
4569 if (Attr.isInvalid())
4570 return;
4571
Aaron Ballman478faed2012-06-19 22:09:27 +00004572 // Type attributes are still treated as declaration attributes by
4573 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4574 // want to process them, however, because we will simply warn about ignoring
4575 // them. So instead, we will bail out early.
4576 if (Attr.isMSTypespecAttribute())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004577 return;
4578
4579 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004580 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004581
4582 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004583 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004584}
4585
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004586/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4587/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004588void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004589 const AttributeList *AttrList,
4590 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004591 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00004592 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola3c9d9472012-05-07 23:58:18 +00004593 }
Rafael Espindolac18086a2010-02-23 22:00:30 +00004594
4595 // GCC accepts
4596 // static int a9 __attribute__((weakref));
4597 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004598 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004599 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00004600 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004601 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004602 }
4603}
4604
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004605// Annotation attributes are the only attributes allowed after an access
4606// specifier.
4607bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4608 const AttributeList *AttrList) {
4609 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004610 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004611 handleAnnotateAttr(*this, ASDecl, *l);
4612 } else {
4613 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4614 return true;
4615 }
4616 }
4617
4618 return false;
4619}
4620
John McCall42856de2011-10-01 05:17:03 +00004621/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4622/// contains any decl attributes that we should warn about.
4623static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4624 for ( ; A; A = A->getNext()) {
4625 // Only warn if the attribute is an unignored, non-type attribute.
4626 if (A->isUsedAsTypeAttr()) continue;
4627 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4628
4629 if (A->getKind() == AttributeList::UnknownAttribute) {
4630 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4631 << A->getName() << A->getRange();
4632 } else {
4633 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4634 << A->getName() << A->getRange();
4635 }
4636 }
4637}
4638
4639/// checkUnusedDeclAttributes - Given a declarator which is not being
4640/// used to build a declaration, complain about any decl attributes
4641/// which might be lying around on it.
4642void Sema::checkUnusedDeclAttributes(Declarator &D) {
4643 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4644 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4645 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4646 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4647}
4648
Ryan Flynn7d470f32009-07-30 03:15:39 +00004649/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004650/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004651NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4652 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004653 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004654 NamedDecl *NewD = 0;
4655 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004656 FunctionDecl *NewFD;
4657 // FIXME: Missing call to CheckFunctionDeclaration().
4658 // FIXME: Mangling?
4659 // FIXME: Is the qualifier info correct?
4660 // FIXME: Is the DeclContext correct?
4661 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4662 Loc, Loc, DeclarationName(II),
4663 FD->getType(), FD->getTypeSourceInfo(),
4664 SC_None, SC_None,
4665 false/*isInlineSpecified*/,
4666 FD->hasPrototype(),
4667 false/*isConstexprSpecified*/);
4668 NewD = NewFD;
4669
4670 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004671 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004672
4673 // Fake up parameter variables; they are declared as if this were
4674 // a typedef.
4675 QualType FDTy = FD->getType();
4676 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4677 SmallVector<ParmVarDecl*, 16> Params;
4678 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4679 AE = FT->arg_type_end(); AI != AE; ++AI) {
4680 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4681 Param->setScopeInfo(0, Params.size());
4682 Params.push_back(Param);
4683 }
David Blaikie9c70e042011-09-21 18:16:56 +00004684 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004685 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004686 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4687 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004688 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004689 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00004690 VD->getStorageClass(),
4691 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00004692 if (VD->getQualifier()) {
4693 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004694 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004695 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004696 }
4697 return NewD;
4698}
4699
James Dennett634962f2012-06-14 21:40:34 +00004700/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004701/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004702void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004703 if (W.getUsed()) return; // only do this once
4704 W.setUsed(true);
4705 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4706 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004707 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004708 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4709 NDId->getName()));
4710 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004711 WeakTopLevelDecl.push_back(NewD);
4712 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4713 // to insert Decl at TU scope, sorry.
4714 DeclContext *SavedContext = CurContext;
4715 CurContext = Context.getTranslationUnitDecl();
4716 PushOnScopeChains(NewD, S);
4717 CurContext = SavedContext;
4718 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004719 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004720 }
4721}
4722
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004723/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4724/// it, apply them to D. This is a bit tricky because PD can have attributes
4725/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004726void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4727 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00004728 // It's valid to "forward-declare" #pragma weak, in which case we
4729 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004730 if (Inheritable) {
4731 LoadExternalWeakUndeclaredIdentifiers();
4732 if (!WeakUndeclaredIdentifiers.empty()) {
4733 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4734 if (IdentifierInfo *Id = ND->getIdentifier()) {
4735 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4736 = WeakUndeclaredIdentifiers.find(Id);
4737 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4738 WeakInfo W = I->second;
4739 DeclApplyPragmaWeak(S, ND, W);
4740 WeakUndeclaredIdentifiers[Id] = W;
4741 }
John McCall6fe02402010-10-27 00:59:00 +00004742 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004743 }
4744 }
4745 }
4746
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004747 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004748 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004749 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004750
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004751 // Walk the declarator structure, applying decl attributes that were in a type
4752 // position to the decl itself. This handles cases like:
4753 // int *__attr__(x)** D;
4754 // when X is a decl attribute.
4755 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4756 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004757 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004758
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004759 // Finally, apply any attributes on the decl itself.
4760 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004761 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004762}
John McCall28a6aea2009-11-04 02:18:39 +00004763
John McCall31168b02011-06-15 23:02:42 +00004764/// Is the given declaration allowed to use a forbidden type?
4765static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4766 // Private ivars are always okay. Unfortunately, people don't
4767 // always properly make their ivars private, even in system headers.
4768 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004769 // Function declarations in sys headers will be marked unavailable.
4770 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4771 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004772 return false;
4773
4774 // Require it to be declared in a system header.
4775 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4776}
4777
4778/// Handle a delayed forbidden-type diagnostic.
4779static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4780 Decl *decl) {
4781 if (decl && isForbiddenTypeAllowed(S, decl)) {
4782 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4783 "this system declaration uses an unsupported type"));
4784 return;
4785 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004786 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004787 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004788 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004789 // kind of forbidden type messages on unavailable functions.
4790 if (FD->hasAttr<UnavailableAttr>() &&
4791 diag.getForbiddenTypeDiagnostic() ==
4792 diag::err_arc_array_param_no_ownership) {
4793 diag.Triggered = true;
4794 return;
4795 }
4796 }
John McCall31168b02011-06-15 23:02:42 +00004797
4798 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4799 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4800 diag.Triggered = true;
4801}
4802
John McCall2ec85372012-05-07 06:16:41 +00004803void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4804 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004805 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004806 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004807
John McCall2ec85372012-05-07 06:16:41 +00004808 // When delaying diagnostics to run in the context of a parsed
4809 // declaration, we only want to actually emit anything if parsing
4810 // succeeds.
4811 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004812
John McCall2ec85372012-05-07 06:16:41 +00004813 // We emit all the active diagnostics in this pool or any of its
4814 // parents. In general, we'll get one pool for the decl spec
4815 // and a child pool for each declarator; in a decl group like:
4816 // deprecated_typedef foo, *bar, baz();
4817 // only the declarator pops will be passed decls. This is correct;
4818 // we really do need to consider delayed diagnostics from the decl spec
4819 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004820 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004821 do {
John McCall6347b682012-05-07 06:16:58 +00004822 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004823 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4824 // This const_cast is a bit lame. Really, Triggered should be mutable.
4825 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004826 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004827 continue;
4828
John McCallc1465822011-02-14 07:13:47 +00004829 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004830 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004831 // Don't bother giving deprecation diagnostics if the decl is invalid.
4832 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004833 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004834 break;
4835
4836 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004837 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004838 break;
John McCall31168b02011-06-15 23:02:42 +00004839
4840 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004841 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004842 break;
John McCall86121512010-01-27 03:50:35 +00004843 }
4844 }
John McCall2ec85372012-05-07 06:16:41 +00004845 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004846}
4847
John McCall6347b682012-05-07 06:16:58 +00004848/// Given a set of delayed diagnostics, re-emit them as if they had
4849/// been delayed in the current context instead of in the given pool.
4850/// Essentially, this just moves them to the current pool.
4851void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4852 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4853 assert(curPool && "re-emitting in undelayed context not supported");
4854 curPool->steal(pool);
4855}
4856
John McCall28a6aea2009-11-04 02:18:39 +00004857static bool isDeclDeprecated(Decl *D) {
4858 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004859 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004860 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004861 // A category implicitly has the availability of the interface.
4862 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4863 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004864 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4865 return false;
4866}
4867
Eli Friedman971bfa12012-08-08 21:52:41 +00004868static void
4869DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4870 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004871 const ObjCInterfaceDecl *UnknownObjCClass,
4872 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00004873 DeclarationName Name = D->getDeclName();
4874 if (!Message.empty()) {
4875 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4876 S.Diag(D->getLocation(),
4877 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4878 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004879 if (ObjCPropery)
4880 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4881 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004882 } else if (!UnknownObjCClass) {
4883 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4884 S.Diag(D->getLocation(),
4885 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4886 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004887 if (ObjCPropery)
4888 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4889 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004890 } else {
4891 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4892 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4893 }
4894}
4895
John McCallb45a1e72010-08-26 02:13:20 +00004896void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004897 Decl *Ctx) {
4898 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004899 return;
4900
John McCall86121512010-01-27 03:50:35 +00004901 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00004902 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4903 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004904 DD.getUnknownObjCClass(),
4905 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004906}
4907
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004908void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004909 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004910 const ObjCInterfaceDecl *UnknownObjCClass,
4911 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004912 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004913 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004914 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4915 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004916 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004917 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004918 return;
4919 }
4920
4921 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004922 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004923 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004924 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004925}