blob: 1142e0dd2db718d8e8bcf1cd362bc93c777cc9fe [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"
Jordan Rosea7d03842013-02-08 22:30:41 +000022#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000023#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000024#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000026#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000027#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000028#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000029#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000030using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000031using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032
John McCall5fca7ea2011-03-02 12:29:23 +000033/// These constants match the enumerated choices of
34/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000035enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000036 ExpectedFunction,
37 ExpectedUnion,
38 ExpectedVariableOrFunction,
39 ExpectedFunctionOrMethod,
40 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000041 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000042 ExpectedFunctionMethodOrClass,
John McCall5fca7ea2011-03-02 12:29:23 +000043 ExpectedFunctionMethodOrParameter,
44 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000045 ExpectedVariable,
46 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000047 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000048 ExpectedFieldOrGlobalVar,
Hans Wennborgd3b01bc2012-06-23 11:51:46 +000049 ExpectedStruct,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000050 ExpectedVariableFunctionOrTag,
Richard Smith9eaab4b2013-02-01 08:25:07 +000051 ExpectedTLSVar,
52 ExpectedVariableOrField,
John McCalld041a9b2013-02-20 01:54:26 +000053 ExpectedVariableFieldOrTag,
54 ExpectedTypeOrNamespace
John McCall5fca7ea2011-03-02 12:29:23 +000055};
56
Chris Lattner58418ff2008-06-29 00:16:31 +000057//===----------------------------------------------------------------------===//
58// Helper functions
59//===----------------------------------------------------------------------===//
60
Chandler Carruthff4c4f02011-07-01 23:49:12 +000061static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000062 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000063 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000064 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000065 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000066 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000067 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000068 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000069 Ty = decl->getUnderlyingType();
70 else
71 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000072
Chris Lattner2c6fcf52008-06-26 18:38:35 +000073 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000074 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000075 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000076 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000077
John McCall9dd450b2009-09-21 23:43:11 +000078 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000079}
80
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000081// FIXME: We should provide an abstraction around a method or function
82// to provide the following bits of information.
83
Nuno Lopes518e3702009-12-20 23:11:08 +000084/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000085/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000086static bool isFunction(const Decl *D) {
87 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000088}
89
90/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000091/// type (function or function-typed variable) or an Objective-C
92/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000094 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000095}
96
Fariborz Jahanian4447e172009-05-15 23:15:03 +000097/// isFunctionOrMethodOrBlock - Return true if the given decl has function
98/// type (function or function-typed variable) or an Objective-C
99/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000100static bool isFunctionOrMethodOrBlock(const Decl *D) {
101 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000102 return true;
103 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000104 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000105 QualType Ty = V->getType();
106 return Ty->isBlockPointerType();
107 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000108 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000109}
110
John McCall3882ace2011-01-05 12:14:39 +0000111/// Return true if the given decl has a declarator that should have
112/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000113static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000114 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
116 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000117}
118
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000119/// hasFunctionProto - Return true if the given decl has a argument
120/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000121/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122static bool hasFunctionProto(const Decl *D) {
123 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000124 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000125 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000126 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000127 return true;
128 }
129}
130
131/// getFunctionOrMethodNumArgs - Return number of function or method
132/// arguments. It is an error to call this on a K&R function (use
133/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000134static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
135 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000136 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000138 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000139 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000140}
141
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000142static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
143 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000144 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000145 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000146 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000147
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000148 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000149}
150
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000151static QualType getFunctionOrMethodResultType(const Decl *D) {
152 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000153 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000154 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000155}
156
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000157static bool isFunctionOrMethodVariadic(const Decl *D) {
158 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000159 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000160 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000161 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000162 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000163 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000164 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000165 }
166}
167
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000168static bool isInstanceMethod(const Decl *D) {
169 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000170 return MethodDecl->isInstance();
171 return false;
172}
173
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000174static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000175 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000176 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000177 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000178
John McCall96fa4842010-05-17 21:00:27 +0000179 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
180 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000181 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000182
John McCall96fa4842010-05-17 21:00:27 +0000183 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000184
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000185 // FIXME: Should we walk the chain of classes?
186 return ClsName == &Ctx.Idents.get("NSString") ||
187 ClsName == &Ctx.Idents.get("NSMutableString");
188}
189
Daniel Dunbar980c6692008-09-26 03:32:58 +0000190static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000191 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000192 if (!PT)
193 return false;
194
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000195 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000196 if (!RT)
197 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000198
Daniel Dunbar980c6692008-09-26 03:32:58 +0000199 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000200 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000201 return false;
202
203 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
204}
205
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000206/// \brief Check if the attribute has exactly as many args as Num. May
207/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000208static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
209 unsigned int Num) {
210 if (Attr.getNumArgs() != Num) {
211 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
212 return false;
213 }
214
215 return true;
216}
217
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000218
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000219/// \brief Check if the attribute has at least as many args as Num. May
220/// output an error.
221static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
222 unsigned int Num) {
223 if (Attr.getNumArgs() < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000224 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
225 return false;
226 }
227
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000228 return true;
229}
230
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000231/// \brief Check if IdxExpr is a valid argument index for a function or
232/// instance method D. May output an error.
233///
234/// \returns true if IdxExpr is a valid index.
235static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
236 StringRef AttrName,
237 SourceLocation AttrLoc,
238 unsigned AttrArgNum,
239 const Expr *IdxExpr,
240 uint64_t &Idx)
241{
242 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
243
244 // In C++ the implicit 'this' function parameter also counts.
245 // Parameters are counted from one.
246 const bool HasImplicitThisParam = isInstanceMethod(D);
247 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
248 const unsigned FirstIdx = 1;
249
250 llvm::APSInt IdxInt;
251 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
252 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
253 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
254 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
255 return false;
256 }
257
258 Idx = IdxInt.getLimitedValue();
259 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
260 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
261 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
262 return false;
263 }
264 Idx--; // Convert to zero-based.
265 if (HasImplicitThisParam) {
266 if (Idx == 0) {
267 S.Diag(AttrLoc,
268 diag::err_attribute_invalid_implicit_this_argument)
269 << AttrName << IdxExpr->getSourceRange();
270 return false;
271 }
272 --Idx;
273 }
274
275 return true;
276}
277
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000278///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000279/// \brief Check if passed in Decl is a field or potentially shared global var
280/// \return true if the Decl is a field or potentially shared global variable
281///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000282static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000283 if (isa<FieldDecl>(D))
284 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000285 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000286 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
287
288 return false;
289}
290
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000291/// \brief Check if the passed-in expression is of type int or bool.
292static bool isIntOrBool(Expr *Exp) {
293 QualType QT = Exp->getType();
294 return QT->isBooleanType() || QT->isIntegerType();
295}
296
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000297
298// Check to see if the type is a smart pointer of some kind. We assume
299// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000300static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
301 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
302 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000303 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000304 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000305
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000306 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
307 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000308 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000309 return false;
310
311 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000312}
313
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000314/// \brief Check if passed in Decl is a pointer type.
315/// Note that this function may produce an error message.
316/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000317static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
318 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000319 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000320 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000321 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000322 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000323
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000324 if (const RecordType *RT = QT->getAs<RecordType>()) {
325 // If it's an incomplete type, it could be a smart pointer; skip it.
326 // (We don't want to force template instantiation if we can avoid it,
327 // since that would alter the order in which templates are instantiated.)
328 if (RT->isIncompleteType())
329 return true;
330
331 if (threadSafetyCheckIsSmartPointer(S, RT))
332 return true;
333 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000334
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000335 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000336 << Attr.getName()->getName() << QT;
337 } else {
338 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
339 << Attr.getName();
340 }
341 return false;
342}
343
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000344/// \brief Checks that the passed in QualType either is of RecordType or points
345/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000346static const RecordType *getRecordType(QualType QT) {
347 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000348 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000349
350 // Now check if we point to record type.
351 if (const PointerType *PT = QT->getAs<PointerType>())
352 return PT->getPointeeType()->getAs<RecordType>();
353
354 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000355}
356
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000357
Jordy Rose740b0c22012-05-08 03:27:22 +0000358static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
359 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000360 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
361 if (RT->getDecl()->getAttr<LockableAttr>())
362 return true;
363 return false;
364}
365
366
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000367/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000368/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000369static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
370 QualType Ty) {
371 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000372
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000373 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000374 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000375 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000376 << Attr.getName() << Ty.getAsString();
377 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000378 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000379
Michael Hana9171bc2012-08-03 17:40:43 +0000380 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000381 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000382 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000383
384 // Allow smart pointers to be used as lockable objects.
385 // FIXME -- Check the type that the smart pointer points to.
386 if (threadSafetyCheckIsSmartPointer(S, RT))
387 return;
388
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000389 // Check if the type is lockable.
390 RecordDecl *RD = RT->getDecl();
391 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000392 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000393
394 // Else check if any base classes are lockable.
395 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
396 CXXBasePaths BPaths(false, false);
397 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
398 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000399 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000400
401 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
402 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000403}
404
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000405/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000406/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000407/// \param Sidx The attribute argument index to start checking with.
408/// \param ParamIdxOk Whether an argument can be indexing into a function
409/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000410static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000411 const AttributeList &Attr,
412 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000413 int Sidx = 0,
414 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000415 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000416 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000417
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000418 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000419 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000420 Args.push_back(ArgExp);
421 continue;
422 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000423
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000424 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000425 if (StrLit->getLength() == 0 ||
426 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000427 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000428 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000429 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000430 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000431 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000432
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000433 // We allow constant strings to be used as a placeholder for expressions
434 // that are not valid C++ syntax, but warn that they are ignored.
435 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
436 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000437 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000438 continue;
439 }
440
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000441 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000442
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000443 // A pointer to member expression of the form &MyClass::mu is treated
444 // specially -- we need to look at the type of the member.
445 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
446 if (UOp->getOpcode() == UO_AddrOf)
447 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
448 if (DRE->getDecl()->isCXXInstanceMember())
449 ArgTy = DRE->getDecl()->getType();
450
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000451 // First see if we can just cast to record type, or point to record type.
452 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000453
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000454 // Now check if we index into a record type function param.
455 if(!RT && ParamIdxOk) {
456 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000457 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
458 if(FD && IL) {
459 unsigned int NumParams = FD->getNumParams();
460 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000461 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
462 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
463 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000464 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
465 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000466 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000467 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000468 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000469 }
470 }
471
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000472 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000473
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000474 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000476}
477
Chris Lattner58418ff2008-06-29 00:16:31 +0000478//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000479// Attribute Implementations
480//===----------------------------------------------------------------------===//
481
Daniel Dunbar032db472008-07-31 22:40:48 +0000482// FIXME: All this manual attribute parsing code is gross. At the
483// least add some helper functions to check most argument patterns (#
484// and types of args).
485
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000486enum ThreadAttributeDeclKind {
487 ThreadExpectedFieldOrGlobalVar,
488 ThreadExpectedFunctionOrMethod,
489 ThreadExpectedClassOrStruct
490};
491
Michael Hana9171bc2012-08-03 17:40:43 +0000492static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000493 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000494 assert(!Attr.isInvalid());
495
496 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000497 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000498
499 // D must be either a member field or global (potentially shared) variable.
500 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000501 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
502 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000503 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000504 }
505
Michael Han3be3b442012-07-23 18:48:41 +0000506 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000507}
508
Michael Han3be3b442012-07-23 18:48:41 +0000509static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
510 if (!checkGuardedVarAttrCommon(S, D, Attr))
511 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000512
Michael Han99315932013-01-24 16:46:58 +0000513 D->addAttr(::new (S.Context)
514 GuardedVarAttr(Attr.getRange(), S.Context,
515 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000516}
517
Michael Hana9171bc2012-08-03 17:40:43 +0000518static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000519 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000520 if (!checkGuardedVarAttrCommon(S, D, Attr))
521 return;
522
523 if (!threadSafetyCheckIsPointer(S, D, Attr))
524 return;
525
Michael Han99315932013-01-24 16:46:58 +0000526 D->addAttr(::new (S.Context)
527 PtGuardedVarAttr(Attr.getRange(), S.Context,
528 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000529}
530
Michael Hana9171bc2012-08-03 17:40:43 +0000531static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
532 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000533 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000534 assert(!Attr.isInvalid());
535
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000536 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000537 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000538
539 // D must be either a member field or global (potentially shared) variable.
540 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000541 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
542 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000543 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000544 }
545
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000546 SmallVector<Expr*, 1> Args;
547 // check that all arguments are lockable objects
548 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
549 unsigned Size = Args.size();
550 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000551 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000552
Michael Han3be3b442012-07-23 18:48:41 +0000553 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000554
Michael Han3be3b442012-07-23 18:48:41 +0000555 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000556}
557
Michael Han3be3b442012-07-23 18:48:41 +0000558static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
559 Expr *Arg = 0;
560 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
561 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000562
Michael Han3be3b442012-07-23 18:48:41 +0000563 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
564}
565
Michael Hana9171bc2012-08-03 17:40:43 +0000566static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000567 const AttributeList &Attr) {
568 Expr *Arg = 0;
569 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
570 return;
571
572 if (!threadSafetyCheckIsPointer(S, D, Attr))
573 return;
574
575 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
576 S.Context, Arg));
577}
578
Michael Hana9171bc2012-08-03 17:40:43 +0000579static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000580 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000581 assert(!Attr.isInvalid());
582
583 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000584 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000585
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000586 // FIXME: Lockable structs for C code.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000587 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000588 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
589 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Han3be3b442012-07-23 18:48:41 +0000590 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000591 }
592
Michael Han3be3b442012-07-23 18:48:41 +0000593 return true;
594}
595
596static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
597 if (!checkLockableAttrCommon(S, D, Attr))
598 return;
599
600 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
601}
602
Michael Hana9171bc2012-08-03 17:40:43 +0000603static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000604 const AttributeList &Attr) {
605 if (!checkLockableAttrCommon(S, D, Attr))
606 return;
607
Michael Han99315932013-01-24 16:46:58 +0000608 D->addAttr(::new (S.Context)
609 ScopedLockableAttr(Attr.getRange(), S.Context,
610 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000611}
612
613static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
614 const AttributeList &Attr) {
615 assert(!Attr.isInvalid());
616
617 if (!checkAttributeNumArgs(S, Attr, 0))
618 return;
619
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000620 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000621 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
622 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000623 return;
624 }
625
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000626 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000627 S.Context));
628}
629
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000630static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000631 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000632 assert(!Attr.isInvalid());
633
634 if (!checkAttributeNumArgs(S, Attr, 0))
635 return;
636
637 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
638 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
639 << Attr.getName() << ExpectedFunctionOrMethod;
640 return;
641 }
642
Michael Han99315932013-01-24 16:46:58 +0000643 D->addAttr(::new (S.Context)
644 NoAddressSafetyAnalysisAttr(Attr.getRange(), S.Context,
645 Attr.getAttributeSpellingListIndex()));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000646}
647
Michael Hana9171bc2012-08-03 17:40:43 +0000648static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
649 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000650 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000651 assert(!Attr.isInvalid());
652
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000653 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000654 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000655
656 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000657 ValueDecl *VD = dyn_cast<ValueDecl>(D);
658 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000659 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
660 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000661 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000662 }
663
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000664 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000665 QualType QT = VD->getType();
666 if (!QT->isDependentType()) {
667 const RecordType *RT = getRecordType(QT);
668 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000669 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000670 << Attr.getName();
671 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000672 }
673 }
674
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000675 // Check that all arguments are lockable objects.
676 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Han3be3b442012-07-23 18:48:41 +0000677 if (Args.size() == 0)
678 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000679
Michael Han3be3b442012-07-23 18:48:41 +0000680 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000681}
682
Michael Hana9171bc2012-08-03 17:40:43 +0000683static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000684 const AttributeList &Attr) {
685 SmallVector<Expr*, 1> Args;
686 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
687 return;
688
689 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000690 D->addAttr(::new (S.Context)
691 AcquiredAfterAttr(Attr.getRange(), S.Context,
692 StartArg, Args.size(),
693 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000694}
695
Michael Hana9171bc2012-08-03 17:40:43 +0000696static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000697 const AttributeList &Attr) {
698 SmallVector<Expr*, 1> Args;
699 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
700 return;
701
702 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000703 D->addAttr(::new (S.Context)
704 AcquiredBeforeAttr(Attr.getRange(), S.Context,
705 StartArg, Args.size(),
706 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000707}
708
Michael Hana9171bc2012-08-03 17:40:43 +0000709static bool checkLockFunAttrCommon(Sema &S, Decl *D,
710 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000711 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000712 assert(!Attr.isInvalid());
713
714 // zero or more arguments ok
715
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000716 // check that the attribute is applied to a function
717 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000718 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
719 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000720 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000721 }
722
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000723 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000724 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000725
Michael Han3be3b442012-07-23 18:48:41 +0000726 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000727}
728
Michael Hana9171bc2012-08-03 17:40:43 +0000729static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000730 const AttributeList &Attr) {
731 SmallVector<Expr*, 1> Args;
732 if (!checkLockFunAttrCommon(S, D, Attr, Args))
733 return;
734
735 unsigned Size = Args.size();
736 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000737 D->addAttr(::new (S.Context)
738 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
739 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000740}
741
Michael Hana9171bc2012-08-03 17:40:43 +0000742static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000743 const AttributeList &Attr) {
744 SmallVector<Expr*, 1> Args;
745 if (!checkLockFunAttrCommon(S, D, Attr, Args))
746 return;
747
748 unsigned Size = Args.size();
749 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000750 D->addAttr(::new (S.Context)
751 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
752 StartArg, Size,
753 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000754}
755
Michael Hana9171bc2012-08-03 17:40:43 +0000756static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
757 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000758 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000759 assert(!Attr.isInvalid());
760
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000761 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000762 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000763
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000764 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000765 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
766 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000767 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000768 }
769
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000770 if (!isIntOrBool(Attr.getArg(0))) {
771 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Han3be3b442012-07-23 18:48:41 +0000772 << Attr.getName();
773 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000774 }
775
776 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000777 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000778
Michael Han3be3b442012-07-23 18:48:41 +0000779 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000780}
781
Michael Hana9171bc2012-08-03 17:40:43 +0000782static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000783 const AttributeList &Attr) {
784 SmallVector<Expr*, 2> Args;
785 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
786 return;
787
788 unsigned Size = Args.size();
789 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000790 D->addAttr(::new (S.Context)
791 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
792 Attr.getArg(0), StartArg, Size,
793 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000794}
795
Michael Hana9171bc2012-08-03 17:40:43 +0000796static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000797 const AttributeList &Attr) {
798 SmallVector<Expr*, 2> Args;
799 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
800 return;
801
802 unsigned Size = Args.size();
803 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000804 D->addAttr(::new (S.Context)
805 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
806 Attr.getArg(0), StartArg, Size,
807 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000808}
809
Michael Hana9171bc2012-08-03 17:40:43 +0000810static bool checkLocksRequiredCommon(Sema &S, Decl *D,
811 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000812 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000813 assert(!Attr.isInvalid());
814
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000815 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000816 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000817
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000818 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000819 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
820 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000821 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000822 }
823
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000824 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000825 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Han3be3b442012-07-23 18:48:41 +0000826 if (Args.size() == 0)
827 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000828
Michael Han3be3b442012-07-23 18:48:41 +0000829 return true;
830}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000831
Michael Hana9171bc2012-08-03 17:40:43 +0000832static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000833 const AttributeList &Attr) {
834 SmallVector<Expr*, 1> Args;
835 if (!checkLocksRequiredCommon(S, D, Attr, Args))
836 return;
837
838 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000839 D->addAttr(::new (S.Context)
840 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
841 StartArg, Args.size(),
842 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000843}
844
Michael Hana9171bc2012-08-03 17:40:43 +0000845static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000846 const AttributeList &Attr) {
847 SmallVector<Expr*, 1> Args;
848 if (!checkLocksRequiredCommon(S, D, Attr, Args))
849 return;
850
851 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000852 D->addAttr(::new (S.Context)
853 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
854 StartArg, Args.size(),
855 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000856}
857
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000858static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000859 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000860 assert(!Attr.isInvalid());
861
862 // zero or more arguments ok
863
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000864 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000865 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
866 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000867 return;
868 }
869
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000870 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000871 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000872 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000873 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000874 Expr **StartArg = Size == 0 ? 0 : &Args[0];
875
Michael Han99315932013-01-24 16:46:58 +0000876 D->addAttr(::new (S.Context)
877 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
878 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000879}
880
881static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000882 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000883 assert(!Attr.isInvalid());
884
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000885 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000886 return;
887
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000888 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000889 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
890 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000891 return;
892 }
893
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000894 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000895 SmallVector<Expr*, 1> Args;
896 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
897 unsigned Size = Args.size();
898 if (Size == 0)
899 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000900
Michael Han99315932013-01-24 16:46:58 +0000901 D->addAttr(::new (S.Context)
902 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
903 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000904}
905
906static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000907 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000908 assert(!Attr.isInvalid());
909
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000910 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000911 return;
912
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000913 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000914 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
915 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000916 return;
917 }
918
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000919 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000920 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000921 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000922 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000923 if (Size == 0)
924 return;
925 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000926
Michael Han99315932013-01-24 16:46:58 +0000927 D->addAttr(::new (S.Context)
928 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
929 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000930}
931
932
Chandler Carruthedc2c642011-07-02 00:01:44 +0000933static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
934 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +0000935 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
936 if (TD == 0) {
937 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
938 // and type-ids.
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000939 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000940 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000941 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000942
Richard Smith1f5a4322013-01-13 02:11:23 +0000943 // Remember this typedef decl, we will need it later for diagnostics.
944 S.ExtVectorDecls.push_back(TD);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000945}
946
Chandler Carruthedc2c642011-07-02 00:01:44 +0000947static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000948 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000949 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000950 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000951
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000952 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000953 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000954 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000955 // If the alignment is less than or equal to 8 bits, the packed attribute
956 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +0000957 if (!FD->getType()->isDependentType() &&
958 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000959 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000960 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000961 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000962 else
Michael Han99315932013-01-24 16:46:58 +0000963 FD->addAttr(::new (S.Context)
964 PackedAttr(Attr.getRange(), S.Context,
965 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000966 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000967 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000968}
969
Chandler Carruthedc2c642011-07-02 00:01:44 +0000970static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +0000971 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han99315932013-01-24 16:46:58 +0000972 RD->addAttr(::new (S.Context)
973 MsStructAttr(Attr.getRange(), S.Context,
974 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000975 else
976 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
977}
978
Chandler Carruthedc2c642011-07-02 00:01:44 +0000979static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000980 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000981 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000982 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000983
Ted Kremenek1f672822010-02-18 03:08:58 +0000984 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000985 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000986 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +0000987 D->addAttr(::new (S.Context)
988 IBActionAttr(Attr.getRange(), S.Context,
989 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +0000990 return;
991 }
992
Ted Kremenekd68ec812011-02-04 06:54:16 +0000993 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000994}
995
Ted Kremenek7fd17232011-09-29 07:02:25 +0000996static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
997 // The IBOutlet/IBOutletCollection attributes only apply to instance
998 // variables or properties of Objective-C classes. The outlet must also
999 // have an object reference type.
1000 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1001 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001002 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001003 << Attr.getName() << VD->getType() << 0;
1004 return false;
1005 }
1006 }
1007 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1008 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001009 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001010 << Attr.getName() << PD->getType() << 1;
1011 return false;
1012 }
1013 }
1014 else {
1015 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1016 return false;
1017 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001018
Ted Kremenek7fd17232011-09-29 07:02:25 +00001019 return true;
1020}
1021
Chandler Carruthedc2c642011-07-02 00:01:44 +00001022static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001023 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001024 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +00001025 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001026
1027 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001028 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001029
Michael Han99315932013-01-24 16:46:58 +00001030 D->addAttr(::new (S.Context)
1031 IBOutletAttr(Attr.getRange(), S.Context,
1032 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001033}
1034
Chandler Carruthedc2c642011-07-02 00:01:44 +00001035static void handleIBOutletCollection(Sema &S, Decl *D,
1036 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001037
1038 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001039 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001040 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1041 return;
1042 }
1043
Ted Kremenek7fd17232011-09-29 07:02:25 +00001044 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001045 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001046
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001047 IdentifierInfo *II = Attr.getParameterName();
1048 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001049 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +00001050
John McCallba7bf592010-08-24 05:47:05 +00001051 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001052 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001053 if (!TypeRep) {
1054 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1055 return;
1056 }
John McCallba7bf592010-08-24 05:47:05 +00001057 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001058 // Diagnose use of non-object type in iboutletcollection attribute.
1059 // FIXME. Gnu attribute extension ignores use of builtin types in
1060 // attributes. So, __attribute__((iboutletcollection(char))) will be
1061 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001062 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001063 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1064 return;
1065 }
Michael Han99315932013-01-24 16:46:58 +00001066 D->addAttr(::new (S.Context)
1067 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1068 QT, Attr.getParameterLoc(),
1069 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001070}
1071
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001072static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001073 if (const RecordType *UT = T->getAsUnionType())
1074 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1075 RecordDecl *UD = UT->getDecl();
1076 for (RecordDecl::field_iterator it = UD->field_begin(),
1077 itend = UD->field_end(); it != itend; ++it) {
1078 QualType QT = it->getType();
1079 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1080 T = QT;
1081 return;
1082 }
1083 }
1084 }
1085}
1086
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001087static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001088 if (!isFunctionOrMethod(D)) {
1089 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1090 << "alloc_size" << ExpectedFunctionOrMethod;
1091 return;
1092 }
1093
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001094 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1095 return;
1096
1097 // In C++ the implicit 'this' function parameter also counts, and they are
1098 // counted from one.
1099 bool HasImplicitThisParam = isInstanceMethod(D);
Argyrios Kyrtzidise9365052013-02-22 06:58:28 +00001100 unsigned NumArgs;
1101 if (hasFunctionProto(D))
1102 NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1103 else
1104 NumArgs = 0;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001105
1106 SmallVector<unsigned, 8> SizeArgs;
1107
1108 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1109 E = Attr.arg_end(); I!=E; ++I) {
1110 // The argument must be an integer constant expression.
1111 Expr *Ex = *I;
1112 llvm::APSInt ArgNum;
1113 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1114 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1115 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1116 << "alloc_size" << Ex->getSourceRange();
1117 return;
1118 }
1119
1120 uint64_t x = ArgNum.getZExtValue();
1121
1122 if (x < 1 || x > NumArgs) {
1123 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1124 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1125 return;
1126 }
1127
1128 --x;
1129 if (HasImplicitThisParam) {
1130 if (x == 0) {
1131 S.Diag(Attr.getLoc(),
1132 diag::err_attribute_invalid_implicit_this_argument)
1133 << "alloc_size" << Ex->getSourceRange();
1134 return;
1135 }
1136 --x;
1137 }
1138
1139 // check if the function argument is of an integer type
1140 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1141 if (!T->isIntegerType()) {
1142 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1143 << "alloc_size" << Ex->getSourceRange();
1144 return;
1145 }
1146
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001147 SizeArgs.push_back(x);
1148 }
1149
1150 // check if the function returns a pointer
1151 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1152 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1153 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1154 }
1155
Michael Han99315932013-01-24 16:46:58 +00001156 D->addAttr(::new (S.Context)
1157 AllocSizeAttr(Attr.getRange(), S.Context,
1158 SizeArgs.data(), SizeArgs.size(),
1159 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001160}
1161
Chandler Carruthedc2c642011-07-02 00:01:44 +00001162static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001163 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1164 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001165 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001166 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001167 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001168 return;
1169 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001170
Chandler Carruth743682b2010-11-16 08:35:43 +00001171 // In C++ the implicit 'this' function parameter also counts, and they are
1172 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001173 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewyckye1121512013-01-24 01:12:16 +00001174 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001175
1176 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001177 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001178
Nick Lewyckye1121512013-01-24 01:12:16 +00001179 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1180 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001181 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001182 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001183 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001184 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1185 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001186 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1187 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001188 return;
1189 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001190
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001191 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001192
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001193 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001194 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +00001195 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001196 return;
1197 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001198
Ted Kremenek5224e6a2008-07-21 22:09:15 +00001199 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001200 if (HasImplicitThisParam) {
1201 if (x == 0) {
1202 S.Diag(Attr.getLoc(),
1203 diag::err_attribute_invalid_implicit_this_argument)
1204 << "nonnull" << Ex->getSourceRange();
1205 return;
1206 }
1207 --x;
1208 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001209
1210 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001211 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001212 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001213
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001214 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001215 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001216 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001217 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001218 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001219 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001220
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001221 NonNullArgs.push_back(x);
1222 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001223
1224 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1225 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001226 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001227 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1228 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001229 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001230 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001231 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001232 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001233
Ted Kremenek22813f42010-10-21 18:49:36 +00001234 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001235 if (NonNullArgs.empty()) {
1236 // Warn the trivial case only if attribute is not coming from a
1237 // macro instantiation.
1238 if (Attr.getLoc().isFileID())
1239 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001240 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001241 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001242 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001243
Nick Lewyckye1121512013-01-24 01:12:16 +00001244 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001245 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001246 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001247 D->addAttr(::new (S.Context)
1248 NonNullAttr(Attr.getRange(), S.Context, start, size,
1249 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001250}
1251
Chandler Carruthedc2c642011-07-02 00:01:44 +00001252static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001253 // This attribute must be applied to a function declaration.
1254 // The first argument to the attribute must be a string,
1255 // the name of the resource, for example "malloc".
1256 // The following arguments must be argument indexes, the arguments must be
1257 // of integer type for Returns, otherwise of pointer type.
1258 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001259 // after being held. free() should be __attribute((ownership_takes)), whereas
1260 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001261
1262 if (!AL.getParameterName()) {
1263 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1264 << AL.getName()->getName() << 1;
1265 return;
1266 }
1267 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001268 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001269 switch (AL.getKind()) {
1270 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001271 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001272 if (AL.getNumArgs() < 1) {
1273 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1274 return;
1275 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001276 break;
1277 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001278 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001279 if (AL.getNumArgs() < 1) {
1280 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1281 return;
1282 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001283 break;
1284 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001285 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001286 if (AL.getNumArgs() > 1) {
1287 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1288 << AL.getNumArgs() + 1;
1289 return;
1290 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001291 break;
1292 default:
1293 // This should never happen given how we are called.
1294 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001295 }
1296
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001297 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001298 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1299 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001300 return;
1301 }
1302
Chandler Carruth743682b2010-11-16 08:35:43 +00001303 // In C++ the implicit 'this' function parameter also counts, and they are
1304 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001305 bool HasImplicitThisParam = isInstanceMethod(D);
1306 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001307
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001308 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001309
1310 // Normalize the argument, __foo__ becomes foo.
1311 if (Module.startswith("__") && Module.endswith("__"))
1312 Module = Module.substr(2, Module.size() - 4);
1313
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001314 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001315
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001316 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1317 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001318
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001319 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001320 llvm::APSInt ArgNum(32);
1321 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1322 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1323 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1324 << AL.getName()->getName() << IdxExpr->getSourceRange();
1325 continue;
1326 }
1327
1328 unsigned x = (unsigned) ArgNum.getZExtValue();
1329
1330 if (x > NumArgs || x < 1) {
1331 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1332 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1333 continue;
1334 }
1335 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001336 if (HasImplicitThisParam) {
1337 if (x == 0) {
1338 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1339 << "ownership" << IdxExpr->getSourceRange();
1340 return;
1341 }
1342 --x;
1343 }
1344
Ted Kremenekd21139a2010-07-31 01:52:11 +00001345 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001346 case OwnershipAttr::Takes:
1347 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001348 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001349 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001350 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1351 // FIXME: Should also highlight argument in decl.
1352 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001353 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001354 << "pointer"
1355 << IdxExpr->getSourceRange();
1356 continue;
1357 }
1358 break;
1359 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001360 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001361 if (AL.getNumArgs() > 1) {
1362 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001363 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001364 llvm::APSInt ArgNum(32);
1365 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1366 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1367 S.Diag(AL.getLoc(), diag::err_ownership_type)
1368 << "ownership_returns" << "integer"
1369 << IdxExpr->getSourceRange();
1370 return;
1371 }
1372 }
1373 break;
1374 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001375 } // switch
1376
1377 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001378 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001379 i = D->specific_attr_begin<OwnershipAttr>(),
1380 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001381 i != e; ++i) {
1382 if ((*i)->getOwnKind() != K) {
1383 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1384 I!=E; ++I) {
1385 if (x == *I) {
1386 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1387 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001388 }
1389 }
1390 }
1391 }
1392 OwnershipArgs.push_back(x);
1393 }
1394
1395 unsigned* start = OwnershipArgs.data();
1396 unsigned size = OwnershipArgs.size();
1397 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001398
1399 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1400 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1401 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001402 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001403
Michael Han99315932013-01-24 16:46:58 +00001404 D->addAttr(::new (S.Context)
1405 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1406 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001407}
1408
Chandler Carruthedc2c642011-07-02 00:01:44 +00001409static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001410 // Check the attribute arguments.
1411 if (Attr.getNumArgs() > 1) {
1412 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1413 return;
1414 }
1415
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001416 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001417 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001418 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001419 return;
1420 }
1421
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001422 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001423
Rafael Espindolac18086a2010-02-23 22:00:30 +00001424 // gcc rejects
1425 // class c {
1426 // static int a __attribute__((weakref ("v2")));
1427 // static int b() __attribute__((weakref ("f3")));
1428 // };
1429 // and ignores the attributes of
1430 // void f(void) {
1431 // static int a __attribute__((weakref ("v2")));
1432 // }
1433 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001434 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001435 if (!Ctx->isFileContext()) {
1436 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001437 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001438 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001439 }
1440
1441 // The GCC manual says
1442 //
1443 // At present, a declaration to which `weakref' is attached can only
1444 // be `static'.
1445 //
1446 // It also says
1447 //
1448 // Without a TARGET,
1449 // given as an argument to `weakref' or to `alias', `weakref' is
1450 // equivalent to `weak'.
1451 //
1452 // gcc 4.4.1 will accept
1453 // int a7 __attribute__((weakref));
1454 // as
1455 // int a7 __attribute__((weak));
1456 // This looks like a bug in gcc. We reject that for now. We should revisit
1457 // it if this behaviour is actually used.
1458
Rafael Espindolac18086a2010-02-23 22:00:30 +00001459 // GCC rejects
1460 // static ((alias ("y"), weakref)).
1461 // Should we? How to check that weakref is before or after alias?
1462
1463 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001464 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001465 Arg = Arg->IgnoreParenCasts();
1466 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1467
Douglas Gregorfb65e592011-07-27 05:40:30 +00001468 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001469 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1470 << "weakref" << 1;
1471 return;
1472 }
1473 // GCC will accept anything as the argument of weakref. Should we
1474 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001475 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001476 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001477 }
1478
Michael Han99315932013-01-24 16:46:58 +00001479 D->addAttr(::new (S.Context)
1480 WeakRefAttr(Attr.getRange(), S.Context,
1481 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001482}
1483
Chandler Carruthedc2c642011-07-02 00:01:44 +00001484static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001485 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001486 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001487 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001488 return;
1489 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001490
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001491 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001492 Arg = Arg->IgnoreParenCasts();
1493 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001494
Douglas Gregorfb65e592011-07-27 05:40:30 +00001495 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001496 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001497 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001498 return;
1499 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001500
Douglas Gregore8bbc122011-09-02 00:18:52 +00001501 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001502 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1503 return;
1504 }
1505
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001506 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001507
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001508 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00001509 Str->getString(),
1510 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001511}
1512
Quentin Colombet4e172062012-11-01 23:55:47 +00001513static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1514 // Check the attribute arguments.
1515 if (!checkAttributeNumArgs(S, Attr, 0))
1516 return;
1517
1518 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1519 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1520 << Attr.getName() << ExpectedFunctionOrMethod;
1521 return;
1522 }
1523
Michael Han99315932013-01-24 16:46:58 +00001524 D->addAttr(::new (S.Context)
1525 MinSizeAttr(Attr.getRange(), S.Context,
1526 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001527}
1528
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001529static void handleColdAttr(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)) {
1535 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1536 << Attr.getName() << ExpectedFunction;
1537 return;
1538 }
1539
1540 if (D->hasAttr<HotAttr>()) {
1541 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1542 << Attr.getName() << "hot";
1543 return;
1544 }
1545
Michael Han99315932013-01-24 16:46:58 +00001546 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1547 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001548}
1549
1550static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1551 // Check the attribute arguments.
1552 if (!checkAttributeNumArgs(S, Attr, 0))
1553 return;
1554
1555 if (!isa<FunctionDecl>(D)) {
1556 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1557 << Attr.getName() << ExpectedFunction;
1558 return;
1559 }
1560
1561 if (D->hasAttr<ColdAttr>()) {
1562 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1563 << Attr.getName() << "cold";
1564 return;
1565 }
1566
Michael Han99315932013-01-24 16:46:58 +00001567 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1568 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001569}
1570
Chandler Carruthedc2c642011-07-02 00:01:44 +00001571static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001572 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001573 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001574 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001575
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001576 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001577 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001578 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001579 return;
1580 }
1581
Michael Han99315932013-01-24 16:46:58 +00001582 D->addAttr(::new (S.Context)
1583 NakedAttr(Attr.getRange(), S.Context,
1584 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001585}
1586
Chandler Carruthedc2c642011-07-02 00:01:44 +00001587static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1588 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001589 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001590 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001591 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1592 return;
1593 }
1594
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001595 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001596 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001597 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001598 return;
1599 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001600
Michael Han99315932013-01-24 16:46:58 +00001601 D->addAttr(::new (S.Context)
1602 AlwaysInlineAttr(Attr.getRange(), S.Context,
1603 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001604}
1605
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001606static void handleTLSModelAttr(Sema &S, Decl *D,
1607 const AttributeList &Attr) {
1608 // Check the attribute arguments.
1609 if (Attr.getNumArgs() != 1) {
1610 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1611 return;
1612 }
1613
1614 Expr *Arg = Attr.getArg(0);
1615 Arg = Arg->IgnoreParenCasts();
1616 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1617
1618 // Check that it is a string.
1619 if (!Str) {
1620 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1621 return;
1622 }
1623
1624 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1625 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1626 << Attr.getName() << ExpectedTLSVar;
1627 return;
1628 }
1629
1630 // Check that the value.
1631 StringRef Model = Str->getString();
1632 if (Model != "global-dynamic" && Model != "local-dynamic"
1633 && Model != "initial-exec" && Model != "local-exec") {
1634 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1635 return;
1636 }
1637
Michael Han99315932013-01-24 16:46:58 +00001638 D->addAttr(::new (S.Context)
1639 TLSModelAttr(Attr.getRange(), S.Context, Model,
1640 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001641}
1642
Chandler Carruthedc2c642011-07-02 00:01:44 +00001643static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001644 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001645 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001646 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1647 return;
1648 }
Mike Stump11289f42009-09-09 15:08:12 +00001649
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001650 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001651 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001652 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001653 D->addAttr(::new (S.Context)
1654 MallocAttr(Attr.getRange(), S.Context,
1655 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001656 return;
1657 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001658 }
1659
Ted Kremenek08479ae2009-08-15 00:51:46 +00001660 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001661}
1662
Chandler Carruthedc2c642011-07-02 00:01:44 +00001663static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001664 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001665 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001666 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001667
Michael Han99315932013-01-24 16:46:58 +00001668 D->addAttr(::new (S.Context)
1669 MayAliasAttr(Attr.getRange(), S.Context,
1670 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001671}
1672
Chandler Carruthedc2c642011-07-02 00:01:44 +00001673static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001674 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001675 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001676 D->addAttr(::new (S.Context)
1677 NoCommonAttr(Attr.getRange(), S.Context,
1678 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001679 else
1680 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001681 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001682}
1683
Chandler Carruthedc2c642011-07-02 00:01:44 +00001684static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001685 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001686 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001687 D->addAttr(::new (S.Context)
1688 CommonAttr(Attr.getRange(), S.Context,
1689 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001690 else
1691 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001692 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001693}
1694
Chandler Carruthedc2c642011-07-02 00:01:44 +00001695static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001696 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001697
1698 if (S.CheckNoReturnAttr(attr)) return;
1699
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001700 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001701 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001702 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001703 return;
1704 }
1705
Michael Han99315932013-01-24 16:46:58 +00001706 D->addAttr(::new (S.Context)
1707 NoReturnAttr(attr.getRange(), S.Context,
1708 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001709}
1710
1711bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001712 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001713 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1714 attr.setInvalid();
1715 return true;
1716 }
1717
1718 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001719}
1720
Chandler Carruthedc2c642011-07-02 00:01:44 +00001721static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1722 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001723
1724 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1725 // because 'analyzer_noreturn' does not impact the type.
1726
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001727 if(!checkAttributeNumArgs(S, Attr, 0))
1728 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001729
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001730 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1731 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001732 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1733 && !VD->getType()->isFunctionPointerType())) {
1734 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001735 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001736 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001737 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001738 return;
1739 }
1740 }
1741
Michael Han99315932013-01-24 16:46:58 +00001742 D->addAttr(::new (S.Context)
1743 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1744 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001745}
1746
Richard Smith10876ef2013-01-17 01:30:42 +00001747static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1748 const AttributeList &Attr) {
1749 // C++11 [dcl.attr.noreturn]p1:
1750 // The attribute may be applied to the declarator-id in a function
1751 // declaration.
1752 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1753 if (!FD) {
1754 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1755 << Attr.getName() << ExpectedFunctionOrMethod;
1756 return;
1757 }
1758
Michael Han99315932013-01-24 16:46:58 +00001759 D->addAttr(::new (S.Context)
1760 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1761 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001762}
1763
John Thompsoncdb847ba2010-08-09 21:53:52 +00001764// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001765static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001766/*
1767 Returning a Vector Class in Registers
1768
Eric Christopherbc638a82010-12-01 22:13:54 +00001769 According to the PPU ABI specifications, a class with a single member of
1770 vector type is returned in memory when used as the return value of a function.
1771 This results in inefficient code when implementing vector classes. To return
1772 the value in a single vector register, add the vecreturn attribute to the
1773 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001774
1775 Example:
1776
1777 struct Vector
1778 {
1779 __vector float xyzw;
1780 } __attribute__((vecreturn));
1781
1782 Vector Add(Vector lhs, Vector rhs)
1783 {
1784 Vector result;
1785 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1786 return result; // This will be returned in a register
1787 }
1788*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001789 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001791 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001792 return;
1793 }
1794
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001795 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001796 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1797 return;
1798 }
1799
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001800 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001801 int count = 0;
1802
1803 if (!isa<CXXRecordDecl>(record)) {
1804 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1805 return;
1806 }
1807
1808 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1809 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1810 return;
1811 }
1812
Eric Christopherbc638a82010-12-01 22:13:54 +00001813 for (RecordDecl::field_iterator iter = record->field_begin();
1814 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001815 if ((count == 1) || !iter->getType()->isVectorType()) {
1816 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1817 return;
1818 }
1819 count++;
1820 }
1821
Michael Han99315932013-01-24 16:46:58 +00001822 D->addAttr(::new (S.Context)
1823 VecReturnAttr(Attr.getRange(), S.Context,
1824 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001825}
1826
Richard Smithe233fbf2013-01-28 22:42:45 +00001827static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1828 const AttributeList &Attr) {
1829 if (isa<ParmVarDecl>(D)) {
1830 // [[carries_dependency]] can only be applied to a parameter if it is a
1831 // parameter of a function declaration or lambda.
1832 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1833 S.Diag(Attr.getLoc(),
1834 diag::err_carries_dependency_param_not_function_decl);
1835 return;
1836 }
1837 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001838 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001839 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001840 return;
1841 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001842
1843 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1844 Attr.getRange(), S.Context,
1845 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001846}
1847
Chandler Carruthedc2c642011-07-02 00:01:44 +00001848static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001849 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001850 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001851 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001852 return;
1853 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001854
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001855 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001856 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001857 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001858 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001859 return;
1860 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001861
Michael Han99315932013-01-24 16:46:58 +00001862 D->addAttr(::new (S.Context)
1863 UnusedAttr(Attr.getRange(), S.Context,
1864 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001865}
1866
Rafael Espindola70107f92011-10-03 14:59:42 +00001867static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1868 const AttributeList &Attr) {
1869 // check the attribute arguments.
1870 if (Attr.hasParameterOrArguments()) {
1871 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1872 return;
1873 }
1874
1875 if (!isa<FunctionDecl>(D)) {
1876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1877 << Attr.getName() << ExpectedFunction;
1878 return;
1879 }
1880
Michael Han99315932013-01-24 16:46:58 +00001881 D->addAttr(::new (S.Context)
1882 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1883 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00001884}
1885
Chandler Carruthedc2c642011-07-02 00:01:44 +00001886static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001887 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001888 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001889 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1890 return;
1891 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001892
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001893 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001894 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001895 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1896 return;
1897 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001898 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001899 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001900 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001901 return;
1902 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001903
Michael Han99315932013-01-24 16:46:58 +00001904 D->addAttr(::new (S.Context)
1905 UsedAttr(Attr.getRange(), S.Context,
1906 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001907}
1908
Chandler Carruthedc2c642011-07-02 00:01:44 +00001909static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001910 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001911 if (Attr.getNumArgs() > 1) {
1912 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001913 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001914 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001915
1916 int priority = 65535; // FIXME: Do not hardcode such constants.
1917 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001918 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001919 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001920 if (E->isTypeDependent() || E->isValueDependent() ||
1921 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001922 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001923 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001924 return;
1925 }
1926 priority = Idx.getZExtValue();
1927 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001928
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001929 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001930 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001931 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001932 return;
1933 }
1934
Michael Han99315932013-01-24 16:46:58 +00001935 D->addAttr(::new (S.Context)
1936 ConstructorAttr(Attr.getRange(), S.Context, priority,
1937 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001938}
1939
Chandler Carruthedc2c642011-07-02 00:01:44 +00001940static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001941 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001942 if (Attr.getNumArgs() > 1) {
1943 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001944 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001945 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001946
1947 int priority = 65535; // FIXME: Do not hardcode such constants.
1948 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001949 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001950 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001951 if (E->isTypeDependent() || E->isValueDependent() ||
1952 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001953 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001954 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001955 return;
1956 }
1957 priority = Idx.getZExtValue();
1958 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001959
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001960 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001961 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001962 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001963 return;
1964 }
1965
Michael Han99315932013-01-24 16:46:58 +00001966 D->addAttr(::new (S.Context)
1967 DestructorAttr(Attr.getRange(), S.Context, priority,
1968 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001969}
1970
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001971template <typename AttrTy>
1972static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1973 const char *Name) {
Chris Lattner190aa102011-02-24 05:42:24 +00001974 unsigned NumArgs = Attr.getNumArgs();
1975 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001976 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001977 return;
1978 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001979
1980 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001981 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001982 if (NumArgs == 1) {
1983 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001984 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001985 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001986 << Name;
Fariborz Jahanian551063102010-10-06 21:18:44 +00001987 return;
1988 }
Chris Lattner190aa102011-02-24 05:42:24 +00001989 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001990 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001991
Michael Han99315932013-01-24 16:46:58 +00001992 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1993 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001994}
1995
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001996static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1997 const AttributeList &Attr) {
1998 unsigned NumArgs = Attr.getNumArgs();
1999 if (NumArgs > 0) {
2000 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2001 return;
2002 }
2003
Michael Han99315932013-01-24 16:46:58 +00002004 D->addAttr(::new (S.Context)
2005 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2006 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002007}
2008
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002009static void handleObjCRootClassAttr(Sema &S, Decl *D,
2010 const AttributeList &Attr) {
2011 if (!isa<ObjCInterfaceDecl>(D)) {
2012 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2013 return;
2014 }
2015
2016 unsigned NumArgs = Attr.getNumArgs();
2017 if (NumArgs > 0) {
2018 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2019 return;
2020 }
2021
Michael Han99315932013-01-24 16:46:58 +00002022 D->addAttr(::new (S.Context)
2023 ObjCRootClassAttr(Attr.getRange(), S.Context,
2024 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002025}
2026
Michael Han99315932013-01-24 16:46:58 +00002027static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2028 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002029 if (!isa<ObjCInterfaceDecl>(D)) {
2030 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2031 return;
2032 }
2033
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002034 unsigned NumArgs = Attr.getNumArgs();
2035 if (NumArgs > 0) {
2036 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2037 return;
2038 }
2039
Michael Han99315932013-01-24 16:46:58 +00002040 D->addAttr(::new (S.Context)
2041 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2042 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002043}
2044
Jordy Rose740b0c22012-05-08 03:27:22 +00002045static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2046 IdentifierInfo *Platform,
2047 VersionTuple Introduced,
2048 VersionTuple Deprecated,
2049 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002050 StringRef PlatformName
2051 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2052 if (PlatformName.empty())
2053 PlatformName = Platform->getName();
2054
2055 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2056 // of these steps are needed).
2057 if (!Introduced.empty() && !Deprecated.empty() &&
2058 !(Introduced <= Deprecated)) {
2059 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2060 << 1 << PlatformName << Deprecated.getAsString()
2061 << 0 << Introduced.getAsString();
2062 return true;
2063 }
2064
2065 if (!Introduced.empty() && !Obsoleted.empty() &&
2066 !(Introduced <= Obsoleted)) {
2067 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2068 << 2 << PlatformName << Obsoleted.getAsString()
2069 << 0 << Introduced.getAsString();
2070 return true;
2071 }
2072
2073 if (!Deprecated.empty() && !Obsoleted.empty() &&
2074 !(Deprecated <= Obsoleted)) {
2075 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2076 << 2 << PlatformName << Obsoleted.getAsString()
2077 << 1 << Deprecated.getAsString();
2078 return true;
2079 }
2080
2081 return false;
2082}
2083
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002084/// \brief Check whether the two versions match.
2085///
2086/// If either version tuple is empty, then they are assumed to match. If
2087/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2088static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2089 bool BeforeIsOkay) {
2090 if (X.empty() || Y.empty())
2091 return true;
2092
2093 if (X == Y)
2094 return true;
2095
2096 if (BeforeIsOkay && X < Y)
2097 return true;
2098
2099 return false;
2100}
2101
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002102AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002103 IdentifierInfo *Platform,
2104 VersionTuple Introduced,
2105 VersionTuple Deprecated,
2106 VersionTuple Obsoleted,
2107 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002108 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002109 bool Override,
2110 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002111 VersionTuple MergedIntroduced = Introduced;
2112 VersionTuple MergedDeprecated = Deprecated;
2113 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002114 bool FoundAny = false;
2115
Rafael Espindolac67f2232012-05-10 02:50:16 +00002116 if (D->hasAttrs()) {
2117 AttrVec &Attrs = D->getAttrs();
2118 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2119 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2120 if (!OldAA) {
2121 ++i;
2122 continue;
2123 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002124
Rafael Espindolac67f2232012-05-10 02:50:16 +00002125 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2126 if (OldPlatform != Platform) {
2127 ++i;
2128 continue;
2129 }
2130
2131 FoundAny = true;
2132 VersionTuple OldIntroduced = OldAA->getIntroduced();
2133 VersionTuple OldDeprecated = OldAA->getDeprecated();
2134 VersionTuple OldObsoleted = OldAA->getObsoleted();
2135 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002136
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002137 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2138 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2139 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2140 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002141 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002142 if (Override) {
2143 int Which = -1;
2144 VersionTuple FirstVersion;
2145 VersionTuple SecondVersion;
2146 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2147 Which = 0;
2148 FirstVersion = OldIntroduced;
2149 SecondVersion = Introduced;
2150 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2151 Which = 1;
2152 FirstVersion = Deprecated;
2153 SecondVersion = OldDeprecated;
2154 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2155 Which = 2;
2156 FirstVersion = Obsoleted;
2157 SecondVersion = OldObsoleted;
2158 }
2159
2160 if (Which == -1) {
2161 Diag(OldAA->getLocation(),
2162 diag::warn_mismatched_availability_override_unavail)
2163 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2164 } else {
2165 Diag(OldAA->getLocation(),
2166 diag::warn_mismatched_availability_override)
2167 << Which
2168 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2169 << FirstVersion.getAsString() << SecondVersion.getAsString();
2170 }
2171 Diag(Range.getBegin(), diag::note_overridden_method);
2172 } else {
2173 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2174 Diag(Range.getBegin(), diag::note_previous_attribute);
2175 }
2176
Rafael Espindolac67f2232012-05-10 02:50:16 +00002177 Attrs.erase(Attrs.begin() + i);
2178 --e;
2179 continue;
2180 }
2181
2182 VersionTuple MergedIntroduced2 = MergedIntroduced;
2183 VersionTuple MergedDeprecated2 = MergedDeprecated;
2184 VersionTuple MergedObsoleted2 = MergedObsoleted;
2185
2186 if (MergedIntroduced2.empty())
2187 MergedIntroduced2 = OldIntroduced;
2188 if (MergedDeprecated2.empty())
2189 MergedDeprecated2 = OldDeprecated;
2190 if (MergedObsoleted2.empty())
2191 MergedObsoleted2 = OldObsoleted;
2192
2193 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2194 MergedIntroduced2, MergedDeprecated2,
2195 MergedObsoleted2)) {
2196 Attrs.erase(Attrs.begin() + i);
2197 --e;
2198 continue;
2199 }
2200
2201 MergedIntroduced = MergedIntroduced2;
2202 MergedDeprecated = MergedDeprecated2;
2203 MergedObsoleted = MergedObsoleted2;
2204 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002205 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002206 }
2207
2208 if (FoundAny &&
2209 MergedIntroduced == Introduced &&
2210 MergedDeprecated == Deprecated &&
2211 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002212 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002213
Rafael Espindolac67f2232012-05-10 02:50:16 +00002214 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002215 MergedDeprecated, MergedObsoleted)) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002216 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2217 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002218 Obsoleted, IsUnavailable, Message,
2219 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002220 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002221 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002222}
2223
Chandler Carruthedc2c642011-07-02 00:01:44 +00002224static void handleAvailabilityAttr(Sema &S, Decl *D,
2225 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002226 IdentifierInfo *Platform = Attr.getParameterName();
2227 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han99315932013-01-24 16:46:58 +00002228 unsigned Index = Attr.getAttributeSpellingListIndex();
2229
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002230 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002231 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2232 << Platform;
2233
Rafael Espindolac231fab2013-01-08 21:30:32 +00002234 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2235 if (!ND) {
2236 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2237 return;
2238 }
2239
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002240 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2241 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2242 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002243 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002244 StringRef Str;
2245 const StringLiteral *SE =
2246 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2247 if (SE)
2248 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002249
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002250 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002251 Platform,
2252 Introduced.Version,
2253 Deprecated.Version,
2254 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002255 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002256 /*Override=*/false,
2257 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002258 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002259 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002260}
2261
John McCalld041a9b2013-02-20 01:54:26 +00002262template <class T>
2263static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2264 typename T::VisibilityType value,
2265 unsigned attrSpellingListIndex) {
2266 T *existingAttr = D->getAttr<T>();
2267 if (existingAttr) {
2268 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2269 if (existingValue == value)
2270 return NULL;
2271 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2272 S.Diag(range.getBegin(), diag::note_previous_attribute);
2273 D->dropAttr<T>();
2274 }
2275 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2276}
2277
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002278VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002279 VisibilityAttr::VisibilityType Vis,
2280 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002281 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2282 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002283}
2284
John McCalld041a9b2013-02-20 01:54:26 +00002285TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2286 TypeVisibilityAttr::VisibilityType Vis,
2287 unsigned AttrSpellingListIndex) {
2288 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2289 AttrSpellingListIndex);
2290}
2291
2292static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2293 bool isTypeVisibility) {
2294 // Visibility attributes don't mean anything on a typedef.
2295 if (isa<TypedefNameDecl>(D)) {
2296 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2297 << Attr.getName();
2298 return;
2299 }
2300
2301 // 'type_visibility' can only go on a type or namespace.
2302 if (isTypeVisibility &&
2303 !(isa<TagDecl>(D) ||
2304 isa<ObjCInterfaceDecl>(D) ||
2305 isa<NamespaceDecl>(D))) {
2306 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2307 << Attr.getName() << ExpectedTypeOrNamespace;
2308 return;
2309 }
2310
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002311 // check the attribute arguments.
John McCalld041a9b2013-02-20 01:54:26 +00002312 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002313 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002314
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002315 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002316 Arg = Arg->IgnoreParenCasts();
2317 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002318
Douglas Gregorfb65e592011-07-27 05:40:30 +00002319 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002320 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld041a9b2013-02-20 01:54:26 +00002321 << (isTypeVisibility ? "type_visibility" : "visibility") << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002322 return;
2323 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002324
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002325 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002326 VisibilityAttr::VisibilityType type;
Michael Han99315932013-01-24 16:46:58 +00002327
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002328 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002329 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002330 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002331 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002332 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002333 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00002334 else if (TypeStr == "protected") {
2335 // Complain about attempts to use protected visibility on targets
2336 // (like Darwin) that don't support it.
2337 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2338 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2339 type = VisibilityAttr::Default;
2340 } else {
2341 type = VisibilityAttr::Protected;
2342 }
2343 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002344 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002345 return;
2346 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002347
Michael Han99315932013-01-24 16:46:58 +00002348 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002349 clang::Attr *newAttr;
2350 if (isTypeVisibility) {
2351 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2352 (TypeVisibilityAttr::VisibilityType) type,
2353 Index);
2354 } else {
2355 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2356 }
2357 if (newAttr)
2358 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002359}
2360
Chandler Carruthedc2c642011-07-02 00:01:44 +00002361static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2362 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002363 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2364 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002365 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002366 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002367 return;
2368 }
2369
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002370 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2371 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2372 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00002373 << "objc_method_family" << 1;
2374 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002375 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002376 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002377 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002378 return;
2379 }
2380
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002381 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002382 ObjCMethodFamilyAttr::FamilyKind family;
2383 if (param == "none")
2384 family = ObjCMethodFamilyAttr::OMF_None;
2385 else if (param == "alloc")
2386 family = ObjCMethodFamilyAttr::OMF_alloc;
2387 else if (param == "copy")
2388 family = ObjCMethodFamilyAttr::OMF_copy;
2389 else if (param == "init")
2390 family = ObjCMethodFamilyAttr::OMF_init;
2391 else if (param == "mutableCopy")
2392 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2393 else if (param == "new")
2394 family = ObjCMethodFamilyAttr::OMF_new;
2395 else {
2396 // Just warn and ignore it. This is future-proof against new
2397 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002398 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002399 return;
2400 }
2401
John McCall31168b02011-06-15 23:02:42 +00002402 if (family == ObjCMethodFamilyAttr::OMF_init &&
2403 !method->getResultType()->isObjCObjectPointerType()) {
2404 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2405 << method->getResultType();
2406 // Ignore the attribute.
2407 return;
2408 }
2409
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002410 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002411 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002412}
2413
Chandler Carruthedc2c642011-07-02 00:01:44 +00002414static void handleObjCExceptionAttr(Sema &S, Decl *D,
2415 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002416 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002417 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002418
Chris Lattner677a3582009-02-14 08:09:34 +00002419 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2420 if (OCI == 0) {
2421 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2422 return;
2423 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002424
Michael Han99315932013-01-24 16:46:58 +00002425 D->addAttr(::new (S.Context)
2426 ObjCExceptionAttr(Attr.getRange(), S.Context,
2427 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002428}
2429
Chandler Carruthedc2c642011-07-02 00:01:44 +00002430static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002431 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002433 return;
2434 }
Richard Smithdda56e42011-04-15 14:24:37 +00002435 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002436 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002437 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002438 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2439 return;
2440 }
2441 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002442 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2443 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002444 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002445 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2446 return;
2447 }
2448 }
2449 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002450 // It is okay to include this attribute on properties, e.g.:
2451 //
2452 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2453 //
2454 // In this case it follows tradition and suppresses an error in the above
2455 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002456 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002457 }
Michael Han99315932013-01-24 16:46:58 +00002458 D->addAttr(::new (S.Context)
2459 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2460 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002461}
2462
Mike Stumpd3bb5572009-07-24 19:02:52 +00002463static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002464handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002465 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002466 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002467 return;
2468 }
2469
2470 if (!isa<FunctionDecl>(D)) {
2471 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2472 return;
2473 }
2474
Michael Han99315932013-01-24 16:46:58 +00002475 D->addAttr(::new (S.Context)
2476 OverloadableAttr(Attr.getRange(), S.Context,
2477 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002478}
2479
Chandler Carruthedc2c642011-07-02 00:01:44 +00002480static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002481 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002482 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002483 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002484 return;
2485 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002486
Steve Naroff3405a732008-09-18 16:44:58 +00002487 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002488 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002489 return;
2490 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002491
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002492 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002493 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002494 type = BlocksAttr::ByRef;
2495 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002496 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002497 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002498 return;
2499 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002500
Michael Han99315932013-01-24 16:46:58 +00002501 D->addAttr(::new (S.Context)
2502 BlocksAttr(Attr.getRange(), S.Context, type,
2503 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002504}
2505
Chandler Carruthedc2c642011-07-02 00:01:44 +00002506static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002507 // check the attribute arguments.
2508 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002509 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002510 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002511 }
2512
John McCallb46f2872011-09-09 07:56:05 +00002513 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002514 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002515 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002516 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002517 if (E->isTypeDependent() || E->isValueDependent() ||
2518 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002519 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002520 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002521 return;
2522 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002523
John McCallb46f2872011-09-09 07:56:05 +00002524 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002525 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2526 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002527 return;
2528 }
John McCallb46f2872011-09-09 07:56:05 +00002529
2530 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002531 }
2532
John McCallb46f2872011-09-09 07:56:05 +00002533 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002534 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002535 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002536 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002537 if (E->isTypeDependent() || E->isValueDependent() ||
2538 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002539 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002540 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002541 return;
2542 }
2543 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002544
John McCallb46f2872011-09-09 07:56:05 +00002545 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002546 // FIXME: This error message could be improved, it would be nice
2547 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002548 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2549 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002550 return;
2551 }
2552 }
2553
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002554 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002555 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002556 if (isa<FunctionNoProtoType>(FT)) {
2557 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2558 return;
2559 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002560
Chris Lattner9363e312009-03-17 23:03:47 +00002561 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002562 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002563 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002564 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002565 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002566 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002567 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002568 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002569 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002570 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2571 if (!BD->isVariadic()) {
2572 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2573 return;
2574 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002575 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002576 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002577 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002578 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002579 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002580 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002581 int m = Ty->isFunctionPointerType() ? 0 : 1;
2582 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002583 return;
2584 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002585 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002586 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002587 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002588 return;
2589 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002590 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002591 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002592 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002593 return;
2594 }
Michael Han99315932013-01-24 16:46:58 +00002595 D->addAttr(::new (S.Context)
2596 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2597 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002598}
2599
Chandler Carruthedc2c642011-07-02 00:01:44 +00002600static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002601 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002602 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002603 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002604
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002605 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002606 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002607 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002608 return;
2609 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002610
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002611 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2612 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2613 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002614 return;
2615 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002616 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2617 if (MD->getResultType()->isVoidType()) {
2618 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2619 << Attr.getName() << 1;
2620 return;
2621 }
2622
Michael Han99315932013-01-24 16:46:58 +00002623 D->addAttr(::new (S.Context)
2624 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2625 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002626}
2627
Chandler Carruthedc2c642011-07-02 00:01:44 +00002628static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002629 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002630 if (Attr.hasParameterOrArguments()) {
2631 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002632 return;
2633 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002634
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002635 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002636 if (isa<CXXRecordDecl>(D)) {
2637 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2638 return;
2639 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002640 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2641 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002642 return;
2643 }
2644
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002645 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002646
Michael Han99315932013-01-24 16:46:58 +00002647 nd->addAttr(::new (S.Context)
2648 WeakAttr(Attr.getRange(), S.Context,
2649 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002650}
2651
Chandler Carruthedc2c642011-07-02 00:01:44 +00002652static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002653 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002654 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002655 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002656
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002657
2658 // weak_import only applies to variable & function declarations.
2659 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002660 if (!D->canBeWeakImported(isDef)) {
2661 if (isDef)
2662 S.Diag(Attr.getLoc(),
2663 diag::warn_attribute_weak_import_invalid_on_definition)
2664 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002665 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002666 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002667 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002668 // Nothing to warn about here.
2669 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002670 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002671 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002672
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002673 return;
2674 }
2675
Michael Han99315932013-01-24 16:46:58 +00002676 D->addAttr(::new (S.Context)
2677 WeakImportAttr(Attr.getRange(), S.Context,
2678 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002679}
2680
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002681// Handles reqd_work_group_size and work_group_size_hint.
2682static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002683 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002684 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2685 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2686
Nate Begemanf2758702009-06-26 06:32:41 +00002687 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002688 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002689
2690 unsigned WGSize[3];
2691 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002692 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002693 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002694 if (E->isTypeDependent() || E->isValueDependent() ||
2695 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002696 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002697 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002698 return;
2699 }
2700 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2701 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002702
2703 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2704 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2705 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2706 if (!(A->getXDim() == WGSize[0] &&
2707 A->getYDim() == WGSize[1] &&
2708 A->getZDim() == WGSize[2])) {
2709 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2710 Attr.getName();
2711 }
2712 }
2713
2714 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2715 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2716 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2717 if (!(A->getXDim() == WGSize[0] &&
2718 A->getYDim() == WGSize[1] &&
2719 A->getZDim() == WGSize[2])) {
2720 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2721 Attr.getName();
2722 }
2723 }
2724
2725 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2726 D->addAttr(::new (S.Context)
2727 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002728 WGSize[0], WGSize[1], WGSize[2],
2729 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002730 else
2731 D->addAttr(::new (S.Context)
2732 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002733 WGSize[0], WGSize[1], WGSize[2],
2734 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002735}
2736
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002737SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002738 StringRef Name,
2739 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002740 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2741 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002742 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002743 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2744 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002745 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002746 }
Michael Han99315932013-01-24 16:46:58 +00002747 return ::new (Context) SectionAttr(Range, Context, Name,
2748 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002749}
2750
Chandler Carruthedc2c642011-07-02 00:01:44 +00002751static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002752 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002753 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002754 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002755
2756 // Make sure that there is a string literal as the sections's single
2757 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002758 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002759 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002760 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002761 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002762 return;
2763 }
Mike Stump11289f42009-09-09 15:08:12 +00002764
Chris Lattner30ba6742009-08-10 19:03:04 +00002765 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002766 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002767 if (!Error.empty()) {
2768 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2769 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002770 return;
2771 }
Mike Stump11289f42009-09-09 15:08:12 +00002772
Chris Lattner20aee9b2010-01-12 20:58:53 +00002773 // This attribute cannot be applied to local variables.
2774 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2775 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2776 return;
2777 }
Michael Han99315932013-01-24 16:46:58 +00002778
2779 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002780 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han99315932013-01-24 16:46:58 +00002781 SE->getString(), Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002782 if (NewAttr)
2783 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002784}
2785
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002786
Chandler Carruthedc2c642011-07-02 00:01:44 +00002787static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002788 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002789 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002791 return;
2792 }
Douglas Gregor88336832011-06-15 05:45:11 +00002793
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002794 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002795 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002796 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002797 } else {
Michael Han99315932013-01-24 16:46:58 +00002798 D->addAttr(::new (S.Context)
2799 NoThrowAttr(Attr.getRange(), S.Context,
2800 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002801 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002802}
2803
Chandler Carruthedc2c642011-07-02 00:01:44 +00002804static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002805 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002806 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002807 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002808 return;
2809 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002810
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002811 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002812 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002813 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002814 } else {
Michael Han99315932013-01-24 16:46:58 +00002815 D->addAttr(::new (S.Context)
2816 ConstAttr(Attr.getRange(), S.Context,
2817 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002818 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002819}
2820
Chandler Carruthedc2c642011-07-02 00:01:44 +00002821static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002822 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002823 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002824 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002825
Michael Han99315932013-01-24 16:46:58 +00002826 D->addAttr(::new (S.Context)
2827 PureAttr(Attr.getRange(), S.Context,
2828 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002829}
2830
Chandler Carruthedc2c642011-07-02 00:01:44 +00002831static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002832 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2834 return;
2835 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002836
Anders Carlssond277d792009-01-31 01:16:18 +00002837 if (Attr.getNumArgs() != 0) {
2838 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2839 return;
2840 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002841
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002842 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002843
Anders Carlssond277d792009-01-31 01:16:18 +00002844 if (!VD || !VD->hasLocalStorage()) {
2845 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2846 return;
2847 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002848
Anders Carlssond277d792009-01-31 01:16:18 +00002849 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002850 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002851 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002852 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2853 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002854 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002855 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002856 Attr.getParameterName();
2857 return;
2858 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002859
Anders Carlssond277d792009-01-31 01:16:18 +00002860 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2861 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002862 S.Diag(Attr.getParameterLoc(),
2863 diag::err_attribute_cleanup_arg_not_function)
2864 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002865 return;
2866 }
2867
Anders Carlssond277d792009-01-31 01:16:18 +00002868 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002869 S.Diag(Attr.getParameterLoc(),
2870 diag::err_attribute_cleanup_func_must_take_one_arg)
2871 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002872 return;
2873 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002874
Anders Carlsson723f55d2009-02-07 23:16:50 +00002875 // We're currently more strict than GCC about what function types we accept.
2876 // If this ever proves to be a problem it should be easy to fix.
2877 QualType Ty = S.Context.getPointerType(VD->getType());
2878 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002879 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2880 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002881 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002882 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2883 Attr.getParameterName() << ParamTy << Ty;
2884 return;
2885 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002886
Michael Han99315932013-01-24 16:46:58 +00002887 D->addAttr(::new (S.Context)
2888 CleanupAttr(Attr.getRange(), S.Context, FD,
2889 Attr.getAttributeSpellingListIndex()));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002890 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewyckya096b142013-02-12 08:08:54 +00002891 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssond277d792009-01-31 01:16:18 +00002892}
2893
Mike Stumpd3bb5572009-07-24 19:02:52 +00002894/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002895/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002896static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002897 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002898 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002899
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002900 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002901 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002902 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002903 return;
2904 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002905
2906 // In C++ the implicit 'this' function parameter also counts, and they are
2907 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002908 bool HasImplicitThisParam = isInstanceMethod(D);
2909 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002910 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002911
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002912 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002913 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002914 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002915 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2916 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002917 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2918 << "format" << 2 << IdxExpr->getSourceRange();
2919 return;
2920 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002921
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002922 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2923 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2924 << "format" << 2 << IdxExpr->getSourceRange();
2925 return;
2926 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002927
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002928 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002929
Chandler Carruth743682b2010-11-16 08:35:43 +00002930 if (HasImplicitThisParam) {
2931 if (ArgIdx == 0) {
2932 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2933 << "format_arg" << IdxExpr->getSourceRange();
2934 return;
2935 }
2936 ArgIdx--;
2937 }
2938
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002939 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002940 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002941
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002942 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2943 if (not_nsstring_type &&
2944 !isCFStringType(Ty, S.Context) &&
2945 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002946 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002947 // FIXME: Should highlight the actual expression that has the wrong type.
2948 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002949 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002950 << IdxExpr->getSourceRange();
2951 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002952 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002953 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002954 if (!isNSStringType(Ty, S.Context) &&
2955 !isCFStringType(Ty, S.Context) &&
2956 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002957 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002958 // FIXME: Should highlight the actual expression that has the wrong type.
2959 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002960 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002961 << IdxExpr->getSourceRange();
2962 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002963 }
2964
Michael Han99315932013-01-24 16:46:58 +00002965 D->addAttr(::new (S.Context)
2966 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
2967 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002968}
2969
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002970enum FormatAttrKind {
2971 CFStringFormat,
2972 NSStringFormat,
2973 StrftimeFormat,
2974 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002975 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002976 InvalidFormat
2977};
2978
2979/// getFormatAttrKind - Map from format attribute names to supported format
2980/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002981static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002982 return llvm::StringSwitch<FormatAttrKind>(Format)
2983 // Check for formats that get handled specially.
2984 .Case("NSString", NSStringFormat)
2985 .Case("CFString", CFStringFormat)
2986 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002987
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002988 // Otherwise, check for supported formats.
2989 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2990 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2991 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002992
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002993 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2994 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002995}
2996
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002997/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002998/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002999static void handleInitPriorityAttr(Sema &S, Decl *D,
3000 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003001 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003002 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3003 return;
3004 }
3005
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003006 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003007 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3008 Attr.setInvalid();
3009 return;
3010 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003011 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003012 if (S.Context.getAsArrayType(T))
3013 T = S.Context.getBaseElementType(T);
3014 if (!T->getAs<RecordType>()) {
3015 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3016 Attr.setInvalid();
3017 return;
3018 }
3019
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003020 if (Attr.getNumArgs() != 1) {
3021 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3022 Attr.setInvalid();
3023 return;
3024 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003025 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003026
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003027 llvm::APSInt priority(32);
3028 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3029 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3030 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3031 << "init_priority" << priorityExpr->getSourceRange();
3032 Attr.setInvalid();
3033 return;
3034 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00003035 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003036 if (prioritynum < 101 || prioritynum > 65535) {
3037 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3038 << priorityExpr->getSourceRange();
3039 Attr.setInvalid();
3040 return;
3041 }
Michael Han99315932013-01-24 16:46:58 +00003042 D->addAttr(::new (S.Context)
3043 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3044 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003045}
3046
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003047FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han99315932013-01-24 16:46:58 +00003048 int FormatIdx, int FirstArg,
3049 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003050 // Check whether we already have an equivalent format attribute.
3051 for (specific_attr_iterator<FormatAttr>
3052 i = D->specific_attr_begin<FormatAttr>(),
3053 e = D->specific_attr_end<FormatAttr>();
3054 i != e ; ++i) {
3055 FormatAttr *f = *i;
3056 if (f->getType() == Format &&
3057 f->getFormatIdx() == FormatIdx &&
3058 f->getFirstArg() == FirstArg) {
3059 // If we don't have a valid location for this attribute, adopt the
3060 // location.
3061 if (f->getLocation().isInvalid())
3062 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003063 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003064 }
3065 }
3066
Michael Han99315932013-01-24 16:46:58 +00003067 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3068 AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003069}
3070
Mike Stumpd3bb5572009-07-24 19:02:52 +00003071/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003072/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003073static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003074
Chris Lattner4a927cb2008-06-28 23:36:30 +00003075 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003076 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003077 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003078 return;
3079 }
3080
Chris Lattner4a927cb2008-06-28 23:36:30 +00003081 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003082 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003083 return;
3084 }
3085
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003086 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003087 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003088 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003089 return;
3090 }
3091
Chandler Carruth743682b2010-11-16 08:35:43 +00003092 // In C++ the implicit 'this' function parameter also counts, and they are
3093 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003094 bool HasImplicitThisParam = isInstanceMethod(D);
3095 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003096 unsigned FirstIdx = 1;
3097
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003098 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003099
3100 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003101 if (Format.startswith("__") && Format.endswith("__"))
3102 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003103
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003104 // Check for supported formats.
3105 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003106
3107 if (Kind == IgnoredFormat)
3108 return;
3109
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003110 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003111 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003112 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003113 return;
3114 }
3115
3116 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003117 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003118 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003119 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3120 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003121 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003122 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003123 return;
3124 }
3125
3126 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003127 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003128 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003129 return;
3130 }
3131
3132 // FIXME: Do we need to bounds check?
3133 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003134
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003135 if (HasImplicitThisParam) {
3136 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003137 S.Diag(Attr.getLoc(),
3138 diag::err_format_attribute_implicit_this_format_string)
3139 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003140 return;
3141 }
3142 ArgIdx--;
3143 }
Mike Stump11289f42009-09-09 15:08:12 +00003144
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003145 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003146 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003147
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003148 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003149 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003150 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3151 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003152 return;
3153 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003154 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003155 // FIXME: do we need to check if the type is NSString*? What are the
3156 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003157 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003158 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003159 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3160 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003161 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003162 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003163 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003164 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003165 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003166 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3167 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003168 return;
3169 }
3170
3171 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003172 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003173 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003174 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3175 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003176 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003177 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003178 return;
3179 }
3180
3181 // check if the function is variadic if the 3rd argument non-zero
3182 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003183 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003184 ++NumArgs; // +1 for ...
3185 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003186 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003187 return;
3188 }
3189 }
3190
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003191 // strftime requires FirstArg to be 0 because it doesn't read from any
3192 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003193 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003194 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003195 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3196 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003197 return;
3198 }
3199 // if 0 it disables parameter checking (to use with e.g. va_list)
3200 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003202 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003203 return;
3204 }
3205
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003206 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3207 Idx.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003208 FirstArg.getZExtValue(),
3209 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003210 if (NewAttr)
3211 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003212}
3213
Chandler Carruthedc2c642011-07-02 00:01:44 +00003214static void handleTransparentUnionAttr(Sema &S, Decl *D,
3215 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003216 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003217 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003218 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003219
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003220
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003221 // Try to find the underlying union declaration.
3222 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003223 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003224 if (TD && TD->getUnderlyingType()->isUnionType())
3225 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3226 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003227 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003228
3229 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003230 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003231 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003232 return;
3233 }
3234
John McCallf937c022011-10-07 06:10:15 +00003235 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003236 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003237 diag::warn_transparent_union_attribute_not_definition);
3238 return;
3239 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003240
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003241 RecordDecl::field_iterator Field = RD->field_begin(),
3242 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003243 if (Field == FieldEnd) {
3244 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3245 return;
3246 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003247
David Blaikie40ed2972012-06-06 20:45:41 +00003248 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003249 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003250 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003251 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003252 diag::warn_transparent_union_attribute_floating)
3253 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003254 return;
3255 }
3256
3257 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3258 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3259 for (; Field != FieldEnd; ++Field) {
3260 QualType FieldType = Field->getType();
3261 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3262 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3263 // Warn if we drop the attribute.
3264 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003265 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003266 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003267 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003268 diag::warn_transparent_union_attribute_field_size_align)
3269 << isSize << Field->getDeclName() << FieldBits;
3270 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003271 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003272 diag::note_transparent_union_first_field_size_align)
3273 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003274 return;
3275 }
3276 }
3277
Michael Han99315932013-01-24 16:46:58 +00003278 RD->addAttr(::new (S.Context)
3279 TransparentUnionAttr(Attr.getRange(), S.Context,
3280 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003281}
3282
Chandler Carruthedc2c642011-07-02 00:01:44 +00003283static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003284 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003285 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003286 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003287
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003288 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003289 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003290
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003291 // Make sure that there is a string literal as the annotation's single
3292 // argument.
3293 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003294 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003295 return;
3296 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003297
3298 // Don't duplicate annotations that are already set.
3299 for (specific_attr_iterator<AnnotateAttr>
3300 i = D->specific_attr_begin<AnnotateAttr>(),
3301 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3302 if ((*i)->getAnnotation() == SE->getString())
3303 return;
3304 }
Michael Han99315932013-01-24 16:46:58 +00003305
3306 D->addAttr(::new (S.Context)
3307 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3308 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003309}
3310
Chandler Carruthedc2c642011-07-02 00:01:44 +00003311static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003312 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003313 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003314 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003315 return;
3316 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003317
Richard Smith848e1f12013-02-01 08:12:08 +00003318 // FIXME: The C++11 version of this attribute should error out when it is
3319 // used to specify a weaker alignment, rather than being silently
3320 // ignored. This constraint cannot be applied until we have seen
3321 // all the attributes which apply to the variable.
3322
3323 if (Attr.getNumArgs() == 0) {
3324 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3325 true, 0, Attr.getAttributeSpellingListIndex()));
3326 return;
3327 }
3328
3329 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3330 Attr.getAttributeSpellingListIndex());
3331}
3332
3333void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3334 unsigned SpellingListIndex) {
3335 // FIXME: Handle pack-expansions here.
3336 if (DiagnoseUnexpandedParameterPack(E))
3337 return;
3338
3339 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3340 SourceLocation AttrLoc = AttrRange.getBegin();
3341
Richard Smith1dba27c2013-01-29 09:02:09 +00003342 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003343 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003344 // C++11 [dcl.align]p1:
3345 // An alignment-specifier may be applied to a variable or to a class
3346 // data member, but it shall not be applied to a bit-field, a function
3347 // parameter, the formal parameter of a catch clause, or a variable
3348 // declared with the register storage class specifier. An
3349 // alignment-specifier may also be applied to the declaration of a class
3350 // or enumeration type.
3351 // C11 6.7.5/2:
3352 // An alignment attribute shall not be specified in a declaration of
3353 // a typedef, or a bit-field, or a function, or a parameter, or an
3354 // object declared with the register storage-class specifier.
3355 int DiagKind = -1;
3356 if (isa<ParmVarDecl>(D)) {
3357 DiagKind = 0;
3358 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3359 if (VD->getStorageClass() == SC_Register)
3360 DiagKind = 1;
3361 if (VD->isExceptionVariable())
3362 DiagKind = 2;
3363 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3364 if (FD->isBitField())
3365 DiagKind = 3;
3366 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003367 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3368 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003369 << (TmpAttr.isC11() ? ExpectedVariableOrField
3370 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003371 return;
3372 }
3373 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003374 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003375 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003376 return;
3377 }
3378 }
3379
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003380 if (E->isTypeDependent() || E->isValueDependent()) {
3381 // Save dependent expressions in the AST to be instantiated.
Richard Smith848e1f12013-02-01 08:12:08 +00003382 D->addAttr(::new (Context) AlignedAttr(TmpAttr));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003383 return;
3384 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003385
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003386 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003387 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003388 ExprResult ICE
3389 = VerifyIntegerConstantExpression(E, &Alignment,
3390 diag::err_aligned_attribute_argument_not_int,
3391 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003392 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003393 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003394
3395 // C++11 [dcl.align]p2:
3396 // -- if the constant expression evaluates to zero, the alignment
3397 // specifier shall have no effect
3398 // C11 6.7.5p6:
3399 // An alignment specification of zero has no effect.
3400 if (!(TmpAttr.isAlignas() && !Alignment) &&
3401 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003402 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3403 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003404 return;
3405 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003406
Richard Smith848e1f12013-02-01 08:12:08 +00003407 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003408 // We've already verified it's a power of 2, now let's make sure it's
3409 // 8192 or less.
3410 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003411 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003412 << E->getSourceRange();
3413 return;
3414 }
3415 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003416
Richard Smith848e1f12013-02-01 08:12:08 +00003417 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true,
3418 ICE.take(), SpellingListIndex));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003419}
3420
Michael Hanaf02bbe2013-02-01 01:19:17 +00003421void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3422 unsigned SpellingListIndex) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003423 // FIXME: Cache the number on the Attr object if non-dependent?
3424 // FIXME: Perform checking of type validity
Michael Hanaf02bbe2013-02-01 01:19:17 +00003425 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3426 SpellingListIndex));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003427 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003428}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003429
Richard Smith848e1f12013-02-01 08:12:08 +00003430void Sema::CheckAlignasUnderalignment(Decl *D) {
3431 assert(D->hasAttrs() && "no attributes on decl");
3432
3433 QualType Ty;
3434 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3435 Ty = VD->getType();
3436 else
3437 Ty = Context.getTagDeclType(cast<TagDecl>(D));
3438 if (Ty->isDependentType())
3439 return;
3440
3441 // C++11 [dcl.align]p5, C11 6.7.5/4:
3442 // The combined effect of all alignment attributes in a declaration shall
3443 // not specify an alignment that is less strict than the alignment that
3444 // would otherwise be required for the entity being declared.
3445 AlignedAttr *AlignasAttr = 0;
3446 unsigned Align = 0;
3447 for (specific_attr_iterator<AlignedAttr>
3448 I = D->specific_attr_begin<AlignedAttr>(),
3449 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3450 if (I->isAlignmentDependent())
3451 return;
3452 if (I->isAlignas())
3453 AlignasAttr = *I;
3454 Align = std::max(Align, I->getAlignment(Context));
3455 }
3456
3457 if (AlignasAttr && Align) {
3458 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3459 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3460 if (NaturalAlign > RequestedAlign)
3461 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3462 << Ty << (unsigned)NaturalAlign.getQuantity();
3463 }
3464}
3465
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003466/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003467/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003468///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003469/// Despite what would be logical, the mode attribute is a decl attribute, not a
3470/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3471/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003472static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003473 // This attribute isn't documented, but glibc uses it. It changes
3474 // the width of an int or unsigned int to the specified size.
3475
3476 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003477 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003478 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003479
Chris Lattneracbc2d22008-06-27 22:18:37 +00003480
3481 IdentifierInfo *Name = Attr.getParameterName();
3482 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003483 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003484 return;
3485 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003486
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003487 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003488
3489 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003490 if (Str.startswith("__") && Str.endswith("__"))
3491 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003492
3493 unsigned DestWidth = 0;
3494 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003495 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003496 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003497 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003498 switch (Str[0]) {
3499 case 'Q': DestWidth = 8; break;
3500 case 'H': DestWidth = 16; break;
3501 case 'S': DestWidth = 32; break;
3502 case 'D': DestWidth = 64; break;
3503 case 'X': DestWidth = 96; break;
3504 case 'T': DestWidth = 128; break;
3505 }
3506 if (Str[1] == 'F') {
3507 IntegerMode = false;
3508 } else if (Str[1] == 'C') {
3509 IntegerMode = false;
3510 ComplexMode = true;
3511 } else if (Str[1] != 'I') {
3512 DestWidth = 0;
3513 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003514 break;
3515 case 4:
3516 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3517 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003518 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003519 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003520 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003521 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003522 break;
3523 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003524 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003525 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003526 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003527 case 11:
3528 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003529 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003530 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003531 }
3532
3533 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003534 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003535 OldTy = TD->getUnderlyingType();
3536 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3537 OldTy = VD->getType();
3538 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003539 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003540 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003541 return;
3542 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003543
John McCall9dd450b2009-09-21 23:43:11 +00003544 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003545 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3546 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003547 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003548 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3549 } else if (ComplexMode) {
3550 if (!OldTy->isComplexType())
3551 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3552 } else {
3553 if (!OldTy->isFloatingType())
3554 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3555 }
3556
Mike Stump87c57ac2009-05-16 07:39:55 +00003557 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3558 // and friends, at least with glibc.
3559 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3560 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003561 // FIXME: Make sure floating-point mappings are accurate
3562 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003563 QualType NewTy;
3564 switch (DestWidth) {
3565 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003566 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003567 return;
3568 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003569 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003570 return;
3571 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003572 if (!IntegerMode) {
3573 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3574 return;
3575 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003576 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003577 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003578 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003579 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003580 break;
3581 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003582 if (!IntegerMode) {
3583 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3584 return;
3585 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003586 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003587 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003588 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003589 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003590 break;
3591 case 32:
3592 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003593 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003594 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003595 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003596 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003597 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003598 break;
3599 case 64:
3600 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003601 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003602 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003603 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003604 NewTy = S.Context.LongTy;
3605 else
3606 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003607 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003608 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003609 NewTy = S.Context.UnsignedLongTy;
3610 else
3611 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003612 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003613 case 96:
3614 NewTy = S.Context.LongDoubleTy;
3615 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003616 case 128:
3617 if (!IntegerMode) {
3618 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3619 return;
3620 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003621 if (OldTy->isSignedIntegerType())
3622 NewTy = S.Context.Int128Ty;
3623 else
3624 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003625 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003626 }
3627
Eli Friedman4735374e2009-03-03 06:41:03 +00003628 if (ComplexMode) {
3629 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003630 }
3631
3632 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003633 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003634 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003635 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003636 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003637 cast<ValueDecl>(D)->setType(NewTy);
3638}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003639
Chandler Carruthedc2c642011-07-02 00:01:44 +00003640static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003641 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003642 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003643 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003644
Nick Lewycky08597072012-07-24 01:40:49 +00003645 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3646 if (!VD->hasGlobalStorage())
3647 S.Diag(Attr.getLoc(),
3648 diag::warn_attribute_requires_functions_or_static_globals)
3649 << Attr.getName();
3650 } else if (!isFunctionOrMethod(D)) {
3651 S.Diag(Attr.getLoc(),
3652 diag::warn_attribute_requires_functions_or_static_globals)
3653 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003654 return;
3655 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003656
Michael Han99315932013-01-24 16:46:58 +00003657 D->addAttr(::new (S.Context)
3658 NoDebugAttr(Attr.getRange(), S.Context,
3659 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003660}
3661
Chandler Carruthedc2c642011-07-02 00:01:44 +00003662static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003663 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003664 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003665 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003666
Mike Stumpd3bb5572009-07-24 19:02:52 +00003667
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003668 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003670 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003671 return;
3672 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003673
Michael Han99315932013-01-24 16:46:58 +00003674 D->addAttr(::new (S.Context)
3675 NoInlineAttr(Attr.getRange(), S.Context,
3676 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003677}
3678
Chandler Carruthedc2c642011-07-02 00:01:44 +00003679static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3680 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003681 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003682 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003683 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003684
Chris Lattner3c77a352010-06-22 00:03:40 +00003685
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003686 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003687 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003688 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003689 return;
3690 }
3691
Michael Han99315932013-01-24 16:46:58 +00003692 D->addAttr(::new (S.Context)
3693 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3694 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003695}
3696
Chandler Carruthedc2c642011-07-02 00:01:44 +00003697static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003698 if (S.LangOpts.CUDA) {
3699 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003700 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003701 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3702 return;
3703 }
3704
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003705 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003706 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003707 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003708 return;
3709 }
3710
Michael Han99315932013-01-24 16:46:58 +00003711 D->addAttr(::new (S.Context)
3712 CUDAConstantAttr(Attr.getRange(), S.Context,
3713 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003714 } else {
3715 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3716 }
3717}
3718
Chandler Carruthedc2c642011-07-02 00:01:44 +00003719static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003720 if (S.LangOpts.CUDA) {
3721 // check the attribute arguments.
3722 if (Attr.getNumArgs() != 0) {
3723 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3724 return;
3725 }
3726
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003727 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003728 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003729 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003730 return;
3731 }
3732
Michael Han99315932013-01-24 16:46:58 +00003733 D->addAttr(::new (S.Context)
3734 CUDADeviceAttr(Attr.getRange(), S.Context,
3735 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003736 } else {
3737 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3738 }
3739}
3740
Chandler Carruthedc2c642011-07-02 00:01:44 +00003741static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003742 if (S.LangOpts.CUDA) {
3743 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003744 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003745 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003746
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003747 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003748 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003749 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003750 return;
3751 }
3752
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003753 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003754 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003755 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003756 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003757 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3758 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003759 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003760 "void");
3761 } else {
3762 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3763 << FD->getType();
3764 }
3765 return;
3766 }
3767
Michael Han99315932013-01-24 16:46:58 +00003768 D->addAttr(::new (S.Context)
3769 CUDAGlobalAttr(Attr.getRange(), S.Context,
3770 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003771 } else {
3772 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3773 }
3774}
3775
Chandler Carruthedc2c642011-07-02 00:01:44 +00003776static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003777 if (S.LangOpts.CUDA) {
3778 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003779 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003780 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003781
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003782
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003783 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003784 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003785 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003786 return;
3787 }
3788
Michael Han99315932013-01-24 16:46:58 +00003789 D->addAttr(::new (S.Context)
3790 CUDAHostAttr(Attr.getRange(), S.Context,
3791 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003792 } else {
3793 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3794 }
3795}
3796
Chandler Carruthedc2c642011-07-02 00:01:44 +00003797static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003798 if (S.LangOpts.CUDA) {
3799 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003800 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003801 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003802
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003803 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003804 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003805 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003806 return;
3807 }
3808
Michael Han99315932013-01-24 16:46:58 +00003809 D->addAttr(::new (S.Context)
3810 CUDASharedAttr(Attr.getRange(), S.Context,
3811 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003812 } else {
3813 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3814 }
3815}
3816
Chandler Carruthedc2c642011-07-02 00:01:44 +00003817static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003818 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003819 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003820 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003821
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003822 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003823 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003824 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003825 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003826 return;
3827 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003828
Douglas Gregor35b57532009-10-27 21:01:01 +00003829 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003830 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003831 return;
3832 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003833
Michael Han99315932013-01-24 16:46:58 +00003834 D->addAttr(::new (S.Context)
3835 GNUInlineAttr(Attr.getRange(), S.Context,
3836 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003837}
3838
Chandler Carruthedc2c642011-07-02 00:01:44 +00003839static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003840 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003841
Aaron Ballman02df2e02012-12-09 17:45:41 +00003842 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003843 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003844 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3845 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003846 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003847 return;
3848
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003849 if (!isa<ObjCMethodDecl>(D)) {
3850 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3851 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003852 return;
3853 }
3854
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003855 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003856 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003857 D->addAttr(::new (S.Context)
3858 FastCallAttr(Attr.getRange(), S.Context,
3859 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003860 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003861 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003862 D->addAttr(::new (S.Context)
3863 StdCallAttr(Attr.getRange(), S.Context,
3864 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003865 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003866 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003867 D->addAttr(::new (S.Context)
3868 ThisCallAttr(Attr.getRange(), S.Context,
3869 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003870 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003871 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003872 D->addAttr(::new (S.Context)
3873 CDeclAttr(Attr.getRange(), S.Context,
3874 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003875 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003876 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003877 D->addAttr(::new (S.Context)
3878 PascalAttr(Attr.getRange(), S.Context,
3879 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003880 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003881 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003882 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003883 switch (CC) {
3884 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003885 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003886 break;
3887 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003888 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003889 break;
3890 default:
3891 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003892 }
3893
Michael Han99315932013-01-24 16:46:58 +00003894 D->addAttr(::new (S.Context)
3895 PcsAttr(Attr.getRange(), S.Context, PCS,
3896 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003897 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003898 }
Derek Schuffa2020962012-10-16 22:30:41 +00003899 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003900 D->addAttr(::new (S.Context)
3901 PnaclCallAttr(Attr.getRange(), S.Context,
3902 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003903 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003904 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003905 D->addAttr(::new (S.Context)
3906 IntelOclBiccAttr(Attr.getRange(), S.Context,
3907 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003908 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003909
Abramo Bagnara50099372010-04-30 13:10:51 +00003910 default:
3911 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003912 }
3913}
3914
Chandler Carruthedc2c642011-07-02 00:01:44 +00003915static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003916 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003917 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003918}
3919
Aaron Ballman02df2e02012-12-09 17:45:41 +00003920bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3921 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003922 if (attr.isInvalid())
3923 return true;
3924
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003925 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3926 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3927 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00003928 attr.setInvalid();
3929 return true;
3930 }
3931
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003932 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3933 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003934 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003935 case AttributeList::AT_CDecl: CC = CC_C; break;
3936 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3937 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3938 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3939 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3940 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003941 Expr *Arg = attr.getArg(0);
3942 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003943 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003944 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3945 << "pcs" << 1;
3946 attr.setInvalid();
3947 return true;
3948 }
3949
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003950 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003951 if (StrRef == "aapcs") {
3952 CC = CC_AAPCS;
3953 break;
3954 } else if (StrRef == "aapcs-vfp") {
3955 CC = CC_AAPCS_VFP;
3956 break;
3957 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003958
3959 attr.setInvalid();
3960 Diag(attr.getLoc(), diag::err_invalid_pcs);
3961 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003962 }
Derek Schuffa2020962012-10-16 22:30:41 +00003963 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003964 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003965 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003966 }
3967
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003968 const TargetInfo &TI = Context.getTargetInfo();
3969 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3970 if (A == TargetInfo::CCCR_Warning) {
3971 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003972
3973 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3974 if (FD)
3975 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3976 TargetInfo::CCMT_NonMember;
3977 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003978 }
3979
John McCall3882ace2011-01-05 12:14:39 +00003980 return false;
3981}
3982
Chandler Carruthedc2c642011-07-02 00:01:44 +00003983static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003984 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003985
3986 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003987 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003988 return;
3989
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003990 if (!isa<ObjCMethodDecl>(D)) {
3991 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3992 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003993 return;
3994 }
Eli Friedman7044b762009-03-27 21:06:47 +00003995
Michael Han99315932013-01-24 16:46:58 +00003996 D->addAttr(::new (S.Context)
3997 RegparmAttr(Attr.getRange(), S.Context, numParams,
3998 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003999}
4000
4001/// Checks a regparm attribute, returning true if it is ill-formed and
4002/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004003bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4004 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004005 return true;
4006
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004007 if (Attr.getNumArgs() != 1) {
4008 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4009 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004010 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004011 }
Eli Friedman7044b762009-03-27 21:06:47 +00004012
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004013 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00004014 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00004015 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00004016 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004017 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00004018 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004019 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004020 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004021 }
4022
Douglas Gregore8bbc122011-09-02 00:18:52 +00004023 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004024 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004025 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004026 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004027 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004028 }
4029
John McCall3882ace2011-01-05 12:14:39 +00004030 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00004031 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004032 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004033 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004034 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004035 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004036 }
4037
John McCall3882ace2011-01-05 12:14:39 +00004038 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004039}
4040
Chandler Carruthedc2c642011-07-02 00:01:44 +00004041static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004042 if (S.LangOpts.CUDA) {
4043 // check the attribute arguments.
4044 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004045 // FIXME: 0 is not okay.
4046 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004047 return;
4048 }
4049
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004050 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004051 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004052 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004053 return;
4054 }
4055
4056 Expr *MaxThreadsExpr = Attr.getArg(0);
4057 llvm::APSInt MaxThreads(32);
4058 if (MaxThreadsExpr->isTypeDependent() ||
4059 MaxThreadsExpr->isValueDependent() ||
4060 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4061 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4062 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4063 return;
4064 }
4065
4066 llvm::APSInt MinBlocks(32);
4067 if (Attr.getNumArgs() > 1) {
4068 Expr *MinBlocksExpr = Attr.getArg(1);
4069 if (MinBlocksExpr->isTypeDependent() ||
4070 MinBlocksExpr->isValueDependent() ||
4071 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4072 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4073 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4074 return;
4075 }
4076 }
4077
Michael Han99315932013-01-24 16:46:58 +00004078 D->addAttr(::new (S.Context)
4079 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4080 MaxThreads.getZExtValue(),
4081 MinBlocks.getZExtValue(),
4082 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004083 } else {
4084 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4085 }
4086}
4087
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004088static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4089 const AttributeList &Attr) {
4090 StringRef AttrName = Attr.getName()->getName();
4091 if (!Attr.getParameterName()) {
4092 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4093 << Attr.getName() << /* arg num = */ 1;
4094 return;
4095 }
4096
4097 if (Attr.getNumArgs() != 2) {
4098 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4099 << /* required args = */ 3;
4100 return;
4101 }
4102
4103 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4104
4105 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4106 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4107 << Attr.getName() << ExpectedFunctionOrMethod;
4108 return;
4109 }
4110
4111 uint64_t ArgumentIdx;
4112 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4113 Attr.getLoc(), 2,
4114 Attr.getArg(0), ArgumentIdx))
4115 return;
4116
4117 uint64_t TypeTagIdx;
4118 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4119 Attr.getLoc(), 3,
4120 Attr.getArg(1), TypeTagIdx))
4121 return;
4122
4123 bool IsPointer = (AttrName == "pointer_with_type_tag");
4124 if (IsPointer) {
4125 // Ensure that buffer has a pointer type.
4126 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4127 if (!BufferTy->isPointerType()) {
4128 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
4129 << AttrName;
4130 }
4131 }
4132
Michael Han99315932013-01-24 16:46:58 +00004133 D->addAttr(::new (S.Context)
4134 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4135 ArgumentIdx, TypeTagIdx, IsPointer,
4136 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004137}
4138
4139static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4140 const AttributeList &Attr) {
4141 IdentifierInfo *PointerKind = Attr.getParameterName();
4142 if (!PointerKind) {
4143 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4144 << "type_tag_for_datatype" << 1;
4145 return;
4146 }
4147
4148 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4149
Michael Han99315932013-01-24 16:46:58 +00004150 D->addAttr(::new (S.Context)
4151 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4152 MatchingCType,
4153 Attr.getLayoutCompatible(),
4154 Attr.getMustBeNull(),
4155 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004156}
4157
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004158//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004159// Checker-specific attribute handlers.
4160//===----------------------------------------------------------------------===//
4161
John McCalled433932011-01-25 03:31:58 +00004162static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004163 return type->isDependentType() ||
4164 type->isObjCObjectPointerType() ||
4165 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004166}
4167static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004168 return type->isDependentType() ||
4169 type->isPointerType() ||
4170 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004171}
4172
Chandler Carruthedc2c642011-07-02 00:01:44 +00004173static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004174 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004175 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004176 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004177 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004178 return;
4179 }
4180
4181 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004182 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004183 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4184 cf = false;
4185 } else {
4186 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4187 cf = true;
4188 }
4189
4190 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004191 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004192 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004193 return;
4194 }
4195
4196 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004197 param->addAttr(::new (S.Context)
4198 CFConsumedAttr(Attr.getRange(), S.Context,
4199 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004200 else
Michael Han99315932013-01-24 16:46:58 +00004201 param->addAttr(::new (S.Context)
4202 NSConsumedAttr(Attr.getRange(), S.Context,
4203 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004204}
4205
Chandler Carruthedc2c642011-07-02 00:01:44 +00004206static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4207 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004208 if (!isa<ObjCMethodDecl>(D)) {
4209 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004210 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004211 return;
4212 }
4213
Michael Han99315932013-01-24 16:46:58 +00004214 D->addAttr(::new (S.Context)
4215 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4216 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004217}
4218
Chandler Carruthedc2c642011-07-02 00:01:44 +00004219static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4220 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004221
John McCalled433932011-01-25 03:31:58 +00004222 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004223
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004224 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004225 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004226 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004227 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004228 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004229 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4230 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004231 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004232 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004233 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004234 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004235 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004236 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004237 return;
4238 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004239
John McCalled433932011-01-25 03:31:58 +00004240 bool typeOK;
4241 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004242 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004243 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004244 case AttributeList::AT_NSReturnsAutoreleased:
4245 case AttributeList::AT_NSReturnsRetained:
4246 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004247 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4248 cf = false;
4249 break;
4250
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004251 case AttributeList::AT_CFReturnsRetained:
4252 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004253 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4254 cf = true;
4255 break;
4256 }
4257
4258 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004259 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004260 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004261 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004262 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004263
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004264 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004265 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004266 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004267 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004268 D->addAttr(::new (S.Context)
4269 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4270 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004271 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004272 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004273 D->addAttr(::new (S.Context)
4274 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4275 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004276 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004277 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004278 D->addAttr(::new (S.Context)
4279 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4280 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004281 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004282 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004283 D->addAttr(::new (S.Context)
4284 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4285 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004286 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004287 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004288 D->addAttr(::new (S.Context)
4289 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4290 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004291 return;
4292 };
4293}
4294
John McCallcf166702011-07-22 08:53:00 +00004295static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4296 const AttributeList &attr) {
4297 SourceLocation loc = attr.getLoc();
4298
4299 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4300
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004301 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00004302 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004303 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00004304 return;
4305 }
4306
4307 // Check that the method returns a normal pointer.
4308 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004309
4310 if (!resultType->isReferenceType() &&
4311 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00004312 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4313 << SourceRange(loc)
4314 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4315
4316 // Drop the attribute.
4317 return;
4318 }
4319
Michael Han99315932013-01-24 16:46:58 +00004320 method->addAttr(::new (S.Context)
4321 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4322 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004323}
4324
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004325static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4326 const AttributeList &attr) {
4327 SourceLocation loc = attr.getLoc();
4328 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4329
4330 if (!method) {
4331 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4332 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4333 return;
4334 }
4335 DeclContext *DC = method->getDeclContext();
4336 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4337 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4338 << attr.getName() << 0;
4339 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4340 return;
4341 }
4342 if (method->getMethodFamily() == OMF_dealloc) {
4343 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4344 << attr.getName() << 1;
4345 return;
4346 }
4347
Michael Han99315932013-01-24 16:46:58 +00004348 method->addAttr(::new (S.Context)
4349 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4350 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004351}
4352
John McCall32f5fe12011-09-30 05:12:12 +00004353/// Handle cf_audited_transfer and cf_unknown_transfer.
4354static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4355 if (!isa<FunctionDecl>(D)) {
4356 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004357 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004358 return;
4359 }
4360
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004361 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004362
4363 // Check whether there's a conflicting attribute already present.
4364 Attr *Existing;
4365 if (IsAudited) {
4366 Existing = D->getAttr<CFUnknownTransferAttr>();
4367 } else {
4368 Existing = D->getAttr<CFAuditedTransferAttr>();
4369 }
4370 if (Existing) {
4371 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4372 << A.getName()
4373 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4374 << A.getRange() << Existing->getRange();
4375 return;
4376 }
4377
4378 // All clear; add the attribute.
4379 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004380 D->addAttr(::new (S.Context)
4381 CFAuditedTransferAttr(A.getRange(), S.Context,
4382 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004383 } else {
Michael Han99315932013-01-24 16:46:58 +00004384 D->addAttr(::new (S.Context)
4385 CFUnknownTransferAttr(A.getRange(), S.Context,
4386 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004387 }
4388}
4389
John McCallf1e8b342011-09-29 07:17:38 +00004390static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4391 const AttributeList &Attr) {
4392 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4393 if (!RD || RD->isUnion()) {
4394 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004395 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004396 }
4397
4398 IdentifierInfo *ParmName = Attr.getParameterName();
4399
4400 // In Objective-C, verify that the type names an Objective-C type.
4401 // We don't want to check this outside of ObjC because people sometimes
4402 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004403 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004404 // Check for an existing type with this name.
4405 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4406 Sema::LookupOrdinaryName);
4407 if (S.LookupName(R, Sc)) {
4408 NamedDecl *Target = R.getFoundDecl();
4409 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4410 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4411 S.Diag(Target->getLocStart(), diag::note_declared_at);
4412 }
4413 }
4414 }
4415
Michael Han99315932013-01-24 16:46:58 +00004416 D->addAttr(::new (S.Context)
4417 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4418 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004419}
4420
Chandler Carruthedc2c642011-07-02 00:01:44 +00004421static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4422 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004423 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004424
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004425 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004426 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004427}
4428
Chandler Carruthedc2c642011-07-02 00:01:44 +00004429static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4430 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004431 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004432 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004433 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004434 return;
4435 }
4436
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004437 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004438 QualType type = vd->getType();
4439
4440 if (!type->isDependentType() &&
4441 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004442 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004443 << type;
4444 return;
4445 }
4446
4447 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4448
4449 // If we have no lifetime yet, check the lifetime we're presumably
4450 // going to infer.
4451 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4452 lifetime = type->getObjCARCImplicitLifetime();
4453
4454 switch (lifetime) {
4455 case Qualifiers::OCL_None:
4456 assert(type->isDependentType() &&
4457 "didn't infer lifetime for non-dependent type?");
4458 break;
4459
4460 case Qualifiers::OCL_Weak: // meaningful
4461 case Qualifiers::OCL_Strong: // meaningful
4462 break;
4463
4464 case Qualifiers::OCL_ExplicitNone:
4465 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004466 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004467 << (lifetime == Qualifiers::OCL_Autoreleasing);
4468 break;
4469 }
4470
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004471 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004472 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4473 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004474}
4475
Francois Picheta83957a2010-12-19 06:50:37 +00004476//===----------------------------------------------------------------------===//
4477// Microsoft specific attribute handlers.
4478//===----------------------------------------------------------------------===//
4479
Chandler Carruthedc2c642011-07-02 00:01:44 +00004480static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00004481 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00004482 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004483 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00004484 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004485
Francois Picheta83957a2010-12-19 06:50:37 +00004486 Expr *Arg = Attr.getArg(0);
4487 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004488 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00004489 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4490 << "uuid" << 1;
4491 return;
4492 }
4493
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004494 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00004495
4496 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4497 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004498
Francois Pichet7da11662010-12-20 01:41:49 +00004499 // Validate GUID length.
4500 if (IsCurly && StrRef.size() != 38) {
4501 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4502 return;
4503 }
4504 if (!IsCurly && StrRef.size() != 36) {
4505 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4506 return;
4507 }
4508
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004509 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00004510 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004511 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00004512 if (IsCurly) // Skip the optional '{'
4513 ++I;
4514
4515 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00004516 if (i == 8 || i == 13 || i == 18 || i == 23) {
4517 if (*I != '-') {
4518 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4519 return;
4520 }
Jordan Rosea7d03842013-02-08 22:30:41 +00004521 } else if (!isHexDigit(*I)) {
Francois Pichet7da11662010-12-20 01:41:49 +00004522 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4523 return;
4524 }
4525 I++;
4526 }
Francois Picheta83957a2010-12-19 06:50:37 +00004527
Michael Han99315932013-01-24 16:46:58 +00004528 D->addAttr(::new (S.Context)
4529 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4530 Attr.getAttributeSpellingListIndex()));
Francois Pichet7da11662010-12-20 01:41:49 +00004531 } else
Francois Picheta83957a2010-12-19 06:50:37 +00004532 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00004533}
4534
John McCall8d32c052012-05-22 21:28:12 +00004535static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weberefa45b22012-11-07 21:31:36 +00004536 if (!S.LangOpts.MicrosoftExt) {
John McCall8d32c052012-05-22 21:28:12 +00004537 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weberefa45b22012-11-07 21:31:36 +00004538 return;
4539 }
4540
4541 AttributeList::Kind Kind = Attr.getKind();
4542 if (Kind == AttributeList::AT_SingleInheritance)
4543 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004544 ::new (S.Context)
4545 SingleInheritanceAttr(Attr.getRange(), S.Context,
4546 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004547 else if (Kind == AttributeList::AT_MultipleInheritance)
4548 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004549 ::new (S.Context)
4550 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4551 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004552 else if (Kind == AttributeList::AT_VirtualInheritance)
4553 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004554 ::new (S.Context)
4555 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4556 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004557}
4558
4559static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4560 if (S.LangOpts.MicrosoftExt) {
4561 AttributeList::Kind Kind = Attr.getKind();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004562 if (Kind == AttributeList::AT_Ptr32)
John McCall8d32c052012-05-22 21:28:12 +00004563 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004564 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context,
4565 Attr.getAttributeSpellingListIndex()));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004566 else if (Kind == AttributeList::AT_Ptr64)
John McCall8d32c052012-05-22 21:28:12 +00004567 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004568 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context,
4569 Attr.getAttributeSpellingListIndex()));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004570 else if (Kind == AttributeList::AT_Win64)
John McCall8d32c052012-05-22 21:28:12 +00004571 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004572 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4573 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004574 } else
4575 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4576}
4577
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004578static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4579 if (S.LangOpts.MicrosoftExt)
Michael Han99315932013-01-24 16:46:58 +00004580 D->addAttr(::new (S.Context)
4581 ForceInlineAttr(Attr.getRange(), S.Context,
4582 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004583 else
4584 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4585}
4586
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004587//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004588// Top Level Sema Entry Points
4589//===----------------------------------------------------------------------===//
4590
Chandler Carruthedc2c642011-07-02 00:01:44 +00004591static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4592 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004593 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004594 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4595 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4596 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004597 default:
4598 break;
4599 }
4600}
Abramo Bagnara50099372010-04-30 13:10:51 +00004601
Chandler Carruthedc2c642011-07-02 00:01:44 +00004602static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4603 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004604 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004605 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4606 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4607 case AttributeList::AT_IBOutletCollection:
4608 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004609 case AttributeList::AT_AddressSpace:
4610 case AttributeList::AT_OpenCLImageAccess:
4611 case AttributeList::AT_ObjCGC:
4612 case AttributeList::AT_VectorSize:
4613 case AttributeList::AT_NeonVectorType:
4614 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004615 // Ignore these, these are type attributes, handled by
4616 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004617 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004618 case AttributeList::AT_CUDADevice:
4619 case AttributeList::AT_CUDAHost:
4620 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004621 // Ignore, this is a non-inheritable attribute, handled
4622 // by ProcessNonInheritableDeclAttr.
4623 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004624 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4625 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4626 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4627 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004628 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004629 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004630 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004631 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004632 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4633 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4634 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004635 handleDependencyAttr(S, scope, D, Attr);
4636 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004637 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4638 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4639 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004640 case AttributeList::AT_CXX11NoReturn:
4641 handleCXX11NoReturnAttr(S, D, Attr);
4642 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004643 case AttributeList::AT_Deprecated:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004644 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4645 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004646 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4647 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004648 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004649 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004650 case AttributeList::AT_MinSize:
4651 handleMinSizeAttr(S, D, Attr);
4652 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004653 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4654 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4655 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4656 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4657 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004658 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004659 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004660 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4661 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4662 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4663 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4664 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004665 case AttributeList::AT_ownership_returns:
4666 case AttributeList::AT_ownership_takes:
4667 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004668 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004669 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4670 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4671 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4672 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4673 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4674 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4675 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004676
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004677 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004678 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004679 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004680 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004681
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004682 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004683 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4684
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004685 case AttributeList::AT_ObjCRequiresSuper:
4686 handleObjCRequiresSuperAttr(S, D, Attr); break;
4687
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004688 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004689 handleNSBridgedAttr(S, scope, D, Attr); break;
4690
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004691 case AttributeList::AT_CFAuditedTransfer:
4692 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004693 handleCFTransferAttr(S, D, Attr); break;
4694
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004695 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004696 case AttributeList::AT_CFConsumed:
4697 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4698 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004699 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004700
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004701 case AttributeList::AT_NSReturnsAutoreleased:
4702 case AttributeList::AT_NSReturnsNotRetained:
4703 case AttributeList::AT_CFReturnsNotRetained:
4704 case AttributeList::AT_NSReturnsRetained:
4705 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004706 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004707
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004708 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004709 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004710 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004711
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004712 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004713 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004714
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004715 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4716 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4717 case AttributeList::AT_Unavailable:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004718 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4719 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004720 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004721 handleArcWeakrefUnavailableAttr (S, D, Attr);
4722 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004723 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004724 handleObjCRootClassAttr(S, D, Attr);
4725 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004726 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004727 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004728 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004729 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4730 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004731 handleReturnsTwiceAttr(S, D, Attr);
4732 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004733 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004734 case AttributeList::AT_Visibility:
4735 handleVisibilityAttr(S, D, Attr, false);
4736 break;
4737 case AttributeList::AT_TypeVisibility:
4738 handleVisibilityAttr(S, D, Attr, true);
4739 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004740 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004741 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004742 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4743 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4744 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4745 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004746 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004747 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004748 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004749 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004750 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004751 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004752 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004753 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004754 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4755 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4756 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4757 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4758 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4759 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4760 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4761 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4762 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004763 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004764 // Just ignore
4765 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004766 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004767 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004768 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004769 case AttributeList::AT_StdCall:
4770 case AttributeList::AT_CDecl:
4771 case AttributeList::AT_FastCall:
4772 case AttributeList::AT_ThisCall:
4773 case AttributeList::AT_Pascal:
4774 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004775 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004776 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004777 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004778 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004779 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004780 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004781 break;
John McCall8d32c052012-05-22 21:28:12 +00004782
4783 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004784 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004785 handleMsStructAttr(S, D, Attr);
4786 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004787 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004788 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004789 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004790 case AttributeList::AT_SingleInheritance:
4791 case AttributeList::AT_MultipleInheritance:
4792 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004793 handleInheritanceAttr(S, D, Attr);
4794 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004795 case AttributeList::AT_Win64:
4796 case AttributeList::AT_Ptr32:
4797 case AttributeList::AT_Ptr64:
John McCall8d32c052012-05-22 21:28:12 +00004798 handlePortabilityAttr(S, D, Attr);
4799 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004800 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004801 handleForceInlineAttr(S, D, Attr);
4802 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004803
4804 // Thread safety attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004805 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004806 handleGuardedVarAttr(S, D, Attr);
4807 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004808 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004809 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004810 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004811 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004812 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004813 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004814 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004815 handleNoAddressSafetyAttr(S, D, Attr);
4816 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004817 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004818 handleNoThreadSafetyAttr(S, D, Attr);
4819 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004820 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004821 handleLockableAttr(S, D, Attr);
4822 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004823 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004824 handleGuardedByAttr(S, D, Attr);
4825 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004826 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004827 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004828 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004829 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004830 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004831 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004832 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004833 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004834 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004835 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004836 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004837 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004838 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004839 handleLockReturnedAttr(S, D, Attr);
4840 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004841 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004842 handleLocksExcludedAttr(S, D, Attr);
4843 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004844 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004845 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004846 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004847 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004848 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004849 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004850 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004851 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004852 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004853 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004854 handleUnlockFunAttr(S, D, Attr);
4855 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004856 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004857 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004858 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004859 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004860 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004861 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004862
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004863 // Type safety attributes.
4864 case AttributeList::AT_ArgumentWithTypeTag:
4865 handleArgumentWithTypeTagAttr(S, D, Attr);
4866 break;
4867 case AttributeList::AT_TypeTagForDatatype:
4868 handleTypeTagForDatatypeAttr(S, D, Attr);
4869 break;
4870
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004871 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004872 // Ask target about the attribute.
4873 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4874 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004875 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4876 diag::warn_unhandled_ms_attribute_ignored :
4877 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004878 break;
4879 }
4880}
4881
Peter Collingbourneb331b262011-01-21 02:08:45 +00004882/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4883/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smith10876ef2013-01-17 01:30:42 +00004884/// silently ignore it if a GNU attribute.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004885static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4886 const AttributeList &Attr,
Richard Smith10876ef2013-01-17 01:30:42 +00004887 bool NonInheritable, bool Inheritable,
4888 bool IncludeCXX11Attributes) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004889 if (Attr.isInvalid())
4890 return;
4891
Richard Smith10876ef2013-01-17 01:30:42 +00004892 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4893 // instead.
4894 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4895 return;
4896
Peter Collingbourneb331b262011-01-21 02:08:45 +00004897 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004898 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004899
4900 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004901 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004902}
4903
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004904/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4905/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004906void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004907 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004908 bool NonInheritable, bool Inheritable,
4909 bool IncludeCXX11Attributes) {
4910 for (const AttributeList* l = AttrList; l; l = l->getNext())
4911 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
4912 IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004913
4914 // GCC accepts
4915 // static int a9 __attribute__((weakref));
4916 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004917 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004918 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004919 cast<NamedDecl>(D)->getNameAsString();
4920 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004921 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004922 }
4923}
4924
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004925// Annotation attributes are the only attributes allowed after an access
4926// specifier.
4927bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4928 const AttributeList *AttrList) {
4929 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004930 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004931 handleAnnotateAttr(*this, ASDecl, *l);
4932 } else {
4933 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4934 return true;
4935 }
4936 }
4937
4938 return false;
4939}
4940
John McCall42856de2011-10-01 05:17:03 +00004941/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4942/// contains any decl attributes that we should warn about.
4943static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4944 for ( ; A; A = A->getNext()) {
4945 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004946 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004947 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4948
4949 if (A->getKind() == AttributeList::UnknownAttribute) {
4950 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4951 << A->getName() << A->getRange();
4952 } else {
4953 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4954 << A->getName() << A->getRange();
4955 }
4956 }
4957}
4958
4959/// checkUnusedDeclAttributes - Given a declarator which is not being
4960/// used to build a declaration, complain about any decl attributes
4961/// which might be lying around on it.
4962void Sema::checkUnusedDeclAttributes(Declarator &D) {
4963 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4964 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4965 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4966 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4967}
4968
Ryan Flynn7d470f32009-07-30 03:15:39 +00004969/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004970/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004971NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4972 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004973 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004974 NamedDecl *NewD = 0;
4975 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004976 FunctionDecl *NewFD;
4977 // FIXME: Missing call to CheckFunctionDeclaration().
4978 // FIXME: Mangling?
4979 // FIXME: Is the qualifier info correct?
4980 // FIXME: Is the DeclContext correct?
4981 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4982 Loc, Loc, DeclarationName(II),
4983 FD->getType(), FD->getTypeSourceInfo(),
4984 SC_None, SC_None,
4985 false/*isInlineSpecified*/,
4986 FD->hasPrototype(),
4987 false/*isConstexprSpecified*/);
4988 NewD = NewFD;
4989
4990 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004991 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004992
4993 // Fake up parameter variables; they are declared as if this were
4994 // a typedef.
4995 QualType FDTy = FD->getType();
4996 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4997 SmallVector<ParmVarDecl*, 16> Params;
4998 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4999 AE = FT->arg_type_end(); AI != AE; ++AI) {
5000 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5001 Param->setScopeInfo(0, Params.size());
5002 Params.push_back(Param);
5003 }
David Blaikie9c70e042011-09-21 18:16:56 +00005004 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005005 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005006 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5007 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005008 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005009 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00005010 VD->getStorageClass(),
5011 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00005012 if (VD->getQualifier()) {
5013 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005014 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005015 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005016 }
5017 return NewD;
5018}
5019
James Dennett634962f2012-06-14 21:40:34 +00005020/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005021/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005022void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005023 if (W.getUsed()) return; // only do this once
5024 W.setUsed(true);
5025 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5026 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005027 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005028 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5029 NDId->getName()));
5030 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005031 WeakTopLevelDecl.push_back(NewD);
5032 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5033 // to insert Decl at TU scope, sorry.
5034 DeclContext *SavedContext = CurContext;
5035 CurContext = Context.getTranslationUnitDecl();
5036 PushOnScopeChains(NewD, S);
5037 CurContext = SavedContext;
5038 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005039 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005040 }
5041}
5042
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005043/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5044/// it, apply them to D. This is a bit tricky because PD can have attributes
5045/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005046void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5047 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00005048 // It's valid to "forward-declare" #pragma weak, in which case we
5049 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00005050 if (Inheritable) {
5051 LoadExternalWeakUndeclaredIdentifiers();
5052 if (!WeakUndeclaredIdentifiers.empty()) {
5053 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
5054 if (IdentifierInfo *Id = ND->getIdentifier()) {
5055 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5056 = WeakUndeclaredIdentifiers.find(Id);
5057 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
5058 WeakInfo W = I->second;
5059 DeclApplyPragmaWeak(S, ND, W);
5060 WeakUndeclaredIdentifiers[Id] = W;
5061 }
John McCall6fe02402010-10-27 00:59:00 +00005062 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005063 }
5064 }
5065 }
5066
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005067 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005068 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005069 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005070
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005071 // Walk the declarator structure, applying decl attributes that were in a type
5072 // position to the decl itself. This handles cases like:
5073 // int *__attr__(x)** D;
5074 // when X is a decl attribute.
5075 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5076 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith10876ef2013-01-17 01:30:42 +00005077 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5078 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005079
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005080 // Finally, apply any attributes on the decl itself.
5081 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005082 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005083}
John McCall28a6aea2009-11-04 02:18:39 +00005084
John McCall31168b02011-06-15 23:02:42 +00005085/// Is the given declaration allowed to use a forbidden type?
5086static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5087 // Private ivars are always okay. Unfortunately, people don't
5088 // always properly make their ivars private, even in system headers.
5089 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005090 // Function declarations in sys headers will be marked unavailable.
5091 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5092 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005093 return false;
5094
5095 // Require it to be declared in a system header.
5096 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5097}
5098
5099/// Handle a delayed forbidden-type diagnostic.
5100static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5101 Decl *decl) {
5102 if (decl && isForbiddenTypeAllowed(S, decl)) {
5103 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5104 "this system declaration uses an unsupported type"));
5105 return;
5106 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005107 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005108 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005109 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005110 // kind of forbidden type messages on unavailable functions.
5111 if (FD->hasAttr<UnavailableAttr>() &&
5112 diag.getForbiddenTypeDiagnostic() ==
5113 diag::err_arc_array_param_no_ownership) {
5114 diag.Triggered = true;
5115 return;
5116 }
5117 }
John McCall31168b02011-06-15 23:02:42 +00005118
5119 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5120 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5121 diag.Triggered = true;
5122}
5123
John McCall2ec85372012-05-07 06:16:41 +00005124void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5125 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005126 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005127 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005128
John McCall2ec85372012-05-07 06:16:41 +00005129 // When delaying diagnostics to run in the context of a parsed
5130 // declaration, we only want to actually emit anything if parsing
5131 // succeeds.
5132 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005133
John McCall2ec85372012-05-07 06:16:41 +00005134 // We emit all the active diagnostics in this pool or any of its
5135 // parents. In general, we'll get one pool for the decl spec
5136 // and a child pool for each declarator; in a decl group like:
5137 // deprecated_typedef foo, *bar, baz();
5138 // only the declarator pops will be passed decls. This is correct;
5139 // we really do need to consider delayed diagnostics from the decl spec
5140 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005141 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005142 do {
John McCall6347b682012-05-07 06:16:58 +00005143 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005144 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5145 // This const_cast is a bit lame. Really, Triggered should be mutable.
5146 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005147 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005148 continue;
5149
John McCallc1465822011-02-14 07:13:47 +00005150 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005151 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005152 // Don't bother giving deprecation diagnostics if the decl is invalid.
5153 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005154 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005155 break;
5156
5157 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005158 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005159 break;
John McCall31168b02011-06-15 23:02:42 +00005160
5161 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005162 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005163 break;
John McCall86121512010-01-27 03:50:35 +00005164 }
5165 }
John McCall2ec85372012-05-07 06:16:41 +00005166 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005167}
5168
John McCall6347b682012-05-07 06:16:58 +00005169/// Given a set of delayed diagnostics, re-emit them as if they had
5170/// been delayed in the current context instead of in the given pool.
5171/// Essentially, this just moves them to the current pool.
5172void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5173 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5174 assert(curPool && "re-emitting in undelayed context not supported");
5175 curPool->steal(pool);
5176}
5177
John McCall28a6aea2009-11-04 02:18:39 +00005178static bool isDeclDeprecated(Decl *D) {
5179 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005180 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005181 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005182 // A category implicitly has the availability of the interface.
5183 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5184 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005185 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5186 return false;
5187}
5188
Eli Friedman971bfa12012-08-08 21:52:41 +00005189static void
5190DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5191 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005192 const ObjCInterfaceDecl *UnknownObjCClass,
5193 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005194 DeclarationName Name = D->getDeclName();
5195 if (!Message.empty()) {
5196 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5197 S.Diag(D->getLocation(),
5198 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5199 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005200 if (ObjCPropery)
5201 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5202 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005203 } else if (!UnknownObjCClass) {
5204 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5205 S.Diag(D->getLocation(),
5206 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5207 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005208 if (ObjCPropery)
5209 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5210 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005211 } else {
5212 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5213 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5214 }
5215}
5216
John McCallb45a1e72010-08-26 02:13:20 +00005217void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005218 Decl *Ctx) {
5219 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005220 return;
5221
John McCall86121512010-01-27 03:50:35 +00005222 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005223 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5224 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005225 DD.getUnknownObjCClass(),
5226 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005227}
5228
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005229void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005230 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005231 const ObjCInterfaceDecl *UnknownObjCClass,
5232 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005233 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005234 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005235 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5236 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005237 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005238 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005239 return;
5240 }
5241
5242 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005243 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005244 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005245 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005246}