blob: c365bfc508ca7bfc1cb225643ff33038d8be001f [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000027#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000028#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000029#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000031using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000032using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033
John McCall5fca7ea2011-03-02 12:29:23 +000034/// These constants match the enumerated choices of
35/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000036enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000037 ExpectedFunction,
38 ExpectedUnion,
39 ExpectedVariableOrFunction,
40 ExpectedFunctionOrMethod,
41 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000042 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000043 ExpectedFunctionMethodOrClass,
John McCall5fca7ea2011-03-02 12:29:23 +000044 ExpectedFunctionMethodOrParameter,
45 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000046 ExpectedVariable,
47 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000048 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000049 ExpectedFieldOrGlobalVar,
Hans Wennborgd3b01bc2012-06-23 11:51:46 +000050 ExpectedStruct,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000051 ExpectedVariableFunctionOrTag,
Richard Smith9eaab4b2013-02-01 08:25:07 +000052 ExpectedTLSVar,
53 ExpectedVariableOrField,
John McCalld041a9b2013-02-20 01:54:26 +000054 ExpectedVariableFieldOrTag,
55 ExpectedTypeOrNamespace
John McCall5fca7ea2011-03-02 12:29:23 +000056};
57
Chris Lattner58418ff2008-06-29 00:16:31 +000058//===----------------------------------------------------------------------===//
59// Helper functions
60//===----------------------------------------------------------------------===//
61
Chandler Carruthff4c4f02011-07-01 23:49:12 +000062static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000063 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000064 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000065 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000066 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000067 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000068 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000069 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000070 Ty = decl->getUnderlyingType();
71 else
72 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000073
Chris Lattner2c6fcf52008-06-26 18:38:35 +000074 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000075 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000076 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000077 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000078
John McCall9dd450b2009-09-21 23:43:11 +000079 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000080}
81
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000082// FIXME: We should provide an abstraction around a method or function
83// to provide the following bits of information.
84
Nuno Lopes518e3702009-12-20 23:11:08 +000085/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000086/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087static bool isFunction(const Decl *D) {
88 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000089}
90
91/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000092/// type (function or function-typed variable) or an Objective-C
93/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000094static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000095 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000096}
97
Fariborz Jahanian4447e172009-05-15 23:15:03 +000098/// isFunctionOrMethodOrBlock - Return true if the given decl has function
99/// type (function or function-typed variable) or an Objective-C
100/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000101static bool isFunctionOrMethodOrBlock(const Decl *D) {
102 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000103 return true;
104 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000105 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000106 QualType Ty = V->getType();
107 return Ty->isBlockPointerType();
108 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000109 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000110}
111
John McCall3882ace2011-01-05 12:14:39 +0000112/// Return true if the given decl has a declarator that should have
113/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000114static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000115 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000116 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
117 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000118}
119
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000120/// hasFunctionProto - Return true if the given decl has a argument
121/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000122/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000123static bool hasFunctionProto(const Decl *D) {
124 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000125 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000126 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000127 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000128 return true;
129 }
130}
131
132/// getFunctionOrMethodNumArgs - Return number of function or method
133/// arguments. It is an error to call this on a K&R function (use
134/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000135static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000137 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000138 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000139 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000140 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000141}
142
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000143static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
144 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000145 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000146 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000147 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000148
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000149 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000150}
151
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152static QualType getFunctionOrMethodResultType(const Decl *D) {
153 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000154 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000156}
157
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000158static bool isFunctionOrMethodVariadic(const Decl *D) {
159 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000160 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000161 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000162 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000163 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000164 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000165 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000166 }
167}
168
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000169static bool isInstanceMethod(const Decl *D) {
170 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000171 return MethodDecl->isInstance();
172 return false;
173}
174
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000175static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000176 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000177 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000178 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000179
John McCall96fa4842010-05-17 21:00:27 +0000180 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
181 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000182 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000183
John McCall96fa4842010-05-17 21:00:27 +0000184 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000185
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000186 // FIXME: Should we walk the chain of classes?
187 return ClsName == &Ctx.Idents.get("NSString") ||
188 ClsName == &Ctx.Idents.get("NSMutableString");
189}
190
Daniel Dunbar980c6692008-09-26 03:32:58 +0000191static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000192 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000193 if (!PT)
194 return false;
195
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000196 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000197 if (!RT)
198 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000199
Daniel Dunbar980c6692008-09-26 03:32:58 +0000200 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000201 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000202 return false;
203
204 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
205}
206
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000207/// \brief Check if the attribute has exactly as many args as Num. May
208/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000209static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
210 unsigned int Num) {
211 if (Attr.getNumArgs() != Num) {
212 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
213 return false;
214 }
215
216 return true;
217}
218
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000219
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000220/// \brief Check if the attribute has at least as many args as Num. May
221/// output an error.
222static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
223 unsigned int Num) {
224 if (Attr.getNumArgs() < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000225 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
226 return false;
227 }
228
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000229 return true;
230}
231
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000232/// \brief Check if IdxExpr is a valid argument index for a function or
233/// instance method D. May output an error.
234///
235/// \returns true if IdxExpr is a valid index.
236static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
237 StringRef AttrName,
238 SourceLocation AttrLoc,
239 unsigned AttrArgNum,
240 const Expr *IdxExpr,
241 uint64_t &Idx)
242{
243 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
244
245 // In C++ the implicit 'this' function parameter also counts.
246 // Parameters are counted from one.
247 const bool HasImplicitThisParam = isInstanceMethod(D);
248 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
249 const unsigned FirstIdx = 1;
250
251 llvm::APSInt IdxInt;
252 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
253 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
254 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
255 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
256 return false;
257 }
258
259 Idx = IdxInt.getLimitedValue();
260 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
261 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
262 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
263 return false;
264 }
265 Idx--; // Convert to zero-based.
266 if (HasImplicitThisParam) {
267 if (Idx == 0) {
268 S.Diag(AttrLoc,
269 diag::err_attribute_invalid_implicit_this_argument)
270 << AttrName << IdxExpr->getSourceRange();
271 return false;
272 }
273 --Idx;
274 }
275
276 return true;
277}
278
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000279///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000280/// \brief Check if passed in Decl is a field or potentially shared global var
281/// \return true if the Decl is a field or potentially shared global variable
282///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000283static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000284 if (isa<FieldDecl>(D))
285 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000286 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smithfd3834f2013-04-13 02:43:54 +0000287 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000288
289 return false;
290}
291
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000292/// \brief Check if the passed-in expression is of type int or bool.
293static bool isIntOrBool(Expr *Exp) {
294 QualType QT = Exp->getType();
295 return QT->isBooleanType() || QT->isIntegerType();
296}
297
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000298
299// Check to see if the type is a smart pointer of some kind. We assume
300// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000301static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
302 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
303 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000304 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000305 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000306
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000307 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
308 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000309 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000310 return false;
311
312 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000313}
314
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000315/// \brief Check if passed in Decl is a pointer type.
316/// Note that this function may produce an error message.
317/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000318static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
319 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000320 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000321 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000322 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000323 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000324
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000325 if (const RecordType *RT = QT->getAs<RecordType>()) {
326 // If it's an incomplete type, it could be a smart pointer; skip it.
327 // (We don't want to force template instantiation if we can avoid it,
328 // since that would alter the order in which templates are instantiated.)
329 if (RT->isIncompleteType())
330 return true;
331
332 if (threadSafetyCheckIsSmartPointer(S, RT))
333 return true;
334 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000335
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000336 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000337 << Attr.getName()->getName() << QT;
338 } else {
339 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
340 << Attr.getName();
341 }
342 return false;
343}
344
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000345/// \brief Checks that the passed in QualType either is of RecordType or points
346/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000347static const RecordType *getRecordType(QualType QT) {
348 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000349 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000350
351 // Now check if we point to record type.
352 if (const PointerType *PT = QT->getAs<PointerType>())
353 return PT->getPointeeType()->getAs<RecordType>();
354
355 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000356}
357
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000358
Jordy Rose740b0c22012-05-08 03:27:22 +0000359static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
360 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000361 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
362 if (RT->getDecl()->getAttr<LockableAttr>())
363 return true;
364 return false;
365}
366
367
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000368/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000369/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000370static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
371 QualType Ty) {
372 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000373
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000374 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000375 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000376 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000377 << Attr.getName() << Ty.getAsString();
378 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000379 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000380
Michael Hana9171bc2012-08-03 17:40:43 +0000381 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000382 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000383 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000384
385 // Allow smart pointers to be used as lockable objects.
386 // FIXME -- Check the type that the smart pointer points to.
387 if (threadSafetyCheckIsSmartPointer(S, RT))
388 return;
389
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000390 // Check if the type is lockable.
391 RecordDecl *RD = RT->getDecl();
392 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000393 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000394
395 // Else check if any base classes are lockable.
396 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
397 CXXBasePaths BPaths(false, false);
398 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
399 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000400 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000401
402 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
403 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000404}
405
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000406/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000407/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000408/// \param Sidx The attribute argument index to start checking with.
409/// \param ParamIdxOk Whether an argument can be indexing into a function
410/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000411static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000412 const AttributeList &Attr,
413 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000414 int Sidx = 0,
415 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000416 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000417 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000418
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000419 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000420 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000421 Args.push_back(ArgExp);
422 continue;
423 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000424
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000425 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000426 if (StrLit->getLength() == 0 ||
427 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000428 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000429 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000430 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000431 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000432 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000433
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000434 // We allow constant strings to be used as a placeholder for expressions
435 // that are not valid C++ syntax, but warn that they are ignored.
436 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
437 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000438 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000439 continue;
440 }
441
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000442 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000443
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000444 // A pointer to member expression of the form &MyClass::mu is treated
445 // specially -- we need to look at the type of the member.
446 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
447 if (UOp->getOpcode() == UO_AddrOf)
448 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
449 if (DRE->getDecl()->isCXXInstanceMember())
450 ArgTy = DRE->getDecl()->getType();
451
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000452 // First see if we can just cast to record type, or point to record type.
453 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000454
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000455 // Now check if we index into a record type function param.
456 if(!RT && ParamIdxOk) {
457 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000458 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
459 if(FD && IL) {
460 unsigned int NumParams = FD->getNumParams();
461 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000462 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
463 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
464 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000465 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
466 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000467 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000468 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000469 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000470 }
471 }
472
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000473 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000474
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000475 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000476 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000477}
478
Chris Lattner58418ff2008-06-29 00:16:31 +0000479//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000480// Attribute Implementations
481//===----------------------------------------------------------------------===//
482
Daniel Dunbar032db472008-07-31 22:40:48 +0000483// FIXME: All this manual attribute parsing code is gross. At the
484// least add some helper functions to check most argument patterns (#
485// and types of args).
486
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000487enum ThreadAttributeDeclKind {
488 ThreadExpectedFieldOrGlobalVar,
489 ThreadExpectedFunctionOrMethod,
490 ThreadExpectedClassOrStruct
491};
492
Michael Hana9171bc2012-08-03 17:40:43 +0000493static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000494 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000495 assert(!Attr.isInvalid());
496
497 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000498 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000499
500 // D must be either a member field or global (potentially shared) variable.
501 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000502 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
503 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000504 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000505 }
506
Michael Han3be3b442012-07-23 18:48:41 +0000507 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000508}
509
Michael Han3be3b442012-07-23 18:48:41 +0000510static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
511 if (!checkGuardedVarAttrCommon(S, D, Attr))
512 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000513
Michael Han99315932013-01-24 16:46:58 +0000514 D->addAttr(::new (S.Context)
515 GuardedVarAttr(Attr.getRange(), S.Context,
516 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000517}
518
Michael Hana9171bc2012-08-03 17:40:43 +0000519static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000520 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000521 if (!checkGuardedVarAttrCommon(S, D, Attr))
522 return;
523
524 if (!threadSafetyCheckIsPointer(S, D, Attr))
525 return;
526
Michael Han99315932013-01-24 16:46:58 +0000527 D->addAttr(::new (S.Context)
528 PtGuardedVarAttr(Attr.getRange(), S.Context,
529 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000530}
531
Michael Hana9171bc2012-08-03 17:40:43 +0000532static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
533 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000534 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000535 assert(!Attr.isInvalid());
536
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000537 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000538 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000539
540 // D must be either a member field or global (potentially shared) variable.
541 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000542 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
543 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000544 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000545 }
546
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000547 SmallVector<Expr*, 1> Args;
548 // check that all arguments are lockable objects
549 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
550 unsigned Size = Args.size();
551 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000552 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000553
Michael Han3be3b442012-07-23 18:48:41 +0000554 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000555
Michael Han3be3b442012-07-23 18:48:41 +0000556 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000557}
558
Michael Han3be3b442012-07-23 18:48:41 +0000559static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
560 Expr *Arg = 0;
561 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
562 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000563
Michael Han3be3b442012-07-23 18:48:41 +0000564 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
565}
566
Michael Hana9171bc2012-08-03 17:40:43 +0000567static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000568 const AttributeList &Attr) {
569 Expr *Arg = 0;
570 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
571 return;
572
573 if (!threadSafetyCheckIsPointer(S, D, Attr))
574 return;
575
576 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
577 S.Context, Arg));
578}
579
Michael Hana9171bc2012-08-03 17:40:43 +0000580static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000581 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000582 assert(!Attr.isInvalid());
583
584 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000585 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000586
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000587 // FIXME: Lockable structs for C code.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000588 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000589 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
590 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Han3be3b442012-07-23 18:48:41 +0000591 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000592 }
593
Michael Han3be3b442012-07-23 18:48:41 +0000594 return true;
595}
596
597static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
598 if (!checkLockableAttrCommon(S, D, Attr))
599 return;
600
601 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
602}
603
Michael Hana9171bc2012-08-03 17:40:43 +0000604static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000605 const AttributeList &Attr) {
606 if (!checkLockableAttrCommon(S, D, Attr))
607 return;
608
Michael Han99315932013-01-24 16:46:58 +0000609 D->addAttr(::new (S.Context)
610 ScopedLockableAttr(Attr.getRange(), S.Context,
611 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000612}
613
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000614static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
615 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000616 assert(!Attr.isInvalid());
617
618 if (!checkAttributeNumArgs(S, Attr, 0))
619 return;
620
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000621 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000622 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
623 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000624 return;
625 }
626
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000627 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000628 S.Context));
629}
630
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000631static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000632 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000633 assert(!Attr.isInvalid());
634
635 if (!checkAttributeNumArgs(S, Attr, 0))
636 return;
637
638 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000639 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000640 << Attr.getName() << ExpectedFunctionOrMethod;
641 return;
642 }
643
Michael Han99315932013-01-24 16:46:58 +0000644 D->addAttr(::new (S.Context)
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000645 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
646 Attr.getAttributeSpellingListIndex()));
647}
648
649static void handleNoSanitizeMemory(Sema &S, Decl *D,
650 const AttributeList &Attr) {
651 assert(!Attr.isInvalid());
652
653 if (!checkAttributeNumArgs(S, Attr, 0))
654 return;
655
656 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
658 << Attr.getName() << ExpectedFunctionOrMethod;
659 return;
660 }
661
662 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
663 S.Context));
664}
665
666static void handleNoSanitizeThread(Sema &S, Decl *D,
667 const AttributeList &Attr) {
668 assert(!Attr.isInvalid());
669
670 if (!checkAttributeNumArgs(S, Attr, 0))
671 return;
672
673 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
674 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
675 << Attr.getName() << ExpectedFunctionOrMethod;
676 return;
677 }
678
679 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
680 S.Context));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000681}
682
Michael Hana9171bc2012-08-03 17:40:43 +0000683static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
684 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000685 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000686 assert(!Attr.isInvalid());
687
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000688 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000689 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000690
691 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000692 ValueDecl *VD = dyn_cast<ValueDecl>(D);
693 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000694 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
695 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000696 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000697 }
698
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000699 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000700 QualType QT = VD->getType();
701 if (!QT->isDependentType()) {
702 const RecordType *RT = getRecordType(QT);
703 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000704 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000705 << Attr.getName();
706 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000707 }
708 }
709
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000710 // Check that all arguments are lockable objects.
711 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000712 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000713 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000714
Michael Han3be3b442012-07-23 18:48:41 +0000715 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000716}
717
Michael Hana9171bc2012-08-03 17:40:43 +0000718static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000719 const AttributeList &Attr) {
720 SmallVector<Expr*, 1> Args;
721 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
722 return;
723
724 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000725 D->addAttr(::new (S.Context)
726 AcquiredAfterAttr(Attr.getRange(), S.Context,
727 StartArg, Args.size(),
728 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000729}
730
Michael Hana9171bc2012-08-03 17:40:43 +0000731static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000732 const AttributeList &Attr) {
733 SmallVector<Expr*, 1> Args;
734 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
735 return;
736
737 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000738 D->addAttr(::new (S.Context)
739 AcquiredBeforeAttr(Attr.getRange(), S.Context,
740 StartArg, Args.size(),
741 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000742}
743
Michael Hana9171bc2012-08-03 17:40:43 +0000744static bool checkLockFunAttrCommon(Sema &S, Decl *D,
745 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000746 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000747 assert(!Attr.isInvalid());
748
749 // zero or more arguments ok
750
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000751 // check that the attribute is applied to a function
752 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000753 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
754 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000755 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000756 }
757
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000758 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000759 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000760
Michael Han3be3b442012-07-23 18:48:41 +0000761 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000762}
763
Michael Hana9171bc2012-08-03 17:40:43 +0000764static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000765 const AttributeList &Attr) {
766 SmallVector<Expr*, 1> Args;
767 if (!checkLockFunAttrCommon(S, D, Attr, Args))
768 return;
769
770 unsigned Size = Args.size();
771 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000772 D->addAttr(::new (S.Context)
773 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
774 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000775}
776
Michael Hana9171bc2012-08-03 17:40:43 +0000777static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000778 const AttributeList &Attr) {
779 SmallVector<Expr*, 1> Args;
780 if (!checkLockFunAttrCommon(S, D, Attr, Args))
781 return;
782
783 unsigned Size = Args.size();
784 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000785 D->addAttr(::new (S.Context)
786 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
787 StartArg, Size,
788 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000789}
790
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000791static void handleAssertSharedLockAttr(Sema &S, Decl *D,
792 const AttributeList &Attr) {
793 SmallVector<Expr*, 1> Args;
794 if (!checkLockFunAttrCommon(S, D, Attr, Args))
795 return;
796
797 unsigned Size = Args.size();
798 Expr **StartArg = Size == 0 ? 0 : &Args[0];
799 D->addAttr(::new (S.Context)
800 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
801 Attr.getAttributeSpellingListIndex()));
802}
803
804static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
805 const AttributeList &Attr) {
806 SmallVector<Expr*, 1> Args;
807 if (!checkLockFunAttrCommon(S, D, Attr, Args))
808 return;
809
810 unsigned Size = Args.size();
811 Expr **StartArg = Size == 0 ? 0 : &Args[0];
812 D->addAttr(::new (S.Context)
813 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
814 StartArg, Size,
815 Attr.getAttributeSpellingListIndex()));
816}
817
818
Michael Hana9171bc2012-08-03 17:40:43 +0000819static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
820 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000821 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000822 assert(!Attr.isInvalid());
823
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000824 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000825 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000826
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000827 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000828 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
829 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000830 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000831 }
832
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000833 if (!isIntOrBool(Attr.getArg(0))) {
834 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Han3be3b442012-07-23 18:48:41 +0000835 << Attr.getName();
836 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000837 }
838
839 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000840 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000841
Michael Han3be3b442012-07-23 18:48:41 +0000842 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000843}
844
Michael Hana9171bc2012-08-03 17:40:43 +0000845static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000846 const AttributeList &Attr) {
847 SmallVector<Expr*, 2> Args;
848 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
849 return;
850
851 unsigned Size = Args.size();
852 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000853 D->addAttr(::new (S.Context)
854 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
855 Attr.getArg(0), StartArg, Size,
856 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000857}
858
Michael Hana9171bc2012-08-03 17:40:43 +0000859static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000860 const AttributeList &Attr) {
861 SmallVector<Expr*, 2> Args;
862 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
863 return;
864
865 unsigned Size = Args.size();
866 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000867 D->addAttr(::new (S.Context)
868 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
869 Attr.getArg(0), StartArg, Size,
870 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000871}
872
Michael Hana9171bc2012-08-03 17:40:43 +0000873static bool checkLocksRequiredCommon(Sema &S, Decl *D,
874 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000875 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000876 assert(!Attr.isInvalid());
877
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000878 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000879 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000880
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000881 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000882 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
883 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000884 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000885 }
886
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000887 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000888 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000889 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000890 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000891
Michael Han3be3b442012-07-23 18:48:41 +0000892 return true;
893}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000894
Michael Hana9171bc2012-08-03 17:40:43 +0000895static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000896 const AttributeList &Attr) {
897 SmallVector<Expr*, 1> Args;
898 if (!checkLocksRequiredCommon(S, D, Attr, Args))
899 return;
900
901 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000902 D->addAttr(::new (S.Context)
903 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
904 StartArg, Args.size(),
905 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000906}
907
Michael Hana9171bc2012-08-03 17:40:43 +0000908static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000909 const AttributeList &Attr) {
910 SmallVector<Expr*, 1> Args;
911 if (!checkLocksRequiredCommon(S, D, Attr, Args))
912 return;
913
914 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000915 D->addAttr(::new (S.Context)
916 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
917 StartArg, Args.size(),
918 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000919}
920
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000921static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000922 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000923 assert(!Attr.isInvalid());
924
925 // zero or more arguments ok
926
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000927 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000928 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
929 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000930 return;
931 }
932
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000933 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000934 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000935 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000936 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000937 Expr **StartArg = Size == 0 ? 0 : &Args[0];
938
Michael Han99315932013-01-24 16:46:58 +0000939 D->addAttr(::new (S.Context)
940 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
941 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000942}
943
944static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000945 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000946 assert(!Attr.isInvalid());
947
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000948 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000949 return;
950
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000951 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000952 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
953 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000954 return;
955 }
956
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000957 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000958 SmallVector<Expr*, 1> Args;
959 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
960 unsigned Size = Args.size();
961 if (Size == 0)
962 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000963
Michael Han99315932013-01-24 16:46:58 +0000964 D->addAttr(::new (S.Context)
965 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
966 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000967}
968
969static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000970 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000971 assert(!Attr.isInvalid());
972
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000973 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000974 return;
975
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000976 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000977 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
978 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000979 return;
980 }
981
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000982 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000983 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000984 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000985 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000986 if (Size == 0)
987 return;
988 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000989
Michael Han99315932013-01-24 16:46:58 +0000990 D->addAttr(::new (S.Context)
991 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
992 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000993}
994
995
Chandler Carruthedc2c642011-07-02 00:01:44 +0000996static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
997 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +0000998 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
999 if (TD == 0) {
1000 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1001 // and type-ids.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001002 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +00001003 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001004 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001005
Richard Smith1f5a4322013-01-13 02:11:23 +00001006 // Remember this typedef decl, we will need it later for diagnostics.
1007 S.ExtVectorDecls.push_back(TD);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001008}
1009
Chandler Carruthedc2c642011-07-02 00:01:44 +00001010static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001011 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001012 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001013 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001014
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001015 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001016 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001017 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001018 // If the alignment is less than or equal to 8 bits, the packed attribute
1019 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001020 if (!FD->getType()->isDependentType() &&
1021 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001022 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001023 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001024 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001025 else
Michael Han99315932013-01-24 16:46:58 +00001026 FD->addAttr(::new (S.Context)
1027 PackedAttr(Attr.getRange(), S.Context,
1028 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001029 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001030 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001031}
1032
Chandler Carruthedc2c642011-07-02 00:01:44 +00001033static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +00001034 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001035 RD->addAttr(::new (S.Context)
1036 MsStructAttr(Attr.getRange(), S.Context,
1037 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001038 else
1039 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1040}
1041
Chandler Carruthedc2c642011-07-02 00:01:44 +00001042static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001043 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001044 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001045 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001046
Ted Kremenek1f672822010-02-18 03:08:58 +00001047 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001048 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +00001049 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +00001050 D->addAttr(::new (S.Context)
1051 IBActionAttr(Attr.getRange(), S.Context,
1052 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +00001053 return;
1054 }
1055
Ted Kremenekd68ec812011-02-04 06:54:16 +00001056 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +00001057}
1058
Ted Kremenek7fd17232011-09-29 07:02:25 +00001059static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1060 // The IBOutlet/IBOutletCollection attributes only apply to instance
1061 // variables or properties of Objective-C classes. The outlet must also
1062 // have an object reference type.
1063 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1064 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001065 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001066 << Attr.getName() << VD->getType() << 0;
1067 return false;
1068 }
1069 }
1070 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1071 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001072 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001073 << Attr.getName() << PD->getType() << 1;
1074 return false;
1075 }
1076 }
1077 else {
1078 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1079 return false;
1080 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001081
Ted Kremenek7fd17232011-09-29 07:02:25 +00001082 return true;
1083}
1084
Chandler Carruthedc2c642011-07-02 00:01:44 +00001085static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001086 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001087 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +00001088 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001089
1090 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001091 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001092
Michael Han99315932013-01-24 16:46:58 +00001093 D->addAttr(::new (S.Context)
1094 IBOutletAttr(Attr.getRange(), S.Context,
1095 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001096}
1097
Chandler Carruthedc2c642011-07-02 00:01:44 +00001098static void handleIBOutletCollection(Sema &S, Decl *D,
1099 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001100
1101 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001102 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001103 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1104 return;
1105 }
1106
Ted Kremenek7fd17232011-09-29 07:02:25 +00001107 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001108 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001109
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001110 IdentifierInfo *II = Attr.getParameterName();
1111 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001112 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +00001113
John McCallba7bf592010-08-24 05:47:05 +00001114 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001115 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001116 if (!TypeRep) {
1117 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1118 return;
1119 }
John McCallba7bf592010-08-24 05:47:05 +00001120 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001121 // Diagnose use of non-object type in iboutletcollection attribute.
1122 // FIXME. Gnu attribute extension ignores use of builtin types in
1123 // attributes. So, __attribute__((iboutletcollection(char))) will be
1124 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001125 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001126 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1127 return;
1128 }
Michael Han99315932013-01-24 16:46:58 +00001129 D->addAttr(::new (S.Context)
1130 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1131 QT, Attr.getParameterLoc(),
1132 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001133}
1134
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001135static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001136 if (const RecordType *UT = T->getAsUnionType())
1137 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1138 RecordDecl *UD = UT->getDecl();
1139 for (RecordDecl::field_iterator it = UD->field_begin(),
1140 itend = UD->field_end(); it != itend; ++it) {
1141 QualType QT = it->getType();
1142 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1143 T = QT;
1144 return;
1145 }
1146 }
1147 }
1148}
1149
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001150static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001151 if (!isFunctionOrMethod(D)) {
1152 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1153 << "alloc_size" << ExpectedFunctionOrMethod;
1154 return;
1155 }
1156
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001157 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1158 return;
1159
1160 // In C++ the implicit 'this' function parameter also counts, and they are
1161 // counted from one.
1162 bool HasImplicitThisParam = isInstanceMethod(D);
Argyrios Kyrtzidise9365052013-02-22 06:58:28 +00001163 unsigned NumArgs;
1164 if (hasFunctionProto(D))
1165 NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1166 else
1167 NumArgs = 0;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001168
1169 SmallVector<unsigned, 8> SizeArgs;
1170
1171 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1172 E = Attr.arg_end(); I!=E; ++I) {
1173 // The argument must be an integer constant expression.
1174 Expr *Ex = *I;
1175 llvm::APSInt ArgNum;
1176 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1177 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1178 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1179 << "alloc_size" << Ex->getSourceRange();
1180 return;
1181 }
1182
1183 uint64_t x = ArgNum.getZExtValue();
1184
1185 if (x < 1 || x > NumArgs) {
1186 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1187 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1188 return;
1189 }
1190
1191 --x;
1192 if (HasImplicitThisParam) {
1193 if (x == 0) {
1194 S.Diag(Attr.getLoc(),
1195 diag::err_attribute_invalid_implicit_this_argument)
1196 << "alloc_size" << Ex->getSourceRange();
1197 return;
1198 }
1199 --x;
1200 }
1201
1202 // check if the function argument is of an integer type
1203 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1204 if (!T->isIntegerType()) {
1205 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1206 << "alloc_size" << Ex->getSourceRange();
1207 return;
1208 }
1209
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001210 SizeArgs.push_back(x);
1211 }
1212
1213 // check if the function returns a pointer
1214 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1215 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1216 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1217 }
1218
Michael Han99315932013-01-24 16:46:58 +00001219 D->addAttr(::new (S.Context)
1220 AllocSizeAttr(Attr.getRange(), S.Context,
1221 SizeArgs.data(), SizeArgs.size(),
1222 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001223}
1224
Chandler Carruthedc2c642011-07-02 00:01:44 +00001225static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001226 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1227 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001228 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001229 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001230 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001231 return;
1232 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001233
Chandler Carruth743682b2010-11-16 08:35:43 +00001234 // In C++ the implicit 'this' function parameter also counts, and they are
1235 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001236 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewyckye1121512013-01-24 01:12:16 +00001237 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001238
1239 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001240 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001241
Nick Lewyckye1121512013-01-24 01:12:16 +00001242 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1243 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001244 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001245 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001246 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001247 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1248 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001249 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1250 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001251 return;
1252 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001253
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001254 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001255
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001256 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001257 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +00001258 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001259 return;
1260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001261
Ted Kremenek5224e6a2008-07-21 22:09:15 +00001262 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001263 if (HasImplicitThisParam) {
1264 if (x == 0) {
1265 S.Diag(Attr.getLoc(),
1266 diag::err_attribute_invalid_implicit_this_argument)
1267 << "nonnull" << Ex->getSourceRange();
1268 return;
1269 }
1270 --x;
1271 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001272
1273 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001274 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001275 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001276
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001277 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001278 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001279 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001280 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001281 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001282 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001283
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001284 NonNullArgs.push_back(x);
1285 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001286
1287 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1288 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001289 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001290 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1291 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001292 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001293 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001294 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001295 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001296
Ted Kremenek22813f42010-10-21 18:49:36 +00001297 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001298 if (NonNullArgs.empty()) {
1299 // Warn the trivial case only if attribute is not coming from a
1300 // macro instantiation.
1301 if (Attr.getLoc().isFileID())
1302 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001303 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001304 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001305 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001306
Nick Lewyckye1121512013-01-24 01:12:16 +00001307 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001308 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001309 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001310 D->addAttr(::new (S.Context)
1311 NonNullAttr(Attr.getRange(), S.Context, start, size,
1312 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001313}
1314
Chandler Carruthedc2c642011-07-02 00:01:44 +00001315static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001316 // This attribute must be applied to a function declaration.
1317 // The first argument to the attribute must be a string,
1318 // the name of the resource, for example "malloc".
1319 // The following arguments must be argument indexes, the arguments must be
1320 // of integer type for Returns, otherwise of pointer type.
1321 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001322 // after being held. free() should be __attribute((ownership_takes)), whereas
1323 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001324
1325 if (!AL.getParameterName()) {
1326 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1327 << AL.getName()->getName() << 1;
1328 return;
1329 }
1330 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001331 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001332 switch (AL.getKind()) {
1333 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001334 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001335 if (AL.getNumArgs() < 1) {
1336 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1337 return;
1338 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001339 break;
1340 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001341 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001342 if (AL.getNumArgs() < 1) {
1343 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1344 return;
1345 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001346 break;
1347 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001348 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001349 if (AL.getNumArgs() > 1) {
1350 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1351 << AL.getNumArgs() + 1;
1352 return;
1353 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001354 break;
1355 default:
1356 // This should never happen given how we are called.
1357 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001358 }
1359
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001360 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001361 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1362 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001363 return;
1364 }
1365
Chandler Carruth743682b2010-11-16 08:35:43 +00001366 // In C++ the implicit 'this' function parameter also counts, and they are
1367 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001368 bool HasImplicitThisParam = isInstanceMethod(D);
1369 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001370
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001371 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001372
1373 // Normalize the argument, __foo__ becomes foo.
1374 if (Module.startswith("__") && Module.endswith("__"))
1375 Module = Module.substr(2, Module.size() - 4);
1376
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001377 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001378
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001379 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1380 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001381
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001382 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001383 llvm::APSInt ArgNum(32);
1384 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1385 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1386 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1387 << AL.getName()->getName() << IdxExpr->getSourceRange();
1388 continue;
1389 }
1390
1391 unsigned x = (unsigned) ArgNum.getZExtValue();
1392
1393 if (x > NumArgs || x < 1) {
1394 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1395 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1396 continue;
1397 }
1398 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001399 if (HasImplicitThisParam) {
1400 if (x == 0) {
1401 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1402 << "ownership" << IdxExpr->getSourceRange();
1403 return;
1404 }
1405 --x;
1406 }
1407
Ted Kremenekd21139a2010-07-31 01:52:11 +00001408 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001409 case OwnershipAttr::Takes:
1410 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001411 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001412 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001413 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1414 // FIXME: Should also highlight argument in decl.
1415 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001416 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001417 << "pointer"
1418 << IdxExpr->getSourceRange();
1419 continue;
1420 }
1421 break;
1422 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001423 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001424 if (AL.getNumArgs() > 1) {
1425 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001426 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001427 llvm::APSInt ArgNum(32);
1428 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1429 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1430 S.Diag(AL.getLoc(), diag::err_ownership_type)
1431 << "ownership_returns" << "integer"
1432 << IdxExpr->getSourceRange();
1433 return;
1434 }
1435 }
1436 break;
1437 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001438 } // switch
1439
1440 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001441 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001442 i = D->specific_attr_begin<OwnershipAttr>(),
1443 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001444 i != e; ++i) {
1445 if ((*i)->getOwnKind() != K) {
1446 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1447 I!=E; ++I) {
1448 if (x == *I) {
1449 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1450 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001451 }
1452 }
1453 }
1454 }
1455 OwnershipArgs.push_back(x);
1456 }
1457
1458 unsigned* start = OwnershipArgs.data();
1459 unsigned size = OwnershipArgs.size();
1460 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001461
1462 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1463 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1464 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001465 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001466
Michael Han99315932013-01-24 16:46:58 +00001467 D->addAttr(::new (S.Context)
1468 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1469 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001470}
1471
Chandler Carruthedc2c642011-07-02 00:01:44 +00001472static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001473 // Check the attribute arguments.
1474 if (Attr.getNumArgs() > 1) {
1475 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1476 return;
1477 }
1478
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001479 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001480 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001481 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001482 return;
1483 }
1484
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001485 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001486
Rafael Espindolac18086a2010-02-23 22:00:30 +00001487 // gcc rejects
1488 // class c {
1489 // static int a __attribute__((weakref ("v2")));
1490 // static int b() __attribute__((weakref ("f3")));
1491 // };
1492 // and ignores the attributes of
1493 // void f(void) {
1494 // static int a __attribute__((weakref ("v2")));
1495 // }
1496 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001497 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001498 if (!Ctx->isFileContext()) {
1499 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001500 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001501 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001502 }
1503
1504 // The GCC manual says
1505 //
1506 // At present, a declaration to which `weakref' is attached can only
1507 // be `static'.
1508 //
1509 // It also says
1510 //
1511 // Without a TARGET,
1512 // given as an argument to `weakref' or to `alias', `weakref' is
1513 // equivalent to `weak'.
1514 //
1515 // gcc 4.4.1 will accept
1516 // int a7 __attribute__((weakref));
1517 // as
1518 // int a7 __attribute__((weak));
1519 // This looks like a bug in gcc. We reject that for now. We should revisit
1520 // it if this behaviour is actually used.
1521
Rafael Espindolac18086a2010-02-23 22:00:30 +00001522 // GCC rejects
1523 // static ((alias ("y"), weakref)).
1524 // Should we? How to check that weakref is before or after alias?
1525
1526 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001527 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001528 Arg = Arg->IgnoreParenCasts();
1529 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1530
Douglas Gregorfb65e592011-07-27 05:40:30 +00001531 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001532 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1533 << "weakref" << 1;
1534 return;
1535 }
1536 // GCC will accept anything as the argument of weakref. Should we
1537 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001538 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001539 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001540 }
1541
Michael Han99315932013-01-24 16:46:58 +00001542 D->addAttr(::new (S.Context)
1543 WeakRefAttr(Attr.getRange(), S.Context,
1544 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001545}
1546
Chandler Carruthedc2c642011-07-02 00:01:44 +00001547static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001548 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001549 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001550 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001551 return;
1552 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001553
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001554 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001555 Arg = Arg->IgnoreParenCasts();
1556 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001557
Douglas Gregorfb65e592011-07-27 05:40:30 +00001558 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001559 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001560 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001561 return;
1562 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001563
Douglas Gregore8bbc122011-09-02 00:18:52 +00001564 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001565 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1566 return;
1567 }
1568
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001569 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001570
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001571 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00001572 Str->getString(),
1573 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001574}
1575
Quentin Colombet4e172062012-11-01 23:55:47 +00001576static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1577 // Check the attribute arguments.
1578 if (!checkAttributeNumArgs(S, Attr, 0))
1579 return;
1580
1581 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1583 << Attr.getName() << ExpectedFunctionOrMethod;
1584 return;
1585 }
1586
Michael Han99315932013-01-24 16:46:58 +00001587 D->addAttr(::new (S.Context)
1588 MinSizeAttr(Attr.getRange(), S.Context,
1589 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001590}
1591
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001592static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1593 // Check the attribute arguments.
1594 if (!checkAttributeNumArgs(S, Attr, 0))
1595 return;
1596
1597 if (!isa<FunctionDecl>(D)) {
1598 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1599 << Attr.getName() << ExpectedFunction;
1600 return;
1601 }
1602
1603 if (D->hasAttr<HotAttr>()) {
1604 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1605 << Attr.getName() << "hot";
1606 return;
1607 }
1608
Michael Han99315932013-01-24 16:46:58 +00001609 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1610 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001611}
1612
1613static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1614 // Check the attribute arguments.
1615 if (!checkAttributeNumArgs(S, Attr, 0))
1616 return;
1617
1618 if (!isa<FunctionDecl>(D)) {
1619 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1620 << Attr.getName() << ExpectedFunction;
1621 return;
1622 }
1623
1624 if (D->hasAttr<ColdAttr>()) {
1625 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1626 << Attr.getName() << "cold";
1627 return;
1628 }
1629
Michael Han99315932013-01-24 16:46:58 +00001630 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1631 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001632}
1633
Chandler Carruthedc2c642011-07-02 00:01:44 +00001634static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001635 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001636 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001637 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001638
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001639 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001640 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001641 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001642 return;
1643 }
1644
Michael Han99315932013-01-24 16:46:58 +00001645 D->addAttr(::new (S.Context)
1646 NakedAttr(Attr.getRange(), S.Context,
1647 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001648}
1649
Chandler Carruthedc2c642011-07-02 00:01:44 +00001650static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1651 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001652 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001653 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001654 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1655 return;
1656 }
1657
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001658 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001659 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001660 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001661 return;
1662 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001663
Michael Han99315932013-01-24 16:46:58 +00001664 D->addAttr(::new (S.Context)
1665 AlwaysInlineAttr(Attr.getRange(), S.Context,
1666 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001667}
1668
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001669static void handleTLSModelAttr(Sema &S, Decl *D,
1670 const AttributeList &Attr) {
1671 // Check the attribute arguments.
1672 if (Attr.getNumArgs() != 1) {
1673 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1674 return;
1675 }
1676
1677 Expr *Arg = Attr.getArg(0);
1678 Arg = Arg->IgnoreParenCasts();
1679 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1680
1681 // Check that it is a string.
1682 if (!Str) {
1683 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1684 return;
1685 }
1686
Richard Smithfd3834f2013-04-13 02:43:54 +00001687 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001688 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1689 << Attr.getName() << ExpectedTLSVar;
1690 return;
1691 }
1692
1693 // Check that the value.
1694 StringRef Model = Str->getString();
1695 if (Model != "global-dynamic" && Model != "local-dynamic"
1696 && Model != "initial-exec" && Model != "local-exec") {
1697 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1698 return;
1699 }
1700
Michael Han99315932013-01-24 16:46:58 +00001701 D->addAttr(::new (S.Context)
1702 TLSModelAttr(Attr.getRange(), S.Context, Model,
1703 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001704}
1705
Chandler Carruthedc2c642011-07-02 00:01:44 +00001706static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001707 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001708 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001709 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1710 return;
1711 }
Mike Stump11289f42009-09-09 15:08:12 +00001712
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001713 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001714 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001715 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001716 D->addAttr(::new (S.Context)
1717 MallocAttr(Attr.getRange(), S.Context,
1718 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001719 return;
1720 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001721 }
1722
Ted Kremenek08479ae2009-08-15 00:51:46 +00001723 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001724}
1725
Chandler Carruthedc2c642011-07-02 00:01:44 +00001726static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001727 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001728 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001729 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001730
Michael Han99315932013-01-24 16:46:58 +00001731 D->addAttr(::new (S.Context)
1732 MayAliasAttr(Attr.getRange(), S.Context,
1733 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001734}
1735
Chandler Carruthedc2c642011-07-02 00:01:44 +00001736static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001737 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001738 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001739 D->addAttr(::new (S.Context)
1740 NoCommonAttr(Attr.getRange(), S.Context,
1741 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001742 else
1743 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001744 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001745}
1746
Chandler Carruthedc2c642011-07-02 00:01:44 +00001747static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001748 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001749 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001750 D->addAttr(::new (S.Context)
1751 CommonAttr(Attr.getRange(), S.Context,
1752 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001753 else
1754 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001755 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001756}
1757
Chandler Carruthedc2c642011-07-02 00:01:44 +00001758static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001759 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001760
1761 if (S.CheckNoReturnAttr(attr)) return;
1762
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001763 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001764 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001765 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001766 return;
1767 }
1768
Michael Han99315932013-01-24 16:46:58 +00001769 D->addAttr(::new (S.Context)
1770 NoReturnAttr(attr.getRange(), S.Context,
1771 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001772}
1773
1774bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001775 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001776 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1777 attr.setInvalid();
1778 return true;
1779 }
1780
1781 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001782}
1783
Chandler Carruthedc2c642011-07-02 00:01:44 +00001784static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1785 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001786
1787 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1788 // because 'analyzer_noreturn' does not impact the type.
1789
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001790 if(!checkAttributeNumArgs(S, Attr, 0))
1791 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001792
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001793 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1794 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001795 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1796 && !VD->getType()->isFunctionPointerType())) {
1797 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001798 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001799 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001800 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001801 return;
1802 }
1803 }
1804
Michael Han99315932013-01-24 16:46:58 +00001805 D->addAttr(::new (S.Context)
1806 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1807 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001808}
1809
Richard Smith10876ef2013-01-17 01:30:42 +00001810static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1811 const AttributeList &Attr) {
1812 // C++11 [dcl.attr.noreturn]p1:
1813 // The attribute may be applied to the declarator-id in a function
1814 // declaration.
1815 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1816 if (!FD) {
1817 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1818 << Attr.getName() << ExpectedFunctionOrMethod;
1819 return;
1820 }
1821
Michael Han99315932013-01-24 16:46:58 +00001822 D->addAttr(::new (S.Context)
1823 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1824 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001825}
1826
John Thompsoncdb847ba2010-08-09 21:53:52 +00001827// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001828static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001829/*
1830 Returning a Vector Class in Registers
1831
Eric Christopherbc638a82010-12-01 22:13:54 +00001832 According to the PPU ABI specifications, a class with a single member of
1833 vector type is returned in memory when used as the return value of a function.
1834 This results in inefficient code when implementing vector classes. To return
1835 the value in a single vector register, add the vecreturn attribute to the
1836 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001837
1838 Example:
1839
1840 struct Vector
1841 {
1842 __vector float xyzw;
1843 } __attribute__((vecreturn));
1844
1845 Vector Add(Vector lhs, Vector rhs)
1846 {
1847 Vector result;
1848 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1849 return result; // This will be returned in a register
1850 }
1851*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001852 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001853 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001854 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001855 return;
1856 }
1857
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001858 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001859 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1860 return;
1861 }
1862
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001863 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001864 int count = 0;
1865
1866 if (!isa<CXXRecordDecl>(record)) {
1867 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1868 return;
1869 }
1870
1871 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1872 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1873 return;
1874 }
1875
Eric Christopherbc638a82010-12-01 22:13:54 +00001876 for (RecordDecl::field_iterator iter = record->field_begin();
1877 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001878 if ((count == 1) || !iter->getType()->isVectorType()) {
1879 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1880 return;
1881 }
1882 count++;
1883 }
1884
Michael Han99315932013-01-24 16:46:58 +00001885 D->addAttr(::new (S.Context)
1886 VecReturnAttr(Attr.getRange(), S.Context,
1887 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001888}
1889
Richard Smithe233fbf2013-01-28 22:42:45 +00001890static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1891 const AttributeList &Attr) {
1892 if (isa<ParmVarDecl>(D)) {
1893 // [[carries_dependency]] can only be applied to a parameter if it is a
1894 // parameter of a function declaration or lambda.
1895 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1896 S.Diag(Attr.getLoc(),
1897 diag::err_carries_dependency_param_not_function_decl);
1898 return;
1899 }
1900 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001901 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001902 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001903 return;
1904 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001905
1906 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1907 Attr.getRange(), S.Context,
1908 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001909}
1910
Chandler Carruthedc2c642011-07-02 00:01:44 +00001911static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001912 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001913 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001914 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001915 return;
1916 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001917
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001918 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001919 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001920 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001921 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001922 return;
1923 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001924
Michael Han99315932013-01-24 16:46:58 +00001925 D->addAttr(::new (S.Context)
1926 UnusedAttr(Attr.getRange(), S.Context,
1927 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001928}
1929
Rafael Espindola70107f92011-10-03 14:59:42 +00001930static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1931 const AttributeList &Attr) {
1932 // check the attribute arguments.
1933 if (Attr.hasParameterOrArguments()) {
1934 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1935 return;
1936 }
1937
1938 if (!isa<FunctionDecl>(D)) {
1939 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1940 << Attr.getName() << ExpectedFunction;
1941 return;
1942 }
1943
Michael Han99315932013-01-24 16:46:58 +00001944 D->addAttr(::new (S.Context)
1945 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1946 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00001947}
1948
Chandler Carruthedc2c642011-07-02 00:01:44 +00001949static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001950 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001951 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001952 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1953 return;
1954 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001955
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001956 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001957 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001958 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1959 return;
1960 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001961 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001962 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001963 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001964 return;
1965 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001966
Michael Han99315932013-01-24 16:46:58 +00001967 D->addAttr(::new (S.Context)
1968 UsedAttr(Attr.getRange(), S.Context,
1969 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001970}
1971
Chandler Carruthedc2c642011-07-02 00:01:44 +00001972static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001973 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001974 if (Attr.getNumArgs() > 1) {
1975 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001976 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001977 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001978
1979 int priority = 65535; // FIXME: Do not hardcode such constants.
1980 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001981 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001982 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001983 if (E->isTypeDependent() || E->isValueDependent() ||
1984 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001985 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001986 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001987 return;
1988 }
1989 priority = Idx.getZExtValue();
1990 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001991
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001992 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001993 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001994 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001995 return;
1996 }
1997
Michael Han99315932013-01-24 16:46:58 +00001998 D->addAttr(::new (S.Context)
1999 ConstructorAttr(Attr.getRange(), S.Context, priority,
2000 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002001}
2002
Chandler Carruthedc2c642011-07-02 00:01:44 +00002003static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002004 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002005 if (Attr.getNumArgs() > 1) {
2006 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002007 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002008 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002009
2010 int priority = 65535; // FIXME: Do not hardcode such constants.
2011 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002012 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00002013 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002014 if (E->isTypeDependent() || E->isValueDependent() ||
2015 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002016 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002017 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00002018 return;
2019 }
2020 priority = Idx.getZExtValue();
2021 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002022
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002023 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002024 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002025 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002026 return;
2027 }
2028
Michael Han99315932013-01-24 16:46:58 +00002029 D->addAttr(::new (S.Context)
2030 DestructorAttr(Attr.getRange(), S.Context, priority,
2031 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002032}
2033
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002034template <typename AttrTy>
2035static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
2036 const char *Name) {
Chris Lattner190aa102011-02-24 05:42:24 +00002037 unsigned NumArgs = Attr.getNumArgs();
2038 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00002039 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002040 return;
2041 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002042
2043 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002044 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00002045 if (NumArgs == 1) {
2046 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00002047 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00002048 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002049 << Name;
Fariborz Jahanian551063102010-10-06 21:18:44 +00002050 return;
2051 }
Chris Lattner190aa102011-02-24 05:42:24 +00002052 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00002053 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002054
Michael Han99315932013-01-24 16:46:58 +00002055 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2056 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002057}
2058
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002059static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2060 const AttributeList &Attr) {
2061 unsigned NumArgs = Attr.getNumArgs();
2062 if (NumArgs > 0) {
2063 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2064 return;
2065 }
2066
Michael Han99315932013-01-24 16:46:58 +00002067 D->addAttr(::new (S.Context)
2068 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2069 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002070}
2071
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002072static void handleObjCRootClassAttr(Sema &S, Decl *D,
2073 const AttributeList &Attr) {
2074 if (!isa<ObjCInterfaceDecl>(D)) {
2075 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2076 return;
2077 }
2078
2079 unsigned NumArgs = Attr.getNumArgs();
2080 if (NumArgs > 0) {
2081 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2082 return;
2083 }
2084
Michael Han99315932013-01-24 16:46:58 +00002085 D->addAttr(::new (S.Context)
2086 ObjCRootClassAttr(Attr.getRange(), S.Context,
2087 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002088}
2089
Michael Han99315932013-01-24 16:46:58 +00002090static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2091 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002092 if (!isa<ObjCInterfaceDecl>(D)) {
2093 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2094 return;
2095 }
2096
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002097 unsigned NumArgs = Attr.getNumArgs();
2098 if (NumArgs > 0) {
2099 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2100 return;
2101 }
2102
Michael Han99315932013-01-24 16:46:58 +00002103 D->addAttr(::new (S.Context)
2104 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2105 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002106}
2107
Jordy Rose740b0c22012-05-08 03:27:22 +00002108static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2109 IdentifierInfo *Platform,
2110 VersionTuple Introduced,
2111 VersionTuple Deprecated,
2112 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002113 StringRef PlatformName
2114 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2115 if (PlatformName.empty())
2116 PlatformName = Platform->getName();
2117
2118 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2119 // of these steps are needed).
2120 if (!Introduced.empty() && !Deprecated.empty() &&
2121 !(Introduced <= Deprecated)) {
2122 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2123 << 1 << PlatformName << Deprecated.getAsString()
2124 << 0 << Introduced.getAsString();
2125 return true;
2126 }
2127
2128 if (!Introduced.empty() && !Obsoleted.empty() &&
2129 !(Introduced <= Obsoleted)) {
2130 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2131 << 2 << PlatformName << Obsoleted.getAsString()
2132 << 0 << Introduced.getAsString();
2133 return true;
2134 }
2135
2136 if (!Deprecated.empty() && !Obsoleted.empty() &&
2137 !(Deprecated <= Obsoleted)) {
2138 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2139 << 2 << PlatformName << Obsoleted.getAsString()
2140 << 1 << Deprecated.getAsString();
2141 return true;
2142 }
2143
2144 return false;
2145}
2146
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002147/// \brief Check whether the two versions match.
2148///
2149/// If either version tuple is empty, then they are assumed to match. If
2150/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2151static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2152 bool BeforeIsOkay) {
2153 if (X.empty() || Y.empty())
2154 return true;
2155
2156 if (X == Y)
2157 return true;
2158
2159 if (BeforeIsOkay && X < Y)
2160 return true;
2161
2162 return false;
2163}
2164
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002165AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002166 IdentifierInfo *Platform,
2167 VersionTuple Introduced,
2168 VersionTuple Deprecated,
2169 VersionTuple Obsoleted,
2170 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002171 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002172 bool Override,
2173 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002174 VersionTuple MergedIntroduced = Introduced;
2175 VersionTuple MergedDeprecated = Deprecated;
2176 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002177 bool FoundAny = false;
2178
Rafael Espindolac67f2232012-05-10 02:50:16 +00002179 if (D->hasAttrs()) {
2180 AttrVec &Attrs = D->getAttrs();
2181 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2182 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2183 if (!OldAA) {
2184 ++i;
2185 continue;
2186 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002187
Rafael Espindolac67f2232012-05-10 02:50:16 +00002188 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2189 if (OldPlatform != Platform) {
2190 ++i;
2191 continue;
2192 }
2193
2194 FoundAny = true;
2195 VersionTuple OldIntroduced = OldAA->getIntroduced();
2196 VersionTuple OldDeprecated = OldAA->getDeprecated();
2197 VersionTuple OldObsoleted = OldAA->getObsoleted();
2198 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002199
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002200 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2201 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2202 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2203 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002204 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002205 if (Override) {
2206 int Which = -1;
2207 VersionTuple FirstVersion;
2208 VersionTuple SecondVersion;
2209 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2210 Which = 0;
2211 FirstVersion = OldIntroduced;
2212 SecondVersion = Introduced;
2213 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2214 Which = 1;
2215 FirstVersion = Deprecated;
2216 SecondVersion = OldDeprecated;
2217 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2218 Which = 2;
2219 FirstVersion = Obsoleted;
2220 SecondVersion = OldObsoleted;
2221 }
2222
2223 if (Which == -1) {
2224 Diag(OldAA->getLocation(),
2225 diag::warn_mismatched_availability_override_unavail)
2226 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2227 } else {
2228 Diag(OldAA->getLocation(),
2229 diag::warn_mismatched_availability_override)
2230 << Which
2231 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2232 << FirstVersion.getAsString() << SecondVersion.getAsString();
2233 }
2234 Diag(Range.getBegin(), diag::note_overridden_method);
2235 } else {
2236 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2237 Diag(Range.getBegin(), diag::note_previous_attribute);
2238 }
2239
Rafael Espindolac67f2232012-05-10 02:50:16 +00002240 Attrs.erase(Attrs.begin() + i);
2241 --e;
2242 continue;
2243 }
2244
2245 VersionTuple MergedIntroduced2 = MergedIntroduced;
2246 VersionTuple MergedDeprecated2 = MergedDeprecated;
2247 VersionTuple MergedObsoleted2 = MergedObsoleted;
2248
2249 if (MergedIntroduced2.empty())
2250 MergedIntroduced2 = OldIntroduced;
2251 if (MergedDeprecated2.empty())
2252 MergedDeprecated2 = OldDeprecated;
2253 if (MergedObsoleted2.empty())
2254 MergedObsoleted2 = OldObsoleted;
2255
2256 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2257 MergedIntroduced2, MergedDeprecated2,
2258 MergedObsoleted2)) {
2259 Attrs.erase(Attrs.begin() + i);
2260 --e;
2261 continue;
2262 }
2263
2264 MergedIntroduced = MergedIntroduced2;
2265 MergedDeprecated = MergedDeprecated2;
2266 MergedObsoleted = MergedObsoleted2;
2267 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002268 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002269 }
2270
2271 if (FoundAny &&
2272 MergedIntroduced == Introduced &&
2273 MergedDeprecated == Deprecated &&
2274 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002275 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002276
Ted Kremenekb5445722013-04-06 00:34:27 +00002277 // Only create a new attribute if !Override, but we want to do
2278 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002279 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002280 MergedDeprecated, MergedObsoleted) &&
2281 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002282 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2283 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002284 Obsoleted, IsUnavailable, Message,
2285 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002286 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002287 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002288}
2289
Chandler Carruthedc2c642011-07-02 00:01:44 +00002290static void handleAvailabilityAttr(Sema &S, Decl *D,
2291 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002292 IdentifierInfo *Platform = Attr.getParameterName();
2293 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han99315932013-01-24 16:46:58 +00002294 unsigned Index = Attr.getAttributeSpellingListIndex();
2295
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002296 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002297 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2298 << Platform;
2299
Rafael Espindolac231fab2013-01-08 21:30:32 +00002300 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2301 if (!ND) {
2302 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2303 return;
2304 }
2305
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002306 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2307 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2308 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002309 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002310 StringRef Str;
2311 const StringLiteral *SE =
2312 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2313 if (SE)
2314 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002315
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002316 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002317 Platform,
2318 Introduced.Version,
2319 Deprecated.Version,
2320 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002321 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002322 /*Override=*/false,
2323 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002324 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002325 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002326}
2327
John McCalld041a9b2013-02-20 01:54:26 +00002328template <class T>
2329static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2330 typename T::VisibilityType value,
2331 unsigned attrSpellingListIndex) {
2332 T *existingAttr = D->getAttr<T>();
2333 if (existingAttr) {
2334 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2335 if (existingValue == value)
2336 return NULL;
2337 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2338 S.Diag(range.getBegin(), diag::note_previous_attribute);
2339 D->dropAttr<T>();
2340 }
2341 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2342}
2343
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002344VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002345 VisibilityAttr::VisibilityType Vis,
2346 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002347 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2348 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002349}
2350
John McCalld041a9b2013-02-20 01:54:26 +00002351TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2352 TypeVisibilityAttr::VisibilityType Vis,
2353 unsigned AttrSpellingListIndex) {
2354 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2355 AttrSpellingListIndex);
2356}
2357
2358static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2359 bool isTypeVisibility) {
2360 // Visibility attributes don't mean anything on a typedef.
2361 if (isa<TypedefNameDecl>(D)) {
2362 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2363 << Attr.getName();
2364 return;
2365 }
2366
2367 // 'type_visibility' can only go on a type or namespace.
2368 if (isTypeVisibility &&
2369 !(isa<TagDecl>(D) ||
2370 isa<ObjCInterfaceDecl>(D) ||
2371 isa<NamespaceDecl>(D))) {
2372 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2373 << Attr.getName() << ExpectedTypeOrNamespace;
2374 return;
2375 }
2376
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002377 // check the attribute arguments.
John McCalld041a9b2013-02-20 01:54:26 +00002378 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002379 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002380
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002381 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002382 Arg = Arg->IgnoreParenCasts();
2383 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002384
Douglas Gregorfb65e592011-07-27 05:40:30 +00002385 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002386 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld041a9b2013-02-20 01:54:26 +00002387 << (isTypeVisibility ? "type_visibility" : "visibility") << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002388 return;
2389 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002390
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002391 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002392 VisibilityAttr::VisibilityType type;
Michael Han99315932013-01-24 16:46:58 +00002393
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002394 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002395 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002396 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002397 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002398 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002399 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00002400 else if (TypeStr == "protected") {
2401 // Complain about attempts to use protected visibility on targets
2402 // (like Darwin) that don't support it.
2403 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2404 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2405 type = VisibilityAttr::Default;
2406 } else {
2407 type = VisibilityAttr::Protected;
2408 }
2409 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002410 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002411 return;
2412 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002413
Michael Han99315932013-01-24 16:46:58 +00002414 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002415 clang::Attr *newAttr;
2416 if (isTypeVisibility) {
2417 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2418 (TypeVisibilityAttr::VisibilityType) type,
2419 Index);
2420 } else {
2421 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2422 }
2423 if (newAttr)
2424 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002425}
2426
Chandler Carruthedc2c642011-07-02 00:01:44 +00002427static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2428 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002429 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2430 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002432 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002433 return;
2434 }
2435
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002436 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2437 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2438 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00002439 << "objc_method_family" << 1;
2440 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002441 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002442 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002443 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002444 return;
2445 }
2446
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002447 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002448 ObjCMethodFamilyAttr::FamilyKind family;
2449 if (param == "none")
2450 family = ObjCMethodFamilyAttr::OMF_None;
2451 else if (param == "alloc")
2452 family = ObjCMethodFamilyAttr::OMF_alloc;
2453 else if (param == "copy")
2454 family = ObjCMethodFamilyAttr::OMF_copy;
2455 else if (param == "init")
2456 family = ObjCMethodFamilyAttr::OMF_init;
2457 else if (param == "mutableCopy")
2458 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2459 else if (param == "new")
2460 family = ObjCMethodFamilyAttr::OMF_new;
2461 else {
2462 // Just warn and ignore it. This is future-proof against new
2463 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002464 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002465 return;
2466 }
2467
John McCall31168b02011-06-15 23:02:42 +00002468 if (family == ObjCMethodFamilyAttr::OMF_init &&
2469 !method->getResultType()->isObjCObjectPointerType()) {
2470 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2471 << method->getResultType();
2472 // Ignore the attribute.
2473 return;
2474 }
2475
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002476 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002477 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002478}
2479
Chandler Carruthedc2c642011-07-02 00:01:44 +00002480static void handleObjCExceptionAttr(Sema &S, Decl *D,
2481 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002482 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002483 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002484
Chris Lattner677a3582009-02-14 08:09:34 +00002485 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2486 if (OCI == 0) {
2487 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2488 return;
2489 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002490
Michael Han99315932013-01-24 16:46:58 +00002491 D->addAttr(::new (S.Context)
2492 ObjCExceptionAttr(Attr.getRange(), S.Context,
2493 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002494}
2495
Chandler Carruthedc2c642011-07-02 00:01:44 +00002496static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002497 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002498 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002499 return;
2500 }
Richard Smithdda56e42011-04-15 14:24:37 +00002501 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002502 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002503 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002504 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2505 return;
2506 }
2507 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002508 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2509 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002510 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002511 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2512 return;
2513 }
2514 }
2515 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002516 // It is okay to include this attribute on properties, e.g.:
2517 //
2518 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2519 //
2520 // In this case it follows tradition and suppresses an error in the above
2521 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002522 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002523 }
Michael Han99315932013-01-24 16:46:58 +00002524 D->addAttr(::new (S.Context)
2525 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2526 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002527}
2528
Mike Stumpd3bb5572009-07-24 19:02:52 +00002529static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002530handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002531 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002532 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002533 return;
2534 }
2535
2536 if (!isa<FunctionDecl>(D)) {
2537 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2538 return;
2539 }
2540
Michael Han99315932013-01-24 16:46:58 +00002541 D->addAttr(::new (S.Context)
2542 OverloadableAttr(Attr.getRange(), S.Context,
2543 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002544}
2545
Chandler Carruthedc2c642011-07-02 00:01:44 +00002546static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002547 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002548 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002549 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002550 return;
2551 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002552
Steve Naroff3405a732008-09-18 16:44:58 +00002553 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002554 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002555 return;
2556 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002557
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002558 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002559 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002560 type = BlocksAttr::ByRef;
2561 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002562 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002563 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002564 return;
2565 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002566
Michael Han99315932013-01-24 16:46:58 +00002567 D->addAttr(::new (S.Context)
2568 BlocksAttr(Attr.getRange(), S.Context, type,
2569 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002570}
2571
Chandler Carruthedc2c642011-07-02 00:01:44 +00002572static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002573 // check the attribute arguments.
2574 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002575 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002576 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002577 }
2578
John McCallb46f2872011-09-09 07:56:05 +00002579 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002580 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002581 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002582 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002583 if (E->isTypeDependent() || E->isValueDependent() ||
2584 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002585 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002586 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002587 return;
2588 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002589
John McCallb46f2872011-09-09 07:56:05 +00002590 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002591 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2592 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002593 return;
2594 }
John McCallb46f2872011-09-09 07:56:05 +00002595
2596 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002597 }
2598
John McCallb46f2872011-09-09 07:56:05 +00002599 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002600 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002601 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002602 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002603 if (E->isTypeDependent() || E->isValueDependent() ||
2604 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002605 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002606 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002607 return;
2608 }
2609 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002610
John McCallb46f2872011-09-09 07:56:05 +00002611 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002612 // FIXME: This error message could be improved, it would be nice
2613 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002614 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2615 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002616 return;
2617 }
2618 }
2619
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002620 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002621 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002622 if (isa<FunctionNoProtoType>(FT)) {
2623 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2624 return;
2625 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002626
Chris Lattner9363e312009-03-17 23:03:47 +00002627 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002628 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002629 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002630 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002631 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002632 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002633 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002634 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002635 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002636 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2637 if (!BD->isVariadic()) {
2638 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2639 return;
2640 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002641 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002642 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002643 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002644 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002645 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002646 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002647 int m = Ty->isFunctionPointerType() ? 0 : 1;
2648 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002649 return;
2650 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002651 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002653 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002654 return;
2655 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002656 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002657 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002658 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002659 return;
2660 }
Michael Han99315932013-01-24 16:46:58 +00002661 D->addAttr(::new (S.Context)
2662 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2663 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002664}
2665
Chandler Carruthedc2c642011-07-02 00:01:44 +00002666static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002667 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002668 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002669 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002670
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002671 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002672 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002673 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002674 return;
2675 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002676
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002677 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2678 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2679 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002680 return;
2681 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002682 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2683 if (MD->getResultType()->isVoidType()) {
2684 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2685 << Attr.getName() << 1;
2686 return;
2687 }
2688
Michael Han99315932013-01-24 16:46:58 +00002689 D->addAttr(::new (S.Context)
2690 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2691 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002692}
2693
Chandler Carruthedc2c642011-07-02 00:01:44 +00002694static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002695 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002696 if (Attr.hasParameterOrArguments()) {
2697 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002698 return;
2699 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002700
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002701 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002702 if (isa<CXXRecordDecl>(D)) {
2703 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2704 return;
2705 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002706 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2707 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002708 return;
2709 }
2710
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002711 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002712
Michael Han99315932013-01-24 16:46:58 +00002713 nd->addAttr(::new (S.Context)
2714 WeakAttr(Attr.getRange(), S.Context,
2715 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002716}
2717
Chandler Carruthedc2c642011-07-02 00:01:44 +00002718static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002719 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002720 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002721 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002722
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002723
2724 // weak_import only applies to variable & function declarations.
2725 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002726 if (!D->canBeWeakImported(isDef)) {
2727 if (isDef)
2728 S.Diag(Attr.getLoc(),
2729 diag::warn_attribute_weak_import_invalid_on_definition)
2730 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002731 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002732 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002733 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002734 // Nothing to warn about here.
2735 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002736 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002737 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002738
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002739 return;
2740 }
2741
Michael Han99315932013-01-24 16:46:58 +00002742 D->addAttr(::new (S.Context)
2743 WeakImportAttr(Attr.getRange(), S.Context,
2744 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002745}
2746
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002747// Handles reqd_work_group_size and work_group_size_hint.
2748static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002749 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002750 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2751 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2752
Nate Begemanf2758702009-06-26 06:32:41 +00002753 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002754 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002755
2756 unsigned WGSize[3];
2757 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002758 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002759 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002760 if (E->isTypeDependent() || E->isValueDependent() ||
2761 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002762 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002763 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002764 return;
2765 }
2766 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2767 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002768
2769 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2770 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2771 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2772 if (!(A->getXDim() == WGSize[0] &&
2773 A->getYDim() == WGSize[1] &&
2774 A->getZDim() == WGSize[2])) {
2775 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2776 Attr.getName();
2777 }
2778 }
2779
2780 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2781 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2782 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2783 if (!(A->getXDim() == WGSize[0] &&
2784 A->getYDim() == WGSize[1] &&
2785 A->getZDim() == WGSize[2])) {
2786 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2787 Attr.getName();
2788 }
2789 }
2790
2791 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2792 D->addAttr(::new (S.Context)
2793 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002794 WGSize[0], WGSize[1], WGSize[2],
2795 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002796 else
2797 D->addAttr(::new (S.Context)
2798 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002799 WGSize[0], WGSize[1], WGSize[2],
2800 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002801}
2802
Joey Goulyaba589c2013-03-08 09:42:32 +00002803static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2804 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2805
2806 // Attribute has 1 argument.
2807 if (!checkAttributeNumArgs(S, Attr, 1))
2808 return;
2809
2810 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2811
2812 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2813 (ParmType->isBooleanType() ||
2814 !ParmType->isIntegralType(S.getASTContext()))) {
2815 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2816 << ParmType;
2817 return;
2818 }
2819
2820 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2821 D->hasAttr<VecTypeHintAttr>()) {
2822 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2823 if (A->getTypeHint() != ParmType) {
2824 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2825 return;
2826 }
2827 }
2828
2829 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2830 ParmType, Attr.getLoc()));
2831}
2832
Joey Gouly0608ae82013-03-14 09:54:43 +00002833static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2834 if (!dyn_cast<VarDecl>(D))
2835 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2836 << 9;
2837 StringRef EndianType = Attr.getParameterName()->getName();
2838 if (EndianType != "host" && EndianType != "device")
2839 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2840}
2841
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002842SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002843 StringRef Name,
2844 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002845 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2846 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002847 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002848 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2849 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002850 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002851 }
Michael Han99315932013-01-24 16:46:58 +00002852 return ::new (Context) SectionAttr(Range, Context, Name,
2853 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002854}
2855
Chandler Carruthedc2c642011-07-02 00:01:44 +00002856static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002857 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002858 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002859 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002860
2861 // Make sure that there is a string literal as the sections's single
2862 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002863 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002864 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002865 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002866 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002867 return;
2868 }
Mike Stump11289f42009-09-09 15:08:12 +00002869
Chris Lattner30ba6742009-08-10 19:03:04 +00002870 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002871 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002872 if (!Error.empty()) {
2873 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2874 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002875 return;
2876 }
Mike Stump11289f42009-09-09 15:08:12 +00002877
Chris Lattner20aee9b2010-01-12 20:58:53 +00002878 // This attribute cannot be applied to local variables.
2879 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2880 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2881 return;
2882 }
Michael Han99315932013-01-24 16:46:58 +00002883
2884 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002885 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han99315932013-01-24 16:46:58 +00002886 SE->getString(), Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002887 if (NewAttr)
2888 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002889}
2890
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002891
Chandler Carruthedc2c642011-07-02 00:01:44 +00002892static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002893 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002894 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002896 return;
2897 }
Douglas Gregor88336832011-06-15 05:45:11 +00002898
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002899 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002900 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002901 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002902 } else {
Michael Han99315932013-01-24 16:46:58 +00002903 D->addAttr(::new (S.Context)
2904 NoThrowAttr(Attr.getRange(), S.Context,
2905 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002906 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002907}
2908
Chandler Carruthedc2c642011-07-02 00:01:44 +00002909static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002910 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002911 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002912 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002913 return;
2914 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002915
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002916 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002917 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002918 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002919 } else {
Michael Han99315932013-01-24 16:46:58 +00002920 D->addAttr(::new (S.Context)
2921 ConstAttr(Attr.getRange(), S.Context,
2922 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002923 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002924}
2925
Chandler Carruthedc2c642011-07-02 00:01:44 +00002926static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002927 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002928 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002929 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002930
Michael Han99315932013-01-24 16:46:58 +00002931 D->addAttr(::new (S.Context)
2932 PureAttr(Attr.getRange(), S.Context,
2933 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002934}
2935
Chandler Carruthedc2c642011-07-02 00:01:44 +00002936static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002937 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002938 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2939 return;
2940 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002941
Anders Carlssond277d792009-01-31 01:16:18 +00002942 if (Attr.getNumArgs() != 0) {
2943 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2944 return;
2945 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002946
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002947 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002948
Anders Carlssond277d792009-01-31 01:16:18 +00002949 if (!VD || !VD->hasLocalStorage()) {
2950 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2951 return;
2952 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002953
Anders Carlssond277d792009-01-31 01:16:18 +00002954 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002955 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002956 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002957 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2958 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002959 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002960 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002961 Attr.getParameterName();
2962 return;
2963 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002964
Anders Carlssond277d792009-01-31 01:16:18 +00002965 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2966 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002967 S.Diag(Attr.getParameterLoc(),
2968 diag::err_attribute_cleanup_arg_not_function)
2969 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002970 return;
2971 }
2972
Anders Carlssond277d792009-01-31 01:16:18 +00002973 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002974 S.Diag(Attr.getParameterLoc(),
2975 diag::err_attribute_cleanup_func_must_take_one_arg)
2976 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002977 return;
2978 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002979
Anders Carlsson723f55d2009-02-07 23:16:50 +00002980 // We're currently more strict than GCC about what function types we accept.
2981 // If this ever proves to be a problem it should be easy to fix.
2982 QualType Ty = S.Context.getPointerType(VD->getType());
2983 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002984 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2985 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002986 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002987 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2988 Attr.getParameterName() << ParamTy << Ty;
2989 return;
2990 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002991
Michael Han99315932013-01-24 16:46:58 +00002992 D->addAttr(::new (S.Context)
2993 CleanupAttr(Attr.getRange(), S.Context, FD,
2994 Attr.getAttributeSpellingListIndex()));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002995 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewyckya096b142013-02-12 08:08:54 +00002996 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssond277d792009-01-31 01:16:18 +00002997}
2998
Mike Stumpd3bb5572009-07-24 19:02:52 +00002999/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003000/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003001static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003002 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003003 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003004
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003005 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003006 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003007 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003008 return;
3009 }
Chandler Carruth743682b2010-11-16 08:35:43 +00003010
3011 // In C++ the implicit 'this' function parameter also counts, and they are
3012 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003013 bool HasImplicitThisParam = isInstanceMethod(D);
3014 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003015 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00003016
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003017 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003018 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003019 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003020 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3021 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003022 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3023 << "format" << 2 << IdxExpr->getSourceRange();
3024 return;
3025 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003026
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003027 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
3028 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3029 << "format" << 2 << IdxExpr->getSourceRange();
3030 return;
3031 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003032
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003033 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003034
Chandler Carruth743682b2010-11-16 08:35:43 +00003035 if (HasImplicitThisParam) {
3036 if (ArgIdx == 0) {
3037 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3038 << "format_arg" << IdxExpr->getSourceRange();
3039 return;
3040 }
3041 ArgIdx--;
3042 }
3043
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003044 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003045 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003046
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003047 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3048 if (not_nsstring_type &&
3049 !isCFStringType(Ty, S.Context) &&
3050 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003051 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003052 // FIXME: Should highlight the actual expression that has the wrong type.
3053 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003054 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003055 << IdxExpr->getSourceRange();
3056 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003057 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003058 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003059 if (!isNSStringType(Ty, S.Context) &&
3060 !isCFStringType(Ty, S.Context) &&
3061 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003062 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003063 // FIXME: Should highlight the actual expression that has the wrong type.
3064 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003065 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003066 << IdxExpr->getSourceRange();
3067 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003068 }
3069
Michael Han99315932013-01-24 16:46:58 +00003070 D->addAttr(::new (S.Context)
3071 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3072 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003073}
3074
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003075enum FormatAttrKind {
3076 CFStringFormat,
3077 NSStringFormat,
3078 StrftimeFormat,
3079 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003080 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003081 InvalidFormat
3082};
3083
3084/// getFormatAttrKind - Map from format attribute names to supported format
3085/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003086static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003087 return llvm::StringSwitch<FormatAttrKind>(Format)
3088 // Check for formats that get handled specially.
3089 .Case("NSString", NSStringFormat)
3090 .Case("CFString", CFStringFormat)
3091 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003092
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003093 // Otherwise, check for supported formats.
3094 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3095 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3096 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003097
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003098 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3099 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003100}
3101
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003102/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003103/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003104static void handleInitPriorityAttr(Sema &S, Decl *D,
3105 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003106 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003107 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3108 return;
3109 }
3110
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003111 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003112 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3113 Attr.setInvalid();
3114 return;
3115 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003116 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003117 if (S.Context.getAsArrayType(T))
3118 T = S.Context.getBaseElementType(T);
3119 if (!T->getAs<RecordType>()) {
3120 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3121 Attr.setInvalid();
3122 return;
3123 }
3124
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003125 if (Attr.getNumArgs() != 1) {
3126 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3127 Attr.setInvalid();
3128 return;
3129 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003130 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003131
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003132 llvm::APSInt priority(32);
3133 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3134 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3135 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3136 << "init_priority" << priorityExpr->getSourceRange();
3137 Attr.setInvalid();
3138 return;
3139 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00003140 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003141 if (prioritynum < 101 || prioritynum > 65535) {
3142 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3143 << priorityExpr->getSourceRange();
3144 Attr.setInvalid();
3145 return;
3146 }
Michael Han99315932013-01-24 16:46:58 +00003147 D->addAttr(::new (S.Context)
3148 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3149 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003150}
3151
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003152FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han99315932013-01-24 16:46:58 +00003153 int FormatIdx, int FirstArg,
3154 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003155 // Check whether we already have an equivalent format attribute.
3156 for (specific_attr_iterator<FormatAttr>
3157 i = D->specific_attr_begin<FormatAttr>(),
3158 e = D->specific_attr_end<FormatAttr>();
3159 i != e ; ++i) {
3160 FormatAttr *f = *i;
3161 if (f->getType() == Format &&
3162 f->getFormatIdx() == FormatIdx &&
3163 f->getFirstArg() == FirstArg) {
3164 // If we don't have a valid location for this attribute, adopt the
3165 // location.
3166 if (f->getLocation().isInvalid())
3167 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003168 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003169 }
3170 }
3171
Michael Han99315932013-01-24 16:46:58 +00003172 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3173 AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003174}
3175
Mike Stumpd3bb5572009-07-24 19:02:52 +00003176/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003177/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003178static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003179
Chris Lattner4a927cb2008-06-28 23:36:30 +00003180 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003181 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003182 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003183 return;
3184 }
3185
Chris Lattner4a927cb2008-06-28 23:36:30 +00003186 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003187 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003188 return;
3189 }
3190
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003191 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003192 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003193 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003194 return;
3195 }
3196
Chandler Carruth743682b2010-11-16 08:35:43 +00003197 // In C++ the implicit 'this' function parameter also counts, and they are
3198 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003199 bool HasImplicitThisParam = isInstanceMethod(D);
3200 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003201 unsigned FirstIdx = 1;
3202
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003203 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003204
3205 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003206 if (Format.startswith("__") && Format.endswith("__"))
3207 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003208
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003209 // Check for supported formats.
3210 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003211
3212 if (Kind == IgnoredFormat)
3213 return;
3214
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003215 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003216 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003217 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003218 return;
3219 }
3220
3221 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003222 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003223 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003224 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3225 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003226 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003227 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003228 return;
3229 }
3230
3231 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003232 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003233 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003234 return;
3235 }
3236
3237 // FIXME: Do we need to bounds check?
3238 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003239
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003240 if (HasImplicitThisParam) {
3241 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003242 S.Diag(Attr.getLoc(),
3243 diag::err_format_attribute_implicit_this_format_string)
3244 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003245 return;
3246 }
3247 ArgIdx--;
3248 }
Mike Stump11289f42009-09-09 15:08:12 +00003249
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003250 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003251 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003252
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003253 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003254 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003255 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3256 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003257 return;
3258 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003259 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003260 // FIXME: do we need to check if the type is NSString*? What are the
3261 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003262 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003263 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003264 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3265 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003266 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003267 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003268 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003269 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003270 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003271 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3272 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003273 return;
3274 }
3275
3276 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003277 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003278 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003279 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3280 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003282 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003283 return;
3284 }
3285
3286 // check if the function is variadic if the 3rd argument non-zero
3287 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003288 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003289 ++NumArgs; // +1 for ...
3290 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003291 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003292 return;
3293 }
3294 }
3295
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003296 // strftime requires FirstArg to be 0 because it doesn't read from any
3297 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003298 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003299 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003300 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3301 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003302 return;
3303 }
3304 // if 0 it disables parameter checking (to use with e.g. va_list)
3305 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003306 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003307 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003308 return;
3309 }
3310
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003311 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3312 Idx.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003313 FirstArg.getZExtValue(),
3314 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003315 if (NewAttr)
3316 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003317}
3318
Chandler Carruthedc2c642011-07-02 00:01:44 +00003319static void handleTransparentUnionAttr(Sema &S, Decl *D,
3320 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003321 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003322 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003323 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003324
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003325
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003326 // Try to find the underlying union declaration.
3327 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003328 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003329 if (TD && TD->getUnderlyingType()->isUnionType())
3330 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3331 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003332 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003333
3334 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003335 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003336 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003337 return;
3338 }
3339
John McCallf937c022011-10-07 06:10:15 +00003340 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003341 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003342 diag::warn_transparent_union_attribute_not_definition);
3343 return;
3344 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003345
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003346 RecordDecl::field_iterator Field = RD->field_begin(),
3347 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003348 if (Field == FieldEnd) {
3349 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3350 return;
3351 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003352
David Blaikie40ed2972012-06-06 20:45:41 +00003353 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003354 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003355 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003356 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003357 diag::warn_transparent_union_attribute_floating)
3358 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003359 return;
3360 }
3361
3362 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3363 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3364 for (; Field != FieldEnd; ++Field) {
3365 QualType FieldType = Field->getType();
3366 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3367 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3368 // Warn if we drop the attribute.
3369 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003370 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003371 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003372 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003373 diag::warn_transparent_union_attribute_field_size_align)
3374 << isSize << Field->getDeclName() << FieldBits;
3375 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003376 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003377 diag::note_transparent_union_first_field_size_align)
3378 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003379 return;
3380 }
3381 }
3382
Michael Han99315932013-01-24 16:46:58 +00003383 RD->addAttr(::new (S.Context)
3384 TransparentUnionAttr(Attr.getRange(), S.Context,
3385 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003386}
3387
Chandler Carruthedc2c642011-07-02 00:01:44 +00003388static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003389 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003390 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003391 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003392
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003393 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003394 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003395
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003396 // Make sure that there is a string literal as the annotation's single
3397 // argument.
3398 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003399 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003400 return;
3401 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003402
3403 // Don't duplicate annotations that are already set.
3404 for (specific_attr_iterator<AnnotateAttr>
3405 i = D->specific_attr_begin<AnnotateAttr>(),
3406 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3407 if ((*i)->getAnnotation() == SE->getString())
3408 return;
3409 }
Michael Han99315932013-01-24 16:46:58 +00003410
3411 D->addAttr(::new (S.Context)
3412 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3413 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003414}
3415
Chandler Carruthedc2c642011-07-02 00:01:44 +00003416static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003417 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003418 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003419 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003420 return;
3421 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003422
Richard Smith848e1f12013-02-01 08:12:08 +00003423 if (Attr.getNumArgs() == 0) {
3424 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3425 true, 0, Attr.getAttributeSpellingListIndex()));
3426 return;
3427 }
3428
Richard Smith44c247f2013-02-22 08:32:16 +00003429 Expr *E = Attr.getArg(0);
3430 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3431 S.Diag(Attr.getEllipsisLoc(),
3432 diag::err_pack_expansion_without_parameter_packs);
3433 return;
3434 }
3435
3436 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3437 return;
3438
3439 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3440 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003441}
3442
3443void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003444 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003445 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3446 SourceLocation AttrLoc = AttrRange.getBegin();
3447
Richard Smith1dba27c2013-01-29 09:02:09 +00003448 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003449 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003450 // C++11 [dcl.align]p1:
3451 // An alignment-specifier may be applied to a variable or to a class
3452 // data member, but it shall not be applied to a bit-field, a function
3453 // parameter, the formal parameter of a catch clause, or a variable
3454 // declared with the register storage class specifier. An
3455 // alignment-specifier may also be applied to the declaration of a class
3456 // or enumeration type.
3457 // C11 6.7.5/2:
3458 // An alignment attribute shall not be specified in a declaration of
3459 // a typedef, or a bit-field, or a function, or a parameter, or an
3460 // object declared with the register storage-class specifier.
3461 int DiagKind = -1;
3462 if (isa<ParmVarDecl>(D)) {
3463 DiagKind = 0;
3464 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3465 if (VD->getStorageClass() == SC_Register)
3466 DiagKind = 1;
3467 if (VD->isExceptionVariable())
3468 DiagKind = 2;
3469 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3470 if (FD->isBitField())
3471 DiagKind = 3;
3472 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003473 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3474 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003475 << (TmpAttr.isC11() ? ExpectedVariableOrField
3476 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003477 return;
3478 }
3479 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003480 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003481 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003482 return;
3483 }
3484 }
3485
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003486 if (E->isTypeDependent() || E->isValueDependent()) {
3487 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003488 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3489 AA->setPackExpansion(IsPackExpansion);
3490 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003491 return;
3492 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003493
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003494 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003495 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003496 ExprResult ICE
3497 = VerifyIntegerConstantExpression(E, &Alignment,
3498 diag::err_aligned_attribute_argument_not_int,
3499 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003500 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003501 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003502
3503 // C++11 [dcl.align]p2:
3504 // -- if the constant expression evaluates to zero, the alignment
3505 // specifier shall have no effect
3506 // C11 6.7.5p6:
3507 // An alignment specification of zero has no effect.
3508 if (!(TmpAttr.isAlignas() && !Alignment) &&
3509 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003510 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3511 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003512 return;
3513 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003514
Richard Smith848e1f12013-02-01 08:12:08 +00003515 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003516 // We've already verified it's a power of 2, now let's make sure it's
3517 // 8192 or less.
3518 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003519 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003520 << E->getSourceRange();
3521 return;
3522 }
3523 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003524
Richard Smith44c247f2013-02-22 08:32:16 +00003525 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3526 ICE.take(), SpellingListIndex);
3527 AA->setPackExpansion(IsPackExpansion);
3528 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003529}
3530
Michael Hanaf02bbe2013-02-01 01:19:17 +00003531void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003532 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003533 // FIXME: Cache the number on the Attr object if non-dependent?
3534 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003535 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3536 SpellingListIndex);
3537 AA->setPackExpansion(IsPackExpansion);
3538 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003539}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003540
Richard Smith848e1f12013-02-01 08:12:08 +00003541void Sema::CheckAlignasUnderalignment(Decl *D) {
3542 assert(D->hasAttrs() && "no attributes on decl");
3543
3544 QualType Ty;
3545 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3546 Ty = VD->getType();
3547 else
3548 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003549 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003550 return;
3551
3552 // C++11 [dcl.align]p5, C11 6.7.5/4:
3553 // The combined effect of all alignment attributes in a declaration shall
3554 // not specify an alignment that is less strict than the alignment that
3555 // would otherwise be required for the entity being declared.
3556 AlignedAttr *AlignasAttr = 0;
3557 unsigned Align = 0;
3558 for (specific_attr_iterator<AlignedAttr>
3559 I = D->specific_attr_begin<AlignedAttr>(),
3560 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3561 if (I->isAlignmentDependent())
3562 return;
3563 if (I->isAlignas())
3564 AlignasAttr = *I;
3565 Align = std::max(Align, I->getAlignment(Context));
3566 }
3567
3568 if (AlignasAttr && Align) {
3569 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3570 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3571 if (NaturalAlign > RequestedAlign)
3572 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3573 << Ty << (unsigned)NaturalAlign.getQuantity();
3574 }
3575}
3576
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003577/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003578/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003579///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003580/// Despite what would be logical, the mode attribute is a decl attribute, not a
3581/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3582/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003583static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003584 // This attribute isn't documented, but glibc uses it. It changes
3585 // the width of an int or unsigned int to the specified size.
3586
3587 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003588 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003589 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003590
Chris Lattneracbc2d22008-06-27 22:18:37 +00003591
3592 IdentifierInfo *Name = Attr.getParameterName();
3593 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003594 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003595 return;
3596 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003597
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003598 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003599
3600 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003601 if (Str.startswith("__") && Str.endswith("__"))
3602 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003603
3604 unsigned DestWidth = 0;
3605 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003606 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003607 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003608 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003609 switch (Str[0]) {
3610 case 'Q': DestWidth = 8; break;
3611 case 'H': DestWidth = 16; break;
3612 case 'S': DestWidth = 32; break;
3613 case 'D': DestWidth = 64; break;
3614 case 'X': DestWidth = 96; break;
3615 case 'T': DestWidth = 128; break;
3616 }
3617 if (Str[1] == 'F') {
3618 IntegerMode = false;
3619 } else if (Str[1] == 'C') {
3620 IntegerMode = false;
3621 ComplexMode = true;
3622 } else if (Str[1] != 'I') {
3623 DestWidth = 0;
3624 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003625 break;
3626 case 4:
3627 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3628 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003629 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003630 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003631 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003632 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003633 break;
3634 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003635 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003636 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003637 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003638 case 11:
3639 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003640 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003641 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003642 }
3643
3644 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003645 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003646 OldTy = TD->getUnderlyingType();
3647 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3648 OldTy = VD->getType();
3649 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003650 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003651 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003652 return;
3653 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003654
John McCall9dd450b2009-09-21 23:43:11 +00003655 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003656 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3657 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003658 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003659 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3660 } else if (ComplexMode) {
3661 if (!OldTy->isComplexType())
3662 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3663 } else {
3664 if (!OldTy->isFloatingType())
3665 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3666 }
3667
Mike Stump87c57ac2009-05-16 07:39:55 +00003668 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3669 // and friends, at least with glibc.
3670 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3671 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003672 // FIXME: Make sure floating-point mappings are accurate
3673 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003674 QualType NewTy;
3675 switch (DestWidth) {
3676 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003677 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003678 return;
3679 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003680 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003681 return;
3682 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003683 if (!IntegerMode) {
3684 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3685 return;
3686 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003687 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003688 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003689 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003690 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003691 break;
3692 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003693 if (!IntegerMode) {
3694 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3695 return;
3696 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003697 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003698 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003699 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003700 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003701 break;
3702 case 32:
3703 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003704 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003705 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003706 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003707 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003708 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003709 break;
3710 case 64:
3711 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003712 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003713 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003714 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003715 NewTy = S.Context.LongTy;
3716 else
3717 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003718 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003719 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003720 NewTy = S.Context.UnsignedLongTy;
3721 else
3722 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003723 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003724 case 96:
3725 NewTy = S.Context.LongDoubleTy;
3726 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003727 case 128:
3728 if (!IntegerMode) {
3729 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3730 return;
3731 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003732 if (OldTy->isSignedIntegerType())
3733 NewTy = S.Context.Int128Ty;
3734 else
3735 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003736 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003737 }
3738
Eli Friedman4735374e2009-03-03 06:41:03 +00003739 if (ComplexMode) {
3740 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003741 }
3742
3743 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003744 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003745 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003746 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003747 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003748 cast<ValueDecl>(D)->setType(NewTy);
3749}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003750
Chandler Carruthedc2c642011-07-02 00:01:44 +00003751static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003752 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003753 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003754 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003755
Nick Lewycky08597072012-07-24 01:40:49 +00003756 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3757 if (!VD->hasGlobalStorage())
3758 S.Diag(Attr.getLoc(),
3759 diag::warn_attribute_requires_functions_or_static_globals)
3760 << Attr.getName();
3761 } else if (!isFunctionOrMethod(D)) {
3762 S.Diag(Attr.getLoc(),
3763 diag::warn_attribute_requires_functions_or_static_globals)
3764 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003765 return;
3766 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003767
Michael Han99315932013-01-24 16:46:58 +00003768 D->addAttr(::new (S.Context)
3769 NoDebugAttr(Attr.getRange(), S.Context,
3770 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003771}
3772
Chandler Carruthedc2c642011-07-02 00:01:44 +00003773static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003774 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003775 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003776 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003777
Mike Stumpd3bb5572009-07-24 19:02:52 +00003778
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003779 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003780 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003781 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003782 return;
3783 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003784
Michael Han99315932013-01-24 16:46:58 +00003785 D->addAttr(::new (S.Context)
3786 NoInlineAttr(Attr.getRange(), S.Context,
3787 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003788}
3789
Chandler Carruthedc2c642011-07-02 00:01:44 +00003790static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3791 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003792 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003793 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003794 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003795
Chris Lattner3c77a352010-06-22 00:03:40 +00003796
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003797 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003798 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003799 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003800 return;
3801 }
3802
Michael Han99315932013-01-24 16:46:58 +00003803 D->addAttr(::new (S.Context)
3804 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3805 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003806}
3807
Chandler Carruthedc2c642011-07-02 00:01:44 +00003808static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003809 if (S.LangOpts.CUDA) {
3810 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003811 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003812 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3813 return;
3814 }
3815
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003816 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003817 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003818 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003819 return;
3820 }
3821
Michael Han99315932013-01-24 16:46:58 +00003822 D->addAttr(::new (S.Context)
3823 CUDAConstantAttr(Attr.getRange(), S.Context,
3824 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003825 } else {
3826 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3827 }
3828}
3829
Chandler Carruthedc2c642011-07-02 00:01:44 +00003830static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003831 if (S.LangOpts.CUDA) {
3832 // check the attribute arguments.
3833 if (Attr.getNumArgs() != 0) {
3834 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3835 return;
3836 }
3837
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003838 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003839 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003840 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003841 return;
3842 }
3843
Michael Han99315932013-01-24 16:46:58 +00003844 D->addAttr(::new (S.Context)
3845 CUDADeviceAttr(Attr.getRange(), S.Context,
3846 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003847 } else {
3848 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3849 }
3850}
3851
Chandler Carruthedc2c642011-07-02 00:01:44 +00003852static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003853 if (S.LangOpts.CUDA) {
3854 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003855 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003856 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003857
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003858 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003859 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003860 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003861 return;
3862 }
3863
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003864 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003865 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003866 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003867 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003868 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3869 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003870 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003871 "void");
3872 } else {
3873 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3874 << FD->getType();
3875 }
3876 return;
3877 }
3878
Michael Han99315932013-01-24 16:46:58 +00003879 D->addAttr(::new (S.Context)
3880 CUDAGlobalAttr(Attr.getRange(), S.Context,
3881 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003882 } else {
3883 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3884 }
3885}
3886
Chandler Carruthedc2c642011-07-02 00:01:44 +00003887static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003888 if (S.LangOpts.CUDA) {
3889 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003890 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003891 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003892
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003893
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003894 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003895 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003896 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003897 return;
3898 }
3899
Michael Han99315932013-01-24 16:46:58 +00003900 D->addAttr(::new (S.Context)
3901 CUDAHostAttr(Attr.getRange(), S.Context,
3902 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003903 } else {
3904 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3905 }
3906}
3907
Chandler Carruthedc2c642011-07-02 00:01:44 +00003908static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003909 if (S.LangOpts.CUDA) {
3910 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003911 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003912 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003913
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003914 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003915 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003916 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003917 return;
3918 }
3919
Michael Han99315932013-01-24 16:46:58 +00003920 D->addAttr(::new (S.Context)
3921 CUDASharedAttr(Attr.getRange(), S.Context,
3922 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003923 } else {
3924 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3925 }
3926}
3927
Chandler Carruthedc2c642011-07-02 00:01:44 +00003928static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003929 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003930 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003931 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003932
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003933 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003934 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003935 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003936 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003937 return;
3938 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003939
Douglas Gregor35b57532009-10-27 21:01:01 +00003940 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003941 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003942 return;
3943 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003944
Michael Han99315932013-01-24 16:46:58 +00003945 D->addAttr(::new (S.Context)
3946 GNUInlineAttr(Attr.getRange(), S.Context,
3947 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003948}
3949
Chandler Carruthedc2c642011-07-02 00:01:44 +00003950static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003951 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003952
Aaron Ballman02df2e02012-12-09 17:45:41 +00003953 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003954 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003955 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3956 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003957 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003958 return;
3959
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003960 if (!isa<ObjCMethodDecl>(D)) {
3961 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3962 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003963 return;
3964 }
3965
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003966 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003967 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003968 D->addAttr(::new (S.Context)
3969 FastCallAttr(Attr.getRange(), S.Context,
3970 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003971 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003972 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003973 D->addAttr(::new (S.Context)
3974 StdCallAttr(Attr.getRange(), S.Context,
3975 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003976 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003977 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003978 D->addAttr(::new (S.Context)
3979 ThisCallAttr(Attr.getRange(), S.Context,
3980 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003981 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003982 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003983 D->addAttr(::new (S.Context)
3984 CDeclAttr(Attr.getRange(), S.Context,
3985 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003986 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003987 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003988 D->addAttr(::new (S.Context)
3989 PascalAttr(Attr.getRange(), S.Context,
3990 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003991 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003992 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003993 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003994 switch (CC) {
3995 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003996 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003997 break;
3998 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003999 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004000 break;
4001 default:
4002 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004003 }
4004
Michael Han99315932013-01-24 16:46:58 +00004005 D->addAttr(::new (S.Context)
4006 PcsAttr(Attr.getRange(), S.Context, PCS,
4007 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004008 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004009 }
Derek Schuffa2020962012-10-16 22:30:41 +00004010 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00004011 D->addAttr(::new (S.Context)
4012 PnaclCallAttr(Attr.getRange(), S.Context,
4013 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004014 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00004015 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004016 D->addAttr(::new (S.Context)
4017 IntelOclBiccAttr(Attr.getRange(), S.Context,
4018 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004019 return;
Derek Schuffa2020962012-10-16 22:30:41 +00004020
Abramo Bagnara50099372010-04-30 13:10:51 +00004021 default:
4022 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004023 }
4024}
4025
Chandler Carruthedc2c642011-07-02 00:01:44 +00004026static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00004027 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004028 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004029}
4030
Guy Benyeifb36ede2013-03-24 13:58:12 +00004031static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4032 assert(!Attr.isInvalid());
4033
4034 Expr *E = Attr.getArg(0);
4035 llvm::APSInt ArgNum(32);
4036 if (E->isTypeDependent() || E->isValueDependent() ||
4037 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4038 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4039 << Attr.getName()->getName() << E->getSourceRange();
4040 return;
4041 }
4042
4043 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4044 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4045}
4046
Aaron Ballman02df2e02012-12-09 17:45:41 +00004047bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4048 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00004049 if (attr.isInvalid())
4050 return true;
4051
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004052 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4053 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
4054 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00004055 attr.setInvalid();
4056 return true;
4057 }
4058
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004059 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4060 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00004061 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004062 case AttributeList::AT_CDecl: CC = CC_C; break;
4063 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4064 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4065 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4066 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4067 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004068 Expr *Arg = attr.getArg(0);
4069 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004070 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004071 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
4072 << "pcs" << 1;
4073 attr.setInvalid();
4074 return true;
4075 }
4076
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004077 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004078 if (StrRef == "aapcs") {
4079 CC = CC_AAPCS;
4080 break;
4081 } else if (StrRef == "aapcs-vfp") {
4082 CC = CC_AAPCS_VFP;
4083 break;
4084 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004085
4086 attr.setInvalid();
4087 Diag(attr.getLoc(), diag::err_invalid_pcs);
4088 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004089 }
Derek Schuffa2020962012-10-16 22:30:41 +00004090 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00004091 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00004092 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004093 }
4094
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004095 const TargetInfo &TI = Context.getTargetInfo();
4096 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4097 if (A == TargetInfo::CCCR_Warning) {
4098 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00004099
4100 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4101 if (FD)
4102 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4103 TargetInfo::CCMT_NonMember;
4104 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004105 }
4106
John McCall3882ace2011-01-05 12:14:39 +00004107 return false;
4108}
4109
Chandler Carruthedc2c642011-07-02 00:01:44 +00004110static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004111 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00004112
4113 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004114 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00004115 return;
4116
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004117 if (!isa<ObjCMethodDecl>(D)) {
4118 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4119 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004120 return;
4121 }
Eli Friedman7044b762009-03-27 21:06:47 +00004122
Michael Han99315932013-01-24 16:46:58 +00004123 D->addAttr(::new (S.Context)
4124 RegparmAttr(Attr.getRange(), S.Context, numParams,
4125 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00004126}
4127
4128/// Checks a regparm attribute, returning true if it is ill-formed and
4129/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004130bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4131 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004132 return true;
4133
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004134 if (Attr.getNumArgs() != 1) {
4135 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4136 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004137 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004138 }
Eli Friedman7044b762009-03-27 21:06:47 +00004139
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004140 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00004141 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00004142 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00004143 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004144 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00004145 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004146 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004147 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004148 }
4149
Douglas Gregore8bbc122011-09-02 00:18:52 +00004150 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004151 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004152 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004153 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004154 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004155 }
4156
John McCall3882ace2011-01-05 12:14:39 +00004157 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00004158 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004159 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004160 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004161 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004162 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004163 }
4164
John McCall3882ace2011-01-05 12:14:39 +00004165 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004166}
4167
Chandler Carruthedc2c642011-07-02 00:01:44 +00004168static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004169 if (S.LangOpts.CUDA) {
4170 // check the attribute arguments.
4171 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004172 // FIXME: 0 is not okay.
4173 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004174 return;
4175 }
4176
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004177 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004178 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004179 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004180 return;
4181 }
4182
4183 Expr *MaxThreadsExpr = Attr.getArg(0);
4184 llvm::APSInt MaxThreads(32);
4185 if (MaxThreadsExpr->isTypeDependent() ||
4186 MaxThreadsExpr->isValueDependent() ||
4187 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4189 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4190 return;
4191 }
4192
4193 llvm::APSInt MinBlocks(32);
4194 if (Attr.getNumArgs() > 1) {
4195 Expr *MinBlocksExpr = Attr.getArg(1);
4196 if (MinBlocksExpr->isTypeDependent() ||
4197 MinBlocksExpr->isValueDependent() ||
4198 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4199 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4200 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4201 return;
4202 }
4203 }
4204
Michael Han99315932013-01-24 16:46:58 +00004205 D->addAttr(::new (S.Context)
4206 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4207 MaxThreads.getZExtValue(),
4208 MinBlocks.getZExtValue(),
4209 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004210 } else {
4211 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4212 }
4213}
4214
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004215static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4216 const AttributeList &Attr) {
4217 StringRef AttrName = Attr.getName()->getName();
4218 if (!Attr.getParameterName()) {
4219 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4220 << Attr.getName() << /* arg num = */ 1;
4221 return;
4222 }
4223
4224 if (Attr.getNumArgs() != 2) {
4225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4226 << /* required args = */ 3;
4227 return;
4228 }
4229
4230 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4231
4232 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4233 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4234 << Attr.getName() << ExpectedFunctionOrMethod;
4235 return;
4236 }
4237
4238 uint64_t ArgumentIdx;
4239 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4240 Attr.getLoc(), 2,
4241 Attr.getArg(0), ArgumentIdx))
4242 return;
4243
4244 uint64_t TypeTagIdx;
4245 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4246 Attr.getLoc(), 3,
4247 Attr.getArg(1), TypeTagIdx))
4248 return;
4249
4250 bool IsPointer = (AttrName == "pointer_with_type_tag");
4251 if (IsPointer) {
4252 // Ensure that buffer has a pointer type.
4253 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4254 if (!BufferTy->isPointerType()) {
4255 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
4256 << AttrName;
4257 }
4258 }
4259
Michael Han99315932013-01-24 16:46:58 +00004260 D->addAttr(::new (S.Context)
4261 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4262 ArgumentIdx, TypeTagIdx, IsPointer,
4263 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004264}
4265
4266static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4267 const AttributeList &Attr) {
4268 IdentifierInfo *PointerKind = Attr.getParameterName();
4269 if (!PointerKind) {
4270 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4271 << "type_tag_for_datatype" << 1;
4272 return;
4273 }
4274
4275 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4276
Michael Han99315932013-01-24 16:46:58 +00004277 D->addAttr(::new (S.Context)
4278 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4279 MatchingCType,
4280 Attr.getLayoutCompatible(),
4281 Attr.getMustBeNull(),
4282 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004283}
4284
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004285//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004286// Checker-specific attribute handlers.
4287//===----------------------------------------------------------------------===//
4288
John McCalled433932011-01-25 03:31:58 +00004289static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004290 return type->isDependentType() ||
4291 type->isObjCObjectPointerType() ||
4292 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004293}
4294static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004295 return type->isDependentType() ||
4296 type->isPointerType() ||
4297 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004298}
4299
Chandler Carruthedc2c642011-07-02 00:01:44 +00004300static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004301 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004302 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004303 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004304 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004305 return;
4306 }
4307
4308 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004309 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004310 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4311 cf = false;
4312 } else {
4313 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4314 cf = true;
4315 }
4316
4317 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004318 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004319 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004320 return;
4321 }
4322
4323 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004324 param->addAttr(::new (S.Context)
4325 CFConsumedAttr(Attr.getRange(), S.Context,
4326 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004327 else
Michael Han99315932013-01-24 16:46:58 +00004328 param->addAttr(::new (S.Context)
4329 NSConsumedAttr(Attr.getRange(), S.Context,
4330 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004331}
4332
Chandler Carruthedc2c642011-07-02 00:01:44 +00004333static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4334 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004335 if (!isa<ObjCMethodDecl>(D)) {
4336 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004337 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004338 return;
4339 }
4340
Michael Han99315932013-01-24 16:46:58 +00004341 D->addAttr(::new (S.Context)
4342 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4343 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004344}
4345
Chandler Carruthedc2c642011-07-02 00:01:44 +00004346static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4347 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004348
John McCalled433932011-01-25 03:31:58 +00004349 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004350
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004351 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004352 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004353 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004354 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004355 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004356 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4357 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004358 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004359 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004360 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004361 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004362 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004363 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004364 return;
4365 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004366
John McCalled433932011-01-25 03:31:58 +00004367 bool typeOK;
4368 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004369 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004370 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004371 case AttributeList::AT_NSReturnsAutoreleased:
4372 case AttributeList::AT_NSReturnsRetained:
4373 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004374 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4375 cf = false;
4376 break;
4377
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004378 case AttributeList::AT_CFReturnsRetained:
4379 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004380 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4381 cf = true;
4382 break;
4383 }
4384
4385 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004386 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004387 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004388 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004389 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004390
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004391 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004392 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004393 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004394 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004395 D->addAttr(::new (S.Context)
4396 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4397 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004398 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004399 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004400 D->addAttr(::new (S.Context)
4401 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4402 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004403 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004404 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004405 D->addAttr(::new (S.Context)
4406 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4407 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004408 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004409 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004410 D->addAttr(::new (S.Context)
4411 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4412 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004413 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004414 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004415 D->addAttr(::new (S.Context)
4416 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4417 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004418 return;
4419 };
4420}
4421
John McCallcf166702011-07-22 08:53:00 +00004422static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4423 const AttributeList &attr) {
4424 SourceLocation loc = attr.getLoc();
4425
4426 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4427
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004428 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00004429 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004430 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00004431 return;
4432 }
4433
4434 // Check that the method returns a normal pointer.
4435 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004436
4437 if (!resultType->isReferenceType() &&
4438 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00004439 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4440 << SourceRange(loc)
4441 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4442
4443 // Drop the attribute.
4444 return;
4445 }
4446
Michael Han99315932013-01-24 16:46:58 +00004447 method->addAttr(::new (S.Context)
4448 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4449 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004450}
4451
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004452static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4453 const AttributeList &attr) {
4454 SourceLocation loc = attr.getLoc();
4455 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4456
4457 if (!method) {
4458 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4459 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4460 return;
4461 }
4462 DeclContext *DC = method->getDeclContext();
4463 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4464 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4465 << attr.getName() << 0;
4466 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4467 return;
4468 }
4469 if (method->getMethodFamily() == OMF_dealloc) {
4470 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4471 << attr.getName() << 1;
4472 return;
4473 }
4474
Michael Han99315932013-01-24 16:46:58 +00004475 method->addAttr(::new (S.Context)
4476 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4477 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004478}
4479
John McCall32f5fe12011-09-30 05:12:12 +00004480/// Handle cf_audited_transfer and cf_unknown_transfer.
4481static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4482 if (!isa<FunctionDecl>(D)) {
4483 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004484 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004485 return;
4486 }
4487
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004488 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004489
4490 // Check whether there's a conflicting attribute already present.
4491 Attr *Existing;
4492 if (IsAudited) {
4493 Existing = D->getAttr<CFUnknownTransferAttr>();
4494 } else {
4495 Existing = D->getAttr<CFAuditedTransferAttr>();
4496 }
4497 if (Existing) {
4498 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4499 << A.getName()
4500 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4501 << A.getRange() << Existing->getRange();
4502 return;
4503 }
4504
4505 // All clear; add the attribute.
4506 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004507 D->addAttr(::new (S.Context)
4508 CFAuditedTransferAttr(A.getRange(), S.Context,
4509 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004510 } else {
Michael Han99315932013-01-24 16:46:58 +00004511 D->addAttr(::new (S.Context)
4512 CFUnknownTransferAttr(A.getRange(), S.Context,
4513 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004514 }
4515}
4516
John McCallf1e8b342011-09-29 07:17:38 +00004517static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4518 const AttributeList &Attr) {
4519 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4520 if (!RD || RD->isUnion()) {
4521 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004522 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004523 }
4524
4525 IdentifierInfo *ParmName = Attr.getParameterName();
4526
4527 // In Objective-C, verify that the type names an Objective-C type.
4528 // We don't want to check this outside of ObjC because people sometimes
4529 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004530 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004531 // Check for an existing type with this name.
4532 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4533 Sema::LookupOrdinaryName);
4534 if (S.LookupName(R, Sc)) {
4535 NamedDecl *Target = R.getFoundDecl();
4536 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4537 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4538 S.Diag(Target->getLocStart(), diag::note_declared_at);
4539 }
4540 }
4541 }
4542
Michael Han99315932013-01-24 16:46:58 +00004543 D->addAttr(::new (S.Context)
4544 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4545 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004546}
4547
Chandler Carruthedc2c642011-07-02 00:01:44 +00004548static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4549 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004550 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004551
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004552 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004553 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004554}
4555
Chandler Carruthedc2c642011-07-02 00:01:44 +00004556static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4557 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004558 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004559 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004560 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004561 return;
4562 }
4563
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004564 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004565 QualType type = vd->getType();
4566
4567 if (!type->isDependentType() &&
4568 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004569 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004570 << type;
4571 return;
4572 }
4573
4574 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4575
4576 // If we have no lifetime yet, check the lifetime we're presumably
4577 // going to infer.
4578 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4579 lifetime = type->getObjCARCImplicitLifetime();
4580
4581 switch (lifetime) {
4582 case Qualifiers::OCL_None:
4583 assert(type->isDependentType() &&
4584 "didn't infer lifetime for non-dependent type?");
4585 break;
4586
4587 case Qualifiers::OCL_Weak: // meaningful
4588 case Qualifiers::OCL_Strong: // meaningful
4589 break;
4590
4591 case Qualifiers::OCL_ExplicitNone:
4592 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004593 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004594 << (lifetime == Qualifiers::OCL_Autoreleasing);
4595 break;
4596 }
4597
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004598 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004599 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4600 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004601}
4602
Francois Picheta83957a2010-12-19 06:50:37 +00004603//===----------------------------------------------------------------------===//
4604// Microsoft specific attribute handlers.
4605//===----------------------------------------------------------------------===//
4606
Reid Kleckner140c4a72013-05-17 14:04:52 +00004607// Check if MS extensions or some other language extensions are enabled. If
4608// not, issue a diagnostic that the given attribute is unused.
4609static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4610 bool OtherExtension = false) {
4611 if (S.LangOpts.MicrosoftExt || OtherExtension)
4612 return true;
4613 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4614 return false;
4615}
4616
Chandler Carruthedc2c642011-07-02 00:01:44 +00004617static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004618 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4619 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004620
Reid Kleckner140c4a72013-05-17 14:04:52 +00004621 // check the attribute arguments.
4622 if (!checkAttributeNumArgs(S, Attr, 1))
4623 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004624
Reid Kleckner140c4a72013-05-17 14:04:52 +00004625 Expr *Arg = Attr.getArg(0);
4626 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4627 if (!Str || !Str->isAscii()) {
4628 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4629 << "uuid" << 1;
4630 return;
4631 }
Francois Pichet7da11662010-12-20 01:41:49 +00004632
Reid Kleckner140c4a72013-05-17 14:04:52 +00004633 StringRef StrRef = Str->getString();
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004634
Reid Kleckner140c4a72013-05-17 14:04:52 +00004635 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4636 StrRef.back() == '}';
Francois Pichet7da11662010-12-20 01:41:49 +00004637
Reid Kleckner140c4a72013-05-17 14:04:52 +00004638 // Validate GUID length.
4639 if (IsCurly && StrRef.size() != 38) {
4640 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4641 return;
4642 }
4643 if (!IsCurly && StrRef.size() != 36) {
4644 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4645 return;
4646 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004647
Reid Kleckner140c4a72013-05-17 14:04:52 +00004648 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4649 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4650 StringRef::iterator I = StrRef.begin();
4651 if (IsCurly) // Skip the optional '{'
4652 ++I;
4653
4654 for (int i = 0; i < 36; ++i) {
4655 if (i == 8 || i == 13 || i == 18 || i == 23) {
4656 if (*I != '-') {
Francois Pichet7da11662010-12-20 01:41:49 +00004657 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4658 return;
4659 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004660 } else if (!isHexDigit(*I)) {
4661 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4662 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004663 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004664 I++;
4665 }
Francois Picheta83957a2010-12-19 06:50:37 +00004666
Reid Kleckner140c4a72013-05-17 14:04:52 +00004667 D->addAttr(::new (S.Context)
4668 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4669 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004670}
4671
John McCall8d32c052012-05-22 21:28:12 +00004672static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004673 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004674 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004675
4676 AttributeList::Kind Kind = Attr.getKind();
4677 if (Kind == AttributeList::AT_SingleInheritance)
4678 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004679 ::new (S.Context)
4680 SingleInheritanceAttr(Attr.getRange(), S.Context,
4681 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004682 else if (Kind == AttributeList::AT_MultipleInheritance)
4683 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004684 ::new (S.Context)
4685 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4686 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004687 else if (Kind == AttributeList::AT_VirtualInheritance)
4688 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004689 ::new (S.Context)
4690 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4691 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004692}
4693
4694static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004695 if (!checkMicrosoftExt(S, Attr))
4696 return;
4697
4698 AttributeList::Kind Kind = Attr.getKind();
4699 if (Kind == AttributeList::AT_Ptr32)
4700 D->addAttr(
4701 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context,
4702 Attr.getAttributeSpellingListIndex()));
4703 else if (Kind == AttributeList::AT_Ptr64)
4704 D->addAttr(
4705 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context,
4706 Attr.getAttributeSpellingListIndex()));
4707 else if (Kind == AttributeList::AT_Win64)
4708 D->addAttr(
4709 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4710 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004711}
4712
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004713static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004714 if (!checkMicrosoftExt(S, Attr))
4715 return;
4716 D->addAttr(::new (S.Context)
4717 ForceInlineAttr(Attr.getRange(), S.Context,
4718 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004719}
4720
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004721//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004722// Top Level Sema Entry Points
4723//===----------------------------------------------------------------------===//
4724
Chandler Carruthedc2c642011-07-02 00:01:44 +00004725static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4726 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004727 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004728 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4729 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4730 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004731 default:
4732 break;
4733 }
4734}
Abramo Bagnara50099372010-04-30 13:10:51 +00004735
Chandler Carruthedc2c642011-07-02 00:01:44 +00004736static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4737 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004738 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004739 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4740 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4741 case AttributeList::AT_IBOutletCollection:
4742 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004743 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004744 case AttributeList::AT_ObjCGC:
4745 case AttributeList::AT_VectorSize:
4746 case AttributeList::AT_NeonVectorType:
4747 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004748 // Ignore these, these are type attributes, handled by
4749 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004750 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004751 case AttributeList::AT_CUDADevice:
4752 case AttributeList::AT_CUDAHost:
4753 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004754 // Ignore, this is a non-inheritable attribute, handled
4755 // by ProcessNonInheritableDeclAttr.
4756 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004757 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4758 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4759 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4760 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004761 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004762 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004763 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004764 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004765 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4766 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4767 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004768 handleDependencyAttr(S, scope, D, Attr);
4769 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004770 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4771 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4772 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004773 case AttributeList::AT_CXX11NoReturn:
4774 handleCXX11NoReturnAttr(S, D, Attr);
4775 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004776 case AttributeList::AT_Deprecated:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004777 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4778 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004779 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4780 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004781 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004782 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004783 case AttributeList::AT_MinSize:
4784 handleMinSizeAttr(S, D, Attr);
4785 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004786 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4787 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4788 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4789 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4790 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004791 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004792 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004793 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4794 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4795 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4796 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4797 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004798 case AttributeList::AT_ownership_returns:
4799 case AttributeList::AT_ownership_takes:
4800 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004801 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004802 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4803 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4804 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4805 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4806 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4807 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4808 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004809
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004810 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004811 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004812 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004813 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004814
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004815 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004816 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4817
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004818 case AttributeList::AT_ObjCRequiresSuper:
4819 handleObjCRequiresSuperAttr(S, D, Attr); break;
4820
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004821 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004822 handleNSBridgedAttr(S, scope, D, Attr); break;
4823
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004824 case AttributeList::AT_CFAuditedTransfer:
4825 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004826 handleCFTransferAttr(S, D, Attr); break;
4827
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004828 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004829 case AttributeList::AT_CFConsumed:
4830 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4831 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004832 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004833
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004834 case AttributeList::AT_NSReturnsAutoreleased:
4835 case AttributeList::AT_NSReturnsNotRetained:
4836 case AttributeList::AT_CFReturnsNotRetained:
4837 case AttributeList::AT_NSReturnsRetained:
4838 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004839 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004840
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004841 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004842 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004843 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004844
Joey Goulyaba589c2013-03-08 09:42:32 +00004845 case AttributeList::AT_VecTypeHint:
4846 handleVecTypeHint(S, D, Attr); break;
4847
Joey Gouly0608ae82013-03-14 09:54:43 +00004848 case AttributeList::AT_Endian:
4849 handleEndianAttr(S, D, Attr);
4850 break;
4851
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004852 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004853 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004854
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004855 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4856 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4857 case AttributeList::AT_Unavailable:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004858 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4859 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004860 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004861 handleArcWeakrefUnavailableAttr (S, D, Attr);
4862 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004863 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004864 handleObjCRootClassAttr(S, D, Attr);
4865 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004866 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004867 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004868 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004869 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4870 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004871 handleReturnsTwiceAttr(S, D, Attr);
4872 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004873 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004874 case AttributeList::AT_Visibility:
4875 handleVisibilityAttr(S, D, Attr, false);
4876 break;
4877 case AttributeList::AT_TypeVisibility:
4878 handleVisibilityAttr(S, D, Attr, true);
4879 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004880 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004881 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004882 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4883 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4884 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4885 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004886 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004887 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004888 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004889 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004890 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004891 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004892 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004893 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004894 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4895 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4896 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4897 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4898 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4899 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4900 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4901 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4902 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004903 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004904 // Just ignore
4905 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004906 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004907 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004908 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004909 case AttributeList::AT_StdCall:
4910 case AttributeList::AT_CDecl:
4911 case AttributeList::AT_FastCall:
4912 case AttributeList::AT_ThisCall:
4913 case AttributeList::AT_Pascal:
4914 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004915 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004916 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004917 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004918 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004919 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004920 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004921 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004922 case AttributeList::AT_OpenCLImageAccess:
4923 handleOpenCLImageAccessAttr(S, D, Attr);
4924 break;
John McCall8d32c052012-05-22 21:28:12 +00004925
4926 // Microsoft attributes:
John McCall5e77d762013-04-16 07:28:30 +00004927 case AttributeList::AT_MsProperty: break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004928 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004929 handleMsStructAttr(S, D, Attr);
4930 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004931 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004932 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004933 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004934 case AttributeList::AT_SingleInheritance:
4935 case AttributeList::AT_MultipleInheritance:
4936 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004937 handleInheritanceAttr(S, D, Attr);
4938 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004939 case AttributeList::AT_Win64:
4940 case AttributeList::AT_Ptr32:
4941 case AttributeList::AT_Ptr64:
John McCall8d32c052012-05-22 21:28:12 +00004942 handlePortabilityAttr(S, D, Attr);
4943 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004944 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004945 handleForceInlineAttr(S, D, Attr);
4946 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004947
4948 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004949 case AttributeList::AT_AssertExclusiveLock:
4950 handleAssertExclusiveLockAttr(S, D, Attr);
4951 break;
4952 case AttributeList::AT_AssertSharedLock:
4953 handleAssertSharedLockAttr(S, D, Attr);
4954 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004955 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004956 handleGuardedVarAttr(S, D, Attr);
4957 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004958 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004959 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004960 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004961 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004962 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004963 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004964 case AttributeList::AT_NoSanitizeAddress:
4965 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004966 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004967 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004968 handleNoThreadSafetyAnalysis(S, D, Attr);
4969 break;
4970 case AttributeList::AT_NoSanitizeThread:
4971 handleNoSanitizeThread(S, D, Attr);
4972 break;
4973 case AttributeList::AT_NoSanitizeMemory:
4974 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004975 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004976 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004977 handleLockableAttr(S, D, Attr);
4978 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004979 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004980 handleGuardedByAttr(S, D, Attr);
4981 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004982 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004983 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004984 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004985 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004986 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004987 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004988 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004989 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004990 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004991 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004992 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004993 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004994 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004995 handleLockReturnedAttr(S, D, Attr);
4996 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004997 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004998 handleLocksExcludedAttr(S, D, Attr);
4999 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005000 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005001 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005002 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005003 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00005004 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005005 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005006 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005007 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005008 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005009 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005010 handleUnlockFunAttr(S, D, Attr);
5011 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005012 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005013 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005014 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005015 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005016 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005017 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005018
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005019 // Type safety attributes.
5020 case AttributeList::AT_ArgumentWithTypeTag:
5021 handleArgumentWithTypeTagAttr(S, D, Attr);
5022 break;
5023 case AttributeList::AT_TypeTagForDatatype:
5024 handleTypeTagForDatatypeAttr(S, D, Attr);
5025 break;
5026
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005027 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005028 // Ask target about the attribute.
5029 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5030 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00005031 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5032 diag::warn_unhandled_ms_attribute_ignored :
5033 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005034 break;
5035 }
5036}
5037
Peter Collingbourneb331b262011-01-21 02:08:45 +00005038/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5039/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smith10876ef2013-01-17 01:30:42 +00005040/// silently ignore it if a GNU attribute.
Chandler Carruthedc2c642011-07-02 00:01:44 +00005041static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5042 const AttributeList &Attr,
Richard Smith10876ef2013-01-17 01:30:42 +00005043 bool NonInheritable, bool Inheritable,
5044 bool IncludeCXX11Attributes) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00005045 if (Attr.isInvalid())
5046 return;
5047
Richard Smith10876ef2013-01-17 01:30:42 +00005048 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5049 // instead.
5050 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5051 return;
5052
Peter Collingbourneb331b262011-01-21 02:08:45 +00005053 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00005054 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00005055
5056 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00005057 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00005058}
5059
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005060/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5061/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005062void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005063 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005064 bool NonInheritable, bool Inheritable,
5065 bool IncludeCXX11Attributes) {
5066 for (const AttributeList* l = AttrList; l; l = l->getNext())
5067 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5068 IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005069
5070 // GCC accepts
5071 // static int a9 __attribute__((weakref));
5072 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005073 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00005074 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00005075 cast<NamedDecl>(D)->getNameAsString();
5076 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005077 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005078 }
5079}
5080
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005081// Annotation attributes are the only attributes allowed after an access
5082// specifier.
5083bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5084 const AttributeList *AttrList) {
5085 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005086 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005087 handleAnnotateAttr(*this, ASDecl, *l);
5088 } else {
5089 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5090 return true;
5091 }
5092 }
5093
5094 return false;
5095}
5096
John McCall42856de2011-10-01 05:17:03 +00005097/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5098/// contains any decl attributes that we should warn about.
5099static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5100 for ( ; A; A = A->getNext()) {
5101 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005102 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005103 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5104
5105 if (A->getKind() == AttributeList::UnknownAttribute) {
5106 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5107 << A->getName() << A->getRange();
5108 } else {
5109 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5110 << A->getName() << A->getRange();
5111 }
5112 }
5113}
5114
5115/// checkUnusedDeclAttributes - Given a declarator which is not being
5116/// used to build a declaration, complain about any decl attributes
5117/// which might be lying around on it.
5118void Sema::checkUnusedDeclAttributes(Declarator &D) {
5119 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5120 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5121 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5122 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5123}
5124
Ryan Flynn7d470f32009-07-30 03:15:39 +00005125/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005126/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005127NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5128 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005129 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005130 NamedDecl *NewD = 0;
5131 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005132 FunctionDecl *NewFD;
5133 // FIXME: Missing call to CheckFunctionDeclaration().
5134 // FIXME: Mangling?
5135 // FIXME: Is the qualifier info correct?
5136 // FIXME: Is the DeclContext correct?
5137 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5138 Loc, Loc, DeclarationName(II),
5139 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005140 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005141 FD->hasPrototype(),
5142 false/*isConstexprSpecified*/);
5143 NewD = NewFD;
5144
5145 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005146 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005147
5148 // Fake up parameter variables; they are declared as if this were
5149 // a typedef.
5150 QualType FDTy = FD->getType();
5151 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5152 SmallVector<ParmVarDecl*, 16> Params;
5153 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5154 AE = FT->arg_type_end(); AI != AE; ++AI) {
5155 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5156 Param->setScopeInfo(0, Params.size());
5157 Params.push_back(Param);
5158 }
David Blaikie9c70e042011-09-21 18:16:56 +00005159 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005160 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005161 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5162 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005163 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005164 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005165 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005166 if (VD->getQualifier()) {
5167 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005168 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005169 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005170 }
5171 return NewD;
5172}
5173
James Dennett634962f2012-06-14 21:40:34 +00005174/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005175/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005176void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005177 if (W.getUsed()) return; // only do this once
5178 W.setUsed(true);
5179 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5180 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005181 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005182 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5183 NDId->getName()));
5184 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005185 WeakTopLevelDecl.push_back(NewD);
5186 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5187 // to insert Decl at TU scope, sorry.
5188 DeclContext *SavedContext = CurContext;
5189 CurContext = Context.getTranslationUnitDecl();
5190 PushOnScopeChains(NewD, S);
5191 CurContext = SavedContext;
5192 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005193 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005194 }
5195}
5196
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005197void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5198 // It's valid to "forward-declare" #pragma weak, in which case we
5199 // have to do this.
5200 LoadExternalWeakUndeclaredIdentifiers();
5201 if (!WeakUndeclaredIdentifiers.empty()) {
5202 NamedDecl *ND = NULL;
5203 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5204 if (VD->isExternC())
5205 ND = VD;
5206 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5207 if (FD->isExternC())
5208 ND = FD;
5209 if (ND) {
5210 if (IdentifierInfo *Id = ND->getIdentifier()) {
5211 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5212 = WeakUndeclaredIdentifiers.find(Id);
5213 if (I != WeakUndeclaredIdentifiers.end()) {
5214 WeakInfo W = I->second;
5215 DeclApplyPragmaWeak(S, ND, W);
5216 WeakUndeclaredIdentifiers[Id] = W;
5217 }
5218 }
5219 }
5220 }
5221}
5222
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005223/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5224/// it, apply them to D. This is a bit tricky because PD can have attributes
5225/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005226void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5227 bool NonInheritable, bool Inheritable) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005228 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005229 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005230 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005231
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005232 // Walk the declarator structure, applying decl attributes that were in a type
5233 // position to the decl itself. This handles cases like:
5234 // int *__attr__(x)** D;
5235 // when X is a decl attribute.
5236 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5237 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith10876ef2013-01-17 01:30:42 +00005238 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5239 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005240
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005241 // Finally, apply any attributes on the decl itself.
5242 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005243 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005244}
John McCall28a6aea2009-11-04 02:18:39 +00005245
John McCall31168b02011-06-15 23:02:42 +00005246/// Is the given declaration allowed to use a forbidden type?
5247static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5248 // Private ivars are always okay. Unfortunately, people don't
5249 // always properly make their ivars private, even in system headers.
5250 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005251 // Function declarations in sys headers will be marked unavailable.
5252 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5253 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005254 return false;
5255
5256 // Require it to be declared in a system header.
5257 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5258}
5259
5260/// Handle a delayed forbidden-type diagnostic.
5261static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5262 Decl *decl) {
5263 if (decl && isForbiddenTypeAllowed(S, decl)) {
5264 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5265 "this system declaration uses an unsupported type"));
5266 return;
5267 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005268 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005269 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005270 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005271 // kind of forbidden type messages on unavailable functions.
5272 if (FD->hasAttr<UnavailableAttr>() &&
5273 diag.getForbiddenTypeDiagnostic() ==
5274 diag::err_arc_array_param_no_ownership) {
5275 diag.Triggered = true;
5276 return;
5277 }
5278 }
John McCall31168b02011-06-15 23:02:42 +00005279
5280 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5281 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5282 diag.Triggered = true;
5283}
5284
John McCall2ec85372012-05-07 06:16:41 +00005285void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5286 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005287 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005288 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005289
John McCall2ec85372012-05-07 06:16:41 +00005290 // When delaying diagnostics to run in the context of a parsed
5291 // declaration, we only want to actually emit anything if parsing
5292 // succeeds.
5293 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005294
John McCall2ec85372012-05-07 06:16:41 +00005295 // We emit all the active diagnostics in this pool or any of its
5296 // parents. In general, we'll get one pool for the decl spec
5297 // and a child pool for each declarator; in a decl group like:
5298 // deprecated_typedef foo, *bar, baz();
5299 // only the declarator pops will be passed decls. This is correct;
5300 // we really do need to consider delayed diagnostics from the decl spec
5301 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005302 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005303 do {
John McCall6347b682012-05-07 06:16:58 +00005304 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005305 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5306 // This const_cast is a bit lame. Really, Triggered should be mutable.
5307 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005308 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005309 continue;
5310
John McCallc1465822011-02-14 07:13:47 +00005311 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005312 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005313 // Don't bother giving deprecation diagnostics if the decl is invalid.
5314 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005315 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005316 break;
5317
5318 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005319 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005320 break;
John McCall31168b02011-06-15 23:02:42 +00005321
5322 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005323 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005324 break;
John McCall86121512010-01-27 03:50:35 +00005325 }
5326 }
John McCall2ec85372012-05-07 06:16:41 +00005327 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005328}
5329
John McCall6347b682012-05-07 06:16:58 +00005330/// Given a set of delayed diagnostics, re-emit them as if they had
5331/// been delayed in the current context instead of in the given pool.
5332/// Essentially, this just moves them to the current pool.
5333void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5334 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5335 assert(curPool && "re-emitting in undelayed context not supported");
5336 curPool->steal(pool);
5337}
5338
John McCall28a6aea2009-11-04 02:18:39 +00005339static bool isDeclDeprecated(Decl *D) {
5340 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005341 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005342 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005343 // A category implicitly has the availability of the interface.
5344 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5345 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005346 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5347 return false;
5348}
5349
Eli Friedman971bfa12012-08-08 21:52:41 +00005350static void
5351DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5352 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005353 const ObjCInterfaceDecl *UnknownObjCClass,
5354 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005355 DeclarationName Name = D->getDeclName();
5356 if (!Message.empty()) {
5357 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5358 S.Diag(D->getLocation(),
5359 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5360 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005361 if (ObjCPropery)
5362 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5363 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005364 } else if (!UnknownObjCClass) {
5365 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5366 S.Diag(D->getLocation(),
5367 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5368 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005369 if (ObjCPropery)
5370 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5371 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005372 } else {
5373 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5374 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5375 }
5376}
5377
John McCallb45a1e72010-08-26 02:13:20 +00005378void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005379 Decl *Ctx) {
5380 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005381 return;
5382
John McCall86121512010-01-27 03:50:35 +00005383 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005384 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5385 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005386 DD.getUnknownObjCClass(),
5387 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005388}
5389
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005390void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005391 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005392 const ObjCInterfaceDecl *UnknownObjCClass,
5393 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005394 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005395 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005396 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5397 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005398 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005399 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005400 return;
5401 }
5402
5403 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005404 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005405 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005406 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005407}