blob: e8c42e6f7c990dd138e6f5a3dea3bfce9604815c [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);
1100 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1101
1102 SmallVector<unsigned, 8> SizeArgs;
1103
1104 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1105 E = Attr.arg_end(); I!=E; ++I) {
1106 // The argument must be an integer constant expression.
1107 Expr *Ex = *I;
1108 llvm::APSInt ArgNum;
1109 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1110 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1111 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1112 << "alloc_size" << Ex->getSourceRange();
1113 return;
1114 }
1115
1116 uint64_t x = ArgNum.getZExtValue();
1117
1118 if (x < 1 || x > NumArgs) {
1119 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1120 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1121 return;
1122 }
1123
1124 --x;
1125 if (HasImplicitThisParam) {
1126 if (x == 0) {
1127 S.Diag(Attr.getLoc(),
1128 diag::err_attribute_invalid_implicit_this_argument)
1129 << "alloc_size" << Ex->getSourceRange();
1130 return;
1131 }
1132 --x;
1133 }
1134
1135 // check if the function argument is of an integer type
1136 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1137 if (!T->isIntegerType()) {
1138 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1139 << "alloc_size" << Ex->getSourceRange();
1140 return;
1141 }
1142
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001143 SizeArgs.push_back(x);
1144 }
1145
1146 // check if the function returns a pointer
1147 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1148 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1149 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1150 }
1151
Michael Han99315932013-01-24 16:46:58 +00001152 D->addAttr(::new (S.Context)
1153 AllocSizeAttr(Attr.getRange(), S.Context,
1154 SizeArgs.data(), SizeArgs.size(),
1155 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001156}
1157
Chandler Carruthedc2c642011-07-02 00:01:44 +00001158static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001159 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1160 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001161 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001162 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001163 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001164 return;
1165 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001166
Chandler Carruth743682b2010-11-16 08:35:43 +00001167 // In C++ the implicit 'this' function parameter also counts, and they are
1168 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001169 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewyckye1121512013-01-24 01:12:16 +00001170 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001171
1172 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001173 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001174
Nick Lewyckye1121512013-01-24 01:12:16 +00001175 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1176 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001177 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001178 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001179 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001180 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1181 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001182 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1183 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001184 return;
1185 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001186
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001187 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001188
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001189 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001190 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +00001191 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001192 return;
1193 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001194
Ted Kremenek5224e6a2008-07-21 22:09:15 +00001195 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001196 if (HasImplicitThisParam) {
1197 if (x == 0) {
1198 S.Diag(Attr.getLoc(),
1199 diag::err_attribute_invalid_implicit_this_argument)
1200 << "nonnull" << Ex->getSourceRange();
1201 return;
1202 }
1203 --x;
1204 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001205
1206 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001207 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001208 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001209
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001210 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001211 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001212 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001213 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001214 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001215 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001216
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001217 NonNullArgs.push_back(x);
1218 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001219
1220 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1221 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001222 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001223 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1224 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001225 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001226 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001227 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001228 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001229
Ted Kremenek22813f42010-10-21 18:49:36 +00001230 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001231 if (NonNullArgs.empty()) {
1232 // Warn the trivial case only if attribute is not coming from a
1233 // macro instantiation.
1234 if (Attr.getLoc().isFileID())
1235 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001236 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001237 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001238 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001239
Nick Lewyckye1121512013-01-24 01:12:16 +00001240 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001241 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001242 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001243 D->addAttr(::new (S.Context)
1244 NonNullAttr(Attr.getRange(), S.Context, start, size,
1245 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001246}
1247
Chandler Carruthedc2c642011-07-02 00:01:44 +00001248static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001249 // This attribute must be applied to a function declaration.
1250 // The first argument to the attribute must be a string,
1251 // the name of the resource, for example "malloc".
1252 // The following arguments must be argument indexes, the arguments must be
1253 // of integer type for Returns, otherwise of pointer type.
1254 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001255 // after being held. free() should be __attribute((ownership_takes)), whereas
1256 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001257
1258 if (!AL.getParameterName()) {
1259 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1260 << AL.getName()->getName() << 1;
1261 return;
1262 }
1263 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001264 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001265 switch (AL.getKind()) {
1266 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001267 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001268 if (AL.getNumArgs() < 1) {
1269 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1270 return;
1271 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001272 break;
1273 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001274 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001275 if (AL.getNumArgs() < 1) {
1276 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1277 return;
1278 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001279 break;
1280 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001281 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001282 if (AL.getNumArgs() > 1) {
1283 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1284 << AL.getNumArgs() + 1;
1285 return;
1286 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001287 break;
1288 default:
1289 // This should never happen given how we are called.
1290 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001291 }
1292
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001293 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001294 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1295 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001296 return;
1297 }
1298
Chandler Carruth743682b2010-11-16 08:35:43 +00001299 // In C++ the implicit 'this' function parameter also counts, and they are
1300 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001301 bool HasImplicitThisParam = isInstanceMethod(D);
1302 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001303
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001304 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001305
1306 // Normalize the argument, __foo__ becomes foo.
1307 if (Module.startswith("__") && Module.endswith("__"))
1308 Module = Module.substr(2, Module.size() - 4);
1309
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001310 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001311
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001312 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1313 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001314
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001315 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001316 llvm::APSInt ArgNum(32);
1317 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1318 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1319 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1320 << AL.getName()->getName() << IdxExpr->getSourceRange();
1321 continue;
1322 }
1323
1324 unsigned x = (unsigned) ArgNum.getZExtValue();
1325
1326 if (x > NumArgs || x < 1) {
1327 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1328 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1329 continue;
1330 }
1331 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001332 if (HasImplicitThisParam) {
1333 if (x == 0) {
1334 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1335 << "ownership" << IdxExpr->getSourceRange();
1336 return;
1337 }
1338 --x;
1339 }
1340
Ted Kremenekd21139a2010-07-31 01:52:11 +00001341 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001342 case OwnershipAttr::Takes:
1343 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001344 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001345 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001346 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1347 // FIXME: Should also highlight argument in decl.
1348 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001349 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001350 << "pointer"
1351 << IdxExpr->getSourceRange();
1352 continue;
1353 }
1354 break;
1355 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001356 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001357 if (AL.getNumArgs() > 1) {
1358 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001359 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001360 llvm::APSInt ArgNum(32);
1361 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1362 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1363 S.Diag(AL.getLoc(), diag::err_ownership_type)
1364 << "ownership_returns" << "integer"
1365 << IdxExpr->getSourceRange();
1366 return;
1367 }
1368 }
1369 break;
1370 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001371 } // switch
1372
1373 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001374 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001375 i = D->specific_attr_begin<OwnershipAttr>(),
1376 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001377 i != e; ++i) {
1378 if ((*i)->getOwnKind() != K) {
1379 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1380 I!=E; ++I) {
1381 if (x == *I) {
1382 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1383 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384 }
1385 }
1386 }
1387 }
1388 OwnershipArgs.push_back(x);
1389 }
1390
1391 unsigned* start = OwnershipArgs.data();
1392 unsigned size = OwnershipArgs.size();
1393 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001394
1395 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1396 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1397 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001398 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001399
Michael Han99315932013-01-24 16:46:58 +00001400 D->addAttr(::new (S.Context)
1401 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1402 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001403}
1404
Chandler Carruthedc2c642011-07-02 00:01:44 +00001405static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001406 // Check the attribute arguments.
1407 if (Attr.getNumArgs() > 1) {
1408 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1409 return;
1410 }
1411
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001412 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001413 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001414 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001415 return;
1416 }
1417
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001418 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001419
Rafael Espindolac18086a2010-02-23 22:00:30 +00001420 // gcc rejects
1421 // class c {
1422 // static int a __attribute__((weakref ("v2")));
1423 // static int b() __attribute__((weakref ("f3")));
1424 // };
1425 // and ignores the attributes of
1426 // void f(void) {
1427 // static int a __attribute__((weakref ("v2")));
1428 // }
1429 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001430 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001431 if (!Ctx->isFileContext()) {
1432 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001433 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001434 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001435 }
1436
1437 // The GCC manual says
1438 //
1439 // At present, a declaration to which `weakref' is attached can only
1440 // be `static'.
1441 //
1442 // It also says
1443 //
1444 // Without a TARGET,
1445 // given as an argument to `weakref' or to `alias', `weakref' is
1446 // equivalent to `weak'.
1447 //
1448 // gcc 4.4.1 will accept
1449 // int a7 __attribute__((weakref));
1450 // as
1451 // int a7 __attribute__((weak));
1452 // This looks like a bug in gcc. We reject that for now. We should revisit
1453 // it if this behaviour is actually used.
1454
Rafael Espindolac18086a2010-02-23 22:00:30 +00001455 // GCC rejects
1456 // static ((alias ("y"), weakref)).
1457 // Should we? How to check that weakref is before or after alias?
1458
1459 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001460 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001461 Arg = Arg->IgnoreParenCasts();
1462 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1463
Douglas Gregorfb65e592011-07-27 05:40:30 +00001464 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001465 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1466 << "weakref" << 1;
1467 return;
1468 }
1469 // GCC will accept anything as the argument of weakref. Should we
1470 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001471 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001472 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001473 }
1474
Michael Han99315932013-01-24 16:46:58 +00001475 D->addAttr(::new (S.Context)
1476 WeakRefAttr(Attr.getRange(), S.Context,
1477 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001478}
1479
Chandler Carruthedc2c642011-07-02 00:01:44 +00001480static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001481 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001482 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001484 return;
1485 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001486
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001487 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001488 Arg = Arg->IgnoreParenCasts();
1489 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001490
Douglas Gregorfb65e592011-07-27 05:40:30 +00001491 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001492 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001493 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001494 return;
1495 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001496
Douglas Gregore8bbc122011-09-02 00:18:52 +00001497 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001498 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1499 return;
1500 }
1501
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001502 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001503
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001504 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00001505 Str->getString(),
1506 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001507}
1508
Quentin Colombet4e172062012-11-01 23:55:47 +00001509static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1510 // Check the attribute arguments.
1511 if (!checkAttributeNumArgs(S, Attr, 0))
1512 return;
1513
1514 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1515 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1516 << Attr.getName() << ExpectedFunctionOrMethod;
1517 return;
1518 }
1519
Michael Han99315932013-01-24 16:46:58 +00001520 D->addAttr(::new (S.Context)
1521 MinSizeAttr(Attr.getRange(), S.Context,
1522 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001523}
1524
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001525static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1526 // Check the attribute arguments.
1527 if (!checkAttributeNumArgs(S, Attr, 0))
1528 return;
1529
1530 if (!isa<FunctionDecl>(D)) {
1531 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1532 << Attr.getName() << ExpectedFunction;
1533 return;
1534 }
1535
1536 if (D->hasAttr<HotAttr>()) {
1537 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1538 << Attr.getName() << "hot";
1539 return;
1540 }
1541
Michael Han99315932013-01-24 16:46:58 +00001542 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1543 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001544}
1545
1546static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1547 // Check the attribute arguments.
1548 if (!checkAttributeNumArgs(S, Attr, 0))
1549 return;
1550
1551 if (!isa<FunctionDecl>(D)) {
1552 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1553 << Attr.getName() << ExpectedFunction;
1554 return;
1555 }
1556
1557 if (D->hasAttr<ColdAttr>()) {
1558 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1559 << Attr.getName() << "cold";
1560 return;
1561 }
1562
Michael Han99315932013-01-24 16:46:58 +00001563 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1564 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001565}
1566
Chandler Carruthedc2c642011-07-02 00:01:44 +00001567static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001568 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001569 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001570 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001571
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001572 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001573 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001574 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001575 return;
1576 }
1577
Michael Han99315932013-01-24 16:46:58 +00001578 D->addAttr(::new (S.Context)
1579 NakedAttr(Attr.getRange(), S.Context,
1580 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001581}
1582
Chandler Carruthedc2c642011-07-02 00:01:44 +00001583static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1584 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001585 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001586 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001587 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1588 return;
1589 }
1590
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001591 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001592 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001593 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001594 return;
1595 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001596
Michael Han99315932013-01-24 16:46:58 +00001597 D->addAttr(::new (S.Context)
1598 AlwaysInlineAttr(Attr.getRange(), S.Context,
1599 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001600}
1601
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001602static void handleTLSModelAttr(Sema &S, Decl *D,
1603 const AttributeList &Attr) {
1604 // Check the attribute arguments.
1605 if (Attr.getNumArgs() != 1) {
1606 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1607 return;
1608 }
1609
1610 Expr *Arg = Attr.getArg(0);
1611 Arg = Arg->IgnoreParenCasts();
1612 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1613
1614 // Check that it is a string.
1615 if (!Str) {
1616 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1617 return;
1618 }
1619
1620 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1621 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1622 << Attr.getName() << ExpectedTLSVar;
1623 return;
1624 }
1625
1626 // Check that the value.
1627 StringRef Model = Str->getString();
1628 if (Model != "global-dynamic" && Model != "local-dynamic"
1629 && Model != "initial-exec" && Model != "local-exec") {
1630 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1631 return;
1632 }
1633
Michael Han99315932013-01-24 16:46:58 +00001634 D->addAttr(::new (S.Context)
1635 TLSModelAttr(Attr.getRange(), S.Context, Model,
1636 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001637}
1638
Chandler Carruthedc2c642011-07-02 00:01:44 +00001639static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001640 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001641 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001642 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1643 return;
1644 }
Mike Stump11289f42009-09-09 15:08:12 +00001645
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001646 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001647 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001648 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001649 D->addAttr(::new (S.Context)
1650 MallocAttr(Attr.getRange(), S.Context,
1651 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001652 return;
1653 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001654 }
1655
Ted Kremenek08479ae2009-08-15 00:51:46 +00001656 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001657}
1658
Chandler Carruthedc2c642011-07-02 00:01:44 +00001659static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001660 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001661 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001662 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001663
Michael Han99315932013-01-24 16:46:58 +00001664 D->addAttr(::new (S.Context)
1665 MayAliasAttr(Attr.getRange(), S.Context,
1666 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001667}
1668
Chandler Carruthedc2c642011-07-02 00:01:44 +00001669static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001670 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001671 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001672 D->addAttr(::new (S.Context)
1673 NoCommonAttr(Attr.getRange(), S.Context,
1674 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001675 else
1676 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001677 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001678}
1679
Chandler Carruthedc2c642011-07-02 00:01:44 +00001680static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001681 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001682 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001683 D->addAttr(::new (S.Context)
1684 CommonAttr(Attr.getRange(), S.Context,
1685 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001686 else
1687 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001688 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001689}
1690
Chandler Carruthedc2c642011-07-02 00:01:44 +00001691static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001692 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001693
1694 if (S.CheckNoReturnAttr(attr)) return;
1695
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001696 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001697 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001698 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001699 return;
1700 }
1701
Michael Han99315932013-01-24 16:46:58 +00001702 D->addAttr(::new (S.Context)
1703 NoReturnAttr(attr.getRange(), S.Context,
1704 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001705}
1706
1707bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001708 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001709 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1710 attr.setInvalid();
1711 return true;
1712 }
1713
1714 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001715}
1716
Chandler Carruthedc2c642011-07-02 00:01:44 +00001717static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1718 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001719
1720 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1721 // because 'analyzer_noreturn' does not impact the type.
1722
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001723 if(!checkAttributeNumArgs(S, Attr, 0))
1724 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001725
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001726 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1727 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001728 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1729 && !VD->getType()->isFunctionPointerType())) {
1730 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001731 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001732 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001733 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001734 return;
1735 }
1736 }
1737
Michael Han99315932013-01-24 16:46:58 +00001738 D->addAttr(::new (S.Context)
1739 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1740 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001741}
1742
Richard Smith10876ef2013-01-17 01:30:42 +00001743static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1744 const AttributeList &Attr) {
1745 // C++11 [dcl.attr.noreturn]p1:
1746 // The attribute may be applied to the declarator-id in a function
1747 // declaration.
1748 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1749 if (!FD) {
1750 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1751 << Attr.getName() << ExpectedFunctionOrMethod;
1752 return;
1753 }
1754
Michael Han99315932013-01-24 16:46:58 +00001755 D->addAttr(::new (S.Context)
1756 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1757 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001758}
1759
John Thompsoncdb847ba2010-08-09 21:53:52 +00001760// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001761static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001762/*
1763 Returning a Vector Class in Registers
1764
Eric Christopherbc638a82010-12-01 22:13:54 +00001765 According to the PPU ABI specifications, a class with a single member of
1766 vector type is returned in memory when used as the return value of a function.
1767 This results in inefficient code when implementing vector classes. To return
1768 the value in a single vector register, add the vecreturn attribute to the
1769 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001770
1771 Example:
1772
1773 struct Vector
1774 {
1775 __vector float xyzw;
1776 } __attribute__((vecreturn));
1777
1778 Vector Add(Vector lhs, Vector rhs)
1779 {
1780 Vector result;
1781 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1782 return result; // This will be returned in a register
1783 }
1784*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001785 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001786 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001787 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001788 return;
1789 }
1790
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001791 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001792 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1793 return;
1794 }
1795
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001796 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001797 int count = 0;
1798
1799 if (!isa<CXXRecordDecl>(record)) {
1800 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1801 return;
1802 }
1803
1804 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1805 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1806 return;
1807 }
1808
Eric Christopherbc638a82010-12-01 22:13:54 +00001809 for (RecordDecl::field_iterator iter = record->field_begin();
1810 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001811 if ((count == 1) || !iter->getType()->isVectorType()) {
1812 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1813 return;
1814 }
1815 count++;
1816 }
1817
Michael Han99315932013-01-24 16:46:58 +00001818 D->addAttr(::new (S.Context)
1819 VecReturnAttr(Attr.getRange(), S.Context,
1820 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001821}
1822
Richard Smithe233fbf2013-01-28 22:42:45 +00001823static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1824 const AttributeList &Attr) {
1825 if (isa<ParmVarDecl>(D)) {
1826 // [[carries_dependency]] can only be applied to a parameter if it is a
1827 // parameter of a function declaration or lambda.
1828 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1829 S.Diag(Attr.getLoc(),
1830 diag::err_carries_dependency_param_not_function_decl);
1831 return;
1832 }
1833 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001834 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001835 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001836 return;
1837 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001838
1839 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1840 Attr.getRange(), S.Context,
1841 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001842}
1843
Chandler Carruthedc2c642011-07-02 00:01:44 +00001844static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001845 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001846 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001847 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001848 return;
1849 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001850
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001851 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001852 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001853 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001854 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001855 return;
1856 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001857
Michael Han99315932013-01-24 16:46:58 +00001858 D->addAttr(::new (S.Context)
1859 UnusedAttr(Attr.getRange(), S.Context,
1860 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001861}
1862
Rafael Espindola70107f92011-10-03 14:59:42 +00001863static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1864 const AttributeList &Attr) {
1865 // check the attribute arguments.
1866 if (Attr.hasParameterOrArguments()) {
1867 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1868 return;
1869 }
1870
1871 if (!isa<FunctionDecl>(D)) {
1872 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1873 << Attr.getName() << ExpectedFunction;
1874 return;
1875 }
1876
Michael Han99315932013-01-24 16:46:58 +00001877 D->addAttr(::new (S.Context)
1878 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1879 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00001880}
1881
Chandler Carruthedc2c642011-07-02 00:01:44 +00001882static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001883 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001884 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001885 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1886 return;
1887 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001888
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001889 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001890 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001891 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1892 return;
1893 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001894 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001895 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001896 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001897 return;
1898 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001899
Michael Han99315932013-01-24 16:46:58 +00001900 D->addAttr(::new (S.Context)
1901 UsedAttr(Attr.getRange(), S.Context,
1902 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001903}
1904
Chandler Carruthedc2c642011-07-02 00:01:44 +00001905static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001906 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001907 if (Attr.getNumArgs() > 1) {
1908 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001909 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001910 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001911
1912 int priority = 65535; // FIXME: Do not hardcode such constants.
1913 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001914 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001915 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001916 if (E->isTypeDependent() || E->isValueDependent() ||
1917 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001918 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001919 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001920 return;
1921 }
1922 priority = Idx.getZExtValue();
1923 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001924
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001925 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001926 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001927 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001928 return;
1929 }
1930
Michael Han99315932013-01-24 16:46:58 +00001931 D->addAttr(::new (S.Context)
1932 ConstructorAttr(Attr.getRange(), S.Context, priority,
1933 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001934}
1935
Chandler Carruthedc2c642011-07-02 00:01:44 +00001936static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001937 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001938 if (Attr.getNumArgs() > 1) {
1939 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001940 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001941 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001942
1943 int priority = 65535; // FIXME: Do not hardcode such constants.
1944 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001945 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001946 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001947 if (E->isTypeDependent() || E->isValueDependent() ||
1948 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001949 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001950 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001951 return;
1952 }
1953 priority = Idx.getZExtValue();
1954 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001955
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001956 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001957 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001958 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001959 return;
1960 }
1961
Michael Han99315932013-01-24 16:46:58 +00001962 D->addAttr(::new (S.Context)
1963 DestructorAttr(Attr.getRange(), S.Context, priority,
1964 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001965}
1966
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001967template <typename AttrTy>
1968static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1969 const char *Name) {
Chris Lattner190aa102011-02-24 05:42:24 +00001970 unsigned NumArgs = Attr.getNumArgs();
1971 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001972 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001973 return;
1974 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001975
1976 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001977 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001978 if (NumArgs == 1) {
1979 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001980 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001981 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001982 << Name;
Fariborz Jahanian551063102010-10-06 21:18:44 +00001983 return;
1984 }
Chris Lattner190aa102011-02-24 05:42:24 +00001985 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001986 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001987
Michael Han99315932013-01-24 16:46:58 +00001988 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1989 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001990}
1991
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001992static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1993 const AttributeList &Attr) {
1994 unsigned NumArgs = Attr.getNumArgs();
1995 if (NumArgs > 0) {
1996 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1997 return;
1998 }
1999
Michael Han99315932013-01-24 16:46:58 +00002000 D->addAttr(::new (S.Context)
2001 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2002 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002003}
2004
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002005static void handleObjCRootClassAttr(Sema &S, Decl *D,
2006 const AttributeList &Attr) {
2007 if (!isa<ObjCInterfaceDecl>(D)) {
2008 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2009 return;
2010 }
2011
2012 unsigned NumArgs = Attr.getNumArgs();
2013 if (NumArgs > 0) {
2014 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2015 return;
2016 }
2017
Michael Han99315932013-01-24 16:46:58 +00002018 D->addAttr(::new (S.Context)
2019 ObjCRootClassAttr(Attr.getRange(), S.Context,
2020 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002021}
2022
Michael Han99315932013-01-24 16:46:58 +00002023static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2024 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002025 if (!isa<ObjCInterfaceDecl>(D)) {
2026 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2027 return;
2028 }
2029
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002030 unsigned NumArgs = Attr.getNumArgs();
2031 if (NumArgs > 0) {
2032 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2033 return;
2034 }
2035
Michael Han99315932013-01-24 16:46:58 +00002036 D->addAttr(::new (S.Context)
2037 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2038 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002039}
2040
Jordy Rose740b0c22012-05-08 03:27:22 +00002041static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2042 IdentifierInfo *Platform,
2043 VersionTuple Introduced,
2044 VersionTuple Deprecated,
2045 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002046 StringRef PlatformName
2047 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2048 if (PlatformName.empty())
2049 PlatformName = Platform->getName();
2050
2051 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2052 // of these steps are needed).
2053 if (!Introduced.empty() && !Deprecated.empty() &&
2054 !(Introduced <= Deprecated)) {
2055 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2056 << 1 << PlatformName << Deprecated.getAsString()
2057 << 0 << Introduced.getAsString();
2058 return true;
2059 }
2060
2061 if (!Introduced.empty() && !Obsoleted.empty() &&
2062 !(Introduced <= Obsoleted)) {
2063 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2064 << 2 << PlatformName << Obsoleted.getAsString()
2065 << 0 << Introduced.getAsString();
2066 return true;
2067 }
2068
2069 if (!Deprecated.empty() && !Obsoleted.empty() &&
2070 !(Deprecated <= Obsoleted)) {
2071 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2072 << 2 << PlatformName << Obsoleted.getAsString()
2073 << 1 << Deprecated.getAsString();
2074 return true;
2075 }
2076
2077 return false;
2078}
2079
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002080/// \brief Check whether the two versions match.
2081///
2082/// If either version tuple is empty, then they are assumed to match. If
2083/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2084static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2085 bool BeforeIsOkay) {
2086 if (X.empty() || Y.empty())
2087 return true;
2088
2089 if (X == Y)
2090 return true;
2091
2092 if (BeforeIsOkay && X < Y)
2093 return true;
2094
2095 return false;
2096}
2097
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002098AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002099 IdentifierInfo *Platform,
2100 VersionTuple Introduced,
2101 VersionTuple Deprecated,
2102 VersionTuple Obsoleted,
2103 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002104 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002105 bool Override,
2106 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002107 VersionTuple MergedIntroduced = Introduced;
2108 VersionTuple MergedDeprecated = Deprecated;
2109 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002110 bool FoundAny = false;
2111
Rafael Espindolac67f2232012-05-10 02:50:16 +00002112 if (D->hasAttrs()) {
2113 AttrVec &Attrs = D->getAttrs();
2114 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2115 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2116 if (!OldAA) {
2117 ++i;
2118 continue;
2119 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002120
Rafael Espindolac67f2232012-05-10 02:50:16 +00002121 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2122 if (OldPlatform != Platform) {
2123 ++i;
2124 continue;
2125 }
2126
2127 FoundAny = true;
2128 VersionTuple OldIntroduced = OldAA->getIntroduced();
2129 VersionTuple OldDeprecated = OldAA->getDeprecated();
2130 VersionTuple OldObsoleted = OldAA->getObsoleted();
2131 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002132
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002133 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2134 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2135 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2136 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002137 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002138 if (Override) {
2139 int Which = -1;
2140 VersionTuple FirstVersion;
2141 VersionTuple SecondVersion;
2142 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2143 Which = 0;
2144 FirstVersion = OldIntroduced;
2145 SecondVersion = Introduced;
2146 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2147 Which = 1;
2148 FirstVersion = Deprecated;
2149 SecondVersion = OldDeprecated;
2150 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2151 Which = 2;
2152 FirstVersion = Obsoleted;
2153 SecondVersion = OldObsoleted;
2154 }
2155
2156 if (Which == -1) {
2157 Diag(OldAA->getLocation(),
2158 diag::warn_mismatched_availability_override_unavail)
2159 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2160 } else {
2161 Diag(OldAA->getLocation(),
2162 diag::warn_mismatched_availability_override)
2163 << Which
2164 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2165 << FirstVersion.getAsString() << SecondVersion.getAsString();
2166 }
2167 Diag(Range.getBegin(), diag::note_overridden_method);
2168 } else {
2169 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2170 Diag(Range.getBegin(), diag::note_previous_attribute);
2171 }
2172
Rafael Espindolac67f2232012-05-10 02:50:16 +00002173 Attrs.erase(Attrs.begin() + i);
2174 --e;
2175 continue;
2176 }
2177
2178 VersionTuple MergedIntroduced2 = MergedIntroduced;
2179 VersionTuple MergedDeprecated2 = MergedDeprecated;
2180 VersionTuple MergedObsoleted2 = MergedObsoleted;
2181
2182 if (MergedIntroduced2.empty())
2183 MergedIntroduced2 = OldIntroduced;
2184 if (MergedDeprecated2.empty())
2185 MergedDeprecated2 = OldDeprecated;
2186 if (MergedObsoleted2.empty())
2187 MergedObsoleted2 = OldObsoleted;
2188
2189 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2190 MergedIntroduced2, MergedDeprecated2,
2191 MergedObsoleted2)) {
2192 Attrs.erase(Attrs.begin() + i);
2193 --e;
2194 continue;
2195 }
2196
2197 MergedIntroduced = MergedIntroduced2;
2198 MergedDeprecated = MergedDeprecated2;
2199 MergedObsoleted = MergedObsoleted2;
2200 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002201 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002202 }
2203
2204 if (FoundAny &&
2205 MergedIntroduced == Introduced &&
2206 MergedDeprecated == Deprecated &&
2207 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002208 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002209
Rafael Espindolac67f2232012-05-10 02:50:16 +00002210 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002211 MergedDeprecated, MergedObsoleted)) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002212 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2213 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002214 Obsoleted, IsUnavailable, Message,
2215 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002216 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002217 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002218}
2219
Chandler Carruthedc2c642011-07-02 00:01:44 +00002220static void handleAvailabilityAttr(Sema &S, Decl *D,
2221 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002222 IdentifierInfo *Platform = Attr.getParameterName();
2223 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han99315932013-01-24 16:46:58 +00002224 unsigned Index = Attr.getAttributeSpellingListIndex();
2225
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002226 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002227 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2228 << Platform;
2229
Rafael Espindolac231fab2013-01-08 21:30:32 +00002230 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2231 if (!ND) {
2232 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2233 return;
2234 }
2235
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002236 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2237 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2238 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002239 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002240 StringRef Str;
2241 const StringLiteral *SE =
2242 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2243 if (SE)
2244 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002245
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002246 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002247 Platform,
2248 Introduced.Version,
2249 Deprecated.Version,
2250 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002251 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002252 /*Override=*/false,
2253 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002254 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002255 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002256}
2257
John McCalld041a9b2013-02-20 01:54:26 +00002258template <class T>
2259static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2260 typename T::VisibilityType value,
2261 unsigned attrSpellingListIndex) {
2262 T *existingAttr = D->getAttr<T>();
2263 if (existingAttr) {
2264 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2265 if (existingValue == value)
2266 return NULL;
2267 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2268 S.Diag(range.getBegin(), diag::note_previous_attribute);
2269 D->dropAttr<T>();
2270 }
2271 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2272}
2273
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002274VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002275 VisibilityAttr::VisibilityType Vis,
2276 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002277 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2278 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002279}
2280
John McCalld041a9b2013-02-20 01:54:26 +00002281TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2282 TypeVisibilityAttr::VisibilityType Vis,
2283 unsigned AttrSpellingListIndex) {
2284 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2285 AttrSpellingListIndex);
2286}
2287
2288static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2289 bool isTypeVisibility) {
2290 // Visibility attributes don't mean anything on a typedef.
2291 if (isa<TypedefNameDecl>(D)) {
2292 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2293 << Attr.getName();
2294 return;
2295 }
2296
2297 // 'type_visibility' can only go on a type or namespace.
2298 if (isTypeVisibility &&
2299 !(isa<TagDecl>(D) ||
2300 isa<ObjCInterfaceDecl>(D) ||
2301 isa<NamespaceDecl>(D))) {
2302 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2303 << Attr.getName() << ExpectedTypeOrNamespace;
2304 return;
2305 }
2306
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002307 // check the attribute arguments.
John McCalld041a9b2013-02-20 01:54:26 +00002308 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002309 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002310
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002311 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002312 Arg = Arg->IgnoreParenCasts();
2313 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002314
Douglas Gregorfb65e592011-07-27 05:40:30 +00002315 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002316 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld041a9b2013-02-20 01:54:26 +00002317 << (isTypeVisibility ? "type_visibility" : "visibility") << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002318 return;
2319 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002320
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002321 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002322 VisibilityAttr::VisibilityType type;
Michael Han99315932013-01-24 16:46:58 +00002323
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002324 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002325 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002326 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002327 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002328 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002329 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00002330 else if (TypeStr == "protected") {
2331 // Complain about attempts to use protected visibility on targets
2332 // (like Darwin) that don't support it.
2333 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2334 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2335 type = VisibilityAttr::Default;
2336 } else {
2337 type = VisibilityAttr::Protected;
2338 }
2339 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002340 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002341 return;
2342 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002343
Michael Han99315932013-01-24 16:46:58 +00002344 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002345 clang::Attr *newAttr;
2346 if (isTypeVisibility) {
2347 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2348 (TypeVisibilityAttr::VisibilityType) type,
2349 Index);
2350 } else {
2351 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2352 }
2353 if (newAttr)
2354 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002355}
2356
Chandler Carruthedc2c642011-07-02 00:01:44 +00002357static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2358 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002359 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2360 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002361 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002362 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002363 return;
2364 }
2365
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002366 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2367 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2368 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00002369 << "objc_method_family" << 1;
2370 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002371 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002372 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002373 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002374 return;
2375 }
2376
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002377 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002378 ObjCMethodFamilyAttr::FamilyKind family;
2379 if (param == "none")
2380 family = ObjCMethodFamilyAttr::OMF_None;
2381 else if (param == "alloc")
2382 family = ObjCMethodFamilyAttr::OMF_alloc;
2383 else if (param == "copy")
2384 family = ObjCMethodFamilyAttr::OMF_copy;
2385 else if (param == "init")
2386 family = ObjCMethodFamilyAttr::OMF_init;
2387 else if (param == "mutableCopy")
2388 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2389 else if (param == "new")
2390 family = ObjCMethodFamilyAttr::OMF_new;
2391 else {
2392 // Just warn and ignore it. This is future-proof against new
2393 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002394 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002395 return;
2396 }
2397
John McCall31168b02011-06-15 23:02:42 +00002398 if (family == ObjCMethodFamilyAttr::OMF_init &&
2399 !method->getResultType()->isObjCObjectPointerType()) {
2400 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2401 << method->getResultType();
2402 // Ignore the attribute.
2403 return;
2404 }
2405
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002406 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002407 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002408}
2409
Chandler Carruthedc2c642011-07-02 00:01:44 +00002410static void handleObjCExceptionAttr(Sema &S, Decl *D,
2411 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002412 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002413 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002414
Chris Lattner677a3582009-02-14 08:09:34 +00002415 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2416 if (OCI == 0) {
2417 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2418 return;
2419 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002420
Michael Han99315932013-01-24 16:46:58 +00002421 D->addAttr(::new (S.Context)
2422 ObjCExceptionAttr(Attr.getRange(), S.Context,
2423 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002424}
2425
Chandler Carruthedc2c642011-07-02 00:01:44 +00002426static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002427 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002428 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002429 return;
2430 }
Richard Smithdda56e42011-04-15 14:24:37 +00002431 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002432 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002433 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002434 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2435 return;
2436 }
2437 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002438 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2439 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002440 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002441 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2442 return;
2443 }
2444 }
2445 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002446 // It is okay to include this attribute on properties, e.g.:
2447 //
2448 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2449 //
2450 // In this case it follows tradition and suppresses an error in the above
2451 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002452 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002453 }
Michael Han99315932013-01-24 16:46:58 +00002454 D->addAttr(::new (S.Context)
2455 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2456 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002457}
2458
Mike Stumpd3bb5572009-07-24 19:02:52 +00002459static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002460handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002461 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002462 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002463 return;
2464 }
2465
2466 if (!isa<FunctionDecl>(D)) {
2467 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2468 return;
2469 }
2470
Michael Han99315932013-01-24 16:46:58 +00002471 D->addAttr(::new (S.Context)
2472 OverloadableAttr(Attr.getRange(), S.Context,
2473 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002474}
2475
Chandler Carruthedc2c642011-07-02 00:01:44 +00002476static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002477 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002478 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002479 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002480 return;
2481 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002482
Steve Naroff3405a732008-09-18 16:44:58 +00002483 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002484 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002485 return;
2486 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002487
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002488 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002489 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002490 type = BlocksAttr::ByRef;
2491 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002492 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002493 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002494 return;
2495 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002496
Michael Han99315932013-01-24 16:46:58 +00002497 D->addAttr(::new (S.Context)
2498 BlocksAttr(Attr.getRange(), S.Context, type,
2499 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002500}
2501
Chandler Carruthedc2c642011-07-02 00:01:44 +00002502static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002503 // check the attribute arguments.
2504 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002505 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002506 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002507 }
2508
John McCallb46f2872011-09-09 07:56:05 +00002509 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002510 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002511 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002512 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002513 if (E->isTypeDependent() || E->isValueDependent() ||
2514 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002515 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002516 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002517 return;
2518 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002519
John McCallb46f2872011-09-09 07:56:05 +00002520 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002521 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2522 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002523 return;
2524 }
John McCallb46f2872011-09-09 07:56:05 +00002525
2526 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002527 }
2528
John McCallb46f2872011-09-09 07:56:05 +00002529 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002530 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002531 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002532 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002533 if (E->isTypeDependent() || E->isValueDependent() ||
2534 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002535 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002536 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002537 return;
2538 }
2539 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002540
John McCallb46f2872011-09-09 07:56:05 +00002541 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002542 // FIXME: This error message could be improved, it would be nice
2543 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002544 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2545 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002546 return;
2547 }
2548 }
2549
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002550 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002551 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002552 if (isa<FunctionNoProtoType>(FT)) {
2553 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2554 return;
2555 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002556
Chris Lattner9363e312009-03-17 23:03:47 +00002557 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002558 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002559 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002560 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002561 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002562 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002563 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002564 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002565 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002566 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2567 if (!BD->isVariadic()) {
2568 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2569 return;
2570 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002571 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002572 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002573 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002574 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002575 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002576 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002577 int m = Ty->isFunctionPointerType() ? 0 : 1;
2578 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002579 return;
2580 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002581 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002582 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002583 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002584 return;
2585 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002586 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002588 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002589 return;
2590 }
Michael Han99315932013-01-24 16:46:58 +00002591 D->addAttr(::new (S.Context)
2592 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2593 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002594}
2595
Chandler Carruthedc2c642011-07-02 00:01:44 +00002596static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002597 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002598 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002599 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002600
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002601 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002602 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002603 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002604 return;
2605 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002606
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002607 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2608 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2609 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002610 return;
2611 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002612 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2613 if (MD->getResultType()->isVoidType()) {
2614 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2615 << Attr.getName() << 1;
2616 return;
2617 }
2618
Michael Han99315932013-01-24 16:46:58 +00002619 D->addAttr(::new (S.Context)
2620 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2621 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002622}
2623
Chandler Carruthedc2c642011-07-02 00:01:44 +00002624static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002625 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002626 if (Attr.hasParameterOrArguments()) {
2627 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628 return;
2629 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002630
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002631 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002632 if (isa<CXXRecordDecl>(D)) {
2633 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2634 return;
2635 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002636 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2637 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002638 return;
2639 }
2640
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002641 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002642
Michael Han99315932013-01-24 16:46:58 +00002643 nd->addAttr(::new (S.Context)
2644 WeakAttr(Attr.getRange(), S.Context,
2645 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002646}
2647
Chandler Carruthedc2c642011-07-02 00:01:44 +00002648static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002649 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002650 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002651 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002652
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002653
2654 // weak_import only applies to variable & function declarations.
2655 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002656 if (!D->canBeWeakImported(isDef)) {
2657 if (isDef)
2658 S.Diag(Attr.getLoc(),
2659 diag::warn_attribute_weak_import_invalid_on_definition)
2660 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002661 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002662 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002663 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002664 // Nothing to warn about here.
2665 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002667 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002668
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002669 return;
2670 }
2671
Michael Han99315932013-01-24 16:46:58 +00002672 D->addAttr(::new (S.Context)
2673 WeakImportAttr(Attr.getRange(), S.Context,
2674 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002675}
2676
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002677// Handles reqd_work_group_size and work_group_size_hint.
2678static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002679 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002680 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2681 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2682
Nate Begemanf2758702009-06-26 06:32:41 +00002683 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002684 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002685
2686 unsigned WGSize[3];
2687 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002688 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002689 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002690 if (E->isTypeDependent() || E->isValueDependent() ||
2691 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002692 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002693 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002694 return;
2695 }
2696 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2697 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002698
2699 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2700 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2701 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2702 if (!(A->getXDim() == WGSize[0] &&
2703 A->getYDim() == WGSize[1] &&
2704 A->getZDim() == WGSize[2])) {
2705 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2706 Attr.getName();
2707 }
2708 }
2709
2710 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2711 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2712 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2713 if (!(A->getXDim() == WGSize[0] &&
2714 A->getYDim() == WGSize[1] &&
2715 A->getZDim() == WGSize[2])) {
2716 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2717 Attr.getName();
2718 }
2719 }
2720
2721 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2722 D->addAttr(::new (S.Context)
2723 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002724 WGSize[0], WGSize[1], WGSize[2],
2725 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002726 else
2727 D->addAttr(::new (S.Context)
2728 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002729 WGSize[0], WGSize[1], WGSize[2],
2730 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002731}
2732
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002733SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002734 StringRef Name,
2735 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002736 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2737 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002738 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002739 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2740 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002741 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002742 }
Michael Han99315932013-01-24 16:46:58 +00002743 return ::new (Context) SectionAttr(Range, Context, Name,
2744 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002745}
2746
Chandler Carruthedc2c642011-07-02 00:01:44 +00002747static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002748 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002749 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002750 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002751
2752 // Make sure that there is a string literal as the sections's single
2753 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002754 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002755 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002756 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002757 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002758 return;
2759 }
Mike Stump11289f42009-09-09 15:08:12 +00002760
Chris Lattner30ba6742009-08-10 19:03:04 +00002761 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002762 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002763 if (!Error.empty()) {
2764 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2765 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002766 return;
2767 }
Mike Stump11289f42009-09-09 15:08:12 +00002768
Chris Lattner20aee9b2010-01-12 20:58:53 +00002769 // This attribute cannot be applied to local variables.
2770 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2771 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2772 return;
2773 }
Michael Han99315932013-01-24 16:46:58 +00002774
2775 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002776 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han99315932013-01-24 16:46:58 +00002777 SE->getString(), Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002778 if (NewAttr)
2779 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002780}
2781
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002782
Chandler Carruthedc2c642011-07-02 00:01:44 +00002783static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002784 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002785 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002786 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002787 return;
2788 }
Douglas Gregor88336832011-06-15 05:45:11 +00002789
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002790 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002791 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002792 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002793 } else {
Michael Han99315932013-01-24 16:46:58 +00002794 D->addAttr(::new (S.Context)
2795 NoThrowAttr(Attr.getRange(), S.Context,
2796 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002797 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002798}
2799
Chandler Carruthedc2c642011-07-02 00:01:44 +00002800static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002801 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002802 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002803 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002804 return;
2805 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002806
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002807 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002808 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002809 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002810 } else {
Michael Han99315932013-01-24 16:46:58 +00002811 D->addAttr(::new (S.Context)
2812 ConstAttr(Attr.getRange(), S.Context,
2813 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002814 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002815}
2816
Chandler Carruthedc2c642011-07-02 00:01:44 +00002817static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002818 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002819 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002820 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002821
Michael Han99315932013-01-24 16:46:58 +00002822 D->addAttr(::new (S.Context)
2823 PureAttr(Attr.getRange(), S.Context,
2824 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002825}
2826
Chandler Carruthedc2c642011-07-02 00:01:44 +00002827static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002828 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002829 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2830 return;
2831 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002832
Anders Carlssond277d792009-01-31 01:16:18 +00002833 if (Attr.getNumArgs() != 0) {
2834 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2835 return;
2836 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002837
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002838 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002839
Anders Carlssond277d792009-01-31 01:16:18 +00002840 if (!VD || !VD->hasLocalStorage()) {
2841 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2842 return;
2843 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002844
Anders Carlssond277d792009-01-31 01:16:18 +00002845 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002846 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002847 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002848 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2849 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002850 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002851 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002852 Attr.getParameterName();
2853 return;
2854 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002855
Anders Carlssond277d792009-01-31 01:16:18 +00002856 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2857 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002858 S.Diag(Attr.getParameterLoc(),
2859 diag::err_attribute_cleanup_arg_not_function)
2860 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002861 return;
2862 }
2863
Anders Carlssond277d792009-01-31 01:16:18 +00002864 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002865 S.Diag(Attr.getParameterLoc(),
2866 diag::err_attribute_cleanup_func_must_take_one_arg)
2867 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002868 return;
2869 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002870
Anders Carlsson723f55d2009-02-07 23:16:50 +00002871 // We're currently more strict than GCC about what function types we accept.
2872 // If this ever proves to be a problem it should be easy to fix.
2873 QualType Ty = S.Context.getPointerType(VD->getType());
2874 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002875 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2876 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002877 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002878 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2879 Attr.getParameterName() << ParamTy << Ty;
2880 return;
2881 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002882
Michael Han99315932013-01-24 16:46:58 +00002883 D->addAttr(::new (S.Context)
2884 CleanupAttr(Attr.getRange(), S.Context, FD,
2885 Attr.getAttributeSpellingListIndex()));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002886 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewyckya096b142013-02-12 08:08:54 +00002887 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssond277d792009-01-31 01:16:18 +00002888}
2889
Mike Stumpd3bb5572009-07-24 19:02:52 +00002890/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002891/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002892static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002893 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002894 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002895
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002896 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002897 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002898 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002899 return;
2900 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002901
2902 // In C++ the implicit 'this' function parameter also counts, and they are
2903 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002904 bool HasImplicitThisParam = isInstanceMethod(D);
2905 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002906 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002907
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002908 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002909 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002910 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002911 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2912 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002913 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2914 << "format" << 2 << IdxExpr->getSourceRange();
2915 return;
2916 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002917
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002918 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2919 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2920 << "format" << 2 << IdxExpr->getSourceRange();
2921 return;
2922 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002923
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002924 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002925
Chandler Carruth743682b2010-11-16 08:35:43 +00002926 if (HasImplicitThisParam) {
2927 if (ArgIdx == 0) {
2928 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2929 << "format_arg" << IdxExpr->getSourceRange();
2930 return;
2931 }
2932 ArgIdx--;
2933 }
2934
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002935 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002936 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002937
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002938 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2939 if (not_nsstring_type &&
2940 !isCFStringType(Ty, S.Context) &&
2941 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002942 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002943 // FIXME: Should highlight the actual expression that has the wrong type.
2944 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002945 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002946 << IdxExpr->getSourceRange();
2947 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002948 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002949 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002950 if (!isNSStringType(Ty, S.Context) &&
2951 !isCFStringType(Ty, S.Context) &&
2952 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002953 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002954 // FIXME: Should highlight the actual expression that has the wrong type.
2955 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002956 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002957 << IdxExpr->getSourceRange();
2958 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002959 }
2960
Michael Han99315932013-01-24 16:46:58 +00002961 D->addAttr(::new (S.Context)
2962 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
2963 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002964}
2965
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002966enum FormatAttrKind {
2967 CFStringFormat,
2968 NSStringFormat,
2969 StrftimeFormat,
2970 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002971 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002972 InvalidFormat
2973};
2974
2975/// getFormatAttrKind - Map from format attribute names to supported format
2976/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002977static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002978 return llvm::StringSwitch<FormatAttrKind>(Format)
2979 // Check for formats that get handled specially.
2980 .Case("NSString", NSStringFormat)
2981 .Case("CFString", CFStringFormat)
2982 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002983
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002984 // Otherwise, check for supported formats.
2985 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2986 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2987 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002988
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002989 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2990 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002991}
2992
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002993/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002994/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002995static void handleInitPriorityAttr(Sema &S, Decl *D,
2996 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002997 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002998 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2999 return;
3000 }
3001
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003002 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003003 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3004 Attr.setInvalid();
3005 return;
3006 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003007 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003008 if (S.Context.getAsArrayType(T))
3009 T = S.Context.getBaseElementType(T);
3010 if (!T->getAs<RecordType>()) {
3011 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3012 Attr.setInvalid();
3013 return;
3014 }
3015
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003016 if (Attr.getNumArgs() != 1) {
3017 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3018 Attr.setInvalid();
3019 return;
3020 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003021 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003022
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003023 llvm::APSInt priority(32);
3024 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3025 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3027 << "init_priority" << priorityExpr->getSourceRange();
3028 Attr.setInvalid();
3029 return;
3030 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00003031 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003032 if (prioritynum < 101 || prioritynum > 65535) {
3033 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3034 << priorityExpr->getSourceRange();
3035 Attr.setInvalid();
3036 return;
3037 }
Michael Han99315932013-01-24 16:46:58 +00003038 D->addAttr(::new (S.Context)
3039 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3040 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003041}
3042
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003043FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han99315932013-01-24 16:46:58 +00003044 int FormatIdx, int FirstArg,
3045 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003046 // Check whether we already have an equivalent format attribute.
3047 for (specific_attr_iterator<FormatAttr>
3048 i = D->specific_attr_begin<FormatAttr>(),
3049 e = D->specific_attr_end<FormatAttr>();
3050 i != e ; ++i) {
3051 FormatAttr *f = *i;
3052 if (f->getType() == Format &&
3053 f->getFormatIdx() == FormatIdx &&
3054 f->getFirstArg() == FirstArg) {
3055 // If we don't have a valid location for this attribute, adopt the
3056 // location.
3057 if (f->getLocation().isInvalid())
3058 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003059 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003060 }
3061 }
3062
Michael Han99315932013-01-24 16:46:58 +00003063 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3064 AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003065}
3066
Mike Stumpd3bb5572009-07-24 19:02:52 +00003067/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003068/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003069static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003070
Chris Lattner4a927cb2008-06-28 23:36:30 +00003071 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003072 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003073 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003074 return;
3075 }
3076
Chris Lattner4a927cb2008-06-28 23:36:30 +00003077 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003078 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003079 return;
3080 }
3081
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003082 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003083 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003084 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003085 return;
3086 }
3087
Chandler Carruth743682b2010-11-16 08:35:43 +00003088 // In C++ the implicit 'this' function parameter also counts, and they are
3089 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003090 bool HasImplicitThisParam = isInstanceMethod(D);
3091 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003092 unsigned FirstIdx = 1;
3093
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003094 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003095
3096 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003097 if (Format.startswith("__") && Format.endswith("__"))
3098 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003099
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003100 // Check for supported formats.
3101 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003102
3103 if (Kind == IgnoredFormat)
3104 return;
3105
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003106 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003107 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003108 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003109 return;
3110 }
3111
3112 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003113 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003114 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003115 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3116 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003117 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003118 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003119 return;
3120 }
3121
3122 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003123 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003124 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003125 return;
3126 }
3127
3128 // FIXME: Do we need to bounds check?
3129 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003130
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003131 if (HasImplicitThisParam) {
3132 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003133 S.Diag(Attr.getLoc(),
3134 diag::err_format_attribute_implicit_this_format_string)
3135 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003136 return;
3137 }
3138 ArgIdx--;
3139 }
Mike Stump11289f42009-09-09 15:08:12 +00003140
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003141 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003142 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003143
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003144 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003145 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003146 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3147 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003148 return;
3149 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003150 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003151 // FIXME: do we need to check if the type is NSString*? What are the
3152 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003153 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003154 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003155 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3156 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003157 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003158 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003159 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003160 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003161 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003162 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3163 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003164 return;
3165 }
3166
3167 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003168 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003169 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003170 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3171 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003172 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003173 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003174 return;
3175 }
3176
3177 // check if the function is variadic if the 3rd argument non-zero
3178 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003179 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003180 ++NumArgs; // +1 for ...
3181 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003182 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003183 return;
3184 }
3185 }
3186
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003187 // strftime requires FirstArg to be 0 because it doesn't read from any
3188 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003189 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003190 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003191 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3192 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003193 return;
3194 }
3195 // if 0 it disables parameter checking (to use with e.g. va_list)
3196 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003197 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003198 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003199 return;
3200 }
3201
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003202 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3203 Idx.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003204 FirstArg.getZExtValue(),
3205 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003206 if (NewAttr)
3207 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003208}
3209
Chandler Carruthedc2c642011-07-02 00:01:44 +00003210static void handleTransparentUnionAttr(Sema &S, Decl *D,
3211 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003212 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003213 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003214 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003215
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003216
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003217 // Try to find the underlying union declaration.
3218 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003219 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003220 if (TD && TD->getUnderlyingType()->isUnionType())
3221 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3222 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003223 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003224
3225 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003226 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003227 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003228 return;
3229 }
3230
John McCallf937c022011-10-07 06:10:15 +00003231 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003232 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003233 diag::warn_transparent_union_attribute_not_definition);
3234 return;
3235 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003236
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003237 RecordDecl::field_iterator Field = RD->field_begin(),
3238 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003239 if (Field == FieldEnd) {
3240 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3241 return;
3242 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003243
David Blaikie40ed2972012-06-06 20:45:41 +00003244 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003245 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003246 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003247 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003248 diag::warn_transparent_union_attribute_floating)
3249 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003250 return;
3251 }
3252
3253 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3254 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3255 for (; Field != FieldEnd; ++Field) {
3256 QualType FieldType = Field->getType();
3257 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3258 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3259 // Warn if we drop the attribute.
3260 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003261 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003262 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003263 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003264 diag::warn_transparent_union_attribute_field_size_align)
3265 << isSize << Field->getDeclName() << FieldBits;
3266 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003267 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003268 diag::note_transparent_union_first_field_size_align)
3269 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003270 return;
3271 }
3272 }
3273
Michael Han99315932013-01-24 16:46:58 +00003274 RD->addAttr(::new (S.Context)
3275 TransparentUnionAttr(Attr.getRange(), S.Context,
3276 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003277}
3278
Chandler Carruthedc2c642011-07-02 00:01:44 +00003279static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003280 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003281 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003282 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003283
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003284 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003285 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003286
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003287 // Make sure that there is a string literal as the annotation's single
3288 // argument.
3289 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003290 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003291 return;
3292 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003293
3294 // Don't duplicate annotations that are already set.
3295 for (specific_attr_iterator<AnnotateAttr>
3296 i = D->specific_attr_begin<AnnotateAttr>(),
3297 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3298 if ((*i)->getAnnotation() == SE->getString())
3299 return;
3300 }
Michael Han99315932013-01-24 16:46:58 +00003301
3302 D->addAttr(::new (S.Context)
3303 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3304 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003305}
3306
Chandler Carruthedc2c642011-07-02 00:01:44 +00003307static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003308 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003309 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003310 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003311 return;
3312 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003313
Richard Smith848e1f12013-02-01 08:12:08 +00003314 // FIXME: The C++11 version of this attribute should error out when it is
3315 // used to specify a weaker alignment, rather than being silently
3316 // ignored. This constraint cannot be applied until we have seen
3317 // all the attributes which apply to the variable.
3318
3319 if (Attr.getNumArgs() == 0) {
3320 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3321 true, 0, Attr.getAttributeSpellingListIndex()));
3322 return;
3323 }
3324
3325 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3326 Attr.getAttributeSpellingListIndex());
3327}
3328
3329void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3330 unsigned SpellingListIndex) {
3331 // FIXME: Handle pack-expansions here.
3332 if (DiagnoseUnexpandedParameterPack(E))
3333 return;
3334
3335 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3336 SourceLocation AttrLoc = AttrRange.getBegin();
3337
Richard Smith1dba27c2013-01-29 09:02:09 +00003338 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003339 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003340 // C++11 [dcl.align]p1:
3341 // An alignment-specifier may be applied to a variable or to a class
3342 // data member, but it shall not be applied to a bit-field, a function
3343 // parameter, the formal parameter of a catch clause, or a variable
3344 // declared with the register storage class specifier. An
3345 // alignment-specifier may also be applied to the declaration of a class
3346 // or enumeration type.
3347 // C11 6.7.5/2:
3348 // An alignment attribute shall not be specified in a declaration of
3349 // a typedef, or a bit-field, or a function, or a parameter, or an
3350 // object declared with the register storage-class specifier.
3351 int DiagKind = -1;
3352 if (isa<ParmVarDecl>(D)) {
3353 DiagKind = 0;
3354 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3355 if (VD->getStorageClass() == SC_Register)
3356 DiagKind = 1;
3357 if (VD->isExceptionVariable())
3358 DiagKind = 2;
3359 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3360 if (FD->isBitField())
3361 DiagKind = 3;
3362 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003363 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3364 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003365 << (TmpAttr.isC11() ? ExpectedVariableOrField
3366 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003367 return;
3368 }
3369 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003370 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
3371 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
3372 << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003373 return;
3374 }
3375 }
3376
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003377 if (E->isTypeDependent() || E->isValueDependent()) {
3378 // Save dependent expressions in the AST to be instantiated.
Richard Smith848e1f12013-02-01 08:12:08 +00003379 D->addAttr(::new (Context) AlignedAttr(TmpAttr));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003380 return;
3381 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003382
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003383 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003384 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003385 ExprResult ICE
3386 = VerifyIntegerConstantExpression(E, &Alignment,
3387 diag::err_aligned_attribute_argument_not_int,
3388 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003389 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003390 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003391
3392 // C++11 [dcl.align]p2:
3393 // -- if the constant expression evaluates to zero, the alignment
3394 // specifier shall have no effect
3395 // C11 6.7.5p6:
3396 // An alignment specification of zero has no effect.
3397 if (!(TmpAttr.isAlignas() && !Alignment) &&
3398 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003399 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3400 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003401 return;
3402 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003403
Richard Smith848e1f12013-02-01 08:12:08 +00003404 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003405 // We've already verified it's a power of 2, now let's make sure it's
3406 // 8192 or less.
3407 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003408 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003409 << E->getSourceRange();
3410 return;
3411 }
3412 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003413
Richard Smith848e1f12013-02-01 08:12:08 +00003414 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true,
3415 ICE.take(), SpellingListIndex));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003416}
3417
Michael Hanaf02bbe2013-02-01 01:19:17 +00003418void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3419 unsigned SpellingListIndex) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003420 // FIXME: Cache the number on the Attr object if non-dependent?
3421 // FIXME: Perform checking of type validity
Michael Hanaf02bbe2013-02-01 01:19:17 +00003422 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3423 SpellingListIndex));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003424 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003425}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003426
Richard Smith848e1f12013-02-01 08:12:08 +00003427void Sema::CheckAlignasUnderalignment(Decl *D) {
3428 assert(D->hasAttrs() && "no attributes on decl");
3429
3430 QualType Ty;
3431 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3432 Ty = VD->getType();
3433 else
3434 Ty = Context.getTagDeclType(cast<TagDecl>(D));
3435 if (Ty->isDependentType())
3436 return;
3437
3438 // C++11 [dcl.align]p5, C11 6.7.5/4:
3439 // The combined effect of all alignment attributes in a declaration shall
3440 // not specify an alignment that is less strict than the alignment that
3441 // would otherwise be required for the entity being declared.
3442 AlignedAttr *AlignasAttr = 0;
3443 unsigned Align = 0;
3444 for (specific_attr_iterator<AlignedAttr>
3445 I = D->specific_attr_begin<AlignedAttr>(),
3446 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3447 if (I->isAlignmentDependent())
3448 return;
3449 if (I->isAlignas())
3450 AlignasAttr = *I;
3451 Align = std::max(Align, I->getAlignment(Context));
3452 }
3453
3454 if (AlignasAttr && Align) {
3455 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3456 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3457 if (NaturalAlign > RequestedAlign)
3458 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3459 << Ty << (unsigned)NaturalAlign.getQuantity();
3460 }
3461}
3462
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003463/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003464/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003465///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003466/// Despite what would be logical, the mode attribute is a decl attribute, not a
3467/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3468/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003469static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003470 // This attribute isn't documented, but glibc uses it. It changes
3471 // the width of an int or unsigned int to the specified size.
3472
3473 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003474 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003475 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003476
Chris Lattneracbc2d22008-06-27 22:18:37 +00003477
3478 IdentifierInfo *Name = Attr.getParameterName();
3479 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003480 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003481 return;
3482 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003483
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003484 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003485
3486 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003487 if (Str.startswith("__") && Str.endswith("__"))
3488 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003489
3490 unsigned DestWidth = 0;
3491 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003492 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003493 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003494 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003495 switch (Str[0]) {
3496 case 'Q': DestWidth = 8; break;
3497 case 'H': DestWidth = 16; break;
3498 case 'S': DestWidth = 32; break;
3499 case 'D': DestWidth = 64; break;
3500 case 'X': DestWidth = 96; break;
3501 case 'T': DestWidth = 128; break;
3502 }
3503 if (Str[1] == 'F') {
3504 IntegerMode = false;
3505 } else if (Str[1] == 'C') {
3506 IntegerMode = false;
3507 ComplexMode = true;
3508 } else if (Str[1] != 'I') {
3509 DestWidth = 0;
3510 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003511 break;
3512 case 4:
3513 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3514 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003515 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003516 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003517 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003518 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003519 break;
3520 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003521 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003522 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003523 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003524 case 11:
3525 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003526 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003527 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003528 }
3529
3530 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003531 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003532 OldTy = TD->getUnderlyingType();
3533 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3534 OldTy = VD->getType();
3535 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003536 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003537 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003538 return;
3539 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003540
John McCall9dd450b2009-09-21 23:43:11 +00003541 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003542 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3543 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003544 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003545 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3546 } else if (ComplexMode) {
3547 if (!OldTy->isComplexType())
3548 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3549 } else {
3550 if (!OldTy->isFloatingType())
3551 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3552 }
3553
Mike Stump87c57ac2009-05-16 07:39:55 +00003554 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3555 // and friends, at least with glibc.
3556 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3557 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003558 // FIXME: Make sure floating-point mappings are accurate
3559 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003560 QualType NewTy;
3561 switch (DestWidth) {
3562 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003563 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003564 return;
3565 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003566 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003567 return;
3568 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003569 if (!IntegerMode) {
3570 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3571 return;
3572 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003573 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003574 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003575 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003576 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003577 break;
3578 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003579 if (!IntegerMode) {
3580 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3581 return;
3582 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003583 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003584 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003585 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003586 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003587 break;
3588 case 32:
3589 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003590 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003591 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003592 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003593 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003594 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003595 break;
3596 case 64:
3597 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003598 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003599 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003600 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003601 NewTy = S.Context.LongTy;
3602 else
3603 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003604 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003605 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003606 NewTy = S.Context.UnsignedLongTy;
3607 else
3608 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003609 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003610 case 96:
3611 NewTy = S.Context.LongDoubleTy;
3612 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003613 case 128:
3614 if (!IntegerMode) {
3615 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3616 return;
3617 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003618 if (OldTy->isSignedIntegerType())
3619 NewTy = S.Context.Int128Ty;
3620 else
3621 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003622 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003623 }
3624
Eli Friedman4735374e2009-03-03 06:41:03 +00003625 if (ComplexMode) {
3626 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003627 }
3628
3629 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003630 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003631 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003632 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003633 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003634 cast<ValueDecl>(D)->setType(NewTy);
3635}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003636
Chandler Carruthedc2c642011-07-02 00:01:44 +00003637static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003638 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003639 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003640 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003641
Nick Lewycky08597072012-07-24 01:40:49 +00003642 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3643 if (!VD->hasGlobalStorage())
3644 S.Diag(Attr.getLoc(),
3645 diag::warn_attribute_requires_functions_or_static_globals)
3646 << Attr.getName();
3647 } else if (!isFunctionOrMethod(D)) {
3648 S.Diag(Attr.getLoc(),
3649 diag::warn_attribute_requires_functions_or_static_globals)
3650 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003651 return;
3652 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003653
Michael Han99315932013-01-24 16:46:58 +00003654 D->addAttr(::new (S.Context)
3655 NoDebugAttr(Attr.getRange(), S.Context,
3656 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003657}
3658
Chandler Carruthedc2c642011-07-02 00:01:44 +00003659static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003660 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003661 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003662 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003663
Mike Stumpd3bb5572009-07-24 19:02:52 +00003664
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003665 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003667 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003668 return;
3669 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003670
Michael Han99315932013-01-24 16:46:58 +00003671 D->addAttr(::new (S.Context)
3672 NoInlineAttr(Attr.getRange(), S.Context,
3673 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003674}
3675
Chandler Carruthedc2c642011-07-02 00:01:44 +00003676static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3677 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003678 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003679 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003680 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003681
Chris Lattner3c77a352010-06-22 00:03:40 +00003682
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003683 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003684 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003685 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003686 return;
3687 }
3688
Michael Han99315932013-01-24 16:46:58 +00003689 D->addAttr(::new (S.Context)
3690 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3691 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003692}
3693
Chandler Carruthedc2c642011-07-02 00:01:44 +00003694static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003695 if (S.LangOpts.CUDA) {
3696 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003697 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003698 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3699 return;
3700 }
3701
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003702 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003703 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003704 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003705 return;
3706 }
3707
Michael Han99315932013-01-24 16:46:58 +00003708 D->addAttr(::new (S.Context)
3709 CUDAConstantAttr(Attr.getRange(), S.Context,
3710 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003711 } else {
3712 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3713 }
3714}
3715
Chandler Carruthedc2c642011-07-02 00:01:44 +00003716static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003717 if (S.LangOpts.CUDA) {
3718 // check the attribute arguments.
3719 if (Attr.getNumArgs() != 0) {
3720 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3721 return;
3722 }
3723
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003724 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003725 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003726 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003727 return;
3728 }
3729
Michael Han99315932013-01-24 16:46:58 +00003730 D->addAttr(::new (S.Context)
3731 CUDADeviceAttr(Attr.getRange(), S.Context,
3732 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003733 } else {
3734 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3735 }
3736}
3737
Chandler Carruthedc2c642011-07-02 00:01:44 +00003738static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003739 if (S.LangOpts.CUDA) {
3740 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003741 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003742 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003743
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003744 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003745 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003746 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003747 return;
3748 }
3749
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003750 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003751 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003752 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003753 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003754 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3755 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003756 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003757 "void");
3758 } else {
3759 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3760 << FD->getType();
3761 }
3762 return;
3763 }
3764
Michael Han99315932013-01-24 16:46:58 +00003765 D->addAttr(::new (S.Context)
3766 CUDAGlobalAttr(Attr.getRange(), S.Context,
3767 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003768 } else {
3769 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3770 }
3771}
3772
Chandler Carruthedc2c642011-07-02 00:01:44 +00003773static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003774 if (S.LangOpts.CUDA) {
3775 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003776 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003777 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003778
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003779
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003780 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003782 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003783 return;
3784 }
3785
Michael Han99315932013-01-24 16:46:58 +00003786 D->addAttr(::new (S.Context)
3787 CUDAHostAttr(Attr.getRange(), S.Context,
3788 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003789 } else {
3790 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3791 }
3792}
3793
Chandler Carruthedc2c642011-07-02 00:01:44 +00003794static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003795 if (S.LangOpts.CUDA) {
3796 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003797 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003798 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003799
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003800 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003801 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003802 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003803 return;
3804 }
3805
Michael Han99315932013-01-24 16:46:58 +00003806 D->addAttr(::new (S.Context)
3807 CUDASharedAttr(Attr.getRange(), S.Context,
3808 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003809 } else {
3810 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3811 }
3812}
3813
Chandler Carruthedc2c642011-07-02 00:01:44 +00003814static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003815 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003816 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003817 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003818
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003819 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003820 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003821 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003822 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003823 return;
3824 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003825
Douglas Gregor35b57532009-10-27 21:01:01 +00003826 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003827 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003828 return;
3829 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003830
Michael Han99315932013-01-24 16:46:58 +00003831 D->addAttr(::new (S.Context)
3832 GNUInlineAttr(Attr.getRange(), S.Context,
3833 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003834}
3835
Chandler Carruthedc2c642011-07-02 00:01:44 +00003836static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003837 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003838
Aaron Ballman02df2e02012-12-09 17:45:41 +00003839 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003840 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003841 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3842 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003843 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003844 return;
3845
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003846 if (!isa<ObjCMethodDecl>(D)) {
3847 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3848 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003849 return;
3850 }
3851
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003852 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003853 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003854 D->addAttr(::new (S.Context)
3855 FastCallAttr(Attr.getRange(), S.Context,
3856 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003857 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003858 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003859 D->addAttr(::new (S.Context)
3860 StdCallAttr(Attr.getRange(), S.Context,
3861 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003862 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003863 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003864 D->addAttr(::new (S.Context)
3865 ThisCallAttr(Attr.getRange(), S.Context,
3866 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003867 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003868 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003869 D->addAttr(::new (S.Context)
3870 CDeclAttr(Attr.getRange(), S.Context,
3871 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003872 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003873 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003874 D->addAttr(::new (S.Context)
3875 PascalAttr(Attr.getRange(), S.Context,
3876 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003877 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003878 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003879 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003880 switch (CC) {
3881 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003882 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003883 break;
3884 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003885 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003886 break;
3887 default:
3888 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003889 }
3890
Michael Han99315932013-01-24 16:46:58 +00003891 D->addAttr(::new (S.Context)
3892 PcsAttr(Attr.getRange(), S.Context, PCS,
3893 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003894 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003895 }
Derek Schuffa2020962012-10-16 22:30:41 +00003896 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003897 D->addAttr(::new (S.Context)
3898 PnaclCallAttr(Attr.getRange(), S.Context,
3899 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003900 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003901 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003902 D->addAttr(::new (S.Context)
3903 IntelOclBiccAttr(Attr.getRange(), S.Context,
3904 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003905 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003906
Abramo Bagnara50099372010-04-30 13:10:51 +00003907 default:
3908 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003909 }
3910}
3911
Chandler Carruthedc2c642011-07-02 00:01:44 +00003912static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003913 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003914 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003915}
3916
Aaron Ballman02df2e02012-12-09 17:45:41 +00003917bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3918 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003919 if (attr.isInvalid())
3920 return true;
3921
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003922 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3923 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3924 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00003925 attr.setInvalid();
3926 return true;
3927 }
3928
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003929 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3930 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003931 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003932 case AttributeList::AT_CDecl: CC = CC_C; break;
3933 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3934 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3935 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3936 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3937 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003938 Expr *Arg = attr.getArg(0);
3939 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003940 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003941 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3942 << "pcs" << 1;
3943 attr.setInvalid();
3944 return true;
3945 }
3946
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003947 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003948 if (StrRef == "aapcs") {
3949 CC = CC_AAPCS;
3950 break;
3951 } else if (StrRef == "aapcs-vfp") {
3952 CC = CC_AAPCS_VFP;
3953 break;
3954 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003955
3956 attr.setInvalid();
3957 Diag(attr.getLoc(), diag::err_invalid_pcs);
3958 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003959 }
Derek Schuffa2020962012-10-16 22:30:41 +00003960 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003961 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003962 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003963 }
3964
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003965 const TargetInfo &TI = Context.getTargetInfo();
3966 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3967 if (A == TargetInfo::CCCR_Warning) {
3968 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003969
3970 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3971 if (FD)
3972 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3973 TargetInfo::CCMT_NonMember;
3974 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003975 }
3976
John McCall3882ace2011-01-05 12:14:39 +00003977 return false;
3978}
3979
Chandler Carruthedc2c642011-07-02 00:01:44 +00003980static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003981 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003982
3983 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003984 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003985 return;
3986
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003987 if (!isa<ObjCMethodDecl>(D)) {
3988 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3989 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003990 return;
3991 }
Eli Friedman7044b762009-03-27 21:06:47 +00003992
Michael Han99315932013-01-24 16:46:58 +00003993 D->addAttr(::new (S.Context)
3994 RegparmAttr(Attr.getRange(), S.Context, numParams,
3995 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003996}
3997
3998/// Checks a regparm attribute, returning true if it is ill-formed and
3999/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004000bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4001 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004002 return true;
4003
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004004 if (Attr.getNumArgs() != 1) {
4005 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4006 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004007 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004008 }
Eli Friedman7044b762009-03-27 21:06:47 +00004009
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004010 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00004011 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00004012 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00004013 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004014 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00004015 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004016 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004017 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004018 }
4019
Douglas Gregore8bbc122011-09-02 00:18:52 +00004020 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004021 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004022 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004023 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004024 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004025 }
4026
John McCall3882ace2011-01-05 12:14:39 +00004027 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00004028 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004029 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004030 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004031 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004032 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004033 }
4034
John McCall3882ace2011-01-05 12:14:39 +00004035 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004036}
4037
Chandler Carruthedc2c642011-07-02 00:01:44 +00004038static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004039 if (S.LangOpts.CUDA) {
4040 // check the attribute arguments.
4041 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004042 // FIXME: 0 is not okay.
4043 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004044 return;
4045 }
4046
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004047 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004048 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004049 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004050 return;
4051 }
4052
4053 Expr *MaxThreadsExpr = Attr.getArg(0);
4054 llvm::APSInt MaxThreads(32);
4055 if (MaxThreadsExpr->isTypeDependent() ||
4056 MaxThreadsExpr->isValueDependent() ||
4057 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4058 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4059 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4060 return;
4061 }
4062
4063 llvm::APSInt MinBlocks(32);
4064 if (Attr.getNumArgs() > 1) {
4065 Expr *MinBlocksExpr = Attr.getArg(1);
4066 if (MinBlocksExpr->isTypeDependent() ||
4067 MinBlocksExpr->isValueDependent() ||
4068 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4069 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4070 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4071 return;
4072 }
4073 }
4074
Michael Han99315932013-01-24 16:46:58 +00004075 D->addAttr(::new (S.Context)
4076 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4077 MaxThreads.getZExtValue(),
4078 MinBlocks.getZExtValue(),
4079 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004080 } else {
4081 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4082 }
4083}
4084
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004085static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4086 const AttributeList &Attr) {
4087 StringRef AttrName = Attr.getName()->getName();
4088 if (!Attr.getParameterName()) {
4089 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4090 << Attr.getName() << /* arg num = */ 1;
4091 return;
4092 }
4093
4094 if (Attr.getNumArgs() != 2) {
4095 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4096 << /* required args = */ 3;
4097 return;
4098 }
4099
4100 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4101
4102 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4103 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4104 << Attr.getName() << ExpectedFunctionOrMethod;
4105 return;
4106 }
4107
4108 uint64_t ArgumentIdx;
4109 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4110 Attr.getLoc(), 2,
4111 Attr.getArg(0), ArgumentIdx))
4112 return;
4113
4114 uint64_t TypeTagIdx;
4115 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4116 Attr.getLoc(), 3,
4117 Attr.getArg(1), TypeTagIdx))
4118 return;
4119
4120 bool IsPointer = (AttrName == "pointer_with_type_tag");
4121 if (IsPointer) {
4122 // Ensure that buffer has a pointer type.
4123 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4124 if (!BufferTy->isPointerType()) {
4125 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
4126 << AttrName;
4127 }
4128 }
4129
Michael Han99315932013-01-24 16:46:58 +00004130 D->addAttr(::new (S.Context)
4131 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4132 ArgumentIdx, TypeTagIdx, IsPointer,
4133 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004134}
4135
4136static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4137 const AttributeList &Attr) {
4138 IdentifierInfo *PointerKind = Attr.getParameterName();
4139 if (!PointerKind) {
4140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4141 << "type_tag_for_datatype" << 1;
4142 return;
4143 }
4144
4145 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4146
Michael Han99315932013-01-24 16:46:58 +00004147 D->addAttr(::new (S.Context)
4148 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4149 MatchingCType,
4150 Attr.getLayoutCompatible(),
4151 Attr.getMustBeNull(),
4152 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004153}
4154
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004155//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004156// Checker-specific attribute handlers.
4157//===----------------------------------------------------------------------===//
4158
John McCalled433932011-01-25 03:31:58 +00004159static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004160 return type->isDependentType() ||
4161 type->isObjCObjectPointerType() ||
4162 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004163}
4164static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004165 return type->isDependentType() ||
4166 type->isPointerType() ||
4167 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004168}
4169
Chandler Carruthedc2c642011-07-02 00:01:44 +00004170static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004171 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004172 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004173 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004174 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004175 return;
4176 }
4177
4178 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004179 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004180 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4181 cf = false;
4182 } else {
4183 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4184 cf = true;
4185 }
4186
4187 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004188 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004189 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004190 return;
4191 }
4192
4193 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004194 param->addAttr(::new (S.Context)
4195 CFConsumedAttr(Attr.getRange(), S.Context,
4196 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004197 else
Michael Han99315932013-01-24 16:46:58 +00004198 param->addAttr(::new (S.Context)
4199 NSConsumedAttr(Attr.getRange(), S.Context,
4200 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004201}
4202
Chandler Carruthedc2c642011-07-02 00:01:44 +00004203static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4204 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004205 if (!isa<ObjCMethodDecl>(D)) {
4206 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004207 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004208 return;
4209 }
4210
Michael Han99315932013-01-24 16:46:58 +00004211 D->addAttr(::new (S.Context)
4212 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4213 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004214}
4215
Chandler Carruthedc2c642011-07-02 00:01:44 +00004216static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4217 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004218
John McCalled433932011-01-25 03:31:58 +00004219 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004220
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004221 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004222 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004223 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004224 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004225 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004226 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4227 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004228 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004229 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004230 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004231 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004232 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004233 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004234 return;
4235 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004236
John McCalled433932011-01-25 03:31:58 +00004237 bool typeOK;
4238 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004239 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004240 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004241 case AttributeList::AT_NSReturnsAutoreleased:
4242 case AttributeList::AT_NSReturnsRetained:
4243 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004244 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4245 cf = false;
4246 break;
4247
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004248 case AttributeList::AT_CFReturnsRetained:
4249 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004250 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4251 cf = true;
4252 break;
4253 }
4254
4255 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004256 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004257 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004258 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004259 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004260
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004261 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004262 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004263 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004264 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004265 D->addAttr(::new (S.Context)
4266 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4267 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004268 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004269 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004270 D->addAttr(::new (S.Context)
4271 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4272 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004273 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004274 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004275 D->addAttr(::new (S.Context)
4276 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4277 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004278 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004279 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004280 D->addAttr(::new (S.Context)
4281 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4282 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004283 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004284 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004285 D->addAttr(::new (S.Context)
4286 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4287 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004288 return;
4289 };
4290}
4291
John McCallcf166702011-07-22 08:53:00 +00004292static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4293 const AttributeList &attr) {
4294 SourceLocation loc = attr.getLoc();
4295
4296 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4297
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004298 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00004299 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004300 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00004301 return;
4302 }
4303
4304 // Check that the method returns a normal pointer.
4305 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004306
4307 if (!resultType->isReferenceType() &&
4308 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00004309 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4310 << SourceRange(loc)
4311 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4312
4313 // Drop the attribute.
4314 return;
4315 }
4316
Michael Han99315932013-01-24 16:46:58 +00004317 method->addAttr(::new (S.Context)
4318 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4319 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004320}
4321
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004322static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4323 const AttributeList &attr) {
4324 SourceLocation loc = attr.getLoc();
4325 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4326
4327 if (!method) {
4328 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4329 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4330 return;
4331 }
4332 DeclContext *DC = method->getDeclContext();
4333 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4334 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4335 << attr.getName() << 0;
4336 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4337 return;
4338 }
4339 if (method->getMethodFamily() == OMF_dealloc) {
4340 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4341 << attr.getName() << 1;
4342 return;
4343 }
4344
Michael Han99315932013-01-24 16:46:58 +00004345 method->addAttr(::new (S.Context)
4346 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4347 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004348}
4349
John McCall32f5fe12011-09-30 05:12:12 +00004350/// Handle cf_audited_transfer and cf_unknown_transfer.
4351static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4352 if (!isa<FunctionDecl>(D)) {
4353 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004354 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004355 return;
4356 }
4357
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004358 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004359
4360 // Check whether there's a conflicting attribute already present.
4361 Attr *Existing;
4362 if (IsAudited) {
4363 Existing = D->getAttr<CFUnknownTransferAttr>();
4364 } else {
4365 Existing = D->getAttr<CFAuditedTransferAttr>();
4366 }
4367 if (Existing) {
4368 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4369 << A.getName()
4370 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4371 << A.getRange() << Existing->getRange();
4372 return;
4373 }
4374
4375 // All clear; add the attribute.
4376 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004377 D->addAttr(::new (S.Context)
4378 CFAuditedTransferAttr(A.getRange(), S.Context,
4379 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004380 } else {
Michael Han99315932013-01-24 16:46:58 +00004381 D->addAttr(::new (S.Context)
4382 CFUnknownTransferAttr(A.getRange(), S.Context,
4383 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004384 }
4385}
4386
John McCallf1e8b342011-09-29 07:17:38 +00004387static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4388 const AttributeList &Attr) {
4389 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4390 if (!RD || RD->isUnion()) {
4391 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004392 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004393 }
4394
4395 IdentifierInfo *ParmName = Attr.getParameterName();
4396
4397 // In Objective-C, verify that the type names an Objective-C type.
4398 // We don't want to check this outside of ObjC because people sometimes
4399 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004400 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004401 // Check for an existing type with this name.
4402 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4403 Sema::LookupOrdinaryName);
4404 if (S.LookupName(R, Sc)) {
4405 NamedDecl *Target = R.getFoundDecl();
4406 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4407 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4408 S.Diag(Target->getLocStart(), diag::note_declared_at);
4409 }
4410 }
4411 }
4412
Michael Han99315932013-01-24 16:46:58 +00004413 D->addAttr(::new (S.Context)
4414 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4415 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004416}
4417
Chandler Carruthedc2c642011-07-02 00:01:44 +00004418static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4419 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004420 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004421
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004422 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004423 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004424}
4425
Chandler Carruthedc2c642011-07-02 00:01:44 +00004426static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4427 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004428 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004429 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004430 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004431 return;
4432 }
4433
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004434 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004435 QualType type = vd->getType();
4436
4437 if (!type->isDependentType() &&
4438 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004439 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004440 << type;
4441 return;
4442 }
4443
4444 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4445
4446 // If we have no lifetime yet, check the lifetime we're presumably
4447 // going to infer.
4448 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4449 lifetime = type->getObjCARCImplicitLifetime();
4450
4451 switch (lifetime) {
4452 case Qualifiers::OCL_None:
4453 assert(type->isDependentType() &&
4454 "didn't infer lifetime for non-dependent type?");
4455 break;
4456
4457 case Qualifiers::OCL_Weak: // meaningful
4458 case Qualifiers::OCL_Strong: // meaningful
4459 break;
4460
4461 case Qualifiers::OCL_ExplicitNone:
4462 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004463 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004464 << (lifetime == Qualifiers::OCL_Autoreleasing);
4465 break;
4466 }
4467
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004468 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004469 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4470 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004471}
4472
Francois Picheta83957a2010-12-19 06:50:37 +00004473//===----------------------------------------------------------------------===//
4474// Microsoft specific attribute handlers.
4475//===----------------------------------------------------------------------===//
4476
Chandler Carruthedc2c642011-07-02 00:01:44 +00004477static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00004478 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00004479 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004480 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00004481 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004482
Francois Picheta83957a2010-12-19 06:50:37 +00004483 Expr *Arg = Attr.getArg(0);
4484 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004485 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00004486 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4487 << "uuid" << 1;
4488 return;
4489 }
4490
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004491 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00004492
4493 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4494 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004495
Francois Pichet7da11662010-12-20 01:41:49 +00004496 // Validate GUID length.
4497 if (IsCurly && StrRef.size() != 38) {
4498 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4499 return;
4500 }
4501 if (!IsCurly && StrRef.size() != 36) {
4502 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4503 return;
4504 }
4505
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004506 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00004507 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004508 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00004509 if (IsCurly) // Skip the optional '{'
4510 ++I;
4511
4512 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00004513 if (i == 8 || i == 13 || i == 18 || i == 23) {
4514 if (*I != '-') {
4515 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4516 return;
4517 }
Jordan Rosea7d03842013-02-08 22:30:41 +00004518 } else if (!isHexDigit(*I)) {
Francois Pichet7da11662010-12-20 01:41:49 +00004519 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4520 return;
4521 }
4522 I++;
4523 }
Francois Picheta83957a2010-12-19 06:50:37 +00004524
Michael Han99315932013-01-24 16:46:58 +00004525 D->addAttr(::new (S.Context)
4526 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4527 Attr.getAttributeSpellingListIndex()));
Francois Pichet7da11662010-12-20 01:41:49 +00004528 } else
Francois Picheta83957a2010-12-19 06:50:37 +00004529 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00004530}
4531
John McCall8d32c052012-05-22 21:28:12 +00004532static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weberefa45b22012-11-07 21:31:36 +00004533 if (!S.LangOpts.MicrosoftExt) {
John McCall8d32c052012-05-22 21:28:12 +00004534 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weberefa45b22012-11-07 21:31:36 +00004535 return;
4536 }
4537
4538 AttributeList::Kind Kind = Attr.getKind();
4539 if (Kind == AttributeList::AT_SingleInheritance)
4540 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004541 ::new (S.Context)
4542 SingleInheritanceAttr(Attr.getRange(), S.Context,
4543 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004544 else if (Kind == AttributeList::AT_MultipleInheritance)
4545 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004546 ::new (S.Context)
4547 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4548 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004549 else if (Kind == AttributeList::AT_VirtualInheritance)
4550 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004551 ::new (S.Context)
4552 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4553 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004554}
4555
4556static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4557 if (S.LangOpts.MicrosoftExt) {
4558 AttributeList::Kind Kind = Attr.getKind();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004559 if (Kind == AttributeList::AT_Ptr32)
John McCall8d32c052012-05-22 21:28:12 +00004560 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004561 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context,
4562 Attr.getAttributeSpellingListIndex()));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004563 else if (Kind == AttributeList::AT_Ptr64)
John McCall8d32c052012-05-22 21:28:12 +00004564 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004565 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context,
4566 Attr.getAttributeSpellingListIndex()));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004567 else if (Kind == AttributeList::AT_Win64)
John McCall8d32c052012-05-22 21:28:12 +00004568 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004569 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4570 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004571 } else
4572 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4573}
4574
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004575static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4576 if (S.LangOpts.MicrosoftExt)
Michael Han99315932013-01-24 16:46:58 +00004577 D->addAttr(::new (S.Context)
4578 ForceInlineAttr(Attr.getRange(), S.Context,
4579 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004580 else
4581 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4582}
4583
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004584//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004585// Top Level Sema Entry Points
4586//===----------------------------------------------------------------------===//
4587
Chandler Carruthedc2c642011-07-02 00:01:44 +00004588static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4589 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004590 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004591 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4592 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4593 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004594 default:
4595 break;
4596 }
4597}
Abramo Bagnara50099372010-04-30 13:10:51 +00004598
Chandler Carruthedc2c642011-07-02 00:01:44 +00004599static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4600 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004601 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004602 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4603 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4604 case AttributeList::AT_IBOutletCollection:
4605 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004606 case AttributeList::AT_AddressSpace:
4607 case AttributeList::AT_OpenCLImageAccess:
4608 case AttributeList::AT_ObjCGC:
4609 case AttributeList::AT_VectorSize:
4610 case AttributeList::AT_NeonVectorType:
4611 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004612 // Ignore these, these are type attributes, handled by
4613 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004614 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004615 case AttributeList::AT_CUDADevice:
4616 case AttributeList::AT_CUDAHost:
4617 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004618 // Ignore, this is a non-inheritable attribute, handled
4619 // by ProcessNonInheritableDeclAttr.
4620 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004621 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4622 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4623 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4624 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004625 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004626 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004627 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004628 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004629 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4630 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4631 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004632 handleDependencyAttr(S, scope, D, Attr);
4633 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004634 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4635 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4636 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004637 case AttributeList::AT_CXX11NoReturn:
4638 handleCXX11NoReturnAttr(S, D, Attr);
4639 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004640 case AttributeList::AT_Deprecated:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004641 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4642 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004643 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4644 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004645 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004646 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004647 case AttributeList::AT_MinSize:
4648 handleMinSizeAttr(S, D, Attr);
4649 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004650 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4651 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4652 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4653 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4654 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004655 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004656 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004657 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4658 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4659 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4660 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4661 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004662 case AttributeList::AT_ownership_returns:
4663 case AttributeList::AT_ownership_takes:
4664 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004665 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004666 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4667 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4668 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4669 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4670 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4671 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4672 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004673
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004674 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004675 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004676 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004677 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004678
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004679 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004680 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4681
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004682 case AttributeList::AT_ObjCRequiresSuper:
4683 handleObjCRequiresSuperAttr(S, D, Attr); break;
4684
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004685 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004686 handleNSBridgedAttr(S, scope, D, Attr); break;
4687
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004688 case AttributeList::AT_CFAuditedTransfer:
4689 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004690 handleCFTransferAttr(S, D, Attr); break;
4691
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004692 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004693 case AttributeList::AT_CFConsumed:
4694 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4695 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004696 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004697
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004698 case AttributeList::AT_NSReturnsAutoreleased:
4699 case AttributeList::AT_NSReturnsNotRetained:
4700 case AttributeList::AT_CFReturnsNotRetained:
4701 case AttributeList::AT_NSReturnsRetained:
4702 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004703 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004704
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004705 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004706 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004707 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004708
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004709 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004710 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004711
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004712 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4713 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4714 case AttributeList::AT_Unavailable:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004715 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4716 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004717 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004718 handleArcWeakrefUnavailableAttr (S, D, Attr);
4719 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004720 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004721 handleObjCRootClassAttr(S, D, Attr);
4722 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004723 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004724 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004725 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004726 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4727 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004728 handleReturnsTwiceAttr(S, D, Attr);
4729 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004730 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004731 case AttributeList::AT_Visibility:
4732 handleVisibilityAttr(S, D, Attr, false);
4733 break;
4734 case AttributeList::AT_TypeVisibility:
4735 handleVisibilityAttr(S, D, Attr, true);
4736 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004737 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004738 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004739 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4740 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4741 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4742 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004743 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004744 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004745 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004746 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004747 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004748 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004749 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004750 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004751 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4752 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4753 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4754 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4755 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4756 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4757 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4758 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4759 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004760 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004761 // Just ignore
4762 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004763 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004764 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004765 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004766 case AttributeList::AT_StdCall:
4767 case AttributeList::AT_CDecl:
4768 case AttributeList::AT_FastCall:
4769 case AttributeList::AT_ThisCall:
4770 case AttributeList::AT_Pascal:
4771 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004772 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004773 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004774 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004775 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004776 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004777 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004778 break;
John McCall8d32c052012-05-22 21:28:12 +00004779
4780 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004781 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004782 handleMsStructAttr(S, D, Attr);
4783 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004784 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004785 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004786 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004787 case AttributeList::AT_SingleInheritance:
4788 case AttributeList::AT_MultipleInheritance:
4789 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004790 handleInheritanceAttr(S, D, Attr);
4791 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004792 case AttributeList::AT_Win64:
4793 case AttributeList::AT_Ptr32:
4794 case AttributeList::AT_Ptr64:
John McCall8d32c052012-05-22 21:28:12 +00004795 handlePortabilityAttr(S, D, Attr);
4796 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004797 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004798 handleForceInlineAttr(S, D, Attr);
4799 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004800
4801 // Thread safety attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004802 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004803 handleGuardedVarAttr(S, D, Attr);
4804 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004805 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004806 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004807 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004808 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004809 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004810 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004811 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004812 handleNoAddressSafetyAttr(S, D, Attr);
4813 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004814 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004815 handleNoThreadSafetyAttr(S, D, Attr);
4816 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004817 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004818 handleLockableAttr(S, D, Attr);
4819 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004820 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004821 handleGuardedByAttr(S, D, Attr);
4822 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004823 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004824 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004825 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004826 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004827 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004828 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004829 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004830 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004831 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004832 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004833 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004834 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004835 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004836 handleLockReturnedAttr(S, D, Attr);
4837 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004838 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004839 handleLocksExcludedAttr(S, D, Attr);
4840 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004841 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004842 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004843 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004844 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004845 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004846 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004847 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004848 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004849 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004850 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004851 handleUnlockFunAttr(S, D, Attr);
4852 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004853 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004854 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004855 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004856 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004857 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004858 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004859
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004860 // Type safety attributes.
4861 case AttributeList::AT_ArgumentWithTypeTag:
4862 handleArgumentWithTypeTagAttr(S, D, Attr);
4863 break;
4864 case AttributeList::AT_TypeTagForDatatype:
4865 handleTypeTagForDatatypeAttr(S, D, Attr);
4866 break;
4867
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004868 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004869 // Ask target about the attribute.
4870 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4871 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004872 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4873 diag::warn_unhandled_ms_attribute_ignored :
4874 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004875 break;
4876 }
4877}
4878
Peter Collingbourneb331b262011-01-21 02:08:45 +00004879/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4880/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smith10876ef2013-01-17 01:30:42 +00004881/// silently ignore it if a GNU attribute.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004882static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4883 const AttributeList &Attr,
Richard Smith10876ef2013-01-17 01:30:42 +00004884 bool NonInheritable, bool Inheritable,
4885 bool IncludeCXX11Attributes) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004886 if (Attr.isInvalid())
4887 return;
4888
Richard Smith10876ef2013-01-17 01:30:42 +00004889 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4890 // instead.
4891 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4892 return;
4893
Peter Collingbourneb331b262011-01-21 02:08:45 +00004894 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004895 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004896
4897 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004898 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004899}
4900
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004901/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4902/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004903void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004904 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004905 bool NonInheritable, bool Inheritable,
4906 bool IncludeCXX11Attributes) {
4907 for (const AttributeList* l = AttrList; l; l = l->getNext())
4908 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
4909 IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004910
4911 // GCC accepts
4912 // static int a9 __attribute__((weakref));
4913 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004914 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004915 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004916 cast<NamedDecl>(D)->getNameAsString();
4917 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004918 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004919 }
4920}
4921
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004922// Annotation attributes are the only attributes allowed after an access
4923// specifier.
4924bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4925 const AttributeList *AttrList) {
4926 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004927 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004928 handleAnnotateAttr(*this, ASDecl, *l);
4929 } else {
4930 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4931 return true;
4932 }
4933 }
4934
4935 return false;
4936}
4937
John McCall42856de2011-10-01 05:17:03 +00004938/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4939/// contains any decl attributes that we should warn about.
4940static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4941 for ( ; A; A = A->getNext()) {
4942 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004943 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004944 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4945
4946 if (A->getKind() == AttributeList::UnknownAttribute) {
4947 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4948 << A->getName() << A->getRange();
4949 } else {
4950 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4951 << A->getName() << A->getRange();
4952 }
4953 }
4954}
4955
4956/// checkUnusedDeclAttributes - Given a declarator which is not being
4957/// used to build a declaration, complain about any decl attributes
4958/// which might be lying around on it.
4959void Sema::checkUnusedDeclAttributes(Declarator &D) {
4960 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4961 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4962 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4963 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4964}
4965
Ryan Flynn7d470f32009-07-30 03:15:39 +00004966/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004967/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004968NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4969 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004970 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004971 NamedDecl *NewD = 0;
4972 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004973 FunctionDecl *NewFD;
4974 // FIXME: Missing call to CheckFunctionDeclaration().
4975 // FIXME: Mangling?
4976 // FIXME: Is the qualifier info correct?
4977 // FIXME: Is the DeclContext correct?
4978 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4979 Loc, Loc, DeclarationName(II),
4980 FD->getType(), FD->getTypeSourceInfo(),
4981 SC_None, SC_None,
4982 false/*isInlineSpecified*/,
4983 FD->hasPrototype(),
4984 false/*isConstexprSpecified*/);
4985 NewD = NewFD;
4986
4987 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004988 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004989
4990 // Fake up parameter variables; they are declared as if this were
4991 // a typedef.
4992 QualType FDTy = FD->getType();
4993 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4994 SmallVector<ParmVarDecl*, 16> Params;
4995 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4996 AE = FT->arg_type_end(); AI != AE; ++AI) {
4997 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4998 Param->setScopeInfo(0, Params.size());
4999 Params.push_back(Param);
5000 }
David Blaikie9c70e042011-09-21 18:16:56 +00005001 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005002 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005003 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5004 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005005 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005006 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00005007 VD->getStorageClass(),
5008 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00005009 if (VD->getQualifier()) {
5010 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005011 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005012 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005013 }
5014 return NewD;
5015}
5016
James Dennett634962f2012-06-14 21:40:34 +00005017/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005018/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005019void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005020 if (W.getUsed()) return; // only do this once
5021 W.setUsed(true);
5022 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5023 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005024 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005025 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5026 NDId->getName()));
5027 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005028 WeakTopLevelDecl.push_back(NewD);
5029 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5030 // to insert Decl at TU scope, sorry.
5031 DeclContext *SavedContext = CurContext;
5032 CurContext = Context.getTranslationUnitDecl();
5033 PushOnScopeChains(NewD, S);
5034 CurContext = SavedContext;
5035 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005036 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005037 }
5038}
5039
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005040/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5041/// it, apply them to D. This is a bit tricky because PD can have attributes
5042/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005043void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5044 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00005045 // It's valid to "forward-declare" #pragma weak, in which case we
5046 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00005047 if (Inheritable) {
5048 LoadExternalWeakUndeclaredIdentifiers();
5049 if (!WeakUndeclaredIdentifiers.empty()) {
5050 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
5051 if (IdentifierInfo *Id = ND->getIdentifier()) {
5052 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5053 = WeakUndeclaredIdentifiers.find(Id);
5054 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
5055 WeakInfo W = I->second;
5056 DeclApplyPragmaWeak(S, ND, W);
5057 WeakUndeclaredIdentifiers[Id] = W;
5058 }
John McCall6fe02402010-10-27 00:59:00 +00005059 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005060 }
5061 }
5062 }
5063
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005064 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005065 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005066 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005067
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005068 // Walk the declarator structure, applying decl attributes that were in a type
5069 // position to the decl itself. This handles cases like:
5070 // int *__attr__(x)** D;
5071 // when X is a decl attribute.
5072 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5073 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith10876ef2013-01-17 01:30:42 +00005074 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5075 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005076
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005077 // Finally, apply any attributes on the decl itself.
5078 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005079 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005080}
John McCall28a6aea2009-11-04 02:18:39 +00005081
John McCall31168b02011-06-15 23:02:42 +00005082/// Is the given declaration allowed to use a forbidden type?
5083static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5084 // Private ivars are always okay. Unfortunately, people don't
5085 // always properly make their ivars private, even in system headers.
5086 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005087 // Function declarations in sys headers will be marked unavailable.
5088 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5089 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005090 return false;
5091
5092 // Require it to be declared in a system header.
5093 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5094}
5095
5096/// Handle a delayed forbidden-type diagnostic.
5097static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5098 Decl *decl) {
5099 if (decl && isForbiddenTypeAllowed(S, decl)) {
5100 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5101 "this system declaration uses an unsupported type"));
5102 return;
5103 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005104 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005105 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005106 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005107 // kind of forbidden type messages on unavailable functions.
5108 if (FD->hasAttr<UnavailableAttr>() &&
5109 diag.getForbiddenTypeDiagnostic() ==
5110 diag::err_arc_array_param_no_ownership) {
5111 diag.Triggered = true;
5112 return;
5113 }
5114 }
John McCall31168b02011-06-15 23:02:42 +00005115
5116 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5117 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5118 diag.Triggered = true;
5119}
5120
John McCall2ec85372012-05-07 06:16:41 +00005121void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5122 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005123 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005124 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005125
John McCall2ec85372012-05-07 06:16:41 +00005126 // When delaying diagnostics to run in the context of a parsed
5127 // declaration, we only want to actually emit anything if parsing
5128 // succeeds.
5129 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005130
John McCall2ec85372012-05-07 06:16:41 +00005131 // We emit all the active diagnostics in this pool or any of its
5132 // parents. In general, we'll get one pool for the decl spec
5133 // and a child pool for each declarator; in a decl group like:
5134 // deprecated_typedef foo, *bar, baz();
5135 // only the declarator pops will be passed decls. This is correct;
5136 // we really do need to consider delayed diagnostics from the decl spec
5137 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005138 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005139 do {
John McCall6347b682012-05-07 06:16:58 +00005140 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005141 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5142 // This const_cast is a bit lame. Really, Triggered should be mutable.
5143 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005144 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005145 continue;
5146
John McCallc1465822011-02-14 07:13:47 +00005147 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005148 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005149 // Don't bother giving deprecation diagnostics if the decl is invalid.
5150 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005151 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005152 break;
5153
5154 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005155 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005156 break;
John McCall31168b02011-06-15 23:02:42 +00005157
5158 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005159 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005160 break;
John McCall86121512010-01-27 03:50:35 +00005161 }
5162 }
John McCall2ec85372012-05-07 06:16:41 +00005163 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005164}
5165
John McCall6347b682012-05-07 06:16:58 +00005166/// Given a set of delayed diagnostics, re-emit them as if they had
5167/// been delayed in the current context instead of in the given pool.
5168/// Essentially, this just moves them to the current pool.
5169void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5170 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5171 assert(curPool && "re-emitting in undelayed context not supported");
5172 curPool->steal(pool);
5173}
5174
John McCall28a6aea2009-11-04 02:18:39 +00005175static bool isDeclDeprecated(Decl *D) {
5176 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005177 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005178 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005179 // A category implicitly has the availability of the interface.
5180 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5181 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005182 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5183 return false;
5184}
5185
Eli Friedman971bfa12012-08-08 21:52:41 +00005186static void
5187DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5188 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005189 const ObjCInterfaceDecl *UnknownObjCClass,
5190 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005191 DeclarationName Name = D->getDeclName();
5192 if (!Message.empty()) {
5193 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5194 S.Diag(D->getLocation(),
5195 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5196 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005197 if (ObjCPropery)
5198 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5199 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005200 } else if (!UnknownObjCClass) {
5201 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5202 S.Diag(D->getLocation(),
5203 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5204 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005205 if (ObjCPropery)
5206 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5207 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005208 } else {
5209 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5210 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5211 }
5212}
5213
John McCallb45a1e72010-08-26 02:13:20 +00005214void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005215 Decl *Ctx) {
5216 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005217 return;
5218
John McCall86121512010-01-27 03:50:35 +00005219 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005220 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5221 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005222 DD.getUnknownObjCClass(),
5223 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005224}
5225
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005226void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005227 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005228 const ObjCInterfaceDecl *UnknownObjCClass,
5229 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005230 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005231 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005232 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5233 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005234 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005235 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005236 return;
5237 }
5238
5239 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005240 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005241 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005242 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005243}