blob: 8d01d638d385015dcd8c453d706f56905d70d79d [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Rafael Espindolad7a60ad2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1a343e22013-09-13 15:35:43 +000026#include "clang/Lex/Preprocessor.h"
John McCall19510852010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000028#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000029#include "clang/Sema/Lookup.h"
Richard Smith3a2b7a12013-01-28 22:42:45 +000030#include "clang/Sema/Scope.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000031#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000032using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000033using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000034
John McCall883cc2c2011-03-02 12:29:23 +000035/// These constants match the enumerated choices of
36/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000037enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000038 ExpectedFunction,
39 ExpectedUnion,
40 ExpectedVariableOrFunction,
41 ExpectedFunctionOrMethod,
42 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000043 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000044 ExpectedFunctionMethodOrClass,
John McCall883cc2c2011-03-02 12:29:23 +000045 ExpectedFunctionMethodOrParameter,
46 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000047 ExpectedVariable,
48 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000049 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000050 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000051 ExpectedStruct,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000052 ExpectedVariableFunctionOrTag,
Richard Smith5f838aa2013-02-01 08:25:07 +000053 ExpectedTLSVar,
54 ExpectedVariableOrField,
John McCalld4c3d662013-02-20 01:54:26 +000055 ExpectedVariableFieldOrTag,
Aaron Ballman37a89532013-07-18 14:56:42 +000056 ExpectedTypeOrNamespace,
57 ExpectedObjectiveCInterface
John McCall883cc2c2011-03-02 12:29:23 +000058};
59
Chris Lattnere5c5ee12008-06-29 00:16:31 +000060//===----------------------------------------------------------------------===//
61// Helper functions
62//===----------------------------------------------------------------------===//
63
Chandler Carruth87c44602011-07-01 23:49:12 +000064static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000065 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000066 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000067 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000068 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000069 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000070 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000071 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000072 Ty = decl->getUnderlyingType();
73 else
74 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000075
Chris Lattner6b6b5372008-06-26 18:38:35 +000076 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000077 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000078 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000079 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000080
John McCall183700f2009-09-21 23:43:11 +000081 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000082}
83
Daniel Dunbar35682492008-09-26 04:12:28 +000084// FIXME: We should provide an abstraction around a method or function
85// to provide the following bits of information.
86
Nuno Lopesd20254f2009-12-20 23:11:08 +000087/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000088/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000089static bool isFunction(const Decl *D) {
90 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000091}
92
93/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000094/// type (function or function-typed variable) or an Objective-C
95/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000096static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000097 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000098}
99
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000100/// isFunctionOrMethodOrBlock - Return true if the given decl has function
101/// type (function or function-typed variable) or an Objective-C
102/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +0000103static bool isFunctionOrMethodOrBlock(const Decl *D) {
104 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000105 return true;
106 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000107 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000108 QualType Ty = V->getType();
109 return Ty->isBlockPointerType();
110 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000111 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000112}
113
John McCall711c52b2011-01-05 12:14:39 +0000114/// Return true if the given decl has a declarator that should have
115/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000116static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000117 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000118 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
119 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000120}
121
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000122/// hasFunctionProto - Return true if the given decl has a argument
123/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000124/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000125static bool hasFunctionProto(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000127 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000128 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000129 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000130 return true;
131 }
132}
133
134/// getFunctionOrMethodNumArgs - Return number of function or method
135/// arguments. It is an error to call this on a K&R function (use
136/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000137static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
138 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000139 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000140 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000141 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000142 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000143}
144
Chandler Carruth87c44602011-07-01 23:49:12 +0000145static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
146 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000147 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000148 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000149 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000150
Chandler Carruth87c44602011-07-01 23:49:12 +0000151 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000152}
153
Chandler Carruth87c44602011-07-01 23:49:12 +0000154static QualType getFunctionOrMethodResultType(const Decl *D) {
155 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000156 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000157 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000158}
159
Chandler Carruth87c44602011-07-01 23:49:12 +0000160static bool isFunctionOrMethodVariadic(const Decl *D) {
161 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000162 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000163 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000164 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000165 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000166 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000167 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000168 }
169}
170
Chandler Carruth87c44602011-07-01 23:49:12 +0000171static bool isInstanceMethod(const Decl *D) {
172 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000173 return MethodDecl->isInstance();
174 return false;
175}
176
Chris Lattner6b6b5372008-06-26 18:38:35 +0000177static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000178 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000179 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000180 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000181
John McCall506b57e2010-05-17 21:00:27 +0000182 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
183 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000184 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000185
John McCall506b57e2010-05-17 21:00:27 +0000186 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000187
Chris Lattner6b6b5372008-06-26 18:38:35 +0000188 // FIXME: Should we walk the chain of classes?
189 return ClsName == &Ctx.Idents.get("NSString") ||
190 ClsName == &Ctx.Idents.get("NSMutableString");
191}
192
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000193static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000194 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000195 if (!PT)
196 return false;
197
Ted Kremenek6217b802009-07-29 21:53:49 +0000198 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000199 if (!RT)
200 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000201
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000202 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000203 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000204 return false;
205
206 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
207}
208
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000209/// \brief Check if the attribute has exactly as many args as Num. May
210/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000211static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
212 unsigned int Num) {
213 if (Attr.getNumArgs() != Num) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000214 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
215 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000216 return false;
217 }
218
219 return true;
220}
221
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000222
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000223/// \brief Check if the attribute has at least as many args as Num. May
224/// output an error.
225static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
226 unsigned int Num) {
227 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000228 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
229 return false;
230 }
231
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000232 return true;
233}
234
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000235/// \brief Check if IdxExpr is a valid argument index for a function or
236/// instance method D. May output an error.
237///
238/// \returns true if IdxExpr is a valid index.
239static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
240 StringRef AttrName,
241 SourceLocation AttrLoc,
242 unsigned AttrArgNum,
243 const Expr *IdxExpr,
244 uint64_t &Idx)
245{
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000246 assert(isFunctionOrMethod(D));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000247
248 // In C++ the implicit 'this' function parameter also counts.
249 // Parameters are counted from one.
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000250 bool HP = hasFunctionProto(D);
251 bool HasImplicitThisParam = isInstanceMethod(D);
252 bool IV = HP && isFunctionOrMethodVariadic(D);
253 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
254 HasImplicitThisParam;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000255
256 llvm::APSInt IdxInt;
257 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
258 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000259 std::string Name = std::string("'") + AttrName.str() + std::string("'");
260 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000261 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000262 return false;
263 }
264
265 Idx = IdxInt.getLimitedValue();
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000266 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000267 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
268 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
269 return false;
270 }
271 Idx--; // Convert to zero-based.
272 if (HasImplicitThisParam) {
273 if (Idx == 0) {
274 S.Diag(AttrLoc,
275 diag::err_attribute_invalid_implicit_this_argument)
276 << AttrName << IdxExpr->getSourceRange();
277 return false;
278 }
279 --Idx;
280 }
281
282 return true;
283}
284
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000285///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000286/// \brief Check if passed in Decl is a field or potentially shared global var
287/// \return true if the Decl is a field or potentially shared global variable
288///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000289static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000290 if (isa<FieldDecl>(D))
291 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000292 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000293 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000294
295 return false;
296}
297
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000298/// \brief Check if the passed-in expression is of type int or bool.
299static bool isIntOrBool(Expr *Exp) {
300 QualType QT = Exp->getType();
301 return QT->isBooleanType() || QT->isIntegerType();
302}
303
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000304
305// Check to see if the type is a smart pointer of some kind. We assume
306// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000307static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
308 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
309 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000310 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000311 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000312
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000313 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
314 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000315 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000316 return false;
317
318 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000319}
320
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000321/// \brief Check if passed in Decl is a pointer type.
322/// Note that this function may produce an error message.
323/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000324static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
325 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000326 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000327 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000328 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000329 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000330
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000331 if (const RecordType *RT = QT->getAs<RecordType>()) {
332 // If it's an incomplete type, it could be a smart pointer; skip it.
333 // (We don't want to force template instantiation if we can avoid it,
334 // since that would alter the order in which templates are instantiated.)
335 if (RT->isIncompleteType())
336 return true;
337
338 if (threadSafetyCheckIsSmartPointer(S, RT))
339 return true;
340 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000341
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000342 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000343 << Attr.getName()->getName() << QT;
344 } else {
345 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
346 << Attr.getName();
347 }
348 return false;
349}
350
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000351/// \brief Checks that the passed in QualType either is of RecordType or points
352/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000353static const RecordType *getRecordType(QualType QT) {
354 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000355 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000356
357 // Now check if we point to record type.
358 if (const PointerType *PT = QT->getAs<PointerType>())
359 return PT->getPointeeType()->getAs<RecordType>();
360
361 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000362}
363
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000364
Jordy Rosefad5de92012-05-08 03:27:22 +0000365static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
366 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000367 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
368 if (RT->getDecl()->getAttr<LockableAttr>())
369 return true;
370 return false;
371}
372
373
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000374/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000375/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000376static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
377 QualType Ty) {
378 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000379
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000380 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000381 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000382 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000383 << Attr.getName() << Ty.getAsString();
384 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000385 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000386
Michael Hanf1aae3b2012-08-03 17:40:43 +0000387 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000388 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000389 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000390
391 // Allow smart pointers to be used as lockable objects.
392 // FIXME -- Check the type that the smart pointer points to.
393 if (threadSafetyCheckIsSmartPointer(S, RT))
394 return;
395
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000396 // Check if the type is lockable.
397 RecordDecl *RD = RT->getDecl();
398 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000399 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000400
401 // Else check if any base classes are lockable.
402 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
403 CXXBasePaths BPaths(false, false);
404 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
405 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000406 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000407
408 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
409 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000410}
411
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000412/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000413/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000414/// \param Sidx The attribute argument index to start checking with.
415/// \param ParamIdxOk Whether an argument can be indexing into a function
416/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000417static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000418 const AttributeList &Attr,
419 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000420 int Sidx = 0,
421 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000422 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000423 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000424
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000425 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000426 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000427 Args.push_back(ArgExp);
428 continue;
429 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000430
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000431 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000432 if (StrLit->getLength() == 0 ||
Benjamin Kramerf37e4f22013-09-13 16:30:12 +0000433 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000434 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000435 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000436 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000437 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000438 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000439
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000440 // We allow constant strings to be used as a placeholder for expressions
441 // that are not valid C++ syntax, but warn that they are ignored.
442 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
443 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000444 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000445 continue;
446 }
447
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000448 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000449
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000450 // A pointer to member expression of the form &MyClass::mu is treated
451 // specially -- we need to look at the type of the member.
452 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
453 if (UOp->getOpcode() == UO_AddrOf)
454 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
455 if (DRE->getDecl()->isCXXInstanceMember())
456 ArgTy = DRE->getDecl()->getType();
457
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000458 // First see if we can just cast to record type, or point to record type.
459 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000460
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000461 // Now check if we index into a record type function param.
462 if(!RT && ParamIdxOk) {
463 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000464 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
465 if(FD && IL) {
466 unsigned int NumParams = FD->getNumParams();
467 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000468 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
469 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
470 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000471 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
472 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000473 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000474 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000475 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000476 }
477 }
478
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000479 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000480
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000481 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000482 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000483}
484
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000485//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000486// Attribute Implementations
487//===----------------------------------------------------------------------===//
488
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000489// FIXME: All this manual attribute parsing code is gross. At the
490// least add some helper functions to check most argument patterns (#
491// and types of args).
492
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000493enum ThreadAttributeDeclKind {
494 ThreadExpectedFieldOrGlobalVar,
495 ThreadExpectedFunctionOrMethod,
496 ThreadExpectedClassOrStruct
497};
498
Michael Hanf1aae3b2012-08-03 17:40:43 +0000499static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000500 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000501 // D must be either a member field or global (potentially shared) variable.
502 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000503 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
504 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000505 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000506 }
507
Michael Handc691572012-07-23 18:48:41 +0000508 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000509}
510
Michael Handc691572012-07-23 18:48:41 +0000511static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
512 if (!checkGuardedVarAttrCommon(S, D, Attr))
513 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000514
Michael Han51d8c522013-01-24 16:46:58 +0000515 D->addAttr(::new (S.Context)
516 GuardedVarAttr(Attr.getRange(), S.Context,
517 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000518}
519
Michael Hanf1aae3b2012-08-03 17:40:43 +0000520static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000521 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000522 if (!checkGuardedVarAttrCommon(S, D, Attr))
523 return;
524
525 if (!threadSafetyCheckIsPointer(S, D, Attr))
526 return;
527
Michael Han51d8c522013-01-24 16:46:58 +0000528 D->addAttr(::new (S.Context)
529 PtGuardedVarAttr(Attr.getRange(), S.Context,
530 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000531}
532
Michael Hanf1aae3b2012-08-03 17:40:43 +0000533static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
534 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000535 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000536 // D must be either a member field or global (potentially shared) variable.
537 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000538 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
539 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000540 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000541 }
542
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000543 SmallVector<Expr*, 1> Args;
544 // check that all arguments are lockable objects
545 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
546 unsigned Size = Args.size();
547 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000548 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000549
Michael Handc691572012-07-23 18:48:41 +0000550 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000551
Michael Handc691572012-07-23 18:48:41 +0000552 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000553}
554
Michael Handc691572012-07-23 18:48:41 +0000555static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
556 Expr *Arg = 0;
557 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
558 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000559
Michael Handc691572012-07-23 18:48:41 +0000560 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
561}
562
Michael Hanf1aae3b2012-08-03 17:40:43 +0000563static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000564 const AttributeList &Attr) {
565 Expr *Arg = 0;
566 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
567 return;
568
569 if (!threadSafetyCheckIsPointer(S, D, Attr))
570 return;
571
572 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
573 S.Context, Arg));
574}
575
Michael Hanf1aae3b2012-08-03 17:40:43 +0000576static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000577 const AttributeList &Attr) {
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000578 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000579 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000580 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
581 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000582 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000583 }
584
Michael Handc691572012-07-23 18:48:41 +0000585 return true;
586}
587
588static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
589 if (!checkLockableAttrCommon(S, D, Attr))
590 return;
591
592 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
593}
594
Michael Hanf1aae3b2012-08-03 17:40:43 +0000595static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000596 const AttributeList &Attr) {
597 if (!checkLockableAttrCommon(S, D, Attr))
598 return;
599
Michael Han51d8c522013-01-24 16:46:58 +0000600 D->addAttr(::new (S.Context)
601 ScopedLockableAttr(Attr.getRange(), S.Context,
602 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000603}
604
Kostya Serebryany85aee962013-02-26 06:58:27 +0000605static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
606 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000607 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000608 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
609 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000610 return;
611 }
612
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000613 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000614 S.Context));
615}
616
Kostya Serebryany85aee962013-02-26 06:58:27 +0000617static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000618 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000619 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000621 << Attr.getName() << ExpectedFunctionOrMethod;
622 return;
623 }
624
Michael Han51d8c522013-01-24 16:46:58 +0000625 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000626 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
627 Attr.getAttributeSpellingListIndex()));
628}
629
630static void handleNoSanitizeMemory(Sema &S, Decl *D,
631 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000632 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
633 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
634 << Attr.getName() << ExpectedFunctionOrMethod;
635 return;
636 }
637
638 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
639 S.Context));
640}
641
642static void handleNoSanitizeThread(Sema &S, Decl *D,
643 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000644 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
646 << Attr.getName() << ExpectedFunctionOrMethod;
647 return;
648 }
649
650 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
651 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000652}
653
Michael Hanf1aae3b2012-08-03 17:40:43 +0000654static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
655 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000656 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000657 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000658 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000659
660 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000661 ValueDecl *VD = dyn_cast<ValueDecl>(D);
662 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000663 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
664 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000665 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000666 }
667
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000668 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000669 QualType QT = VD->getType();
670 if (!QT->isDependentType()) {
671 const RecordType *RT = getRecordType(QT);
672 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000673 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000674 << Attr.getName();
675 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000676 }
677 }
678
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000679 // Check that all arguments are lockable objects.
680 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000681 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000682 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000683
Michael Handc691572012-07-23 18:48:41 +0000684 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000685}
686
Michael Hanf1aae3b2012-08-03 17:40:43 +0000687static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000688 const AttributeList &Attr) {
689 SmallVector<Expr*, 1> Args;
690 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
691 return;
692
693 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000694 D->addAttr(::new (S.Context)
695 AcquiredAfterAttr(Attr.getRange(), S.Context,
696 StartArg, Args.size(),
697 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000698}
699
Michael Hanf1aae3b2012-08-03 17:40:43 +0000700static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000701 const AttributeList &Attr) {
702 SmallVector<Expr*, 1> Args;
703 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
704 return;
705
706 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000707 D->addAttr(::new (S.Context)
708 AcquiredBeforeAttr(Attr.getRange(), S.Context,
709 StartArg, Args.size(),
710 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000711}
712
Michael Hanf1aae3b2012-08-03 17:40:43 +0000713static bool checkLockFunAttrCommon(Sema &S, Decl *D,
714 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000715 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000716 // zero or more arguments ok
717
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000718 // check that the attribute is applied to a function
719 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000720 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
721 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000722 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000723 }
724
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000725 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000726 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000727
Michael Handc691572012-07-23 18:48:41 +0000728 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000729}
730
Michael Hanf1aae3b2012-08-03 17:40:43 +0000731static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000732 const AttributeList &Attr) {
733 SmallVector<Expr*, 1> Args;
734 if (!checkLockFunAttrCommon(S, D, Attr, Args))
735 return;
736
737 unsigned Size = Args.size();
738 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000739 D->addAttr(::new (S.Context)
740 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
741 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000742}
743
Michael Hanf1aae3b2012-08-03 17:40:43 +0000744static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000745 const AttributeList &Attr) {
746 SmallVector<Expr*, 1> Args;
747 if (!checkLockFunAttrCommon(S, D, Attr, Args))
748 return;
749
750 unsigned Size = Args.size();
751 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000752 D->addAttr(::new (S.Context)
753 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
754 StartArg, Size,
755 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000756}
757
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000758static void handleAssertSharedLockAttr(Sema &S, Decl *D,
759 const AttributeList &Attr) {
760 SmallVector<Expr*, 1> Args;
761 if (!checkLockFunAttrCommon(S, D, Attr, Args))
762 return;
763
764 unsigned Size = Args.size();
765 Expr **StartArg = Size == 0 ? 0 : &Args[0];
766 D->addAttr(::new (S.Context)
767 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
768 Attr.getAttributeSpellingListIndex()));
769}
770
771static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
772 const AttributeList &Attr) {
773 SmallVector<Expr*, 1> Args;
774 if (!checkLockFunAttrCommon(S, D, Attr, Args))
775 return;
776
777 unsigned Size = Args.size();
778 Expr **StartArg = Size == 0 ? 0 : &Args[0];
779 D->addAttr(::new (S.Context)
780 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
781 StartArg, Size,
782 Attr.getAttributeSpellingListIndex()));
783}
784
785
Michael Hanf1aae3b2012-08-03 17:40:43 +0000786static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
787 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000788 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000789 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000790 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000791
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000792 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000793 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
794 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000795 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000796 }
797
Aaron Ballman624421f2013-08-31 01:11:41 +0000798 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000799 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000800 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000801 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000802 }
803
804 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000805 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000806
Michael Handc691572012-07-23 18:48:41 +0000807 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000808}
809
Michael Hanf1aae3b2012-08-03 17:40:43 +0000810static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000811 const AttributeList &Attr) {
812 SmallVector<Expr*, 2> Args;
813 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
814 return;
815
Michael Han51d8c522013-01-24 16:46:58 +0000816 D->addAttr(::new (S.Context)
817 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000818 Attr.getArgAsExpr(0),
819 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000820 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000821}
822
Michael Hanf1aae3b2012-08-03 17:40:43 +0000823static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000824 const AttributeList &Attr) {
825 SmallVector<Expr*, 2> Args;
826 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
827 return;
828
Michael Han51d8c522013-01-24 16:46:58 +0000829 D->addAttr(::new (S.Context)
830 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000831 Attr.getArgAsExpr(0),
832 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000833 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000834}
835
Michael Hanf1aae3b2012-08-03 17:40:43 +0000836static bool checkLocksRequiredCommon(Sema &S, Decl *D,
837 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000838 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000839 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000840 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000841
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000842 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000843 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
844 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000845 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000846 }
847
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000848 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000849 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000850 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000851 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000852
Michael Handc691572012-07-23 18:48:41 +0000853 return true;
854}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000855
Michael Hanf1aae3b2012-08-03 17:40:43 +0000856static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000857 const AttributeList &Attr) {
858 SmallVector<Expr*, 1> Args;
859 if (!checkLocksRequiredCommon(S, D, Attr, Args))
860 return;
861
862 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000863 D->addAttr(::new (S.Context)
864 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
865 StartArg, Args.size(),
866 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000867}
868
Michael Hanf1aae3b2012-08-03 17:40:43 +0000869static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000870 const AttributeList &Attr) {
871 SmallVector<Expr*, 1> Args;
872 if (!checkLocksRequiredCommon(S, D, Attr, Args))
873 return;
874
875 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000876 D->addAttr(::new (S.Context)
877 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
878 StartArg, Args.size(),
879 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000880}
881
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000882static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000883 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000884 // zero or more arguments ok
885
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000887 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
888 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000889 return;
890 }
891
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000892 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000893 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000894 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000895 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000896 Expr **StartArg = Size == 0 ? 0 : &Args[0];
897
Michael Han51d8c522013-01-24 16:46:58 +0000898 D->addAttr(::new (S.Context)
899 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
900 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000901}
902
903static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000904 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000905 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000906 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
907 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000908 return;
909 }
910
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000911 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000912 SmallVector<Expr*, 1> Args;
913 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
914 unsigned Size = Args.size();
915 if (Size == 0)
916 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000917
Michael Han51d8c522013-01-24 16:46:58 +0000918 D->addAttr(::new (S.Context)
919 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
920 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000921}
922
923static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000924 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000925 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000926 return;
927
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000928 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000929 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
930 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000931 return;
932 }
933
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000934 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000935 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000936 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000937 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000938 if (Size == 0)
939 return;
940 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000941
Michael Han51d8c522013-01-24 16:46:58 +0000942 D->addAttr(::new (S.Context)
943 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
944 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000945}
946
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000947static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikiea33ab602013-09-06 01:28:43 +0000948 ConsumableAttr::ConsumedState DefaultState;
949
950 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +0000951 IdentifierLoc *IL = Attr.getArgAsIdent(0);
952 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
953 DefaultState)) {
954 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
955 << Attr.getName() << IL->Ident;
David Blaikiea33ab602013-09-06 01:28:43 +0000956 return;
957 }
David Blaikiea33ab602013-09-06 01:28:43 +0000958 } else {
959 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
960 << Attr.getName() << AANT_ArgumentIdentifier;
961 return;
962 }
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000963
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000964 if (!isa<CXXRecordDecl>(D)) {
965 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
966 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000967 return;
968 }
969
970 D->addAttr(::new (S.Context)
David Blaikiea33ab602013-09-06 01:28:43 +0000971 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000972 Attr.getAttributeSpellingListIndex()));
973}
974
975static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
976 const AttributeList &Attr) {
977 ASTContext &CurrContext = S.getASTContext();
978 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
979
980 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
981 if (!RD->hasAttr<ConsumableAttr>()) {
982 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
983 RD->getNameAsString();
984
985 return false;
986 }
987 }
988
989 return true;
990}
991
992static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000993 if (!isa<CXXMethodDecl>(D)) {
994 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
995 Attr.getName() << ExpectedMethod;
996 return;
997 }
998
999 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1000 return;
1001
1002 D->addAttr(::new (S.Context)
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001003 ConsumesAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001004 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001005}
1006
1007static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D,
1008 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001009 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001010 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1011 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001012 return;
1013 }
1014
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001015 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1016 return;
1017
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001018 D->addAttr(::new (S.Context)
1019 CallableWhenUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001020 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001021}
1022
1023static void handleTestsConsumedAttr(Sema &S, Decl *D,
1024 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001025 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001026 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1027 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001028 return;
1029 }
1030
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001031 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1032 return;
1033
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001034 D->addAttr(::new (S.Context)
1035 TestsConsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001036 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001037}
1038
1039static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
1040 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001041 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001042 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1043 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001044 return;
1045 }
1046
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001047 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1048 return;
1049
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001050 D->addAttr(::new (S.Context)
1051 TestsUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001052 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001053}
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001054
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001055static void handleReturnTypestateAttr(Sema &S, Decl *D,
1056 const AttributeList &Attr) {
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001057 ReturnTypestateAttr::ConsumedState ReturnState;
1058
1059 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +00001060 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1061 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1062 ReturnState)) {
1063 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1064 << Attr.getName() << IL->Ident;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001065 return;
1066 }
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001067 } else {
1068 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1069 Attr.getName() << AANT_ArgumentIdentifier;
1070 return;
1071 }
1072
1073 if (!isa<FunctionDecl>(D)) {
1074 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1075 Attr.getName() << ExpectedFunction;
1076 return;
1077 }
1078
1079 // FIXME: This check is currently being done in the analysis. It can be
1080 // enabled here only after the parser propagates attributes at
1081 // template specialization definition, not declaration.
1082 //QualType ReturnType;
1083 //
1084 //if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1085 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1086 //
1087 //} else {
1088 //
1089 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1090 //}
1091 //
1092 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1093 //
1094 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1095 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1096 // ReturnType.getAsString();
1097 // return;
1098 //}
1099
1100 D->addAttr(::new (S.Context)
1101 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1102 Attr.getAttributeSpellingListIndex()));
1103}
1104
Chandler Carruth1b03c872011-07-02 00:01:44 +00001105static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1106 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001107 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1108 if (TD == 0) {
1109 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1110 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001111 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001112 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001113 }
Mike Stumpbf916502009-07-24 19:02:52 +00001114
Richard Smitha4fa9002013-01-13 02:11:23 +00001115 // Remember this typedef decl, we will need it later for diagnostics.
1116 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001117}
1118
Chandler Carruth1b03c872011-07-02 00:01:44 +00001119static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001120 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001121 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001122 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001123 // If the alignment is less than or equal to 8 bits, the packed attribute
1124 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001125 if (!FD->getType()->isDependentType() &&
1126 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001127 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001128 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001129 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001130 else
Michael Han51d8c522013-01-24 16:46:58 +00001131 FD->addAttr(::new (S.Context)
1132 PackedAttr(Attr.getRange(), S.Context,
1133 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001134 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001135 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001136}
1137
Chandler Carruth1b03c872011-07-02 00:01:44 +00001138static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001139 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001140 RD->addAttr(::new (S.Context)
1141 MsStructAttr(Attr.getRange(), S.Context,
1142 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001143 else
1144 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1145}
1146
Chandler Carruth1b03c872011-07-02 00:01:44 +00001147static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001148 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001149 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001150 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001151 D->addAttr(::new (S.Context)
1152 IBActionAttr(Attr.getRange(), S.Context,
1153 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001154 return;
1155 }
1156
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001157 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001158}
1159
Ted Kremenek2f041d02011-09-29 07:02:25 +00001160static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1161 // The IBOutlet/IBOutletCollection attributes only apply to instance
1162 // variables or properties of Objective-C classes. The outlet must also
1163 // have an object reference type.
1164 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1165 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001166 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001167 << Attr.getName() << VD->getType() << 0;
1168 return false;
1169 }
1170 }
1171 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1172 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001173 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001174 << Attr.getName() << PD->getType() << 1;
1175 return false;
1176 }
1177 }
1178 else {
1179 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1180 return false;
1181 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001182
Ted Kremenek2f041d02011-09-29 07:02:25 +00001183 return true;
1184}
1185
Chandler Carruth1b03c872011-07-02 00:01:44 +00001186static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek2f041d02011-09-29 07:02:25 +00001187 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001188 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001189
Michael Han51d8c522013-01-24 16:46:58 +00001190 D->addAttr(::new (S.Context)
1191 IBOutletAttr(Attr.getRange(), S.Context,
1192 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001193}
1194
Chandler Carruth1b03c872011-07-02 00:01:44 +00001195static void handleIBOutletCollection(Sema &S, Decl *D,
1196 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001197
1198 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001199 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001200 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1201 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001202 return;
1203 }
1204
Ted Kremenek2f041d02011-09-29 07:02:25 +00001205 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001206 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001207
Aaron Ballman624421f2013-08-31 01:11:41 +00001208 IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
1209 IdentifierInfo *II;
1210 SourceLocation ILS;
1211 if (IL) {
1212 II = IL->Ident;
1213 ILS = IL->Loc;
1214 } else {
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001215 II = &S.Context.Idents.get("NSObject");
Aaron Ballman624421f2013-08-31 01:11:41 +00001216 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001217
John McCallb3d87482010-08-24 05:47:05 +00001218 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001219 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001220 if (!TypeRep) {
1221 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1222 return;
1223 }
John McCallb3d87482010-08-24 05:47:05 +00001224 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001225 // Diagnose use of non-object type in iboutletcollection attribute.
1226 // FIXME. Gnu attribute extension ignores use of builtin types in
1227 // attributes. So, __attribute__((iboutletcollection(char))) will be
1228 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001229 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001230 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1231 return;
1232 }
Michael Han51d8c522013-01-24 16:46:58 +00001233 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00001234 IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS,
Michael Han51d8c522013-01-24 16:46:58 +00001235 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001236}
1237
Chandler Carruthd309c812011-07-01 23:49:16 +00001238static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001239 if (const RecordType *UT = T->getAsUnionType())
1240 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1241 RecordDecl *UD = UT->getDecl();
1242 for (RecordDecl::field_iterator it = UD->field_begin(),
1243 itend = UD->field_end(); it != itend; ++it) {
1244 QualType QT = it->getType();
1245 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1246 T = QT;
1247 return;
1248 }
1249 }
1250 }
1251}
1252
Nuno Lopes587de5b2012-05-24 00:22:00 +00001253static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001254 if (!isFunctionOrMethod(D)) {
1255 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001256 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001257 return;
1258 }
1259
Nuno Lopes587de5b2012-05-24 00:22:00 +00001260 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1261 return;
1262
Nuno Lopes587de5b2012-05-24 00:22:00 +00001263 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001264 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001265 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001266 uint64_t Idx;
1267 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1268 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001269 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001270
1271 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001272 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001273 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001274 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1275 << Attr.getName() << AANT_ArgumentIntegerConstant
1276 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001277 return;
1278 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001279 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001280 }
1281
1282 // check if the function returns a pointer
1283 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1284 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001285 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001286 }
1287
Michael Han51d8c522013-01-24 16:46:58 +00001288 D->addAttr(::new (S.Context)
1289 AllocSizeAttr(Attr.getRange(), S.Context,
1290 SizeArgs.data(), SizeArgs.size(),
1291 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001292}
1293
Chandler Carruth1b03c872011-07-02 00:01:44 +00001294static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001295 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1296 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001297 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001298 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001299 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001300 return;
1301 }
Mike Stumpbf916502009-07-24 19:02:52 +00001302
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001303 SmallVector<unsigned, 8> NonNullArgs;
1304 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001305 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001306 uint64_t Idx;
1307 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1308 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001309 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001310
1311 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001312 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001313 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001314
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001315 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001316 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001317 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001318 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001319 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001320 }
Mike Stumpbf916502009-07-24 19:02:52 +00001321
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001322 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001323 }
Mike Stumpbf916502009-07-24 19:02:52 +00001324
1325 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1326 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001327 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001328 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1329 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001330 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001331 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001332 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001333 }
Mike Stumpbf916502009-07-24 19:02:52 +00001334
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001335 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001336 if (NonNullArgs.empty()) {
1337 // Warn the trivial case only if attribute is not coming from a
1338 // macro instantiation.
1339 if (Attr.getLoc().isFileID())
1340 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001341 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001342 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001343 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001344
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001345 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001346 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001347 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001348 D->addAttr(::new (S.Context)
1349 NonNullAttr(Attr.getRange(), S.Context, start, size,
1350 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001351}
1352
Chandler Carruth1b03c872011-07-02 00:01:44 +00001353static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001354 // This attribute must be applied to a function declaration.
1355 // The first argument to the attribute must be a string,
1356 // the name of the resource, for example "malloc".
1357 // The following arguments must be argument indexes, the arguments must be
1358 // of integer type for Returns, otherwise of pointer type.
1359 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001360 // after being held. free() should be __attribute((ownership_takes)), whereas
1361 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001362
Aaron Ballman624421f2013-08-31 01:11:41 +00001363 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001364 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001365 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001366 return;
1367 }
1368 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001369 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001370 switch (AL.getKind()) {
1371 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001372 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001373 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001374 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1375 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001376 return;
1377 }
Jordy Rose2a479922010-08-12 08:54:03 +00001378 break;
1379 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001380 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001381 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001382 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1383 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001384 return;
1385 }
Jordy Rose2a479922010-08-12 08:54:03 +00001386 break;
1387 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001388 K = OwnershipAttr::Returns;
Aaron Ballman624421f2013-08-31 01:11:41 +00001389 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001390 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballman624421f2013-08-31 01:11:41 +00001391 << AL.getName() << AL.getNumArgs() + 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001392 return;
1393 }
Jordy Rose2a479922010-08-12 08:54:03 +00001394 break;
1395 default:
1396 // This should never happen given how we are called.
1397 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001398 }
1399
Chandler Carruth87c44602011-07-01 23:49:12 +00001400 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001401 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1402 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001403 return;
1404 }
1405
Aaron Ballman624421f2013-08-31 01:11:41 +00001406 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001407
1408 // Normalize the argument, __foo__ becomes foo.
1409 if (Module.startswith("__") && Module.endswith("__"))
1410 Module = Module.substr(2, Module.size() - 4);
1411
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001412
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001413 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001414 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1415 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001416 uint64_t Idx;
1417 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001418 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001419 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001420
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001421 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001422 case OwnershipAttr::Takes:
1423 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001424 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001425 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001426 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1427 // FIXME: Should also highlight argument in decl.
1428 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001429 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001430 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001431 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001432 continue;
1433 }
1434 break;
1435 }
Sean Huntcf807c42010-08-18 23:23:40 +00001436 case OwnershipAttr::Returns: {
Aaron Ballman624421f2013-08-31 01:11:41 +00001437 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001438 // Is the function argument an integer type?
Aaron Ballman624421f2013-08-31 01:11:41 +00001439 Expr *IdxExpr = AL.getArgAsExpr(1);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001440 llvm::APSInt ArgNum(32);
1441 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1442 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1443 S.Diag(AL.getLoc(), diag::err_ownership_type)
1444 << "ownership_returns" << "integer"
1445 << IdxExpr->getSourceRange();
1446 return;
1447 }
1448 }
1449 break;
1450 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001451 } // switch
1452
1453 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001454 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001455 i = D->specific_attr_begin<OwnershipAttr>(),
1456 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001457 i != e; ++i) {
1458 if ((*i)->getOwnKind() != K) {
1459 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1460 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001461 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001462 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1463 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001464 }
1465 }
1466 }
1467 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001468 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001469 }
1470
1471 unsigned* start = OwnershipArgs.data();
1472 unsigned size = OwnershipArgs.size();
1473 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001474
1475 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001476 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1477 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001478 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001479 }
Sean Huntcf807c42010-08-18 23:23:40 +00001480
Michael Han51d8c522013-01-24 16:46:58 +00001481 D->addAttr(::new (S.Context)
1482 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1483 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001484}
1485
Chandler Carruth1b03c872011-07-02 00:01:44 +00001486static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001487 // Check the attribute arguments.
1488 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001489 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1490 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001491 return;
1492 }
1493
Chandler Carruth87c44602011-07-01 23:49:12 +00001494 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001495 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001496 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001497 return;
1498 }
1499
Chandler Carruth87c44602011-07-01 23:49:12 +00001500 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001501
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001502 // gcc rejects
1503 // class c {
1504 // static int a __attribute__((weakref ("v2")));
1505 // static int b() __attribute__((weakref ("f3")));
1506 // };
1507 // and ignores the attributes of
1508 // void f(void) {
1509 // static int a __attribute__((weakref ("v2")));
1510 // }
1511 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001512 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001513 if (!Ctx->isFileContext()) {
1514 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001515 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001516 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001517 }
1518
1519 // The GCC manual says
1520 //
1521 // At present, a declaration to which `weakref' is attached can only
1522 // be `static'.
1523 //
1524 // It also says
1525 //
1526 // Without a TARGET,
1527 // given as an argument to `weakref' or to `alias', `weakref' is
1528 // equivalent to `weak'.
1529 //
1530 // gcc 4.4.1 will accept
1531 // int a7 __attribute__((weakref));
1532 // as
1533 // int a7 __attribute__((weak));
1534 // This looks like a bug in gcc. We reject that for now. We should revisit
1535 // it if this behaviour is actually used.
1536
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001537 // GCC rejects
1538 // static ((alias ("y"), weakref)).
1539 // Should we? How to check that weakref is before or after alias?
1540
Aaron Ballmanbe116e22013-09-09 23:40:31 +00001541 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1542 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1543 // StringRef parameter it was given anyway.
Aaron Ballman624421f2013-08-31 01:11:41 +00001544 if (Attr.isArgExpr(0)) {
1545 Expr *Arg = Attr.getArgAsExpr(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001546 Arg = Arg->IgnoreParenCasts();
1547 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1548
Douglas Gregor5cee1192011-07-27 05:40:30 +00001549 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001550 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001551 << Attr.getName() << 1 << AANT_ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001552 return;
1553 }
1554 // GCC will accept anything as the argument of weakref. Should we
1555 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001556 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001557 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001558 }
1559
Michael Han51d8c522013-01-24 16:46:58 +00001560 D->addAttr(::new (S.Context)
1561 WeakRefAttr(Attr.getRange(), S.Context,
1562 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001563}
1564
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001565/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
1566/// If not emit an error and return false. If the argument is an identifier it
1567/// will emit an error with a fixit hint and treat it as if it was a string
1568/// literal.
1569static bool checkStringLiteralArgument(Sema &S, StringRef &Str,
1570 const AttributeList &Attr,
1571 unsigned ArgNum,
1572 SourceLocation *ArgLocation = 0) {
1573 // Look for identifiers. If we have one emit a hint to fix it to a literal.
1574 if (Attr.isArgIdent(ArgNum)) {
1575 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
1576 S.Diag(Loc->Loc, diag::err_attribute_argument_type)
1577 << Attr.getName() << AANT_ArgumentString
1578 << FixItHint::CreateInsertion(Loc->Loc, "\"")
1579 << FixItHint::CreateInsertion(S.PP.getLocForEndOfToken(Loc->Loc), "\"");
1580 Str = Loc->Ident->getName();
1581 if (ArgLocation)
1582 *ArgLocation = Loc->Loc;
1583 return true;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001584 }
Mike Stumpbf916502009-07-24 19:02:52 +00001585
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001586 // Now check for an actual string literal.
1587 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
1588 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
1589 if (ArgLocation)
1590 *ArgLocation = ArgExpr->getLocStart();
1591
1592 if (!Literal || !Literal->isAscii()) {
1593 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
1594 << Attr.getName() << AANT_ArgumentString;
1595 return false;
1596 }
1597
1598 Str = Literal->getString();
1599 return true;
1600}
1601
1602static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1603 StringRef Str;
1604 if (!checkStringLiteralArgument(S, Str, Attr, 0))
1605 return;
1606
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001607 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001608 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1609 return;
1610 }
1611
Chris Lattner6b6b5372008-06-26 18:38:35 +00001612 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001613
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001614 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00001615 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001616}
1617
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001618static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001619 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1621 << Attr.getName() << ExpectedFunctionOrMethod;
1622 return;
1623 }
1624
Michael Han51d8c522013-01-24 16:46:58 +00001625 D->addAttr(::new (S.Context)
1626 MinSizeAttr(Attr.getRange(), S.Context,
1627 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001628}
1629
Benjamin Krameree409a92012-05-12 21:10:52 +00001630static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001631 if (!isa<FunctionDecl>(D)) {
1632 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1633 << Attr.getName() << ExpectedFunction;
1634 return;
1635 }
1636
1637 if (D->hasAttr<HotAttr>()) {
1638 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1639 << Attr.getName() << "hot";
1640 return;
1641 }
1642
Michael Han51d8c522013-01-24 16:46:58 +00001643 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1644 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001645}
1646
1647static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001648 if (!isa<FunctionDecl>(D)) {
1649 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1650 << Attr.getName() << ExpectedFunction;
1651 return;
1652 }
1653
1654 if (D->hasAttr<ColdAttr>()) {
1655 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1656 << Attr.getName() << "cold";
1657 return;
1658 }
1659
Michael Han51d8c522013-01-24 16:46:58 +00001660 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1661 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001662}
1663
Chandler Carruth1b03c872011-07-02 00:01:44 +00001664static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001665 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001667 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001668 return;
1669 }
1670
Michael Han51d8c522013-01-24 16:46:58 +00001671 D->addAttr(::new (S.Context)
1672 NakedAttr(Attr.getRange(), S.Context,
1673 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001674}
1675
Chandler Carruth1b03c872011-07-02 00:01:44 +00001676static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1677 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001678 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001679 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001680 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001681 return;
1682 }
Mike Stumpbf916502009-07-24 19:02:52 +00001683
Michael Han51d8c522013-01-24 16:46:58 +00001684 D->addAttr(::new (S.Context)
1685 AlwaysInlineAttr(Attr.getRange(), S.Context,
1686 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001687}
1688
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001689static void handleTLSModelAttr(Sema &S, Decl *D,
1690 const AttributeList &Attr) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001691 StringRef Model;
1692 SourceLocation LiteralLoc;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001693 // Check that it is a string.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001694 if (!checkStringLiteralArgument(S, Model, Attr, 0, &LiteralLoc))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001695 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001696
Richard Smith38afbc72013-04-13 02:43:54 +00001697 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001698 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1699 << Attr.getName() << ExpectedTLSVar;
1700 return;
1701 }
1702
1703 // Check that the value.
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001704 if (Model != "global-dynamic" && Model != "local-dynamic"
1705 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001706 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001707 return;
1708 }
1709
Michael Han51d8c522013-01-24 16:46:58 +00001710 D->addAttr(::new (S.Context)
1711 TLSModelAttr(Attr.getRange(), S.Context, Model,
1712 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001713}
1714
Chandler Carruth1b03c872011-07-02 00:01:44 +00001715static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001716 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001717 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001718 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001719 D->addAttr(::new (S.Context)
1720 MallocAttr(Attr.getRange(), S.Context,
1721 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001722 return;
1723 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001724 }
1725
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001726 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001727}
1728
Chandler Carruth1b03c872011-07-02 00:01:44 +00001729static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00001730 D->addAttr(::new (S.Context)
1731 MayAliasAttr(Attr.getRange(), S.Context,
1732 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001733}
1734
Chandler Carruth1b03c872011-07-02 00:01:44 +00001735static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001736 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001737 D->addAttr(::new (S.Context)
1738 NoCommonAttr(Attr.getRange(), S.Context,
1739 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001740 else
1741 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001742 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001743}
1744
Chandler Carruth1b03c872011-07-02 00:01:44 +00001745static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001746 if (S.LangOpts.CPlusPlus) {
1747 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1748 return;
1749 }
1750
Chandler Carruth87c44602011-07-01 23:49:12 +00001751 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001752 D->addAttr(::new (S.Context)
1753 CommonAttr(Attr.getRange(), S.Context,
1754 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001755 else
1756 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001757 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001758}
1759
Chandler Carruth1b03c872011-07-02 00:01:44 +00001760static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001761 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001762
1763 if (S.CheckNoReturnAttr(attr)) return;
1764
Chandler Carruth87c44602011-07-01 23:49:12 +00001765 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001766 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001767 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001768 return;
1769 }
1770
Michael Han51d8c522013-01-24 16:46:58 +00001771 D->addAttr(::new (S.Context)
1772 NoReturnAttr(attr.getRange(), S.Context,
1773 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001774}
1775
1776bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001777 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001778 attr.setInvalid();
1779 return true;
1780 }
1781
1782 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001783}
1784
Chandler Carruth1b03c872011-07-02 00:01:44 +00001785static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1786 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001787
1788 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1789 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruth87c44602011-07-01 23:49:12 +00001790 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1791 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001792 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1793 && !VD->getType()->isFunctionPointerType())) {
1794 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001795 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001796 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001797 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001798 return;
1799 }
1800 }
1801
Michael Han51d8c522013-01-24 16:46:58 +00001802 D->addAttr(::new (S.Context)
1803 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1804 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001805}
1806
Richard Smithcd8ab512013-01-17 01:30:42 +00001807static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1808 const AttributeList &Attr) {
1809 // C++11 [dcl.attr.noreturn]p1:
1810 // The attribute may be applied to the declarator-id in a function
1811 // declaration.
1812 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1813 if (!FD) {
1814 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1815 << Attr.getName() << ExpectedFunctionOrMethod;
1816 return;
1817 }
1818
Michael Han51d8c522013-01-24 16:46:58 +00001819 D->addAttr(::new (S.Context)
1820 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1821 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001822}
1823
John Thompson35cc9622010-08-09 21:53:52 +00001824// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001825static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001826/*
1827 Returning a Vector Class in Registers
1828
Eric Christopherf48f3672010-12-01 22:13:54 +00001829 According to the PPU ABI specifications, a class with a single member of
1830 vector type is returned in memory when used as the return value of a function.
1831 This results in inefficient code when implementing vector classes. To return
1832 the value in a single vector register, add the vecreturn attribute to the
1833 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001834
1835 Example:
1836
1837 struct Vector
1838 {
1839 __vector float xyzw;
1840 } __attribute__((vecreturn));
1841
1842 Vector Add(Vector lhs, Vector rhs)
1843 {
1844 Vector result;
1845 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1846 return result; // This will be returned in a register
1847 }
1848*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001849 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001850 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001851 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001852 return;
1853 }
1854
Chandler Carruth87c44602011-07-01 23:49:12 +00001855 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001856 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1857 return;
1858 }
1859
Chandler Carruth87c44602011-07-01 23:49:12 +00001860 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001861 int count = 0;
1862
1863 if (!isa<CXXRecordDecl>(record)) {
1864 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1865 return;
1866 }
1867
1868 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1869 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1870 return;
1871 }
1872
Eric Christopherf48f3672010-12-01 22:13:54 +00001873 for (RecordDecl::field_iterator iter = record->field_begin();
1874 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001875 if ((count == 1) || !iter->getType()->isVectorType()) {
1876 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1877 return;
1878 }
1879 count++;
1880 }
1881
Michael Han51d8c522013-01-24 16:46:58 +00001882 D->addAttr(::new (S.Context)
1883 VecReturnAttr(Attr.getRange(), S.Context,
1884 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001885}
1886
Richard Smith3a2b7a12013-01-28 22:42:45 +00001887static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1888 const AttributeList &Attr) {
1889 if (isa<ParmVarDecl>(D)) {
1890 // [[carries_dependency]] can only be applied to a parameter if it is a
1891 // parameter of a function declaration or lambda.
1892 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1893 S.Diag(Attr.getLoc(),
1894 diag::err_carries_dependency_param_not_function_decl);
1895 return;
1896 }
1897 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001898 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001899 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001900 return;
1901 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001902
1903 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1904 Attr.getRange(), S.Context,
1905 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001906}
1907
Chandler Carruth1b03c872011-07-02 00:01:44 +00001908static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001909 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001910 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001911 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001912 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001913 return;
1914 }
Mike Stumpbf916502009-07-24 19:02:52 +00001915
Michael Han51d8c522013-01-24 16:46:58 +00001916 D->addAttr(::new (S.Context)
1917 UnusedAttr(Attr.getRange(), S.Context,
1918 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001919}
1920
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001921static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1922 const AttributeList &Attr) {
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001923 if (!isa<FunctionDecl>(D)) {
1924 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1925 << Attr.getName() << ExpectedFunction;
1926 return;
1927 }
1928
Michael Han51d8c522013-01-24 16:46:58 +00001929 D->addAttr(::new (S.Context)
1930 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1931 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001932}
1933
Chandler Carruth1b03c872011-07-02 00:01:44 +00001934static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001935 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001936 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001937 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1938 return;
1939 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001940 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001941 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001942 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001943 return;
1944 }
Mike Stumpbf916502009-07-24 19:02:52 +00001945
Michael Han51d8c522013-01-24 16:46:58 +00001946 D->addAttr(::new (S.Context)
1947 UsedAttr(Attr.getRange(), S.Context,
1948 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001949}
1950
Chandler Carruth1b03c872011-07-02 00:01:44 +00001951static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001952 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001953 if (Attr.getNumArgs() > 1) {
1954 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001955 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001956 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001957
1958 int priority = 65535; // FIXME: Do not hardcode such constants.
1959 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001960 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001961 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001962 if (E->isTypeDependent() || E->isValueDependent() ||
1963 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001964 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001965 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001966 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001967 return;
1968 }
1969 priority = Idx.getZExtValue();
1970 }
Mike Stumpbf916502009-07-24 19:02:52 +00001971
Chandler Carruth87c44602011-07-01 23:49:12 +00001972 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001973 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001974 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001975 return;
1976 }
1977
Michael Han51d8c522013-01-24 16:46:58 +00001978 D->addAttr(::new (S.Context)
1979 ConstructorAttr(Attr.getRange(), S.Context, priority,
1980 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001981}
1982
Chandler Carruth1b03c872011-07-02 00:01:44 +00001983static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001984 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001985 if (Attr.getNumArgs() > 1) {
1986 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001987 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001988 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001989
1990 int priority = 65535; // FIXME: Do not hardcode such constants.
1991 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001992 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001993 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001994 if (E->isTypeDependent() || E->isValueDependent() ||
1995 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001997 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001998 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001999 return;
2000 }
2001 priority = Idx.getZExtValue();
2002 }
Mike Stumpbf916502009-07-24 19:02:52 +00002003
Chandler Carruth87c44602011-07-01 23:49:12 +00002004 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002006 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002007 return;
2008 }
2009
Michael Han51d8c522013-01-24 16:46:58 +00002010 D->addAttr(::new (S.Context)
2011 DestructorAttr(Attr.getRange(), S.Context, priority,
2012 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002013}
2014
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002015template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002016static void handleAttrWithMessage(Sema &S, Decl *D,
2017 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002018 unsigned NumArgs = Attr.getNumArgs();
2019 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002020 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002021 return;
2022 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002023
2024 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002025 StringRef Str;
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002026 if (NumArgs == 1 && !checkStringLiteralArgument(S, Str, Attr, 0))
2027 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002028
Michael Han51d8c522013-01-24 16:46:58 +00002029 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2030 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002031}
2032
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002033static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2034 const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002035 D->addAttr(::new (S.Context)
2036 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2037 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002038}
2039
Patrick Beardb2f68202012-04-06 18:12:22 +00002040static void handleObjCRootClassAttr(Sema &S, Decl *D,
2041 const AttributeList &Attr) {
2042 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002043 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2044 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002045 return;
2046 }
2047
Michael Han51d8c522013-01-24 16:46:58 +00002048 D->addAttr(::new (S.Context)
2049 ObjCRootClassAttr(Attr.getRange(), S.Context,
2050 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002051}
2052
Michael Han51d8c522013-01-24 16:46:58 +00002053static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2054 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002055 if (!isa<ObjCInterfaceDecl>(D)) {
2056 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2057 return;
2058 }
2059
Michael Han51d8c522013-01-24 16:46:58 +00002060 D->addAttr(::new (S.Context)
2061 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2062 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002063}
2064
Jordy Rosefad5de92012-05-08 03:27:22 +00002065static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2066 IdentifierInfo *Platform,
2067 VersionTuple Introduced,
2068 VersionTuple Deprecated,
2069 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002070 StringRef PlatformName
2071 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2072 if (PlatformName.empty())
2073 PlatformName = Platform->getName();
2074
2075 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2076 // of these steps are needed).
2077 if (!Introduced.empty() && !Deprecated.empty() &&
2078 !(Introduced <= Deprecated)) {
2079 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2080 << 1 << PlatformName << Deprecated.getAsString()
2081 << 0 << Introduced.getAsString();
2082 return true;
2083 }
2084
2085 if (!Introduced.empty() && !Obsoleted.empty() &&
2086 !(Introduced <= Obsoleted)) {
2087 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2088 << 2 << PlatformName << Obsoleted.getAsString()
2089 << 0 << Introduced.getAsString();
2090 return true;
2091 }
2092
2093 if (!Deprecated.empty() && !Obsoleted.empty() &&
2094 !(Deprecated <= Obsoleted)) {
2095 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2096 << 2 << PlatformName << Obsoleted.getAsString()
2097 << 1 << Deprecated.getAsString();
2098 return true;
2099 }
2100
2101 return false;
2102}
2103
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002104/// \brief Check whether the two versions match.
2105///
2106/// If either version tuple is empty, then they are assumed to match. If
2107/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2108static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2109 bool BeforeIsOkay) {
2110 if (X.empty() || Y.empty())
2111 return true;
2112
2113 if (X == Y)
2114 return true;
2115
2116 if (BeforeIsOkay && X < Y)
2117 return true;
2118
2119 return false;
2120}
2121
Rafael Espindola51be6e32013-01-08 22:04:34 +00002122AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002123 IdentifierInfo *Platform,
2124 VersionTuple Introduced,
2125 VersionTuple Deprecated,
2126 VersionTuple Obsoleted,
2127 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002128 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002129 bool Override,
2130 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002131 VersionTuple MergedIntroduced = Introduced;
2132 VersionTuple MergedDeprecated = Deprecated;
2133 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002134 bool FoundAny = false;
2135
Rafael Espindola98ae8342012-05-10 02:50:16 +00002136 if (D->hasAttrs()) {
2137 AttrVec &Attrs = D->getAttrs();
2138 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2139 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2140 if (!OldAA) {
2141 ++i;
2142 continue;
2143 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002144
Rafael Espindola98ae8342012-05-10 02:50:16 +00002145 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2146 if (OldPlatform != Platform) {
2147 ++i;
2148 continue;
2149 }
2150
2151 FoundAny = true;
2152 VersionTuple OldIntroduced = OldAA->getIntroduced();
2153 VersionTuple OldDeprecated = OldAA->getDeprecated();
2154 VersionTuple OldObsoleted = OldAA->getObsoleted();
2155 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002156
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002157 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2158 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2159 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2160 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002161 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002162 if (Override) {
2163 int Which = -1;
2164 VersionTuple FirstVersion;
2165 VersionTuple SecondVersion;
2166 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2167 Which = 0;
2168 FirstVersion = OldIntroduced;
2169 SecondVersion = Introduced;
2170 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2171 Which = 1;
2172 FirstVersion = Deprecated;
2173 SecondVersion = OldDeprecated;
2174 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2175 Which = 2;
2176 FirstVersion = Obsoleted;
2177 SecondVersion = OldObsoleted;
2178 }
2179
2180 if (Which == -1) {
2181 Diag(OldAA->getLocation(),
2182 diag::warn_mismatched_availability_override_unavail)
2183 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2184 } else {
2185 Diag(OldAA->getLocation(),
2186 diag::warn_mismatched_availability_override)
2187 << Which
2188 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2189 << FirstVersion.getAsString() << SecondVersion.getAsString();
2190 }
2191 Diag(Range.getBegin(), diag::note_overridden_method);
2192 } else {
2193 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2194 Diag(Range.getBegin(), diag::note_previous_attribute);
2195 }
2196
Rafael Espindola98ae8342012-05-10 02:50:16 +00002197 Attrs.erase(Attrs.begin() + i);
2198 --e;
2199 continue;
2200 }
2201
2202 VersionTuple MergedIntroduced2 = MergedIntroduced;
2203 VersionTuple MergedDeprecated2 = MergedDeprecated;
2204 VersionTuple MergedObsoleted2 = MergedObsoleted;
2205
2206 if (MergedIntroduced2.empty())
2207 MergedIntroduced2 = OldIntroduced;
2208 if (MergedDeprecated2.empty())
2209 MergedDeprecated2 = OldDeprecated;
2210 if (MergedObsoleted2.empty())
2211 MergedObsoleted2 = OldObsoleted;
2212
2213 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2214 MergedIntroduced2, MergedDeprecated2,
2215 MergedObsoleted2)) {
2216 Attrs.erase(Attrs.begin() + i);
2217 --e;
2218 continue;
2219 }
2220
2221 MergedIntroduced = MergedIntroduced2;
2222 MergedDeprecated = MergedDeprecated2;
2223 MergedObsoleted = MergedObsoleted2;
2224 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002225 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002226 }
2227
2228 if (FoundAny &&
2229 MergedIntroduced == Introduced &&
2230 MergedDeprecated == Deprecated &&
2231 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002232 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002233
Ted Kremenekcb344392013-04-06 00:34:27 +00002234 // Only create a new attribute if !Override, but we want to do
2235 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002236 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002237 MergedDeprecated, MergedObsoleted) &&
2238 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002239 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2240 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002241 Obsoleted, IsUnavailable, Message,
2242 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002243 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002244 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002245}
2246
Chandler Carruth1b03c872011-07-02 00:01:44 +00002247static void handleAvailabilityAttr(Sema &S, Decl *D,
2248 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002249 if (!checkAttributeNumArgs(S, Attr, 1))
2250 return;
2251 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002252 unsigned Index = Attr.getAttributeSpellingListIndex();
2253
Aaron Ballman624421f2013-08-31 01:11:41 +00002254 IdentifierInfo *II = Platform->Ident;
2255 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2256 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2257 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002258
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002259 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2260 if (!ND) {
2261 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2262 return;
2263 }
2264
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002265 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2266 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2267 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002268 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002269 StringRef Str;
Benjamin Kramerf37e4f22013-09-13 16:30:12 +00002270 if (Attr.getMessageExpr()) {
2271 const StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getMessageExpr());
2272 if (!SE || !SE->isAscii()) {
2273 S.Diag(Attr.getMessageExpr()->getLocStart(),
2274 diag::err_attribute_argument_type)
2275 << Attr.getName() << AANT_ArgumentString;
2276 return;
2277 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002278 Str = SE->getString();
Benjamin Kramerf37e4f22013-09-13 16:30:12 +00002279 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002280
Aaron Ballman624421f2013-08-31 01:11:41 +00002281 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002282 Introduced.Version,
2283 Deprecated.Version,
2284 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002285 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002286 /*Override=*/false,
2287 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002288 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002289 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002290}
2291
John McCalld4c3d662013-02-20 01:54:26 +00002292template <class T>
2293static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2294 typename T::VisibilityType value,
2295 unsigned attrSpellingListIndex) {
2296 T *existingAttr = D->getAttr<T>();
2297 if (existingAttr) {
2298 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2299 if (existingValue == value)
2300 return NULL;
2301 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2302 S.Diag(range.getBegin(), diag::note_previous_attribute);
2303 D->dropAttr<T>();
2304 }
2305 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2306}
2307
Rafael Espindola599f1b72012-05-13 03:25:18 +00002308VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002309 VisibilityAttr::VisibilityType Vis,
2310 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002311 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2312 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002313}
2314
John McCalld4c3d662013-02-20 01:54:26 +00002315TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2316 TypeVisibilityAttr::VisibilityType Vis,
2317 unsigned AttrSpellingListIndex) {
2318 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2319 AttrSpellingListIndex);
2320}
2321
2322static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2323 bool isTypeVisibility) {
2324 // Visibility attributes don't mean anything on a typedef.
2325 if (isa<TypedefNameDecl>(D)) {
2326 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2327 << Attr.getName();
2328 return;
2329 }
2330
2331 // 'type_visibility' can only go on a type or namespace.
2332 if (isTypeVisibility &&
2333 !(isa<TagDecl>(D) ||
2334 isa<ObjCInterfaceDecl>(D) ||
2335 isa<NamespaceDecl>(D))) {
2336 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2337 << Attr.getName() << ExpectedTypeOrNamespace;
2338 return;
2339 }
2340
Benjamin Kramerae3a83f2013-09-09 15:08:57 +00002341 // Check that the argument is a string literal.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002342 StringRef TypeStr;
2343 SourceLocation LiteralLoc;
2344 if (!checkStringLiteralArgument(S, TypeStr, Attr, 0, &LiteralLoc))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002345 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002346
Sean Huntcf807c42010-08-18 23:23:40 +00002347 VisibilityAttr::VisibilityType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002348 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002349 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmand0686072013-09-11 19:47:58 +00002350 << Attr.getName() << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002351 return;
2352 }
Aaron Ballmand0686072013-09-11 19:47:58 +00002353
2354 // Complain about attempts to use protected visibility on targets
2355 // (like Darwin) that don't support it.
2356 if (type == VisibilityAttr::Protected &&
2357 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2358 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2359 type = VisibilityAttr::Default;
2360 }
Mike Stumpbf916502009-07-24 19:02:52 +00002361
Michael Han51d8c522013-01-24 16:46:58 +00002362 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002363 clang::Attr *newAttr;
2364 if (isTypeVisibility) {
2365 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2366 (TypeVisibilityAttr::VisibilityType) type,
2367 Index);
2368 } else {
2369 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2370 }
2371 if (newAttr)
2372 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002373}
2374
Chandler Carruth1b03c872011-07-02 00:01:44 +00002375static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2376 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002377 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2378 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002379 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002380 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002381 return;
2382 }
2383
Aaron Ballman624421f2013-08-31 01:11:41 +00002384 if (!Attr.isArgIdent(0)) {
2385 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2386 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002387 return;
2388 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002389
Aaron Ballmand0686072013-09-11 19:47:58 +00002390 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2391 ObjCMethodFamilyAttr::FamilyKind F;
2392 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2393 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2394 << IL->Ident;
John McCalld5313b02011-03-02 11:33:24 +00002395 return;
2396 }
2397
Aaron Ballmand0686072013-09-11 19:47:58 +00002398 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCallf85e1932011-06-15 23:02:42 +00002399 !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 Kyrtzidis768d6ca2011-09-13 16:05:58 +00002406 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballmand0686072013-09-11 19:47:58 +00002407 S.Context, F));
John McCalld5313b02011-03-02 11:33:24 +00002408}
2409
Chandler Carruth1b03c872011-07-02 00:01:44 +00002410static void handleObjCExceptionAttr(Sema &S, Decl *D,
2411 const AttributeList &Attr) {
Chris Lattner0db29ec2009-02-14 08:09:34 +00002412 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2413 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002414 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2415 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002416 return;
2417 }
Mike Stumpbf916502009-07-24 19:02:52 +00002418
Michael Han51d8c522013-01-24 16:46:58 +00002419 D->addAttr(::new (S.Context)
2420 ObjCExceptionAttr(Attr.getRange(), S.Context,
2421 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002422}
2423
Chandler Carruth1b03c872011-07-02 00:01:44 +00002424static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smith162e1c12011-04-15 14:24:37 +00002425 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002426 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002427 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002428 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2429 return;
2430 }
2431 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002432 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2433 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002434 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002435 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2436 return;
2437 }
2438 }
2439 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002440 // It is okay to include this attribute on properties, e.g.:
2441 //
2442 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2443 //
2444 // In this case it follows tradition and suppresses an error in the above
2445 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002446 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002447 }
Michael Han51d8c522013-01-24 16:46:58 +00002448 D->addAttr(::new (S.Context)
2449 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2450 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002451}
2452
Mike Stumpbf916502009-07-24 19:02:52 +00002453static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002454handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002455 if (!isa<FunctionDecl>(D)) {
2456 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2457 return;
2458 }
2459
Michael Han51d8c522013-01-24 16:46:58 +00002460 D->addAttr(::new (S.Context)
2461 OverloadableAttr(Attr.getRange(), S.Context,
2462 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002463}
2464
Chandler Carruth1b03c872011-07-02 00:01:44 +00002465static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002466 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002467 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002468 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002469 return;
2470 }
Mike Stumpbf916502009-07-24 19:02:52 +00002471
Aaron Ballman624421f2013-08-31 01:11:41 +00002472 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002473 BlocksAttr::BlockType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002474 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2475 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2476 << Attr.getName() << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002477 return;
2478 }
Mike Stumpbf916502009-07-24 19:02:52 +00002479
Michael Han51d8c522013-01-24 16:46:58 +00002480 D->addAttr(::new (S.Context)
2481 BlocksAttr(Attr.getRange(), S.Context, type,
2482 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002483}
2484
Chandler Carruth1b03c872011-07-02 00:01:44 +00002485static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002486 // check the attribute arguments.
2487 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002488 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002489 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002490 }
2491
John McCall3323fad2011-09-09 07:56:05 +00002492 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002493 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002494 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002495 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002496 if (E->isTypeDependent() || E->isValueDependent() ||
2497 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002498 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002499 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002500 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002501 return;
2502 }
Mike Stumpbf916502009-07-24 19:02:52 +00002503
John McCall3323fad2011-09-09 07:56:05 +00002504 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002505 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2506 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002507 return;
2508 }
John McCall3323fad2011-09-09 07:56:05 +00002509
2510 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002511 }
2512
John McCall3323fad2011-09-09 07:56:05 +00002513 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002514 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002515 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002516 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002517 if (E->isTypeDependent() || E->isValueDependent() ||
2518 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002519 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002520 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002521 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002522 return;
2523 }
2524 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002525
John McCall3323fad2011-09-09 07:56:05 +00002526 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002527 // FIXME: This error message could be improved, it would be nice
2528 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002529 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2530 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002531 return;
2532 }
2533 }
2534
Chandler Carruth87c44602011-07-01 23:49:12 +00002535 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002536 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002537 if (isa<FunctionNoProtoType>(FT)) {
2538 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2539 return;
2540 }
Mike Stumpbf916502009-07-24 19:02:52 +00002541
Chris Lattner897cd902009-03-17 23:03:47 +00002542 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002543 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002544 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002545 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002546 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002547 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002548 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002549 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002550 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002551 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2552 if (!BD->isVariadic()) {
2553 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2554 return;
2555 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002556 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002557 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002558 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002559 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002560 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002561 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002562 int m = Ty->isFunctionPointerType() ? 0 : 1;
2563 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002564 return;
2565 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002566 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002567 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002568 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002569 return;
2570 }
Anders Carlsson77091822008-10-05 18:05:59 +00002571 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002572 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002573 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002574 return;
2575 }
Michael Han51d8c522013-01-24 16:46:58 +00002576 D->addAttr(::new (S.Context)
2577 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2578 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002579}
2580
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002581static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002582 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2583 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2584 else
2585 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2586}
2587
Chandler Carruth1b03c872011-07-02 00:01:44 +00002588static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002589 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002590 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002591 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002592 return;
2593 }
Mike Stumpbf916502009-07-24 19:02:52 +00002594
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002595 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2596 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2597 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002598 return;
2599 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002600 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2601 if (MD->getResultType()->isVoidType()) {
2602 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2603 << Attr.getName() << 1;
2604 return;
2605 }
2606
Michael Han51d8c522013-01-24 16:46:58 +00002607 D->addAttr(::new (S.Context)
2608 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2609 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002610}
2611
Chandler Carruth1b03c872011-07-02 00:01:44 +00002612static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002613 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002614 if (isa<CXXRecordDecl>(D)) {
2615 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2616 return;
2617 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002618 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2619 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002620 return;
2621 }
2622
Chandler Carruth87c44602011-07-01 23:49:12 +00002623 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002624
Michael Han51d8c522013-01-24 16:46:58 +00002625 nd->addAttr(::new (S.Context)
2626 WeakAttr(Attr.getRange(), S.Context,
2627 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002628}
2629
Chandler Carruth1b03c872011-07-02 00:01:44 +00002630static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002631 // weak_import only applies to variable & function declarations.
2632 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002633 if (!D->canBeWeakImported(isDef)) {
2634 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002635 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2636 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002637 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002638 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002639 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002640 // Nothing to warn about here.
2641 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002642 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002643 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002644
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002645 return;
2646 }
2647
Michael Han51d8c522013-01-24 16:46:58 +00002648 D->addAttr(::new (S.Context)
2649 WeakImportAttr(Attr.getRange(), S.Context,
2650 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002651}
2652
Tanya Lattner0df579e2012-07-09 22:06:01 +00002653// Handles reqd_work_group_size and work_group_size_hint.
2654static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002655 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002656 unsigned WGSize[3];
2657 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002658 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002659 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002660 if (E->isTypeDependent() || E->isValueDependent() ||
2661 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002662 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2663 << Attr.getName() << AANT_ArgumentIntegerConstant
2664 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002665 return;
2666 }
2667 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2668 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002669
2670 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2671 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2672 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2673 if (!(A->getXDim() == WGSize[0] &&
2674 A->getYDim() == WGSize[1] &&
2675 A->getZDim() == WGSize[2])) {
2676 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2677 Attr.getName();
2678 }
2679 }
2680
2681 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2682 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2683 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2684 if (!(A->getXDim() == WGSize[0] &&
2685 A->getYDim() == WGSize[1] &&
2686 A->getZDim() == WGSize[2])) {
2687 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2688 Attr.getName();
2689 }
2690 }
2691
2692 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2693 D->addAttr(::new (S.Context)
2694 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002695 WGSize[0], WGSize[1], WGSize[2],
2696 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002697 else
2698 D->addAttr(::new (S.Context)
2699 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002700 WGSize[0], WGSize[1], WGSize[2],
2701 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002702}
2703
Joey Gouly37453b92013-03-08 09:42:32 +00002704static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2705 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2706
Aaron Ballman624421f2013-08-31 01:11:41 +00002707 if (!checkAttributeNumArgs(S, Attr, 0))
Joey Gouly37453b92013-03-08 09:42:32 +00002708 return;
2709
Aaron Ballman624421f2013-08-31 01:11:41 +00002710 if (!Attr.hasParsedType()) {
2711 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2712 << Attr.getName() << 1;
2713 return;
2714 }
2715
Joey Gouly37453b92013-03-08 09:42:32 +00002716 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2717
2718 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2719 (ParmType->isBooleanType() ||
2720 !ParmType->isIntegralType(S.getASTContext()))) {
2721 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2722 << ParmType;
2723 return;
2724 }
2725
2726 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2727 D->hasAttr<VecTypeHintAttr>()) {
2728 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2729 if (A->getTypeHint() != ParmType) {
2730 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2731 return;
2732 }
2733 }
2734
2735 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2736 ParmType, Attr.getLoc()));
2737}
2738
Rafael Espindola599f1b72012-05-13 03:25:18 +00002739SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002740 StringRef Name,
2741 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002742 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2743 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002744 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002745 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2746 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002747 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002748 }
Michael Han51d8c522013-01-24 16:46:58 +00002749 return ::new (Context) SectionAttr(Range, Context, Name,
2750 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002751}
2752
Chandler Carruth1b03c872011-07-02 00:01:44 +00002753static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002754 // Make sure that there is a string literal as the sections's single
2755 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002756 StringRef Str;
2757 SourceLocation LiteralLoc;
2758 if (!checkStringLiteralArgument(S, Str, Attr, 0, &LiteralLoc))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002759 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002760
Chris Lattner797c3c42009-08-10 19:03:04 +00002761 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002762 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002763 if (!Error.empty()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002764 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002765 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002766 return;
2767 }
Mike Stump1eb44332009-09-09 15:08:12 +00002768
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002769 // This attribute cannot be applied to local variables.
2770 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002771 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002772 return;
2773 }
Michael Han51d8c522013-01-24 16:46:58 +00002774
2775 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002776 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002777 if (NewAttr)
2778 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002779}
2780
Chris Lattner6b6b5372008-06-26 18:38:35 +00002781
Chandler Carruth1b03c872011-07-02 00:01:44 +00002782static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002783 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002784 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002785 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002786 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002787 D->addAttr(::new (S.Context)
2788 NoThrowAttr(Attr.getRange(), S.Context,
2789 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002790 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002791}
2792
Chandler Carruth1b03c872011-07-02 00:01:44 +00002793static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002794 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002795 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002796 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002797 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002798 D->addAttr(::new (S.Context)
2799 ConstAttr(Attr.getRange(), S.Context,
2800 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002801 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002802}
2803
Chandler Carruth1b03c872011-07-02 00:01:44 +00002804static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002805 D->addAttr(::new (S.Context)
2806 PureAttr(Attr.getRange(), S.Context,
2807 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002808}
2809
Chandler Carruth1b03c872011-07-02 00:01:44 +00002810static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002811 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002812 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002813 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002814 return;
2815 }
Mike Stumpbf916502009-07-24 19:02:52 +00002816
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002817 Expr *E = Attr.getArgAsExpr(0);
2818 SourceLocation Loc = E->getExprLoc();
2819 FunctionDecl *FD = 0;
2820 DeclarationNameInfo NI;
Aaron Ballman624421f2013-08-31 01:11:41 +00002821
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002822 // gcc only allows for simple identifiers. Since we support more than gcc, we
2823 // will warn the user.
2824 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2825 if (DRE->hasQualifier())
2826 S.Diag(Loc, diag::warn_cleanup_ext);
2827 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2828 NI = DRE->getNameInfo();
2829 if (!FD) {
2830 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2831 << NI.getName();
2832 return;
2833 }
2834 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2835 if (ULE->hasExplicitTemplateArgs())
2836 S.Diag(Loc, diag::warn_cleanup_ext);
Mike Stumpbf916502009-07-24 19:02:52 +00002837
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002838 // This will diagnose the case where the function cannot be found.
2839 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2840 NI = ULE->getNameInfo();
2841 } else {
2842 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002843 return;
2844 }
2845
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002846 if (FD->getNumParams() != 1) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002847 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2848 << NI.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002849 return;
2850 }
Mike Stumpbf916502009-07-24 19:02:52 +00002851
Anders Carlsson89941c12009-02-07 23:16:50 +00002852 // We're currently more strict than GCC about what function types we accept.
2853 // If this ever proves to be a problem it should be easy to fix.
2854 QualType Ty = S.Context.getPointerType(VD->getType());
2855 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002856 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2857 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002858 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2859 << NI.getName() << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002860 return;
2861 }
Mike Stumpbf916502009-07-24 19:02:52 +00002862
Michael Han51d8c522013-01-24 16:46:58 +00002863 D->addAttr(::new (S.Context)
2864 CleanupAttr(Attr.getRange(), S.Context, FD,
2865 Attr.getAttributeSpellingListIndex()));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002866}
2867
Mike Stumpbf916502009-07-24 19:02:52 +00002868/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002869/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002870static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002871 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002872 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002873 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002874 return;
2875 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002876
Aaron Ballman624421f2013-08-31 01:11:41 +00002877 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002878 uint64_t ArgIdx;
2879 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2880 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002881 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002882
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002883 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002884 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002885
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002886 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2887 if (not_nsstring_type &&
2888 !isCFStringType(Ty, S.Context) &&
2889 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002890 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002891 // FIXME: Should highlight the actual expression that has the wrong type.
2892 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002893 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002894 << IdxExpr->getSourceRange();
2895 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002896 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002897 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002898 if (!isNSStringType(Ty, S.Context) &&
2899 !isCFStringType(Ty, S.Context) &&
2900 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002901 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002902 // FIXME: Should highlight the actual expression that has the wrong type.
2903 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002904 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002905 << IdxExpr->getSourceRange();
2906 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002907 }
2908
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002909 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2910 // because that has corrected for the implicit this parameter, and is zero-
2911 // based. The attribute expects what the user wrote explicitly.
2912 llvm::APSInt Val;
2913 IdxExpr->EvaluateAsInt(Val, S.Context);
2914
Michael Han51d8c522013-01-24 16:46:58 +00002915 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002916 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00002917 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002918}
2919
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002920enum FormatAttrKind {
2921 CFStringFormat,
2922 NSStringFormat,
2923 StrftimeFormat,
2924 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002925 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002926 InvalidFormat
2927};
2928
2929/// getFormatAttrKind - Map from format attribute names to supported format
2930/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002931static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002932 return llvm::StringSwitch<FormatAttrKind>(Format)
2933 // Check for formats that get handled specially.
2934 .Case("NSString", NSStringFormat)
2935 .Case("CFString", CFStringFormat)
2936 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002937
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002938 // Otherwise, check for supported formats.
2939 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2940 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2941 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002942
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002943 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2944 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002945}
2946
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002947/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002948/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002949static void handleInitPriorityAttr(Sema &S, Decl *D,
2950 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002951 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002952 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2953 return;
2954 }
2955
Chandler Carruth87c44602011-07-01 23:49:12 +00002956 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002957 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2958 Attr.setInvalid();
2959 return;
2960 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002961 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002962 if (S.Context.getAsArrayType(T))
2963 T = S.Context.getBaseElementType(T);
2964 if (!T->getAs<RecordType>()) {
2965 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2966 Attr.setInvalid();
2967 return;
2968 }
2969
Aaron Ballman624421f2013-08-31 01:11:41 +00002970 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002971
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002972 llvm::APSInt priority(32);
2973 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2974 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002975 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2976 << Attr.getName() << AANT_ArgumentIntegerConstant
2977 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002978 Attr.setInvalid();
2979 return;
2980 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002981 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002982 if (prioritynum < 101 || prioritynum > 65535) {
2983 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2984 << priorityExpr->getSourceRange();
2985 Attr.setInvalid();
2986 return;
2987 }
Michael Han51d8c522013-01-24 16:46:58 +00002988 D->addAttr(::new (S.Context)
2989 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2990 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002991}
2992
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00002993FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2994 IdentifierInfo *Format, int FormatIdx,
2995 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00002996 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002997 // Check whether we already have an equivalent format attribute.
2998 for (specific_attr_iterator<FormatAttr>
2999 i = D->specific_attr_begin<FormatAttr>(),
3000 e = D->specific_attr_end<FormatAttr>();
3001 i != e ; ++i) {
3002 FormatAttr *f = *i;
3003 if (f->getType() == Format &&
3004 f->getFormatIdx() == FormatIdx &&
3005 f->getFirstArg() == FirstArg) {
3006 // If we don't have a valid location for this attribute, adopt the
3007 // location.
3008 if (f->getLocation().isInvalid())
3009 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003010 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003011 }
3012 }
3013
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003014 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3015 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003016}
3017
Mike Stumpbf916502009-07-24 19:02:52 +00003018/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003019/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003020static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003021 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003022 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003023 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003024 return;
3025 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003026
Chandler Carruth87c44602011-07-01 23:49:12 +00003027 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003028 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003029 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003030 return;
3031 }
3032
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003033 // In C++ the implicit 'this' function parameter also counts, and they are
3034 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003035 bool HasImplicitThisParam = isInstanceMethod(D);
3036 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003037 unsigned FirstIdx = 1;
3038
Aaron Ballman624421f2013-08-31 01:11:41 +00003039 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3040 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003041
3042 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003043 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003044 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003045 // If we've modified the string name, we need a new identifier for it.
3046 II = &S.Context.Idents.get(Format);
3047 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003048
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003049 // Check for supported formats.
3050 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003051
3052 if (Kind == IgnoredFormat)
3053 return;
3054
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003055 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003056 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003057 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003058 return;
3059 }
3060
3061 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003062 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003063 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003064 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3065 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003066 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003067 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003068 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003069 return;
3070 }
3071
3072 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003073 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003074 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003075 return;
3076 }
3077
3078 // FIXME: Do we need to bounds check?
3079 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003080
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003081 if (HasImplicitThisParam) {
3082 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003083 S.Diag(Attr.getLoc(),
3084 diag::err_format_attribute_implicit_this_format_string)
3085 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003086 return;
3087 }
3088 ArgIdx--;
3089 }
Mike Stump1eb44332009-09-09 15:08:12 +00003090
Chris Lattner6b6b5372008-06-26 18:38:35 +00003091 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003092 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003093
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003094 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003095 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003096 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3097 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003098 return;
3099 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003100 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003101 // FIXME: do we need to check if the type is NSString*? What are the
3102 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003103 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003104 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003105 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3106 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003107 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003108 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003109 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003110 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003111 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003112 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3113 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003114 return;
3115 }
3116
3117 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003118 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003119 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003120 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3121 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003122 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003123 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003124 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003125 return;
3126 }
3127
3128 // check if the function is variadic if the 3rd argument non-zero
3129 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003130 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003131 ++NumArgs; // +1 for ...
3132 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003133 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003134 return;
3135 }
3136 }
3137
Chris Lattner3c73c412008-11-19 08:23:25 +00003138 // strftime requires FirstArg to be 0 because it doesn't read from any
3139 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003140 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003141 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003142 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3143 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003144 return;
3145 }
3146 // if 0 it disables parameter checking (to use with e.g. va_list)
3147 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003148 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003149 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003150 return;
3151 }
3152
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003153 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003154 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003155 FirstArg.getZExtValue(),
3156 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003157 if (NewAttr)
3158 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003159}
3160
Chandler Carruth1b03c872011-07-02 00:01:44 +00003161static void handleTransparentUnionAttr(Sema &S, Decl *D,
3162 const AttributeList &Attr) {
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003163 // Try to find the underlying union declaration.
3164 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003165 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003166 if (TD && TD->getUnderlyingType()->isUnionType())
3167 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3168 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003169 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003170
3171 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003172 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003173 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003174 return;
3175 }
3176
John McCall5e1cdac2011-10-07 06:10:15 +00003177 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003178 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003179 diag::warn_transparent_union_attribute_not_definition);
3180 return;
3181 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003182
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003183 RecordDecl::field_iterator Field = RD->field_begin(),
3184 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003185 if (Field == FieldEnd) {
3186 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3187 return;
3188 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003189
David Blaikie581deb32012-06-06 20:45:41 +00003190 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003191 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003192 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003193 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003194 diag::warn_transparent_union_attribute_floating)
3195 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003196 return;
3197 }
3198
3199 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3200 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3201 for (; Field != FieldEnd; ++Field) {
3202 QualType FieldType = Field->getType();
3203 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3204 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3205 // Warn if we drop the attribute.
3206 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003207 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003208 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003209 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003210 diag::warn_transparent_union_attribute_field_size_align)
3211 << isSize << Field->getDeclName() << FieldBits;
3212 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003213 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003214 diag::note_transparent_union_first_field_size_align)
3215 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003216 return;
3217 }
3218 }
3219
Michael Han51d8c522013-01-24 16:46:58 +00003220 RD->addAttr(::new (S.Context)
3221 TransparentUnionAttr(Attr.getRange(), S.Context,
3222 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003223}
3224
Chandler Carruth1b03c872011-07-02 00:01:44 +00003225static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003226 // Make sure that there is a string literal as the annotation's single
3227 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003228 StringRef Str;
3229 if (!checkStringLiteralArgument(S, Str, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003230 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003231
3232 // Don't duplicate annotations that are already set.
3233 for (specific_attr_iterator<AnnotateAttr>
3234 i = D->specific_attr_begin<AnnotateAttr>(),
3235 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003236 if ((*i)->getAnnotation() == Str)
3237 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003238 }
Michael Han51d8c522013-01-24 16:46:58 +00003239
3240 D->addAttr(::new (S.Context)
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003241 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00003242 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003243}
3244
Chandler Carruth1b03c872011-07-02 00:01:44 +00003245static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003246 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003247 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3249 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003250 return;
3251 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003252
Richard Smithbe507b62013-02-01 08:12:08 +00003253 if (Attr.getNumArgs() == 0) {
3254 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3255 true, 0, Attr.getAttributeSpellingListIndex()));
3256 return;
3257 }
3258
Aaron Ballman624421f2013-08-31 01:11:41 +00003259 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003260 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3261 S.Diag(Attr.getEllipsisLoc(),
3262 diag::err_pack_expansion_without_parameter_packs);
3263 return;
3264 }
3265
3266 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3267 return;
3268
3269 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3270 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003271}
3272
3273void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003274 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003275 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3276 SourceLocation AttrLoc = AttrRange.getBegin();
3277
Richard Smith4cd81c52013-01-29 09:02:09 +00003278 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003279 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003280 // C++11 [dcl.align]p1:
3281 // An alignment-specifier may be applied to a variable or to a class
3282 // data member, but it shall not be applied to a bit-field, a function
3283 // parameter, the formal parameter of a catch clause, or a variable
3284 // declared with the register storage class specifier. An
3285 // alignment-specifier may also be applied to the declaration of a class
3286 // or enumeration type.
3287 // C11 6.7.5/2:
3288 // An alignment attribute shall not be specified in a declaration of
3289 // a typedef, or a bit-field, or a function, or a parameter, or an
3290 // object declared with the register storage-class specifier.
3291 int DiagKind = -1;
3292 if (isa<ParmVarDecl>(D)) {
3293 DiagKind = 0;
3294 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3295 if (VD->getStorageClass() == SC_Register)
3296 DiagKind = 1;
3297 if (VD->isExceptionVariable())
3298 DiagKind = 2;
3299 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3300 if (FD->isBitField())
3301 DiagKind = 3;
3302 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003303 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3304 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003305 << (TmpAttr.isC11() ? ExpectedVariableOrField
3306 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003307 return;
3308 }
3309 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003310 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003311 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003312 return;
3313 }
3314 }
3315
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003316 if (E->isTypeDependent() || E->isValueDependent()) {
3317 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003318 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3319 AA->setPackExpansion(IsPackExpansion);
3320 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003321 return;
3322 }
Michael Hana31f65b2013-02-01 01:19:17 +00003323
Sean Huntcf807c42010-08-18 23:23:40 +00003324 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003325 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003326 ExprResult ICE
3327 = VerifyIntegerConstantExpression(E, &Alignment,
3328 diag::err_aligned_attribute_argument_not_int,
3329 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003330 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003331 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003332
3333 // C++11 [dcl.align]p2:
3334 // -- if the constant expression evaluates to zero, the alignment
3335 // specifier shall have no effect
3336 // C11 6.7.5p6:
3337 // An alignment specification of zero has no effect.
3338 if (!(TmpAttr.isAlignas() && !Alignment) &&
3339 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003340 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3341 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003342 return;
3343 }
Michael Hana31f65b2013-02-01 01:19:17 +00003344
Richard Smithbe507b62013-02-01 08:12:08 +00003345 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003346 // We've already verified it's a power of 2, now let's make sure it's
3347 // 8192 or less.
3348 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003349 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003350 << E->getSourceRange();
3351 return;
3352 }
3353 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003354
Richard Smithf6565a92013-02-22 08:32:16 +00003355 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3356 ICE.take(), SpellingListIndex);
3357 AA->setPackExpansion(IsPackExpansion);
3358 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003359}
3360
Michael Hana31f65b2013-02-01 01:19:17 +00003361void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003362 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003363 // FIXME: Cache the number on the Attr object if non-dependent?
3364 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003365 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3366 SpellingListIndex);
3367 AA->setPackExpansion(IsPackExpansion);
3368 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003369}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003370
Richard Smithbe507b62013-02-01 08:12:08 +00003371void Sema::CheckAlignasUnderalignment(Decl *D) {
3372 assert(D->hasAttrs() && "no attributes on decl");
3373
3374 QualType Ty;
3375 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3376 Ty = VD->getType();
3377 else
3378 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003379 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003380 return;
3381
3382 // C++11 [dcl.align]p5, C11 6.7.5/4:
3383 // The combined effect of all alignment attributes in a declaration shall
3384 // not specify an alignment that is less strict than the alignment that
3385 // would otherwise be required for the entity being declared.
3386 AlignedAttr *AlignasAttr = 0;
3387 unsigned Align = 0;
3388 for (specific_attr_iterator<AlignedAttr>
3389 I = D->specific_attr_begin<AlignedAttr>(),
3390 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3391 if (I->isAlignmentDependent())
3392 return;
3393 if (I->isAlignas())
3394 AlignasAttr = *I;
3395 Align = std::max(Align, I->getAlignment(Context));
3396 }
3397
3398 if (AlignasAttr && Align) {
3399 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3400 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3401 if (NaturalAlign > RequestedAlign)
3402 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3403 << Ty << (unsigned)NaturalAlign.getQuantity();
3404 }
3405}
3406
Chandler Carruthd309c812011-07-01 23:49:16 +00003407/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003408/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003409///
Mike Stumpbf916502009-07-24 19:02:52 +00003410/// Despite what would be logical, the mode attribute is a decl attribute, not a
3411/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3412/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003413static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003414 // This attribute isn't documented, but glibc uses it. It changes
3415 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003416 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003417 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3418 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003419 return;
3420 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003421
Aaron Ballman624421f2013-08-31 01:11:41 +00003422 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3423 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003424
3425 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003426 if (Str.startswith("__") && Str.endswith("__"))
3427 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003428
3429 unsigned DestWidth = 0;
3430 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003431 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003432 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003433 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003434 switch (Str[0]) {
3435 case 'Q': DestWidth = 8; break;
3436 case 'H': DestWidth = 16; break;
3437 case 'S': DestWidth = 32; break;
3438 case 'D': DestWidth = 64; break;
3439 case 'X': DestWidth = 96; break;
3440 case 'T': DestWidth = 128; break;
3441 }
3442 if (Str[1] == 'F') {
3443 IntegerMode = false;
3444 } else if (Str[1] == 'C') {
3445 IntegerMode = false;
3446 ComplexMode = true;
3447 } else if (Str[1] != 'I') {
3448 DestWidth = 0;
3449 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003450 break;
3451 case 4:
3452 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3453 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003454 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003455 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003456 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003457 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003458 break;
3459 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003460 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003461 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003462 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003463 case 11:
3464 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003465 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003466 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003467 }
3468
3469 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003470 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003471 OldTy = TD->getUnderlyingType();
3472 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3473 OldTy = VD->getType();
3474 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003475 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003476 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003477 return;
3478 }
Eli Friedman73397492009-03-03 06:41:03 +00003479
John McCall183700f2009-09-21 23:43:11 +00003480 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003481 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3482 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003483 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003484 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3485 } else if (ComplexMode) {
3486 if (!OldTy->isComplexType())
3487 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3488 } else {
3489 if (!OldTy->isFloatingType())
3490 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3491 }
3492
Mike Stump390b4cc2009-05-16 07:39:55 +00003493 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3494 // and friends, at least with glibc.
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003495 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3496 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003497 // FIXME: Make sure floating-point mappings are accurate
3498 // FIXME: Support XF and TF types
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003499 QualType NewTy;
3500 switch (DestWidth) {
3501 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003502 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003503 return;
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003504 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003505 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003506 return;
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003507 case 8:
3508 if (!IntegerMode) {
3509 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3510 return;
3511 }
3512 if (OldTy->isSignedIntegerType())
3513 NewTy = S.Context.SignedCharTy;
3514 else
3515 NewTy = S.Context.UnsignedCharTy;
3516 break;
3517 case 16:
3518 if (!IntegerMode) {
3519 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3520 return;
3521 }
3522 if (OldTy->isSignedIntegerType())
3523 NewTy = S.Context.ShortTy;
3524 else
3525 NewTy = S.Context.UnsignedShortTy;
3526 break;
3527 case 32:
3528 if (!IntegerMode)
3529 NewTy = S.Context.FloatTy;
3530 else if (OldTy->isSignedIntegerType())
3531 NewTy = S.Context.IntTy;
3532 else
3533 NewTy = S.Context.UnsignedIntTy;
3534 break;
3535 case 64:
3536 if (!IntegerMode)
3537 NewTy = S.Context.DoubleTy;
3538 else if (OldTy->isSignedIntegerType())
3539 if (S.Context.getTargetInfo().getLongWidth() == 64)
3540 NewTy = S.Context.LongTy;
3541 else
3542 NewTy = S.Context.LongLongTy;
3543 else
3544 if (S.Context.getTargetInfo().getLongWidth() == 64)
3545 NewTy = S.Context.UnsignedLongTy;
3546 else
3547 NewTy = S.Context.UnsignedLongLongTy;
3548 break;
3549 case 96:
3550 NewTy = S.Context.LongDoubleTy;
3551 break;
3552 case 128:
3553 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3554 &llvm::APFloat::PPCDoubleDouble) {
3555 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3556 return;
3557 }
3558 if (IntegerMode) {
3559 if (OldTy->isSignedIntegerType())
3560 NewTy = S.Context.Int128Ty;
3561 else
3562 NewTy = S.Context.UnsignedInt128Ty;
3563 } else
3564 NewTy = S.Context.LongDoubleTy;
3565 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003566 }
3567
Eli Friedman73397492009-03-03 06:41:03 +00003568 if (ComplexMode) {
3569 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003570 }
3571
3572 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003573 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3574 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3575 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003576 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003577
3578 D->addAttr(::new (S.Context)
3579 ModeAttr(Attr.getRange(), S.Context, Name,
3580 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003581}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003582
Chandler Carruth1b03c872011-07-02 00:01:44 +00003583static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky78d1a102012-07-24 01:40:49 +00003584 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3585 if (!VD->hasGlobalStorage())
3586 S.Diag(Attr.getLoc(),
3587 diag::warn_attribute_requires_functions_or_static_globals)
3588 << Attr.getName();
3589 } else if (!isFunctionOrMethod(D)) {
3590 S.Diag(Attr.getLoc(),
3591 diag::warn_attribute_requires_functions_or_static_globals)
3592 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003593 return;
3594 }
Mike Stumpbf916502009-07-24 19:02:52 +00003595
Michael Han51d8c522013-01-24 16:46:58 +00003596 D->addAttr(::new (S.Context)
3597 NoDebugAttr(Attr.getRange(), S.Context,
3598 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003599}
3600
Chandler Carruth1b03c872011-07-02 00:01:44 +00003601static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003602 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003603 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003604 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003605 return;
3606 }
Mike Stumpbf916502009-07-24 19:02:52 +00003607
Michael Han51d8c522013-01-24 16:46:58 +00003608 D->addAttr(::new (S.Context)
3609 NoInlineAttr(Attr.getRange(), S.Context,
3610 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003611}
3612
Chandler Carruth1b03c872011-07-02 00:01:44 +00003613static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3614 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003615 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003616 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003617 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003618 return;
3619 }
3620
Michael Han51d8c522013-01-24 16:46:58 +00003621 D->addAttr(::new (S.Context)
3622 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3623 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003624}
3625
Chandler Carruth1b03c872011-07-02 00:01:44 +00003626static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003627 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003628 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003630 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003631 return;
3632 }
3633
Michael Han51d8c522013-01-24 16:46:58 +00003634 D->addAttr(::new (S.Context)
3635 CUDAConstantAttr(Attr.getRange(), S.Context,
3636 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003637 } else {
3638 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3639 }
3640}
3641
Chandler Carruth1b03c872011-07-02 00:01:44 +00003642static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003643 if (S.LangOpts.CUDA) {
3644 // check the attribute arguments.
3645 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003646 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3647 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003648 return;
3649 }
3650
Chandler Carruth87c44602011-07-01 23:49:12 +00003651 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003653 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003654 return;
3655 }
3656
Michael Han51d8c522013-01-24 16:46:58 +00003657 D->addAttr(::new (S.Context)
3658 CUDADeviceAttr(Attr.getRange(), S.Context,
3659 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003660 } else {
3661 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3662 }
3663}
3664
Chandler Carruth1b03c872011-07-02 00:01:44 +00003665static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003666 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003667 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003669 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003670 return;
3671 }
3672
Chandler Carruth87c44602011-07-01 23:49:12 +00003673 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003674 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003675 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003676 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003677 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3678 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003679 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003680 "void");
3681 } else {
3682 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3683 << FD->getType();
3684 }
3685 return;
3686 }
3687
Michael Han51d8c522013-01-24 16:46:58 +00003688 D->addAttr(::new (S.Context)
3689 CUDAGlobalAttr(Attr.getRange(), S.Context,
3690 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003691 } else {
3692 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3693 }
3694}
3695
Chandler Carruth1b03c872011-07-02 00:01:44 +00003696static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003697 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003698 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003699 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003700 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003701 return;
3702 }
3703
Michael Han51d8c522013-01-24 16:46:58 +00003704 D->addAttr(::new (S.Context)
3705 CUDAHostAttr(Attr.getRange(), S.Context,
3706 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003707 } else {
3708 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3709 }
3710}
3711
Chandler Carruth1b03c872011-07-02 00:01:44 +00003712static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003713 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003714 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003716 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003717 return;
3718 }
3719
Michael Han51d8c522013-01-24 16:46:58 +00003720 D->addAttr(::new (S.Context)
3721 CUDASharedAttr(Attr.getRange(), S.Context,
3722 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003723 } else {
3724 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3725 }
3726}
3727
Chandler Carruth1b03c872011-07-02 00:01:44 +00003728static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003729 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003730 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003731 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003732 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003733 return;
3734 }
Mike Stumpbf916502009-07-24 19:02:52 +00003735
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003736 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003737 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003738 return;
3739 }
Mike Stumpbf916502009-07-24 19:02:52 +00003740
Michael Han51d8c522013-01-24 16:46:58 +00003741 D->addAttr(::new (S.Context)
3742 GNUInlineAttr(Attr.getRange(), S.Context,
3743 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003744}
3745
Chandler Carruth1b03c872011-07-02 00:01:44 +00003746static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003747 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003748
Aaron Ballmanfff32482012-12-09 17:45:41 +00003749 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003750 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003751 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3752 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003753 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003754 return;
3755
Chandler Carruth87c44602011-07-01 23:49:12 +00003756 if (!isa<ObjCMethodDecl>(D)) {
3757 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3758 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003759 return;
3760 }
3761
Chandler Carruth87c44602011-07-01 23:49:12 +00003762 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003763 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003764 D->addAttr(::new (S.Context)
3765 FastCallAttr(Attr.getRange(), S.Context,
3766 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003767 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003768 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003769 D->addAttr(::new (S.Context)
3770 StdCallAttr(Attr.getRange(), S.Context,
3771 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003772 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003773 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003774 D->addAttr(::new (S.Context)
3775 ThisCallAttr(Attr.getRange(), S.Context,
3776 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003777 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003778 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003779 D->addAttr(::new (S.Context)
3780 CDeclAttr(Attr.getRange(), S.Context,
3781 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003782 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003783 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003784 D->addAttr(::new (S.Context)
3785 PascalAttr(Attr.getRange(), S.Context,
3786 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003787 return;
Charles Davise8519c32013-08-30 04:39:01 +00003788 case AttributeList::AT_MSABI:
3789 D->addAttr(::new (S.Context)
3790 MSABIAttr(Attr.getRange(), S.Context,
3791 Attr.getAttributeSpellingListIndex()));
3792 return;
3793 case AttributeList::AT_SysVABI:
3794 D->addAttr(::new (S.Context)
3795 SysVABIAttr(Attr.getRange(), S.Context,
3796 Attr.getAttributeSpellingListIndex()));
3797 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003798 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003799 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003800 switch (CC) {
3801 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003802 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003803 break;
3804 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003805 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003806 break;
3807 default:
3808 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003809 }
3810
Michael Han51d8c522013-01-24 16:46:58 +00003811 D->addAttr(::new (S.Context)
3812 PcsAttr(Attr.getRange(), S.Context, PCS,
3813 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003814 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003815 }
Derek Schuff263366f2012-10-16 22:30:41 +00003816 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003817 D->addAttr(::new (S.Context)
3818 PnaclCallAttr(Attr.getRange(), S.Context,
3819 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003820 return;
Guy Benyei38980082012-12-25 08:53:55 +00003821 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003822 D->addAttr(::new (S.Context)
3823 IntelOclBiccAttr(Attr.getRange(), S.Context,
3824 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003825 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003826
Abramo Bagnarae215f722010-04-30 13:10:51 +00003827 default:
3828 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003829 }
3830}
3831
Chandler Carruth1b03c872011-07-02 00:01:44 +00003832static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003833 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003834}
3835
Guy Benyei1db70402013-03-24 13:58:12 +00003836static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00003837 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00003838 llvm::APSInt ArgNum(32);
3839 if (E->isTypeDependent() || E->isValueDependent() ||
3840 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003841 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3842 << Attr.getName() << AANT_ArgumentIntegerConstant
3843 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003844 return;
3845 }
3846
3847 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3848 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3849}
3850
Aaron Ballmanfff32482012-12-09 17:45:41 +00003851bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3852 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003853 if (attr.isInvalid())
3854 return true;
3855
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003856 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00003857 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00003858 attr.setInvalid();
3859 return true;
3860 }
3861
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003862 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3863 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003864 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003865 case AttributeList::AT_CDecl: CC = CC_C; break;
3866 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3867 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3868 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3869 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00003870 case AttributeList::AT_MSABI:
3871 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3872 CC_X86_64Win64;
3873 break;
3874 case AttributeList::AT_SysVABI:
3875 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3876 CC_C;
3877 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003878 case AttributeList::AT_Pcs: {
Aaron Ballman624421f2013-08-31 01:11:41 +00003879 StringLiteral *Str = 0;
3880 if (attr.isArgExpr(0))
3881 Str = dyn_cast<StringLiteral>(attr.getArgAsExpr(0));
3882
Douglas Gregor5cee1192011-07-27 05:40:30 +00003883 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003884 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
3885 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003886 attr.setInvalid();
3887 return true;
3888 }
3889
Chris Lattner5f9e2722011-07-23 10:55:15 +00003890 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003891 if (StrRef == "aapcs") {
3892 CC = CC_AAPCS;
3893 break;
3894 } else if (StrRef == "aapcs-vfp") {
3895 CC = CC_AAPCS_VFP;
3896 break;
3897 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003898
3899 attr.setInvalid();
3900 Diag(attr.getLoc(), diag::err_invalid_pcs);
3901 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003902 }
Derek Schuff263366f2012-10-16 22:30:41 +00003903 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003904 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003905 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003906 }
3907
Aaron Ballman82bfa192012-10-02 14:26:08 +00003908 const TargetInfo &TI = Context.getTargetInfo();
3909 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3910 if (A == TargetInfo::CCCR_Warning) {
3911 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003912
3913 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3914 if (FD)
3915 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3916 TargetInfo::CCMT_NonMember;
3917 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003918 }
3919
John McCall711c52b2011-01-05 12:14:39 +00003920 return false;
3921}
3922
Chandler Carruth1b03c872011-07-02 00:01:44 +00003923static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003924 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003925
3926 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003927 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003928 return;
3929
Chandler Carruth87c44602011-07-01 23:49:12 +00003930 if (!isa<ObjCMethodDecl>(D)) {
3931 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3932 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003933 return;
3934 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003935
Michael Han51d8c522013-01-24 16:46:58 +00003936 D->addAttr(::new (S.Context)
3937 RegparmAttr(Attr.getRange(), S.Context, numParams,
3938 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003939}
3940
3941/// Checks a regparm attribute, returning true if it is ill-formed and
3942/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003943bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3944 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003945 return true;
3946
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003947 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003948 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003949 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003950 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003951
Aaron Ballman624421f2013-08-31 01:11:41 +00003952 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003953 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003954 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003955 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003956 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3957 << Attr.getName() << AANT_ArgumentIntegerConstant
3958 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003959 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003960 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003961 }
3962
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003963 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003964 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003965 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003966 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003967 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003968 }
3969
John McCall711c52b2011-01-05 12:14:39 +00003970 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003971 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003972 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003973 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003974 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003975 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003976 }
3977
John McCall711c52b2011-01-05 12:14:39 +00003978 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003979}
3980
Chandler Carruth1b03c872011-07-02 00:01:44 +00003981static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003982 if (S.LangOpts.CUDA) {
3983 // check the attribute arguments.
3984 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003985 // FIXME: 0 is not okay.
3986 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003987 return;
3988 }
3989
Chandler Carruth87c44602011-07-01 23:49:12 +00003990 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003991 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003992 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003993 return;
3994 }
3995
Aaron Ballman624421f2013-08-31 01:11:41 +00003996 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003997 llvm::APSInt MaxThreads(32);
3998 if (MaxThreadsExpr->isTypeDependent() ||
3999 MaxThreadsExpr->isValueDependent() ||
4000 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004001 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004002 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004003 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004004 return;
4005 }
4006
4007 llvm::APSInt MinBlocks(32);
4008 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004009 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004010 if (MinBlocksExpr->isTypeDependent() ||
4011 MinBlocksExpr->isValueDependent() ||
4012 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004013 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004014 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004015 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004016 return;
4017 }
4018 }
4019
Michael Han51d8c522013-01-24 16:46:58 +00004020 D->addAttr(::new (S.Context)
4021 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4022 MaxThreads.getZExtValue(),
4023 MinBlocks.getZExtValue(),
4024 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004025 } else {
4026 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4027 }
4028}
4029
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004030static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4031 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004032 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004033 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004034 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004035 return;
4036 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004037
4038 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004039 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004040
Aaron Ballman624421f2013-08-31 01:11:41 +00004041 StringRef AttrName = Attr.getName()->getName();
4042 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004043
4044 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4045 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4046 << Attr.getName() << ExpectedFunctionOrMethod;
4047 return;
4048 }
4049
4050 uint64_t ArgumentIdx;
4051 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4052 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004053 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004054 return;
4055
4056 uint64_t TypeTagIdx;
4057 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4058 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004059 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004060 return;
4061
4062 bool IsPointer = (AttrName == "pointer_with_type_tag");
4063 if (IsPointer) {
4064 // Ensure that buffer has a pointer type.
4065 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4066 if (!BufferTy->isPointerType()) {
4067 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004068 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004069 }
4070 }
4071
Michael Han51d8c522013-01-24 16:46:58 +00004072 D->addAttr(::new (S.Context)
4073 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4074 ArgumentIdx, TypeTagIdx, IsPointer,
4075 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004076}
4077
4078static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4079 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004080 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004081 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004082 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004083 return;
4084 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004085
4086 if (!checkAttributeNumArgs(S, Attr, 1))
4087 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004088
Aaron Ballman624421f2013-08-31 01:11:41 +00004089 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004090 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4091
Michael Han51d8c522013-01-24 16:46:58 +00004092 D->addAttr(::new (S.Context)
4093 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4094 MatchingCType,
4095 Attr.getLayoutCompatible(),
4096 Attr.getMustBeNull(),
4097 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004098}
4099
Chris Lattner0744e5f2008-06-29 00:23:49 +00004100//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004101// Checker-specific attribute handlers.
4102//===----------------------------------------------------------------------===//
4103
John McCallc7ad3812011-01-25 03:31:58 +00004104static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004105 return type->isDependentType() ||
4106 type->isObjCObjectPointerType() ||
4107 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004108}
4109static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004110 return type->isDependentType() ||
4111 type->isPointerType() ||
4112 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004113}
4114
Chandler Carruth1b03c872011-07-02 00:01:44 +00004115static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004116 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004117 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004118 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004119 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004120 return;
4121 }
4122
4123 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004124 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004125 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4126 cf = false;
4127 } else {
4128 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4129 cf = true;
4130 }
4131
4132 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004133 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004134 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004135 return;
4136 }
4137
4138 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004139 param->addAttr(::new (S.Context)
4140 CFConsumedAttr(Attr.getRange(), S.Context,
4141 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004142 else
Michael Han51d8c522013-01-24 16:46:58 +00004143 param->addAttr(::new (S.Context)
4144 NSConsumedAttr(Attr.getRange(), S.Context,
4145 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004146}
4147
Chandler Carruth1b03c872011-07-02 00:01:44 +00004148static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4149 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004150 if (!isa<ObjCMethodDecl>(D)) {
4151 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004152 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004153 return;
4154 }
4155
Michael Han51d8c522013-01-24 16:46:58 +00004156 D->addAttr(::new (S.Context)
4157 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4158 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004159}
4160
Chandler Carruth1b03c872011-07-02 00:01:44 +00004161static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4162 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004163
John McCallc7ad3812011-01-25 03:31:58 +00004164 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004165
Chandler Carruth87c44602011-07-01 23:49:12 +00004166 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004167 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004168 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004169 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004170 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004171 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4172 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004173 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004174 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004175 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004176 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004177 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004178 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004179 return;
4180 }
Mike Stumpbf916502009-07-24 19:02:52 +00004181
John McCallc7ad3812011-01-25 03:31:58 +00004182 bool typeOK;
4183 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004184 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004185 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004186 case AttributeList::AT_NSReturnsAutoreleased:
4187 case AttributeList::AT_NSReturnsRetained:
4188 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004189 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4190 cf = false;
4191 break;
4192
Sean Hunt8e083e72012-06-19 23:57:03 +00004193 case AttributeList::AT_CFReturnsRetained:
4194 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004195 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4196 cf = true;
4197 break;
4198 }
4199
4200 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004201 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004202 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004203 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004204 }
Mike Stumpbf916502009-07-24 19:02:52 +00004205
Chandler Carruth87c44602011-07-01 23:49:12 +00004206 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004207 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004208 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004209 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004210 D->addAttr(::new (S.Context)
4211 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4212 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004213 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004214 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004215 D->addAttr(::new (S.Context)
4216 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4217 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004218 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004219 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004220 D->addAttr(::new (S.Context)
4221 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4222 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004223 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004224 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004225 D->addAttr(::new (S.Context)
4226 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4227 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004228 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004229 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004230 D->addAttr(::new (S.Context)
4231 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4232 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004233 return;
4234 };
4235}
4236
John McCalldc7c5ad2011-07-22 08:53:00 +00004237static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4238 const AttributeList &attr) {
4239 SourceLocation loc = attr.getLoc();
4240
4241 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4242
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004243 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004244 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004245 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004246 return;
4247 }
4248
4249 // Check that the method returns a normal pointer.
4250 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004251
4252 if (!resultType->isReferenceType() &&
4253 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004254 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4255 << SourceRange(loc)
4256 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4257
4258 // Drop the attribute.
4259 return;
4260 }
4261
Michael Han51d8c522013-01-24 16:46:58 +00004262 method->addAttr(::new (S.Context)
4263 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4264 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004265}
4266
Fariborz Jahanian84101132012-09-07 23:46:23 +00004267static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4268 const AttributeList &attr) {
4269 SourceLocation loc = attr.getLoc();
4270 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4271
4272 if (!method) {
4273 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4274 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4275 return;
4276 }
4277 DeclContext *DC = method->getDeclContext();
4278 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4279 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4280 << attr.getName() << 0;
4281 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4282 return;
4283 }
4284 if (method->getMethodFamily() == OMF_dealloc) {
4285 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4286 << attr.getName() << 1;
4287 return;
4288 }
4289
Michael Han51d8c522013-01-24 16:46:58 +00004290 method->addAttr(::new (S.Context)
4291 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4292 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004293}
4294
John McCall8dfac0b2011-09-30 05:12:12 +00004295/// Handle cf_audited_transfer and cf_unknown_transfer.
4296static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4297 if (!isa<FunctionDecl>(D)) {
4298 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004299 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004300 return;
4301 }
4302
Sean Hunt8e083e72012-06-19 23:57:03 +00004303 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004304
4305 // Check whether there's a conflicting attribute already present.
4306 Attr *Existing;
4307 if (IsAudited) {
4308 Existing = D->getAttr<CFUnknownTransferAttr>();
4309 } else {
4310 Existing = D->getAttr<CFAuditedTransferAttr>();
4311 }
4312 if (Existing) {
4313 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4314 << A.getName()
4315 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4316 << A.getRange() << Existing->getRange();
4317 return;
4318 }
4319
4320 // All clear; add the attribute.
4321 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004322 D->addAttr(::new (S.Context)
4323 CFAuditedTransferAttr(A.getRange(), S.Context,
4324 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004325 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004326 D->addAttr(::new (S.Context)
4327 CFUnknownTransferAttr(A.getRange(), S.Context,
4328 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004329 }
4330}
4331
John McCallfe98da02011-09-29 07:17:38 +00004332static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4333 const AttributeList &Attr) {
4334 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4335 if (!RD || RD->isUnion()) {
4336 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004337 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004338 }
4339
Aaron Ballman624421f2013-08-31 01:11:41 +00004340 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004341
4342 // In Objective-C, verify that the type names an Objective-C type.
4343 // We don't want to check this outside of ObjC because people sometimes
4344 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004345 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004346 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004347 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004348 Sema::LookupOrdinaryName);
4349 if (S.LookupName(R, Sc)) {
4350 NamedDecl *Target = R.getFoundDecl();
4351 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4352 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4353 S.Diag(Target->getLocStart(), diag::note_declared_at);
4354 }
4355 }
4356 }
4357
Michael Han51d8c522013-01-24 16:46:58 +00004358 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004359 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004360 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004361}
4362
Chandler Carruth1b03c872011-07-02 00:01:44 +00004363static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4364 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004365 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004366
Chandler Carruth87c44602011-07-01 23:49:12 +00004367 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004368 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004369}
4370
Chandler Carruth1b03c872011-07-02 00:01:44 +00004371static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4372 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004373 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004374 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004375 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004376 return;
4377 }
4378
Chandler Carruth87c44602011-07-01 23:49:12 +00004379 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004380 QualType type = vd->getType();
4381
4382 if (!type->isDependentType() &&
4383 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004384 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004385 << type;
4386 return;
4387 }
4388
4389 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4390
4391 // If we have no lifetime yet, check the lifetime we're presumably
4392 // going to infer.
4393 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4394 lifetime = type->getObjCARCImplicitLifetime();
4395
4396 switch (lifetime) {
4397 case Qualifiers::OCL_None:
4398 assert(type->isDependentType() &&
4399 "didn't infer lifetime for non-dependent type?");
4400 break;
4401
4402 case Qualifiers::OCL_Weak: // meaningful
4403 case Qualifiers::OCL_Strong: // meaningful
4404 break;
4405
4406 case Qualifiers::OCL_ExplicitNone:
4407 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004408 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004409 << (lifetime == Qualifiers::OCL_Autoreleasing);
4410 break;
4411 }
4412
Chandler Carruth87c44602011-07-01 23:49:12 +00004413 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004414 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4415 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004416}
4417
Francois Pichet11542142010-12-19 06:50:37 +00004418//===----------------------------------------------------------------------===//
4419// Microsoft specific attribute handlers.
4420//===----------------------------------------------------------------------===//
4421
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004422// Check if MS extensions or some other language extensions are enabled. If
4423// not, issue a diagnostic that the given attribute is unused.
4424static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4425 bool OtherExtension = false) {
4426 if (S.LangOpts.MicrosoftExt || OtherExtension)
4427 return true;
4428 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4429 return false;
4430}
4431
Chandler Carruth1b03c872011-07-02 00:01:44 +00004432static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004433 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4434 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004435
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004436 StringRef StrRef;
4437 SourceLocation LiteralLoc;
4438 if (!checkStringLiteralArgument(S, StrRef, Attr, 0, &LiteralLoc))
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004439 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004440
David Majnemerbb0ed292013-08-09 08:56:20 +00004441 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4442 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemerbb0ed292013-08-09 08:56:20 +00004443 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4444 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004445
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004446 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004447 if (StrRef.size() != 36) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004448 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004449 return;
4450 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004451
David Majnemerbb0ed292013-08-09 08:56:20 +00004452 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004453 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004454 if (StrRef[i] != '-') {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004455 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichetd3d3be92010-12-20 01:41:49 +00004456 return;
4457 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004458 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004459 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004460 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004461 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004462 }
Francois Pichet11542142010-12-19 06:50:37 +00004463
David Majnemerbb0ed292013-08-09 08:56:20 +00004464 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4465 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004466}
4467
John McCallc052dbb2012-05-22 21:28:12 +00004468static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004469 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004470 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004471
4472 AttributeList::Kind Kind = Attr.getKind();
4473 if (Kind == AttributeList::AT_SingleInheritance)
4474 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004475 ::new (S.Context)
4476 SingleInheritanceAttr(Attr.getRange(), S.Context,
4477 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004478 else if (Kind == AttributeList::AT_MultipleInheritance)
4479 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004480 ::new (S.Context)
4481 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4482 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004483 else if (Kind == AttributeList::AT_VirtualInheritance)
4484 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004485 ::new (S.Context)
4486 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4487 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004488}
4489
4490static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004491 if (!checkMicrosoftExt(S, Attr))
4492 return;
4493
4494 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004495 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004496 D->addAttr(
4497 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4498 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004499}
4500
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004501static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004502 if (!checkMicrosoftExt(S, Attr))
4503 return;
4504 D->addAttr(::new (S.Context)
4505 ForceInlineAttr(Attr.getRange(), S.Context,
4506 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004507}
4508
Reid Klecknera7225342013-05-20 14:02:37 +00004509static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4510 if (!checkMicrosoftExt(S, Attr))
4511 return;
4512 // Check linkage after possibly merging declaratinos. See
4513 // checkAttributesAfterMerging().
4514 D->addAttr(::new (S.Context)
4515 SelectAnyAttr(Attr.getRange(), S.Context,
4516 Attr.getAttributeSpellingListIndex()));
4517}
4518
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004519/// Handles semantic checking for features that are common to all attributes,
4520/// such as checking whether a parameter was properly specified, or the correct
4521/// number of arguments were passed, etc.
4522static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4523 const AttributeList &Attr) {
4524 // Several attributes carry different semantics than the parsing requires, so
4525 // those are opted out of the common handling.
4526 //
4527 // We also bail on unknown and ignored attributes because those are handled
4528 // as part of the target-specific handling logic.
4529 if (Attr.hasCustomParsing() ||
4530 Attr.getKind() == AttributeList::UnknownAttribute ||
4531 Attr.getKind() == AttributeList::IgnoredAttribute)
4532 return false;
4533
4534 // If there are no optional arguments, then checking for the argument count
4535 // is trivial.
4536 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4537 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4538 return true;
4539 return false;
4540}
4541
Ted Kremenekb71368d2009-05-09 02:44:38 +00004542//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004543// Top Level Sema Entry Points
4544//===----------------------------------------------------------------------===//
4545
Richard Smith4a97b8e2013-08-29 00:47:48 +00004546/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4547/// the attribute applies to decls. If the attribute is a type attribute, just
4548/// silently ignore it if a GNU attribute.
4549static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4550 const AttributeList &Attr,
4551 bool IncludeCXX11Attributes) {
4552 if (Attr.isInvalid())
4553 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004554
Richard Smith4a97b8e2013-08-29 00:47:48 +00004555 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4556 // instead.
4557 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4558 return;
4559
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004560 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4561 return;
4562
Chris Lattner803d0802008-06-29 00:43:07 +00004563 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004564 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4565 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4566 case AttributeList::AT_IBOutletCollection:
4567 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004568 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004569 case AttributeList::AT_ObjCGC:
4570 case AttributeList::AT_VectorSize:
4571 case AttributeList::AT_NeonVectorType:
4572 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004573 case AttributeList::AT_Ptr32:
4574 case AttributeList::AT_Ptr64:
4575 case AttributeList::AT_SPtr:
4576 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004577 // Ignore these, these are type attributes, handled by
4578 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004579 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004580 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4581 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4582 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4583 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004584 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004585 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004586 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004587 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004588 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4589 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4590 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004591 handleDependencyAttr(S, scope, D, Attr);
4592 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004593 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4594 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4595 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004596 case AttributeList::AT_CXX11NoReturn:
4597 handleCXX11NoReturnAttr(S, D, Attr);
4598 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004599 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004600 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004601 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004602 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4603 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004604 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004605 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004606 case AttributeList::AT_MinSize:
4607 handleMinSizeAttr(S, D, Attr);
4608 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004609 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4610 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4611 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004612 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4613 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004614 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4615 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004616 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004617 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004618 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4619 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004620 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004621 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4622 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004623 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004624 case AttributeList::AT_ownership_returns:
4625 case AttributeList::AT_ownership_takes:
4626 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004627 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004628 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4629 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4630 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4631 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4632 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4633 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4634 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004635
Sean Hunt8e083e72012-06-19 23:57:03 +00004636 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004637 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004638 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004639 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004640
Sean Hunt8e083e72012-06-19 23:57:03 +00004641 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004642 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4643
Fariborz Jahanian84101132012-09-07 23:46:23 +00004644 case AttributeList::AT_ObjCRequiresSuper:
4645 handleObjCRequiresSuperAttr(S, D, Attr); break;
4646
Sean Hunt8e083e72012-06-19 23:57:03 +00004647 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004648 handleNSBridgedAttr(S, scope, D, Attr); break;
4649
Sean Hunt8e083e72012-06-19 23:57:03 +00004650 case AttributeList::AT_CFAuditedTransfer:
4651 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004652 handleCFTransferAttr(S, D, Attr); break;
4653
Ted Kremenekb71368d2009-05-09 02:44:38 +00004654 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004655 case AttributeList::AT_CFConsumed:
4656 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4657 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004658 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004659
Sean Hunt8e083e72012-06-19 23:57:03 +00004660 case AttributeList::AT_NSReturnsAutoreleased:
4661 case AttributeList::AT_NSReturnsNotRetained:
4662 case AttributeList::AT_CFReturnsNotRetained:
4663 case AttributeList::AT_NSReturnsRetained:
4664 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004665 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004666
Tanya Lattner0df579e2012-07-09 22:06:01 +00004667 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004668 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004669 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004670
Joey Gouly37453b92013-03-08 09:42:32 +00004671 case AttributeList::AT_VecTypeHint:
4672 handleVecTypeHint(S, D, Attr); break;
4673
Sean Hunt8e083e72012-06-19 23:57:03 +00004674 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004675 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004676
Sean Hunt8e083e72012-06-19 23:57:03 +00004677 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4678 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4679 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004680 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004681 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004682 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004683 handleArcWeakrefUnavailableAttr (S, D, Attr);
4684 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004685 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004686 handleObjCRootClassAttr(S, D, Attr);
4687 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004688 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004689 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004690 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004691 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4692 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004693 handleReturnsTwiceAttr(S, D, Attr);
4694 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004695 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004696 case AttributeList::AT_Visibility:
4697 handleVisibilityAttr(S, D, Attr, false);
4698 break;
4699 case AttributeList::AT_TypeVisibility:
4700 handleVisibilityAttr(S, D, Attr, true);
4701 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004702 case AttributeList::AT_WarnUnused:
4703 handleWarnUnusedAttr(S, D, Attr);
4704 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004705 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004706 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004707 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4708 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4709 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4710 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004711 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004712 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004713 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004714 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004715 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004716 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004717 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004718 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004719 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4720 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4721 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4722 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4723 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4724 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4725 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4726 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4727 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004728 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004729 // Just ignore
4730 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004731 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004732 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004733 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004734 case AttributeList::AT_StdCall:
4735 case AttributeList::AT_CDecl:
4736 case AttributeList::AT_FastCall:
4737 case AttributeList::AT_ThisCall:
4738 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004739 case AttributeList::AT_MSABI:
4740 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004741 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004742 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004743 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004744 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004745 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004746 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004747 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004748 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004749 case AttributeList::AT_OpenCLImageAccess:
4750 handleOpenCLImageAccessAttr(S, D, Attr);
4751 break;
John McCallc052dbb2012-05-22 21:28:12 +00004752
4753 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004754 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004755 handleMsStructAttr(S, D, Attr);
4756 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004757 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004758 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004759 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004760 case AttributeList::AT_SingleInheritance:
4761 case AttributeList::AT_MultipleInheritance:
4762 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004763 handleInheritanceAttr(S, D, Attr);
4764 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004765 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004766 handlePortabilityAttr(S, D, Attr);
4767 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004768 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004769 handleForceInlineAttr(S, D, Attr);
4770 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004771 case AttributeList::AT_SelectAny:
4772 handleSelectAnyAttr(S, D, Attr);
4773 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004774
4775 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004776 case AttributeList::AT_AssertExclusiveLock:
4777 handleAssertExclusiveLockAttr(S, D, Attr);
4778 break;
4779 case AttributeList::AT_AssertSharedLock:
4780 handleAssertSharedLockAttr(S, D, Attr);
4781 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004782 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004783 handleGuardedVarAttr(S, D, Attr);
4784 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004785 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004786 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004787 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004788 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004789 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004790 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004791 case AttributeList::AT_NoSanitizeAddress:
4792 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004793 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004794 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004795 handleNoThreadSafetyAnalysis(S, D, Attr);
4796 break;
4797 case AttributeList::AT_NoSanitizeThread:
4798 handleNoSanitizeThread(S, D, Attr);
4799 break;
4800 case AttributeList::AT_NoSanitizeMemory:
4801 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004802 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004803 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004804 handleLockableAttr(S, D, Attr);
4805 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004806 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004807 handleGuardedByAttr(S, D, Attr);
4808 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004809 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004810 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004811 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004812 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004813 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004814 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004815 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004816 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004817 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004818 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004819 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004820 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004821 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004822 handleLockReturnedAttr(S, D, Attr);
4823 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004824 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004825 handleLocksExcludedAttr(S, D, Attr);
4826 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004827 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004828 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004829 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004830 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004831 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004832 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004833 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004834 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004835 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004836 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004837 handleUnlockFunAttr(S, D, Attr);
4838 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004839 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004840 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004841 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004842 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004843 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004844 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004845
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004846 // Uniqueness analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00004847 case AttributeList::AT_Consumable:
4848 handleConsumableAttr(S, D, Attr);
4849 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004850 case AttributeList::AT_Consumes:
4851 handleConsumesAttr(S, D, Attr);
4852 break;
4853 case AttributeList::AT_CallableWhenUnconsumed:
4854 handleCallableWhenUnconsumedAttr(S, D, Attr);
4855 break;
4856 case AttributeList::AT_TestsConsumed:
4857 handleTestsConsumedAttr(S, D, Attr);
4858 break;
4859 case AttributeList::AT_TestsUnconsumed:
4860 handleTestsUnconsumedAttr(S, D, Attr);
4861 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00004862 case AttributeList::AT_ReturnTypestate:
4863 handleReturnTypestateAttr(S, D, Attr);
4864 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004865
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004866 // Type safety attributes.
4867 case AttributeList::AT_ArgumentWithTypeTag:
4868 handleArgumentWithTypeTagAttr(S, D, Attr);
4869 break;
4870 case AttributeList::AT_TypeTagForDatatype:
4871 handleTypeTagForDatatypeAttr(S, D, Attr);
4872 break;
4873
Chris Lattner803d0802008-06-29 00:43:07 +00004874 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004875 // Ask target about the attribute.
4876 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4877 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004878 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4879 diag::warn_unhandled_ms_attribute_ignored :
4880 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004881 break;
4882 }
4883}
4884
4885/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4886/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004887void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004888 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004889 bool IncludeCXX11Attributes) {
4890 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004891 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004892
4893 // GCC accepts
4894 // static int a9 __attribute__((weakref));
4895 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004896 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004897 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004898 cast<NamedDecl>(D)->getNameAsString();
4899 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004900 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004901 }
4902}
4903
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004904// Annotation attributes are the only attributes allowed after an access
4905// specifier.
4906bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4907 const AttributeList *AttrList) {
4908 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004909 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004910 handleAnnotateAttr(*this, ASDecl, *l);
4911 } else {
4912 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4913 return true;
4914 }
4915 }
4916
4917 return false;
4918}
4919
John McCalle82247a2011-10-01 05:17:03 +00004920/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4921/// contains any decl attributes that we should warn about.
4922static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4923 for ( ; A; A = A->getNext()) {
4924 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00004925 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00004926 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4927
4928 if (A->getKind() == AttributeList::UnknownAttribute) {
4929 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4930 << A->getName() << A->getRange();
4931 } else {
4932 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4933 << A->getName() << A->getRange();
4934 }
4935 }
4936}
4937
4938/// checkUnusedDeclAttributes - Given a declarator which is not being
4939/// used to build a declaration, complain about any decl attributes
4940/// which might be lying around on it.
4941void Sema::checkUnusedDeclAttributes(Declarator &D) {
4942 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4943 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4944 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4945 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4946}
4947
Ryan Flynne25ff832009-07-30 03:15:39 +00004948/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004949/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004950NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4951 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004952 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004953 NamedDecl *NewD = 0;
4954 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004955 FunctionDecl *NewFD;
4956 // FIXME: Missing call to CheckFunctionDeclaration().
4957 // FIXME: Mangling?
4958 // FIXME: Is the qualifier info correct?
4959 // FIXME: Is the DeclContext correct?
4960 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4961 Loc, Loc, DeclarationName(II),
4962 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004963 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00004964 FD->hasPrototype(),
4965 false/*isConstexprSpecified*/);
4966 NewD = NewFD;
4967
4968 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004969 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004970
4971 // Fake up parameter variables; they are declared as if this were
4972 // a typedef.
4973 QualType FDTy = FD->getType();
4974 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4975 SmallVector<ParmVarDecl*, 16> Params;
4976 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4977 AE = FT->arg_type_end(); AI != AE; ++AI) {
4978 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4979 Param->setScopeInfo(0, Params.size());
4980 Params.push_back(Param);
4981 }
David Blaikie4278c652011-09-21 18:16:56 +00004982 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004983 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004984 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4985 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004986 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004987 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004988 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00004989 if (VD->getQualifier()) {
4990 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004991 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004992 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004993 }
4994 return NewD;
4995}
4996
James Dennett1dfbd922012-06-14 21:40:34 +00004997/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004998/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004999void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005000 if (W.getUsed()) return; // only do this once
5001 W.setUsed(true);
5002 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5003 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005004 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005005 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5006 NDId->getName()));
5007 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005008 WeakTopLevelDecl.push_back(NewD);
5009 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5010 // to insert Decl at TU scope, sorry.
5011 DeclContext *SavedContext = CurContext;
5012 CurContext = Context.getTranslationUnitDecl();
5013 PushOnScopeChains(NewD, S);
5014 CurContext = SavedContext;
5015 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005016 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005017 }
5018}
5019
Rafael Espindola65611bf2013-03-02 21:41:48 +00005020void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5021 // It's valid to "forward-declare" #pragma weak, in which case we
5022 // have to do this.
5023 LoadExternalWeakUndeclaredIdentifiers();
5024 if (!WeakUndeclaredIdentifiers.empty()) {
5025 NamedDecl *ND = NULL;
5026 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5027 if (VD->isExternC())
5028 ND = VD;
5029 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5030 if (FD->isExternC())
5031 ND = FD;
5032 if (ND) {
5033 if (IdentifierInfo *Id = ND->getIdentifier()) {
5034 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5035 = WeakUndeclaredIdentifiers.find(Id);
5036 if (I != WeakUndeclaredIdentifiers.end()) {
5037 WeakInfo W = I->second;
5038 DeclApplyPragmaWeak(S, ND, W);
5039 WeakUndeclaredIdentifiers[Id] = W;
5040 }
5041 }
5042 }
5043 }
5044}
5045
Chris Lattner0744e5f2008-06-29 00:23:49 +00005046/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5047/// it, apply them to D. This is a bit tricky because PD can have attributes
5048/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005049void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005050 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005051 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005052 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005053
Chris Lattner0744e5f2008-06-29 00:23:49 +00005054 // Walk the declarator structure, applying decl attributes that were in a type
5055 // position to the decl itself. This handles cases like:
5056 // int *__attr__(x)** D;
5057 // when X is a decl attribute.
5058 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5059 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005060 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005061
Chris Lattner0744e5f2008-06-29 00:23:49 +00005062 // Finally, apply any attributes on the decl itself.
5063 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005064 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005065}
John McCall54abf7d2009-11-04 02:18:39 +00005066
John McCallf85e1932011-06-15 23:02:42 +00005067/// Is the given declaration allowed to use a forbidden type?
5068static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5069 // Private ivars are always okay. Unfortunately, people don't
5070 // always properly make their ivars private, even in system headers.
5071 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005072 // Function declarations in sys headers will be marked unavailable.
5073 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5074 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005075 return false;
5076
5077 // Require it to be declared in a system header.
5078 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5079}
5080
5081/// Handle a delayed forbidden-type diagnostic.
5082static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5083 Decl *decl) {
5084 if (decl && isForbiddenTypeAllowed(S, decl)) {
5085 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5086 "this system declaration uses an unsupported type"));
5087 return;
5088 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005089 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005090 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005091 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005092 // kind of forbidden type messages on unavailable functions.
5093 if (FD->hasAttr<UnavailableAttr>() &&
5094 diag.getForbiddenTypeDiagnostic() ==
5095 diag::err_arc_array_param_no_ownership) {
5096 diag.Triggered = true;
5097 return;
5098 }
5099 }
John McCallf85e1932011-06-15 23:02:42 +00005100
5101 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5102 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5103 diag.Triggered = true;
5104}
5105
John McCall92576642012-05-07 06:16:41 +00005106void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5107 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005108 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005109 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005110
John McCall92576642012-05-07 06:16:41 +00005111 // When delaying diagnostics to run in the context of a parsed
5112 // declaration, we only want to actually emit anything if parsing
5113 // succeeds.
5114 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005115
John McCall92576642012-05-07 06:16:41 +00005116 // We emit all the active diagnostics in this pool or any of its
5117 // parents. In general, we'll get one pool for the decl spec
5118 // and a child pool for each declarator; in a decl group like:
5119 // deprecated_typedef foo, *bar, baz();
5120 // only the declarator pops will be passed decls. This is correct;
5121 // we really do need to consider delayed diagnostics from the decl spec
5122 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005123 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005124 do {
John McCall13489672012-05-07 06:16:58 +00005125 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005126 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5127 // This const_cast is a bit lame. Really, Triggered should be mutable.
5128 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005129 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005130 continue;
5131
John McCalleee1d542011-02-14 07:13:47 +00005132 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005133 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005134 // Don't bother giving deprecation diagnostics if the decl is invalid.
5135 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005136 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005137 break;
5138
5139 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005140 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005141 break;
John McCallf85e1932011-06-15 23:02:42 +00005142
5143 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005144 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005145 break;
John McCall2f514482010-01-27 03:50:35 +00005146 }
5147 }
John McCall92576642012-05-07 06:16:41 +00005148 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005149}
5150
John McCall13489672012-05-07 06:16:58 +00005151/// Given a set of delayed diagnostics, re-emit them as if they had
5152/// been delayed in the current context instead of in the given pool.
5153/// Essentially, this just moves them to the current pool.
5154void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5155 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5156 assert(curPool && "re-emitting in undelayed context not supported");
5157 curPool->steal(pool);
5158}
5159
John McCall54abf7d2009-11-04 02:18:39 +00005160static bool isDeclDeprecated(Decl *D) {
5161 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005162 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005163 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005164 // A category implicitly has the availability of the interface.
5165 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5166 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005167 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5168 return false;
5169}
5170
Eli Friedmanc3b23082012-08-08 21:52:41 +00005171static void
5172DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5173 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005174 const ObjCInterfaceDecl *UnknownObjCClass,
5175 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005176 DeclarationName Name = D->getDeclName();
5177 if (!Message.empty()) {
5178 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5179 S.Diag(D->getLocation(),
5180 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5181 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005182 if (ObjCPropery)
5183 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5184 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005185 } else if (!UnknownObjCClass) {
5186 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5187 S.Diag(D->getLocation(),
5188 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5189 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005190 if (ObjCPropery)
5191 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5192 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005193 } else {
5194 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5195 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5196 }
5197}
5198
John McCall9c3087b2010-08-26 02:13:20 +00005199void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005200 Decl *Ctx) {
5201 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005202 return;
5203
John McCall2f514482010-01-27 03:50:35 +00005204 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005205 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5206 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005207 DD.getUnknownObjCClass(),
5208 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005209}
5210
Chris Lattner5f9e2722011-07-23 10:55:15 +00005211void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005212 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005213 const ObjCInterfaceDecl *UnknownObjCClass,
5214 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005215 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005216 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005217 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5218 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005219 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005220 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005221 return;
5222 }
5223
5224 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005225 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005226 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005227 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005228}