blob: a254caeff75195ced4f009e533c3cd587a65d436 [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());
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001749
1750 if (S.LangOpts.CPlusPlus) {
1751 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1752 return;
1753 }
1754
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001755 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001756 D->addAttr(::new (S.Context)
1757 CommonAttr(Attr.getRange(), S.Context,
1758 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001759 else
1760 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001761 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001762}
1763
Chandler Carruthedc2c642011-07-02 00:01:44 +00001764static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001765 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001766
1767 if (S.CheckNoReturnAttr(attr)) return;
1768
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001769 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001770 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001771 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001772 return;
1773 }
1774
Michael Han99315932013-01-24 16:46:58 +00001775 D->addAttr(::new (S.Context)
1776 NoReturnAttr(attr.getRange(), S.Context,
1777 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001778}
1779
1780bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001781 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001782 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1783 attr.setInvalid();
1784 return true;
1785 }
1786
1787 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001788}
1789
Chandler Carruthedc2c642011-07-02 00:01:44 +00001790static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1791 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001792
1793 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1794 // because 'analyzer_noreturn' does not impact the type.
1795
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001796 if(!checkAttributeNumArgs(S, Attr, 0))
1797 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001798
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001799 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1800 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001801 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1802 && !VD->getType()->isFunctionPointerType())) {
1803 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001804 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001805 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001806 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001807 return;
1808 }
1809 }
1810
Michael Han99315932013-01-24 16:46:58 +00001811 D->addAttr(::new (S.Context)
1812 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1813 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001814}
1815
Richard Smith10876ef2013-01-17 01:30:42 +00001816static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1817 const AttributeList &Attr) {
1818 // C++11 [dcl.attr.noreturn]p1:
1819 // The attribute may be applied to the declarator-id in a function
1820 // declaration.
1821 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1822 if (!FD) {
1823 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1824 << Attr.getName() << ExpectedFunctionOrMethod;
1825 return;
1826 }
1827
Michael Han99315932013-01-24 16:46:58 +00001828 D->addAttr(::new (S.Context)
1829 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1830 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001831}
1832
John Thompsoncdb847ba2010-08-09 21:53:52 +00001833// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001834static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001835/*
1836 Returning a Vector Class in Registers
1837
Eric Christopherbc638a82010-12-01 22:13:54 +00001838 According to the PPU ABI specifications, a class with a single member of
1839 vector type is returned in memory when used as the return value of a function.
1840 This results in inefficient code when implementing vector classes. To return
1841 the value in a single vector register, add the vecreturn attribute to the
1842 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001843
1844 Example:
1845
1846 struct Vector
1847 {
1848 __vector float xyzw;
1849 } __attribute__((vecreturn));
1850
1851 Vector Add(Vector lhs, Vector rhs)
1852 {
1853 Vector result;
1854 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1855 return result; // This will be returned in a register
1856 }
1857*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001858 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001859 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001860 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001861 return;
1862 }
1863
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001864 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001865 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1866 return;
1867 }
1868
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001869 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001870 int count = 0;
1871
1872 if (!isa<CXXRecordDecl>(record)) {
1873 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1874 return;
1875 }
1876
1877 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1878 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1879 return;
1880 }
1881
Eric Christopherbc638a82010-12-01 22:13:54 +00001882 for (RecordDecl::field_iterator iter = record->field_begin();
1883 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001884 if ((count == 1) || !iter->getType()->isVectorType()) {
1885 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1886 return;
1887 }
1888 count++;
1889 }
1890
Michael Han99315932013-01-24 16:46:58 +00001891 D->addAttr(::new (S.Context)
1892 VecReturnAttr(Attr.getRange(), S.Context,
1893 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001894}
1895
Richard Smithe233fbf2013-01-28 22:42:45 +00001896static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1897 const AttributeList &Attr) {
1898 if (isa<ParmVarDecl>(D)) {
1899 // [[carries_dependency]] can only be applied to a parameter if it is a
1900 // parameter of a function declaration or lambda.
1901 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1902 S.Diag(Attr.getLoc(),
1903 diag::err_carries_dependency_param_not_function_decl);
1904 return;
1905 }
1906 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001907 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001908 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001909 return;
1910 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001911
1912 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1913 Attr.getRange(), S.Context,
1914 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001915}
1916
Chandler Carruthedc2c642011-07-02 00:01:44 +00001917static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001918 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001919 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001920 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001921 return;
1922 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001923
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001924 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001925 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001926 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001927 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001928 return;
1929 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001930
Michael Han99315932013-01-24 16:46:58 +00001931 D->addAttr(::new (S.Context)
1932 UnusedAttr(Attr.getRange(), S.Context,
1933 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001934}
1935
Rafael Espindola70107f92011-10-03 14:59:42 +00001936static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1937 const AttributeList &Attr) {
1938 // check the attribute arguments.
1939 if (Attr.hasParameterOrArguments()) {
1940 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1941 return;
1942 }
1943
1944 if (!isa<FunctionDecl>(D)) {
1945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1946 << Attr.getName() << ExpectedFunction;
1947 return;
1948 }
1949
Michael Han99315932013-01-24 16:46:58 +00001950 D->addAttr(::new (S.Context)
1951 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1952 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00001953}
1954
Chandler Carruthedc2c642011-07-02 00:01:44 +00001955static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001956 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001957 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001958 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1959 return;
1960 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001961
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001962 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001963 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001964 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1965 return;
1966 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001967 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001968 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001969 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001970 return;
1971 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001972
Michael Han99315932013-01-24 16:46:58 +00001973 D->addAttr(::new (S.Context)
1974 UsedAttr(Attr.getRange(), S.Context,
1975 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001976}
1977
Chandler Carruthedc2c642011-07-02 00:01:44 +00001978static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001979 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001980 if (Attr.getNumArgs() > 1) {
1981 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001982 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001983 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001984
1985 int priority = 65535; // FIXME: Do not hardcode such constants.
1986 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001987 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001988 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001989 if (E->isTypeDependent() || E->isValueDependent() ||
1990 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001991 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001992 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001993 return;
1994 }
1995 priority = Idx.getZExtValue();
1996 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001997
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001998 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001999 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002000 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002001 return;
2002 }
2003
Michael Han99315932013-01-24 16:46:58 +00002004 D->addAttr(::new (S.Context)
2005 ConstructorAttr(Attr.getRange(), S.Context, priority,
2006 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002007}
2008
Chandler Carruthedc2c642011-07-02 00:01:44 +00002009static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002010 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002011 if (Attr.getNumArgs() > 1) {
2012 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002013 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002014 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002015
2016 int priority = 65535; // FIXME: Do not hardcode such constants.
2017 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002018 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00002019 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002020 if (E->isTypeDependent() || E->isValueDependent() ||
2021 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002022 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002023 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00002024 return;
2025 }
2026 priority = Idx.getZExtValue();
2027 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002028
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002029 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002030 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002031 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002032 return;
2033 }
2034
Michael Han99315932013-01-24 16:46:58 +00002035 D->addAttr(::new (S.Context)
2036 DestructorAttr(Attr.getRange(), S.Context, priority,
2037 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002038}
2039
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002040template <typename AttrTy>
2041static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
2042 const char *Name) {
Chris Lattner190aa102011-02-24 05:42:24 +00002043 unsigned NumArgs = Attr.getNumArgs();
2044 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00002045 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002046 return;
2047 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002048
2049 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002050 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00002051 if (NumArgs == 1) {
2052 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00002053 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00002054 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002055 << Name;
Fariborz Jahanian551063102010-10-06 21:18:44 +00002056 return;
2057 }
Chris Lattner190aa102011-02-24 05:42:24 +00002058 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00002059 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002060
Michael Han99315932013-01-24 16:46:58 +00002061 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2062 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002063}
2064
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002065static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2066 const AttributeList &Attr) {
2067 unsigned NumArgs = Attr.getNumArgs();
2068 if (NumArgs > 0) {
2069 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2070 return;
2071 }
2072
Michael Han99315932013-01-24 16:46:58 +00002073 D->addAttr(::new (S.Context)
2074 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2075 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002076}
2077
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002078static void handleObjCRootClassAttr(Sema &S, Decl *D,
2079 const AttributeList &Attr) {
2080 if (!isa<ObjCInterfaceDecl>(D)) {
2081 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2082 return;
2083 }
2084
2085 unsigned NumArgs = Attr.getNumArgs();
2086 if (NumArgs > 0) {
2087 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2088 return;
2089 }
2090
Michael Han99315932013-01-24 16:46:58 +00002091 D->addAttr(::new (S.Context)
2092 ObjCRootClassAttr(Attr.getRange(), S.Context,
2093 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002094}
2095
Michael Han99315932013-01-24 16:46:58 +00002096static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2097 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002098 if (!isa<ObjCInterfaceDecl>(D)) {
2099 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2100 return;
2101 }
2102
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002103 unsigned NumArgs = Attr.getNumArgs();
2104 if (NumArgs > 0) {
2105 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2106 return;
2107 }
2108
Michael Han99315932013-01-24 16:46:58 +00002109 D->addAttr(::new (S.Context)
2110 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2111 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002112}
2113
Jordy Rose740b0c22012-05-08 03:27:22 +00002114static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2115 IdentifierInfo *Platform,
2116 VersionTuple Introduced,
2117 VersionTuple Deprecated,
2118 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002119 StringRef PlatformName
2120 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2121 if (PlatformName.empty())
2122 PlatformName = Platform->getName();
2123
2124 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2125 // of these steps are needed).
2126 if (!Introduced.empty() && !Deprecated.empty() &&
2127 !(Introduced <= Deprecated)) {
2128 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2129 << 1 << PlatformName << Deprecated.getAsString()
2130 << 0 << Introduced.getAsString();
2131 return true;
2132 }
2133
2134 if (!Introduced.empty() && !Obsoleted.empty() &&
2135 !(Introduced <= Obsoleted)) {
2136 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2137 << 2 << PlatformName << Obsoleted.getAsString()
2138 << 0 << Introduced.getAsString();
2139 return true;
2140 }
2141
2142 if (!Deprecated.empty() && !Obsoleted.empty() &&
2143 !(Deprecated <= Obsoleted)) {
2144 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2145 << 2 << PlatformName << Obsoleted.getAsString()
2146 << 1 << Deprecated.getAsString();
2147 return true;
2148 }
2149
2150 return false;
2151}
2152
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002153/// \brief Check whether the two versions match.
2154///
2155/// If either version tuple is empty, then they are assumed to match. If
2156/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2157static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2158 bool BeforeIsOkay) {
2159 if (X.empty() || Y.empty())
2160 return true;
2161
2162 if (X == Y)
2163 return true;
2164
2165 if (BeforeIsOkay && X < Y)
2166 return true;
2167
2168 return false;
2169}
2170
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002171AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002172 IdentifierInfo *Platform,
2173 VersionTuple Introduced,
2174 VersionTuple Deprecated,
2175 VersionTuple Obsoleted,
2176 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002177 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002178 bool Override,
2179 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002180 VersionTuple MergedIntroduced = Introduced;
2181 VersionTuple MergedDeprecated = Deprecated;
2182 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002183 bool FoundAny = false;
2184
Rafael Espindolac67f2232012-05-10 02:50:16 +00002185 if (D->hasAttrs()) {
2186 AttrVec &Attrs = D->getAttrs();
2187 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2188 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2189 if (!OldAA) {
2190 ++i;
2191 continue;
2192 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002193
Rafael Espindolac67f2232012-05-10 02:50:16 +00002194 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2195 if (OldPlatform != Platform) {
2196 ++i;
2197 continue;
2198 }
2199
2200 FoundAny = true;
2201 VersionTuple OldIntroduced = OldAA->getIntroduced();
2202 VersionTuple OldDeprecated = OldAA->getDeprecated();
2203 VersionTuple OldObsoleted = OldAA->getObsoleted();
2204 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002205
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002206 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2207 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2208 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2209 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002210 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002211 if (Override) {
2212 int Which = -1;
2213 VersionTuple FirstVersion;
2214 VersionTuple SecondVersion;
2215 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2216 Which = 0;
2217 FirstVersion = OldIntroduced;
2218 SecondVersion = Introduced;
2219 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2220 Which = 1;
2221 FirstVersion = Deprecated;
2222 SecondVersion = OldDeprecated;
2223 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2224 Which = 2;
2225 FirstVersion = Obsoleted;
2226 SecondVersion = OldObsoleted;
2227 }
2228
2229 if (Which == -1) {
2230 Diag(OldAA->getLocation(),
2231 diag::warn_mismatched_availability_override_unavail)
2232 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2233 } else {
2234 Diag(OldAA->getLocation(),
2235 diag::warn_mismatched_availability_override)
2236 << Which
2237 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2238 << FirstVersion.getAsString() << SecondVersion.getAsString();
2239 }
2240 Diag(Range.getBegin(), diag::note_overridden_method);
2241 } else {
2242 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2243 Diag(Range.getBegin(), diag::note_previous_attribute);
2244 }
2245
Rafael Espindolac67f2232012-05-10 02:50:16 +00002246 Attrs.erase(Attrs.begin() + i);
2247 --e;
2248 continue;
2249 }
2250
2251 VersionTuple MergedIntroduced2 = MergedIntroduced;
2252 VersionTuple MergedDeprecated2 = MergedDeprecated;
2253 VersionTuple MergedObsoleted2 = MergedObsoleted;
2254
2255 if (MergedIntroduced2.empty())
2256 MergedIntroduced2 = OldIntroduced;
2257 if (MergedDeprecated2.empty())
2258 MergedDeprecated2 = OldDeprecated;
2259 if (MergedObsoleted2.empty())
2260 MergedObsoleted2 = OldObsoleted;
2261
2262 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2263 MergedIntroduced2, MergedDeprecated2,
2264 MergedObsoleted2)) {
2265 Attrs.erase(Attrs.begin() + i);
2266 --e;
2267 continue;
2268 }
2269
2270 MergedIntroduced = MergedIntroduced2;
2271 MergedDeprecated = MergedDeprecated2;
2272 MergedObsoleted = MergedObsoleted2;
2273 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002274 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002275 }
2276
2277 if (FoundAny &&
2278 MergedIntroduced == Introduced &&
2279 MergedDeprecated == Deprecated &&
2280 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002281 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002282
Ted Kremenekb5445722013-04-06 00:34:27 +00002283 // Only create a new attribute if !Override, but we want to do
2284 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002285 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002286 MergedDeprecated, MergedObsoleted) &&
2287 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002288 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2289 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002290 Obsoleted, IsUnavailable, Message,
2291 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002292 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002293 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002294}
2295
Chandler Carruthedc2c642011-07-02 00:01:44 +00002296static void handleAvailabilityAttr(Sema &S, Decl *D,
2297 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002298 IdentifierInfo *Platform = Attr.getParameterName();
2299 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han99315932013-01-24 16:46:58 +00002300 unsigned Index = Attr.getAttributeSpellingListIndex();
2301
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002302 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002303 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2304 << Platform;
2305
Rafael Espindolac231fab2013-01-08 21:30:32 +00002306 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2307 if (!ND) {
2308 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2309 return;
2310 }
2311
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002312 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2313 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2314 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002315 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002316 StringRef Str;
2317 const StringLiteral *SE =
2318 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2319 if (SE)
2320 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002321
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002322 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002323 Platform,
2324 Introduced.Version,
2325 Deprecated.Version,
2326 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002327 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002328 /*Override=*/false,
2329 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002330 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002331 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002332}
2333
John McCalld041a9b2013-02-20 01:54:26 +00002334template <class T>
2335static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2336 typename T::VisibilityType value,
2337 unsigned attrSpellingListIndex) {
2338 T *existingAttr = D->getAttr<T>();
2339 if (existingAttr) {
2340 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2341 if (existingValue == value)
2342 return NULL;
2343 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2344 S.Diag(range.getBegin(), diag::note_previous_attribute);
2345 D->dropAttr<T>();
2346 }
2347 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2348}
2349
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002350VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002351 VisibilityAttr::VisibilityType Vis,
2352 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002353 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2354 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002355}
2356
John McCalld041a9b2013-02-20 01:54:26 +00002357TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2358 TypeVisibilityAttr::VisibilityType Vis,
2359 unsigned AttrSpellingListIndex) {
2360 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2361 AttrSpellingListIndex);
2362}
2363
2364static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2365 bool isTypeVisibility) {
2366 // Visibility attributes don't mean anything on a typedef.
2367 if (isa<TypedefNameDecl>(D)) {
2368 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2369 << Attr.getName();
2370 return;
2371 }
2372
2373 // 'type_visibility' can only go on a type or namespace.
2374 if (isTypeVisibility &&
2375 !(isa<TagDecl>(D) ||
2376 isa<ObjCInterfaceDecl>(D) ||
2377 isa<NamespaceDecl>(D))) {
2378 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2379 << Attr.getName() << ExpectedTypeOrNamespace;
2380 return;
2381 }
2382
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002383 // check the attribute arguments.
John McCalld041a9b2013-02-20 01:54:26 +00002384 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002385 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002386
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002387 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002388 Arg = Arg->IgnoreParenCasts();
2389 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002390
Douglas Gregorfb65e592011-07-27 05:40:30 +00002391 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002392 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld041a9b2013-02-20 01:54:26 +00002393 << (isTypeVisibility ? "type_visibility" : "visibility") << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002394 return;
2395 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002396
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002397 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002398 VisibilityAttr::VisibilityType type;
Michael Han99315932013-01-24 16:46:58 +00002399
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002400 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002401 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002402 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002403 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002404 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002405 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00002406 else if (TypeStr == "protected") {
2407 // Complain about attempts to use protected visibility on targets
2408 // (like Darwin) that don't support it.
2409 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2410 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2411 type = VisibilityAttr::Default;
2412 } else {
2413 type = VisibilityAttr::Protected;
2414 }
2415 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002416 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002417 return;
2418 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002419
Michael Han99315932013-01-24 16:46:58 +00002420 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002421 clang::Attr *newAttr;
2422 if (isTypeVisibility) {
2423 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2424 (TypeVisibilityAttr::VisibilityType) type,
2425 Index);
2426 } else {
2427 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2428 }
2429 if (newAttr)
2430 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002431}
2432
Chandler Carruthedc2c642011-07-02 00:01:44 +00002433static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2434 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002435 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2436 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002438 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002439 return;
2440 }
2441
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002442 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2443 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2444 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00002445 << "objc_method_family" << 1;
2446 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002448 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002449 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002450 return;
2451 }
2452
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002453 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002454 ObjCMethodFamilyAttr::FamilyKind family;
2455 if (param == "none")
2456 family = ObjCMethodFamilyAttr::OMF_None;
2457 else if (param == "alloc")
2458 family = ObjCMethodFamilyAttr::OMF_alloc;
2459 else if (param == "copy")
2460 family = ObjCMethodFamilyAttr::OMF_copy;
2461 else if (param == "init")
2462 family = ObjCMethodFamilyAttr::OMF_init;
2463 else if (param == "mutableCopy")
2464 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2465 else if (param == "new")
2466 family = ObjCMethodFamilyAttr::OMF_new;
2467 else {
2468 // Just warn and ignore it. This is future-proof against new
2469 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002470 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002471 return;
2472 }
2473
John McCall31168b02011-06-15 23:02:42 +00002474 if (family == ObjCMethodFamilyAttr::OMF_init &&
2475 !method->getResultType()->isObjCObjectPointerType()) {
2476 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2477 << method->getResultType();
2478 // Ignore the attribute.
2479 return;
2480 }
2481
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002482 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002483 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002484}
2485
Chandler Carruthedc2c642011-07-02 00:01:44 +00002486static void handleObjCExceptionAttr(Sema &S, Decl *D,
2487 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002488 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002489 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002490
Chris Lattner677a3582009-02-14 08:09:34 +00002491 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2492 if (OCI == 0) {
2493 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2494 return;
2495 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002496
Michael Han99315932013-01-24 16:46:58 +00002497 D->addAttr(::new (S.Context)
2498 ObjCExceptionAttr(Attr.getRange(), S.Context,
2499 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002500}
2501
Chandler Carruthedc2c642011-07-02 00:01:44 +00002502static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002503 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002505 return;
2506 }
Richard Smithdda56e42011-04-15 14:24:37 +00002507 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002508 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002509 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002510 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2511 return;
2512 }
2513 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002514 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2515 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002516 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002517 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2518 return;
2519 }
2520 }
2521 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002522 // It is okay to include this attribute on properties, e.g.:
2523 //
2524 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2525 //
2526 // In this case it follows tradition and suppresses an error in the above
2527 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002528 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002529 }
Michael Han99315932013-01-24 16:46:58 +00002530 D->addAttr(::new (S.Context)
2531 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2532 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002533}
2534
Mike Stumpd3bb5572009-07-24 19:02:52 +00002535static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002536handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002537 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002538 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002539 return;
2540 }
2541
2542 if (!isa<FunctionDecl>(D)) {
2543 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2544 return;
2545 }
2546
Michael Han99315932013-01-24 16:46:58 +00002547 D->addAttr(::new (S.Context)
2548 OverloadableAttr(Attr.getRange(), S.Context,
2549 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002550}
2551
Chandler Carruthedc2c642011-07-02 00:01:44 +00002552static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002553 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002554 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002555 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002556 return;
2557 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002558
Steve Naroff3405a732008-09-18 16:44:58 +00002559 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002560 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002561 return;
2562 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002563
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002564 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002565 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002566 type = BlocksAttr::ByRef;
2567 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002568 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002569 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002570 return;
2571 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002572
Michael Han99315932013-01-24 16:46:58 +00002573 D->addAttr(::new (S.Context)
2574 BlocksAttr(Attr.getRange(), S.Context, type,
2575 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002576}
2577
Chandler Carruthedc2c642011-07-02 00:01:44 +00002578static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002579 // check the attribute arguments.
2580 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002581 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002582 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002583 }
2584
John McCallb46f2872011-09-09 07:56:05 +00002585 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002586 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002587 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002588 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002589 if (E->isTypeDependent() || E->isValueDependent() ||
2590 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002591 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002592 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002593 return;
2594 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002595
John McCallb46f2872011-09-09 07:56:05 +00002596 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002597 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2598 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002599 return;
2600 }
John McCallb46f2872011-09-09 07:56:05 +00002601
2602 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002603 }
2604
John McCallb46f2872011-09-09 07:56:05 +00002605 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002606 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002607 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002608 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002609 if (E->isTypeDependent() || E->isValueDependent() ||
2610 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002611 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002612 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002613 return;
2614 }
2615 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002616
John McCallb46f2872011-09-09 07:56:05 +00002617 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002618 // FIXME: This error message could be improved, it would be nice
2619 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002620 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2621 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002622 return;
2623 }
2624 }
2625
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002626 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002627 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002628 if (isa<FunctionNoProtoType>(FT)) {
2629 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2630 return;
2631 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002632
Chris Lattner9363e312009-03-17 23:03:47 +00002633 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002634 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002635 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002636 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002637 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002638 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002639 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002640 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002641 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002642 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2643 if (!BD->isVariadic()) {
2644 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2645 return;
2646 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002647 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002648 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002649 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002650 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002651 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002652 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002653 int m = Ty->isFunctionPointerType() ? 0 : 1;
2654 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002655 return;
2656 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002657 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002658 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002659 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002660 return;
2661 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002662 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002663 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002664 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002665 return;
2666 }
Michael Han99315932013-01-24 16:46:58 +00002667 D->addAttr(::new (S.Context)
2668 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2669 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002670}
2671
Chandler Carruthedc2c642011-07-02 00:01:44 +00002672static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002673 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002674 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002675 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002676
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002677 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002678 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002679 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002680 return;
2681 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002682
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002683 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2684 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2685 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002686 return;
2687 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002688 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2689 if (MD->getResultType()->isVoidType()) {
2690 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2691 << Attr.getName() << 1;
2692 return;
2693 }
2694
Michael Han99315932013-01-24 16:46:58 +00002695 D->addAttr(::new (S.Context)
2696 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2697 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002698}
2699
Chandler Carruthedc2c642011-07-02 00:01:44 +00002700static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002701 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002702 if (Attr.hasParameterOrArguments()) {
2703 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002704 return;
2705 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002706
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002707 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002708 if (isa<CXXRecordDecl>(D)) {
2709 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2710 return;
2711 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002712 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2713 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002714 return;
2715 }
2716
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002717 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002718
Michael Han99315932013-01-24 16:46:58 +00002719 nd->addAttr(::new (S.Context)
2720 WeakAttr(Attr.getRange(), S.Context,
2721 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002722}
2723
Chandler Carruthedc2c642011-07-02 00:01:44 +00002724static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002725 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002726 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002727 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002728
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002729
2730 // weak_import only applies to variable & function declarations.
2731 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002732 if (!D->canBeWeakImported(isDef)) {
2733 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002734 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2735 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002736 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002737 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002738 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002739 // Nothing to warn about here.
2740 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002741 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002742 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002743
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002744 return;
2745 }
2746
Michael Han99315932013-01-24 16:46:58 +00002747 D->addAttr(::new (S.Context)
2748 WeakImportAttr(Attr.getRange(), S.Context,
2749 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002750}
2751
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002752// Handles reqd_work_group_size and work_group_size_hint.
2753static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002754 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002755 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2756 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2757
Nate Begemanf2758702009-06-26 06:32:41 +00002758 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002759 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002760
2761 unsigned WGSize[3];
2762 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002763 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002764 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002765 if (E->isTypeDependent() || E->isValueDependent() ||
2766 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002767 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002768 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002769 return;
2770 }
2771 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2772 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002773
2774 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2775 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2776 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2777 if (!(A->getXDim() == WGSize[0] &&
2778 A->getYDim() == WGSize[1] &&
2779 A->getZDim() == WGSize[2])) {
2780 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2781 Attr.getName();
2782 }
2783 }
2784
2785 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2786 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2787 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2788 if (!(A->getXDim() == WGSize[0] &&
2789 A->getYDim() == WGSize[1] &&
2790 A->getZDim() == WGSize[2])) {
2791 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2792 Attr.getName();
2793 }
2794 }
2795
2796 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2797 D->addAttr(::new (S.Context)
2798 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002799 WGSize[0], WGSize[1], WGSize[2],
2800 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002801 else
2802 D->addAttr(::new (S.Context)
2803 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002804 WGSize[0], WGSize[1], WGSize[2],
2805 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002806}
2807
Joey Goulyaba589c2013-03-08 09:42:32 +00002808static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2809 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2810
2811 // Attribute has 1 argument.
2812 if (!checkAttributeNumArgs(S, Attr, 1))
2813 return;
2814
2815 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2816
2817 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2818 (ParmType->isBooleanType() ||
2819 !ParmType->isIntegralType(S.getASTContext()))) {
2820 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2821 << ParmType;
2822 return;
2823 }
2824
2825 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2826 D->hasAttr<VecTypeHintAttr>()) {
2827 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2828 if (A->getTypeHint() != ParmType) {
2829 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2830 return;
2831 }
2832 }
2833
2834 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2835 ParmType, Attr.getLoc()));
2836}
2837
Joey Gouly0608ae82013-03-14 09:54:43 +00002838static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2839 if (!dyn_cast<VarDecl>(D))
2840 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2841 << 9;
2842 StringRef EndianType = Attr.getParameterName()->getName();
2843 if (EndianType != "host" && EndianType != "device")
2844 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2845}
2846
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002847SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002848 StringRef Name,
2849 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002850 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2851 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002852 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002853 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2854 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002855 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002856 }
Michael Han99315932013-01-24 16:46:58 +00002857 return ::new (Context) SectionAttr(Range, Context, Name,
2858 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002859}
2860
Chandler Carruthedc2c642011-07-02 00:01:44 +00002861static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002862 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002863 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002864 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002865
2866 // Make sure that there is a string literal as the sections's single
2867 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002868 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002869 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002870 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002871 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002872 return;
2873 }
Mike Stump11289f42009-09-09 15:08:12 +00002874
Chris Lattner30ba6742009-08-10 19:03:04 +00002875 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002876 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002877 if (!Error.empty()) {
2878 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2879 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002880 return;
2881 }
Mike Stump11289f42009-09-09 15:08:12 +00002882
Chris Lattner20aee9b2010-01-12 20:58:53 +00002883 // This attribute cannot be applied to local variables.
2884 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2885 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2886 return;
2887 }
Michael Han99315932013-01-24 16:46:58 +00002888
2889 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002890 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han99315932013-01-24 16:46:58 +00002891 SE->getString(), Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002892 if (NewAttr)
2893 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002894}
2895
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002896
Chandler Carruthedc2c642011-07-02 00:01:44 +00002897static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002898 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002899 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002900 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002901 return;
2902 }
Douglas Gregor88336832011-06-15 05:45:11 +00002903
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002904 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002905 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002906 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002907 } else {
Michael Han99315932013-01-24 16:46:58 +00002908 D->addAttr(::new (S.Context)
2909 NoThrowAttr(Attr.getRange(), S.Context,
2910 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002911 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002912}
2913
Chandler Carruthedc2c642011-07-02 00:01:44 +00002914static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002915 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002916 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002917 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002918 return;
2919 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002920
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002921 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002922 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002923 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002924 } else {
Michael Han99315932013-01-24 16:46:58 +00002925 D->addAttr(::new (S.Context)
2926 ConstAttr(Attr.getRange(), S.Context,
2927 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002928 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002929}
2930
Chandler Carruthedc2c642011-07-02 00:01:44 +00002931static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002932 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002933 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002934 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002935
Michael Han99315932013-01-24 16:46:58 +00002936 D->addAttr(::new (S.Context)
2937 PureAttr(Attr.getRange(), S.Context,
2938 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002939}
2940
Chandler Carruthedc2c642011-07-02 00:01:44 +00002941static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002942 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002943 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2944 return;
2945 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002946
Anders Carlssond277d792009-01-31 01:16:18 +00002947 if (Attr.getNumArgs() != 0) {
2948 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2949 return;
2950 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002951
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002952 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002953
Anders Carlssond277d792009-01-31 01:16:18 +00002954 if (!VD || !VD->hasLocalStorage()) {
2955 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2956 return;
2957 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002958
Anders Carlssond277d792009-01-31 01:16:18 +00002959 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002960 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002961 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002962 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2963 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002964 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002965 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002966 Attr.getParameterName();
2967 return;
2968 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002969
Anders Carlssond277d792009-01-31 01:16:18 +00002970 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2971 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002972 S.Diag(Attr.getParameterLoc(),
2973 diag::err_attribute_cleanup_arg_not_function)
2974 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002975 return;
2976 }
2977
Anders Carlssond277d792009-01-31 01:16:18 +00002978 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002979 S.Diag(Attr.getParameterLoc(),
2980 diag::err_attribute_cleanup_func_must_take_one_arg)
2981 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002982 return;
2983 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002984
Anders Carlsson723f55d2009-02-07 23:16:50 +00002985 // We're currently more strict than GCC about what function types we accept.
2986 // If this ever proves to be a problem it should be easy to fix.
2987 QualType Ty = S.Context.getPointerType(VD->getType());
2988 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002989 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2990 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002991 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002992 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2993 Attr.getParameterName() << ParamTy << Ty;
2994 return;
2995 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002996
Michael Han99315932013-01-24 16:46:58 +00002997 D->addAttr(::new (S.Context)
2998 CleanupAttr(Attr.getRange(), S.Context, FD,
2999 Attr.getAttributeSpellingListIndex()));
Eli Friedmanfa0df832012-02-02 03:46:19 +00003000 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewyckya096b142013-02-12 08:08:54 +00003001 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssond277d792009-01-31 01:16:18 +00003002}
3003
Mike Stumpd3bb5572009-07-24 19:02:52 +00003004/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003005/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003006static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003007 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003008 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003009
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003010 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003011 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003012 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003013 return;
3014 }
Chandler Carruth743682b2010-11-16 08:35:43 +00003015
3016 // In C++ the implicit 'this' function parameter also counts, and they are
3017 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003018 bool HasImplicitThisParam = isInstanceMethod(D);
3019 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003020 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00003021
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003022 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003023 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003024 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003025 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3026 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003027 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3028 << "format" << 2 << IdxExpr->getSourceRange();
3029 return;
3030 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003031
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003032 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
3033 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3034 << "format" << 2 << IdxExpr->getSourceRange();
3035 return;
3036 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003037
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003038 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003039
Chandler Carruth743682b2010-11-16 08:35:43 +00003040 if (HasImplicitThisParam) {
3041 if (ArgIdx == 0) {
3042 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3043 << "format_arg" << IdxExpr->getSourceRange();
3044 return;
3045 }
3046 ArgIdx--;
3047 }
3048
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003049 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003050 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003051
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003052 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3053 if (not_nsstring_type &&
3054 !isCFStringType(Ty, S.Context) &&
3055 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003056 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003057 // FIXME: Should highlight the actual expression that has the wrong type.
3058 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003059 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003060 << IdxExpr->getSourceRange();
3061 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003062 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003063 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003064 if (!isNSStringType(Ty, S.Context) &&
3065 !isCFStringType(Ty, S.Context) &&
3066 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003067 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003068 // FIXME: Should highlight the actual expression that has the wrong type.
3069 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003070 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003071 << IdxExpr->getSourceRange();
3072 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003073 }
3074
Michael Han99315932013-01-24 16:46:58 +00003075 D->addAttr(::new (S.Context)
3076 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3077 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003078}
3079
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003080enum FormatAttrKind {
3081 CFStringFormat,
3082 NSStringFormat,
3083 StrftimeFormat,
3084 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003085 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003086 InvalidFormat
3087};
3088
3089/// getFormatAttrKind - Map from format attribute names to supported format
3090/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003091static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003092 return llvm::StringSwitch<FormatAttrKind>(Format)
3093 // Check for formats that get handled specially.
3094 .Case("NSString", NSStringFormat)
3095 .Case("CFString", CFStringFormat)
3096 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003097
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003098 // Otherwise, check for supported formats.
3099 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3100 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3101 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003102
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003103 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3104 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003105}
3106
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003107/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003108/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003109static void handleInitPriorityAttr(Sema &S, Decl *D,
3110 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003111 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003112 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3113 return;
3114 }
3115
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003116 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003117 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3118 Attr.setInvalid();
3119 return;
3120 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003121 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003122 if (S.Context.getAsArrayType(T))
3123 T = S.Context.getBaseElementType(T);
3124 if (!T->getAs<RecordType>()) {
3125 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3126 Attr.setInvalid();
3127 return;
3128 }
3129
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003130 if (Attr.getNumArgs() != 1) {
3131 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3132 Attr.setInvalid();
3133 return;
3134 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003135 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003136
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003137 llvm::APSInt priority(32);
3138 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3139 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3141 << "init_priority" << priorityExpr->getSourceRange();
3142 Attr.setInvalid();
3143 return;
3144 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00003145 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003146 if (prioritynum < 101 || prioritynum > 65535) {
3147 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3148 << priorityExpr->getSourceRange();
3149 Attr.setInvalid();
3150 return;
3151 }
Michael Han99315932013-01-24 16:46:58 +00003152 D->addAttr(::new (S.Context)
3153 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3154 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003155}
3156
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003157FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han99315932013-01-24 16:46:58 +00003158 int FormatIdx, int FirstArg,
3159 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003160 // Check whether we already have an equivalent format attribute.
3161 for (specific_attr_iterator<FormatAttr>
3162 i = D->specific_attr_begin<FormatAttr>(),
3163 e = D->specific_attr_end<FormatAttr>();
3164 i != e ; ++i) {
3165 FormatAttr *f = *i;
3166 if (f->getType() == Format &&
3167 f->getFormatIdx() == FormatIdx &&
3168 f->getFirstArg() == FirstArg) {
3169 // If we don't have a valid location for this attribute, adopt the
3170 // location.
3171 if (f->getLocation().isInvalid())
3172 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003173 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003174 }
3175 }
3176
Michael Han99315932013-01-24 16:46:58 +00003177 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3178 AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003179}
3180
Mike Stumpd3bb5572009-07-24 19:02:52 +00003181/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003182/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003183static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003184
Chris Lattner4a927cb2008-06-28 23:36:30 +00003185 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003186 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003187 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003188 return;
3189 }
3190
Chris Lattner4a927cb2008-06-28 23:36:30 +00003191 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003192 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003193 return;
3194 }
3195
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003196 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003197 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003198 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003199 return;
3200 }
3201
Chandler Carruth743682b2010-11-16 08:35:43 +00003202 // In C++ the implicit 'this' function parameter also counts, and they are
3203 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003204 bool HasImplicitThisParam = isInstanceMethod(D);
3205 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003206 unsigned FirstIdx = 1;
3207
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003208 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003209
3210 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003211 if (Format.startswith("__") && Format.endswith("__"))
3212 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003213
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003214 // Check for supported formats.
3215 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003216
3217 if (Kind == IgnoredFormat)
3218 return;
3219
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003220 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003221 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003222 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003223 return;
3224 }
3225
3226 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003227 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003228 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003229 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3230 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003232 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003233 return;
3234 }
3235
3236 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003237 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003238 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003239 return;
3240 }
3241
3242 // FIXME: Do we need to bounds check?
3243 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003244
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003245 if (HasImplicitThisParam) {
3246 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003247 S.Diag(Attr.getLoc(),
3248 diag::err_format_attribute_implicit_this_format_string)
3249 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003250 return;
3251 }
3252 ArgIdx--;
3253 }
Mike Stump11289f42009-09-09 15:08:12 +00003254
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003255 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003256 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003257
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003258 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003259 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003260 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3261 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003262 return;
3263 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003264 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003265 // FIXME: do we need to check if the type is NSString*? What are the
3266 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003267 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003268 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003269 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3270 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003271 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003272 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003273 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003274 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003275 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003276 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3277 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003278 return;
3279 }
3280
3281 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003282 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003283 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003284 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3285 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003287 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003288 return;
3289 }
3290
3291 // check if the function is variadic if the 3rd argument non-zero
3292 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003293 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003294 ++NumArgs; // +1 for ...
3295 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003296 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003297 return;
3298 }
3299 }
3300
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003301 // strftime requires FirstArg to be 0 because it doesn't read from any
3302 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003303 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003304 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003305 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3306 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003307 return;
3308 }
3309 // if 0 it disables parameter checking (to use with e.g. va_list)
3310 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003311 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003312 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003313 return;
3314 }
3315
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003316 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3317 Idx.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003318 FirstArg.getZExtValue(),
3319 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003320 if (NewAttr)
3321 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003322}
3323
Chandler Carruthedc2c642011-07-02 00:01:44 +00003324static void handleTransparentUnionAttr(Sema &S, Decl *D,
3325 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003326 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003327 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003328 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003329
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003330
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003331 // Try to find the underlying union declaration.
3332 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003333 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003334 if (TD && TD->getUnderlyingType()->isUnionType())
3335 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3336 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003337 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003338
3339 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003340 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003341 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003342 return;
3343 }
3344
John McCallf937c022011-10-07 06:10:15 +00003345 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003346 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003347 diag::warn_transparent_union_attribute_not_definition);
3348 return;
3349 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003350
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003351 RecordDecl::field_iterator Field = RD->field_begin(),
3352 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003353 if (Field == FieldEnd) {
3354 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3355 return;
3356 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003357
David Blaikie40ed2972012-06-06 20:45:41 +00003358 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003359 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003360 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003361 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003362 diag::warn_transparent_union_attribute_floating)
3363 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003364 return;
3365 }
3366
3367 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3368 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3369 for (; Field != FieldEnd; ++Field) {
3370 QualType FieldType = Field->getType();
3371 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3372 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3373 // Warn if we drop the attribute.
3374 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003375 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003376 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003377 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003378 diag::warn_transparent_union_attribute_field_size_align)
3379 << isSize << Field->getDeclName() << FieldBits;
3380 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003381 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003382 diag::note_transparent_union_first_field_size_align)
3383 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003384 return;
3385 }
3386 }
3387
Michael Han99315932013-01-24 16:46:58 +00003388 RD->addAttr(::new (S.Context)
3389 TransparentUnionAttr(Attr.getRange(), S.Context,
3390 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003391}
3392
Chandler Carruthedc2c642011-07-02 00:01:44 +00003393static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003394 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003395 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003396 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003397
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003398 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003399 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003400
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003401 // Make sure that there is a string literal as the annotation's single
3402 // argument.
3403 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003404 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003405 return;
3406 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003407
3408 // Don't duplicate annotations that are already set.
3409 for (specific_attr_iterator<AnnotateAttr>
3410 i = D->specific_attr_begin<AnnotateAttr>(),
3411 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3412 if ((*i)->getAnnotation() == SE->getString())
3413 return;
3414 }
Michael Han99315932013-01-24 16:46:58 +00003415
3416 D->addAttr(::new (S.Context)
3417 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3418 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003419}
3420
Chandler Carruthedc2c642011-07-02 00:01:44 +00003421static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003422 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003423 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003425 return;
3426 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003427
Richard Smith848e1f12013-02-01 08:12:08 +00003428 if (Attr.getNumArgs() == 0) {
3429 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3430 true, 0, Attr.getAttributeSpellingListIndex()));
3431 return;
3432 }
3433
Richard Smith44c247f2013-02-22 08:32:16 +00003434 Expr *E = Attr.getArg(0);
3435 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3436 S.Diag(Attr.getEllipsisLoc(),
3437 diag::err_pack_expansion_without_parameter_packs);
3438 return;
3439 }
3440
3441 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3442 return;
3443
3444 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3445 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003446}
3447
3448void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003449 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003450 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3451 SourceLocation AttrLoc = AttrRange.getBegin();
3452
Richard Smith1dba27c2013-01-29 09:02:09 +00003453 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003454 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003455 // C++11 [dcl.align]p1:
3456 // An alignment-specifier may be applied to a variable or to a class
3457 // data member, but it shall not be applied to a bit-field, a function
3458 // parameter, the formal parameter of a catch clause, or a variable
3459 // declared with the register storage class specifier. An
3460 // alignment-specifier may also be applied to the declaration of a class
3461 // or enumeration type.
3462 // C11 6.7.5/2:
3463 // An alignment attribute shall not be specified in a declaration of
3464 // a typedef, or a bit-field, or a function, or a parameter, or an
3465 // object declared with the register storage-class specifier.
3466 int DiagKind = -1;
3467 if (isa<ParmVarDecl>(D)) {
3468 DiagKind = 0;
3469 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3470 if (VD->getStorageClass() == SC_Register)
3471 DiagKind = 1;
3472 if (VD->isExceptionVariable())
3473 DiagKind = 2;
3474 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3475 if (FD->isBitField())
3476 DiagKind = 3;
3477 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003478 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3479 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003480 << (TmpAttr.isC11() ? ExpectedVariableOrField
3481 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003482 return;
3483 }
3484 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003485 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003486 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003487 return;
3488 }
3489 }
3490
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003491 if (E->isTypeDependent() || E->isValueDependent()) {
3492 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003493 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3494 AA->setPackExpansion(IsPackExpansion);
3495 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003496 return;
3497 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003498
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003499 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003500 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003501 ExprResult ICE
3502 = VerifyIntegerConstantExpression(E, &Alignment,
3503 diag::err_aligned_attribute_argument_not_int,
3504 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003505 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003506 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003507
3508 // C++11 [dcl.align]p2:
3509 // -- if the constant expression evaluates to zero, the alignment
3510 // specifier shall have no effect
3511 // C11 6.7.5p6:
3512 // An alignment specification of zero has no effect.
3513 if (!(TmpAttr.isAlignas() && !Alignment) &&
3514 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003515 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3516 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003517 return;
3518 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003519
Richard Smith848e1f12013-02-01 08:12:08 +00003520 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003521 // We've already verified it's a power of 2, now let's make sure it's
3522 // 8192 or less.
3523 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003524 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003525 << E->getSourceRange();
3526 return;
3527 }
3528 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003529
Richard Smith44c247f2013-02-22 08:32:16 +00003530 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3531 ICE.take(), SpellingListIndex);
3532 AA->setPackExpansion(IsPackExpansion);
3533 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003534}
3535
Michael Hanaf02bbe2013-02-01 01:19:17 +00003536void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003537 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003538 // FIXME: Cache the number on the Attr object if non-dependent?
3539 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003540 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3541 SpellingListIndex);
3542 AA->setPackExpansion(IsPackExpansion);
3543 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003544}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003545
Richard Smith848e1f12013-02-01 08:12:08 +00003546void Sema::CheckAlignasUnderalignment(Decl *D) {
3547 assert(D->hasAttrs() && "no attributes on decl");
3548
3549 QualType Ty;
3550 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3551 Ty = VD->getType();
3552 else
3553 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003554 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003555 return;
3556
3557 // C++11 [dcl.align]p5, C11 6.7.5/4:
3558 // The combined effect of all alignment attributes in a declaration shall
3559 // not specify an alignment that is less strict than the alignment that
3560 // would otherwise be required for the entity being declared.
3561 AlignedAttr *AlignasAttr = 0;
3562 unsigned Align = 0;
3563 for (specific_attr_iterator<AlignedAttr>
3564 I = D->specific_attr_begin<AlignedAttr>(),
3565 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3566 if (I->isAlignmentDependent())
3567 return;
3568 if (I->isAlignas())
3569 AlignasAttr = *I;
3570 Align = std::max(Align, I->getAlignment(Context));
3571 }
3572
3573 if (AlignasAttr && Align) {
3574 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3575 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3576 if (NaturalAlign > RequestedAlign)
3577 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3578 << Ty << (unsigned)NaturalAlign.getQuantity();
3579 }
3580}
3581
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003582/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003583/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003584///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003585/// Despite what would be logical, the mode attribute is a decl attribute, not a
3586/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3587/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003588static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003589 // This attribute isn't documented, but glibc uses it. It changes
3590 // the width of an int or unsigned int to the specified size.
3591
3592 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003593 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003594 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003595
Chris Lattneracbc2d22008-06-27 22:18:37 +00003596
3597 IdentifierInfo *Name = Attr.getParameterName();
3598 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003599 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003600 return;
3601 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003602
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003603 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003604
3605 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003606 if (Str.startswith("__") && Str.endswith("__"))
3607 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003608
3609 unsigned DestWidth = 0;
3610 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003611 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003612 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003613 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003614 switch (Str[0]) {
3615 case 'Q': DestWidth = 8; break;
3616 case 'H': DestWidth = 16; break;
3617 case 'S': DestWidth = 32; break;
3618 case 'D': DestWidth = 64; break;
3619 case 'X': DestWidth = 96; break;
3620 case 'T': DestWidth = 128; break;
3621 }
3622 if (Str[1] == 'F') {
3623 IntegerMode = false;
3624 } else if (Str[1] == 'C') {
3625 IntegerMode = false;
3626 ComplexMode = true;
3627 } else if (Str[1] != 'I') {
3628 DestWidth = 0;
3629 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003630 break;
3631 case 4:
3632 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3633 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003634 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003635 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003636 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003637 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003638 break;
3639 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003640 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003641 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003642 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003643 case 11:
3644 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003645 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003646 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003647 }
3648
3649 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003650 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003651 OldTy = TD->getUnderlyingType();
3652 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3653 OldTy = VD->getType();
3654 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003655 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003656 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003657 return;
3658 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003659
John McCall9dd450b2009-09-21 23:43:11 +00003660 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003661 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3662 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003663 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003664 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3665 } else if (ComplexMode) {
3666 if (!OldTy->isComplexType())
3667 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3668 } else {
3669 if (!OldTy->isFloatingType())
3670 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3671 }
3672
Mike Stump87c57ac2009-05-16 07:39:55 +00003673 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3674 // and friends, at least with glibc.
3675 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3676 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003677 // FIXME: Make sure floating-point mappings are accurate
3678 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003679 QualType NewTy;
3680 switch (DestWidth) {
3681 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003682 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003683 return;
3684 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003685 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003686 return;
3687 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003688 if (!IntegerMode) {
3689 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3690 return;
3691 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003692 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003693 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003694 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003695 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003696 break;
3697 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003698 if (!IntegerMode) {
3699 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3700 return;
3701 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003702 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003703 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003704 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003705 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003706 break;
3707 case 32:
3708 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003709 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003710 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003711 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003712 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003713 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003714 break;
3715 case 64:
3716 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003717 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003718 else if (OldTy->isSignedIntegerType())
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.LongTy;
3721 else
3722 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003723 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003724 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003725 NewTy = S.Context.UnsignedLongTy;
3726 else
3727 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003728 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003729 case 96:
3730 NewTy = S.Context.LongDoubleTy;
3731 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003732 case 128:
3733 if (!IntegerMode) {
3734 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3735 return;
3736 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003737 if (OldTy->isSignedIntegerType())
3738 NewTy = S.Context.Int128Ty;
3739 else
3740 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003741 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003742 }
3743
Eli Friedman4735374e2009-03-03 06:41:03 +00003744 if (ComplexMode) {
3745 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003746 }
3747
3748 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003749 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3750 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3751 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003752 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003753
3754 D->addAttr(::new (S.Context)
3755 ModeAttr(Attr.getRange(), S.Context, Name,
3756 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003757}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003758
Chandler Carruthedc2c642011-07-02 00:01:44 +00003759static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003760 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003761 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003762 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003763
Nick Lewycky08597072012-07-24 01:40:49 +00003764 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3765 if (!VD->hasGlobalStorage())
3766 S.Diag(Attr.getLoc(),
3767 diag::warn_attribute_requires_functions_or_static_globals)
3768 << Attr.getName();
3769 } else if (!isFunctionOrMethod(D)) {
3770 S.Diag(Attr.getLoc(),
3771 diag::warn_attribute_requires_functions_or_static_globals)
3772 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003773 return;
3774 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003775
Michael Han99315932013-01-24 16:46:58 +00003776 D->addAttr(::new (S.Context)
3777 NoDebugAttr(Attr.getRange(), S.Context,
3778 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003779}
3780
Chandler Carruthedc2c642011-07-02 00:01:44 +00003781static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003782 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003783 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003784 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003785
Mike Stumpd3bb5572009-07-24 19:02:52 +00003786
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003787 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003788 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003789 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003790 return;
3791 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003792
Michael Han99315932013-01-24 16:46:58 +00003793 D->addAttr(::new (S.Context)
3794 NoInlineAttr(Attr.getRange(), S.Context,
3795 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003796}
3797
Chandler Carruthedc2c642011-07-02 00:01:44 +00003798static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3799 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003800 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003801 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003802 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003803
Chris Lattner3c77a352010-06-22 00:03:40 +00003804
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003805 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003806 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003807 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003808 return;
3809 }
3810
Michael Han99315932013-01-24 16:46:58 +00003811 D->addAttr(::new (S.Context)
3812 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3813 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003814}
3815
Chandler Carruthedc2c642011-07-02 00:01:44 +00003816static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003817 if (S.LangOpts.CUDA) {
3818 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003819 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003820 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3821 return;
3822 }
3823
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003824 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003825 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003826 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003827 return;
3828 }
3829
Michael Han99315932013-01-24 16:46:58 +00003830 D->addAttr(::new (S.Context)
3831 CUDAConstantAttr(Attr.getRange(), S.Context,
3832 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003833 } else {
3834 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3835 }
3836}
3837
Chandler Carruthedc2c642011-07-02 00:01:44 +00003838static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003839 if (S.LangOpts.CUDA) {
3840 // check the attribute arguments.
3841 if (Attr.getNumArgs() != 0) {
3842 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3843 return;
3844 }
3845
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003846 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003847 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003848 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003849 return;
3850 }
3851
Michael Han99315932013-01-24 16:46:58 +00003852 D->addAttr(::new (S.Context)
3853 CUDADeviceAttr(Attr.getRange(), S.Context,
3854 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003855 } else {
3856 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3857 }
3858}
3859
Chandler Carruthedc2c642011-07-02 00:01:44 +00003860static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003861 if (S.LangOpts.CUDA) {
3862 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003863 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003864 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003865
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003866 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003867 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003868 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003869 return;
3870 }
3871
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003872 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003873 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003874 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003875 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003876 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3877 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003878 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003879 "void");
3880 } else {
3881 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3882 << FD->getType();
3883 }
3884 return;
3885 }
3886
Michael Han99315932013-01-24 16:46:58 +00003887 D->addAttr(::new (S.Context)
3888 CUDAGlobalAttr(Attr.getRange(), S.Context,
3889 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003890 } else {
3891 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3892 }
3893}
3894
Chandler Carruthedc2c642011-07-02 00:01:44 +00003895static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003896 if (S.LangOpts.CUDA) {
3897 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003898 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003899 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003900
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003901
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003902 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003903 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003904 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003905 return;
3906 }
3907
Michael Han99315932013-01-24 16:46:58 +00003908 D->addAttr(::new (S.Context)
3909 CUDAHostAttr(Attr.getRange(), S.Context,
3910 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003911 } else {
3912 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3913 }
3914}
3915
Chandler Carruthedc2c642011-07-02 00:01:44 +00003916static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003917 if (S.LangOpts.CUDA) {
3918 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003919 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003920 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003921
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003922 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003923 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003924 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003925 return;
3926 }
3927
Michael Han99315932013-01-24 16:46:58 +00003928 D->addAttr(::new (S.Context)
3929 CUDASharedAttr(Attr.getRange(), S.Context,
3930 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003931 } else {
3932 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3933 }
3934}
3935
Chandler Carruthedc2c642011-07-02 00:01:44 +00003936static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003937 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003938 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003939 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003940
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003941 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003942 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003943 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003944 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003945 return;
3946 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003947
Douglas Gregor35b57532009-10-27 21:01:01 +00003948 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003949 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003950 return;
3951 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003952
Michael Han99315932013-01-24 16:46:58 +00003953 D->addAttr(::new (S.Context)
3954 GNUInlineAttr(Attr.getRange(), S.Context,
3955 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003956}
3957
Chandler Carruthedc2c642011-07-02 00:01:44 +00003958static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003959 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003960
Aaron Ballman02df2e02012-12-09 17:45:41 +00003961 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003962 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003963 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3964 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003965 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003966 return;
3967
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003968 if (!isa<ObjCMethodDecl>(D)) {
3969 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3970 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003971 return;
3972 }
3973
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003974 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003975 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003976 D->addAttr(::new (S.Context)
3977 FastCallAttr(Attr.getRange(), S.Context,
3978 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003979 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003980 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003981 D->addAttr(::new (S.Context)
3982 StdCallAttr(Attr.getRange(), S.Context,
3983 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003984 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003985 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003986 D->addAttr(::new (S.Context)
3987 ThisCallAttr(Attr.getRange(), S.Context,
3988 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003989 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003990 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003991 D->addAttr(::new (S.Context)
3992 CDeclAttr(Attr.getRange(), S.Context,
3993 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003994 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003995 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003996 D->addAttr(::new (S.Context)
3997 PascalAttr(Attr.getRange(), S.Context,
3998 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003999 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004000 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004001 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004002 switch (CC) {
4003 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004004 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004005 break;
4006 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004007 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004008 break;
4009 default:
4010 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004011 }
4012
Michael Han99315932013-01-24 16:46:58 +00004013 D->addAttr(::new (S.Context)
4014 PcsAttr(Attr.getRange(), S.Context, PCS,
4015 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004016 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004017 }
Derek Schuffa2020962012-10-16 22:30:41 +00004018 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00004019 D->addAttr(::new (S.Context)
4020 PnaclCallAttr(Attr.getRange(), S.Context,
4021 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004022 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00004023 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004024 D->addAttr(::new (S.Context)
4025 IntelOclBiccAttr(Attr.getRange(), S.Context,
4026 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004027 return;
Derek Schuffa2020962012-10-16 22:30:41 +00004028
Abramo Bagnara50099372010-04-30 13:10:51 +00004029 default:
4030 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004031 }
4032}
4033
Chandler Carruthedc2c642011-07-02 00:01:44 +00004034static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00004035 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004036 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004037}
4038
Guy Benyeifb36ede2013-03-24 13:58:12 +00004039static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4040 assert(!Attr.isInvalid());
4041
4042 Expr *E = Attr.getArg(0);
4043 llvm::APSInt ArgNum(32);
4044 if (E->isTypeDependent() || E->isValueDependent() ||
4045 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4046 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4047 << Attr.getName()->getName() << E->getSourceRange();
4048 return;
4049 }
4050
4051 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4052 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4053}
4054
Aaron Ballman02df2e02012-12-09 17:45:41 +00004055bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4056 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00004057 if (attr.isInvalid())
4058 return true;
4059
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004060 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4061 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
4062 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00004063 attr.setInvalid();
4064 return true;
4065 }
4066
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004067 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4068 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00004069 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004070 case AttributeList::AT_CDecl: CC = CC_C; break;
4071 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4072 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4073 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4074 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4075 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004076 Expr *Arg = attr.getArg(0);
4077 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004078 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004079 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
4080 << "pcs" << 1;
4081 attr.setInvalid();
4082 return true;
4083 }
4084
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004085 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004086 if (StrRef == "aapcs") {
4087 CC = CC_AAPCS;
4088 break;
4089 } else if (StrRef == "aapcs-vfp") {
4090 CC = CC_AAPCS_VFP;
4091 break;
4092 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004093
4094 attr.setInvalid();
4095 Diag(attr.getLoc(), diag::err_invalid_pcs);
4096 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004097 }
Derek Schuffa2020962012-10-16 22:30:41 +00004098 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00004099 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00004100 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004101 }
4102
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004103 const TargetInfo &TI = Context.getTargetInfo();
4104 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4105 if (A == TargetInfo::CCCR_Warning) {
4106 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00004107
4108 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4109 if (FD)
4110 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4111 TargetInfo::CCMT_NonMember;
4112 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004113 }
4114
John McCall3882ace2011-01-05 12:14:39 +00004115 return false;
4116}
4117
Chandler Carruthedc2c642011-07-02 00:01:44 +00004118static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004119 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00004120
4121 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004122 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00004123 return;
4124
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004125 if (!isa<ObjCMethodDecl>(D)) {
4126 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4127 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004128 return;
4129 }
Eli Friedman7044b762009-03-27 21:06:47 +00004130
Michael Han99315932013-01-24 16:46:58 +00004131 D->addAttr(::new (S.Context)
4132 RegparmAttr(Attr.getRange(), S.Context, numParams,
4133 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00004134}
4135
4136/// Checks a regparm attribute, returning true if it is ill-formed and
4137/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004138bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4139 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004140 return true;
4141
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004142 if (Attr.getNumArgs() != 1) {
4143 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4144 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004145 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004146 }
Eli Friedman7044b762009-03-27 21:06:47 +00004147
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004148 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00004149 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00004150 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00004151 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004152 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00004153 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004154 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004155 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004156 }
4157
Douglas Gregore8bbc122011-09-02 00:18:52 +00004158 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004159 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004160 << 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 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00004166 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004167 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004168 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004169 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004170 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004171 }
4172
John McCall3882ace2011-01-05 12:14:39 +00004173 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004174}
4175
Chandler Carruthedc2c642011-07-02 00:01:44 +00004176static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004177 if (S.LangOpts.CUDA) {
4178 // check the attribute arguments.
4179 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004180 // FIXME: 0 is not okay.
4181 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004182 return;
4183 }
4184
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004185 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004186 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004187 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004188 return;
4189 }
4190
4191 Expr *MaxThreadsExpr = Attr.getArg(0);
4192 llvm::APSInt MaxThreads(32);
4193 if (MaxThreadsExpr->isTypeDependent() ||
4194 MaxThreadsExpr->isValueDependent() ||
4195 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4196 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4197 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4198 return;
4199 }
4200
4201 llvm::APSInt MinBlocks(32);
4202 if (Attr.getNumArgs() > 1) {
4203 Expr *MinBlocksExpr = Attr.getArg(1);
4204 if (MinBlocksExpr->isTypeDependent() ||
4205 MinBlocksExpr->isValueDependent() ||
4206 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4208 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4209 return;
4210 }
4211 }
4212
Michael Han99315932013-01-24 16:46:58 +00004213 D->addAttr(::new (S.Context)
4214 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4215 MaxThreads.getZExtValue(),
4216 MinBlocks.getZExtValue(),
4217 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004218 } else {
4219 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4220 }
4221}
4222
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004223static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4224 const AttributeList &Attr) {
4225 StringRef AttrName = Attr.getName()->getName();
4226 if (!Attr.getParameterName()) {
4227 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4228 << Attr.getName() << /* arg num = */ 1;
4229 return;
4230 }
4231
4232 if (Attr.getNumArgs() != 2) {
4233 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4234 << /* required args = */ 3;
4235 return;
4236 }
4237
4238 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4239
4240 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4241 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4242 << Attr.getName() << ExpectedFunctionOrMethod;
4243 return;
4244 }
4245
4246 uint64_t ArgumentIdx;
4247 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4248 Attr.getLoc(), 2,
4249 Attr.getArg(0), ArgumentIdx))
4250 return;
4251
4252 uint64_t TypeTagIdx;
4253 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4254 Attr.getLoc(), 3,
4255 Attr.getArg(1), TypeTagIdx))
4256 return;
4257
4258 bool IsPointer = (AttrName == "pointer_with_type_tag");
4259 if (IsPointer) {
4260 // Ensure that buffer has a pointer type.
4261 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4262 if (!BufferTy->isPointerType()) {
4263 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004264 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004265 }
4266 }
4267
Michael Han99315932013-01-24 16:46:58 +00004268 D->addAttr(::new (S.Context)
4269 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4270 ArgumentIdx, TypeTagIdx, IsPointer,
4271 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004272}
4273
4274static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4275 const AttributeList &Attr) {
4276 IdentifierInfo *PointerKind = Attr.getParameterName();
4277 if (!PointerKind) {
4278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4279 << "type_tag_for_datatype" << 1;
4280 return;
4281 }
4282
4283 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4284
Michael Han99315932013-01-24 16:46:58 +00004285 D->addAttr(::new (S.Context)
4286 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4287 MatchingCType,
4288 Attr.getLayoutCompatible(),
4289 Attr.getMustBeNull(),
4290 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004291}
4292
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004293//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004294// Checker-specific attribute handlers.
4295//===----------------------------------------------------------------------===//
4296
John McCalled433932011-01-25 03:31:58 +00004297static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004298 return type->isDependentType() ||
4299 type->isObjCObjectPointerType() ||
4300 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004301}
4302static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004303 return type->isDependentType() ||
4304 type->isPointerType() ||
4305 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004306}
4307
Chandler Carruthedc2c642011-07-02 00:01:44 +00004308static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004309 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004310 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004311 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004312 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004313 return;
4314 }
4315
4316 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004317 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004318 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4319 cf = false;
4320 } else {
4321 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4322 cf = true;
4323 }
4324
4325 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004326 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004327 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004328 return;
4329 }
4330
4331 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004332 param->addAttr(::new (S.Context)
4333 CFConsumedAttr(Attr.getRange(), S.Context,
4334 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004335 else
Michael Han99315932013-01-24 16:46:58 +00004336 param->addAttr(::new (S.Context)
4337 NSConsumedAttr(Attr.getRange(), S.Context,
4338 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004339}
4340
Chandler Carruthedc2c642011-07-02 00:01:44 +00004341static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4342 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004343 if (!isa<ObjCMethodDecl>(D)) {
4344 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004345 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004346 return;
4347 }
4348
Michael Han99315932013-01-24 16:46:58 +00004349 D->addAttr(::new (S.Context)
4350 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4351 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004352}
4353
Chandler Carruthedc2c642011-07-02 00:01:44 +00004354static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4355 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004356
John McCalled433932011-01-25 03:31:58 +00004357 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004358
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004359 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004360 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004361 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004362 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004363 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004364 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4365 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004366 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004367 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004368 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004369 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004370 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004371 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004372 return;
4373 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004374
John McCalled433932011-01-25 03:31:58 +00004375 bool typeOK;
4376 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004377 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004378 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004379 case AttributeList::AT_NSReturnsAutoreleased:
4380 case AttributeList::AT_NSReturnsRetained:
4381 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004382 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4383 cf = false;
4384 break;
4385
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004386 case AttributeList::AT_CFReturnsRetained:
4387 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004388 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4389 cf = true;
4390 break;
4391 }
4392
4393 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004394 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004395 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004396 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004397 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004398
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004399 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004400 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004401 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004402 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004403 D->addAttr(::new (S.Context)
4404 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4405 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004406 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004407 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004408 D->addAttr(::new (S.Context)
4409 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4410 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004411 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004412 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004413 D->addAttr(::new (S.Context)
4414 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4415 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004416 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004417 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004418 D->addAttr(::new (S.Context)
4419 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4420 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004421 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004422 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004423 D->addAttr(::new (S.Context)
4424 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4425 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004426 return;
4427 };
4428}
4429
John McCallcf166702011-07-22 08:53:00 +00004430static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4431 const AttributeList &attr) {
4432 SourceLocation loc = attr.getLoc();
4433
4434 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4435
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004436 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00004437 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004438 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00004439 return;
4440 }
4441
4442 // Check that the method returns a normal pointer.
4443 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004444
4445 if (!resultType->isReferenceType() &&
4446 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00004447 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4448 << SourceRange(loc)
4449 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4450
4451 // Drop the attribute.
4452 return;
4453 }
4454
Michael Han99315932013-01-24 16:46:58 +00004455 method->addAttr(::new (S.Context)
4456 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4457 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004458}
4459
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004460static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4461 const AttributeList &attr) {
4462 SourceLocation loc = attr.getLoc();
4463 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4464
4465 if (!method) {
4466 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4467 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4468 return;
4469 }
4470 DeclContext *DC = method->getDeclContext();
4471 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4472 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4473 << attr.getName() << 0;
4474 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4475 return;
4476 }
4477 if (method->getMethodFamily() == OMF_dealloc) {
4478 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4479 << attr.getName() << 1;
4480 return;
4481 }
4482
Michael Han99315932013-01-24 16:46:58 +00004483 method->addAttr(::new (S.Context)
4484 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4485 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004486}
4487
John McCall32f5fe12011-09-30 05:12:12 +00004488/// Handle cf_audited_transfer and cf_unknown_transfer.
4489static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4490 if (!isa<FunctionDecl>(D)) {
4491 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004492 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004493 return;
4494 }
4495
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004496 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004497
4498 // Check whether there's a conflicting attribute already present.
4499 Attr *Existing;
4500 if (IsAudited) {
4501 Existing = D->getAttr<CFUnknownTransferAttr>();
4502 } else {
4503 Existing = D->getAttr<CFAuditedTransferAttr>();
4504 }
4505 if (Existing) {
4506 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4507 << A.getName()
4508 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4509 << A.getRange() << Existing->getRange();
4510 return;
4511 }
4512
4513 // All clear; add the attribute.
4514 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004515 D->addAttr(::new (S.Context)
4516 CFAuditedTransferAttr(A.getRange(), S.Context,
4517 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004518 } else {
Michael Han99315932013-01-24 16:46:58 +00004519 D->addAttr(::new (S.Context)
4520 CFUnknownTransferAttr(A.getRange(), S.Context,
4521 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004522 }
4523}
4524
John McCallf1e8b342011-09-29 07:17:38 +00004525static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4526 const AttributeList &Attr) {
4527 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4528 if (!RD || RD->isUnion()) {
4529 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004530 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004531 }
4532
4533 IdentifierInfo *ParmName = Attr.getParameterName();
4534
4535 // In Objective-C, verify that the type names an Objective-C type.
4536 // We don't want to check this outside of ObjC because people sometimes
4537 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004538 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004539 // Check for an existing type with this name.
4540 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4541 Sema::LookupOrdinaryName);
4542 if (S.LookupName(R, Sc)) {
4543 NamedDecl *Target = R.getFoundDecl();
4544 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4545 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4546 S.Diag(Target->getLocStart(), diag::note_declared_at);
4547 }
4548 }
4549 }
4550
Michael Han99315932013-01-24 16:46:58 +00004551 D->addAttr(::new (S.Context)
4552 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4553 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004554}
4555
Chandler Carruthedc2c642011-07-02 00:01:44 +00004556static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4557 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004558 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004559
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004560 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004561 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004562}
4563
Chandler Carruthedc2c642011-07-02 00:01:44 +00004564static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4565 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004566 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004567 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004568 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004569 return;
4570 }
4571
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004572 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004573 QualType type = vd->getType();
4574
4575 if (!type->isDependentType() &&
4576 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004577 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004578 << type;
4579 return;
4580 }
4581
4582 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4583
4584 // If we have no lifetime yet, check the lifetime we're presumably
4585 // going to infer.
4586 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4587 lifetime = type->getObjCARCImplicitLifetime();
4588
4589 switch (lifetime) {
4590 case Qualifiers::OCL_None:
4591 assert(type->isDependentType() &&
4592 "didn't infer lifetime for non-dependent type?");
4593 break;
4594
4595 case Qualifiers::OCL_Weak: // meaningful
4596 case Qualifiers::OCL_Strong: // meaningful
4597 break;
4598
4599 case Qualifiers::OCL_ExplicitNone:
4600 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004601 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004602 << (lifetime == Qualifiers::OCL_Autoreleasing);
4603 break;
4604 }
4605
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004606 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004607 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4608 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004609}
4610
Francois Picheta83957a2010-12-19 06:50:37 +00004611//===----------------------------------------------------------------------===//
4612// Microsoft specific attribute handlers.
4613//===----------------------------------------------------------------------===//
4614
Reid Kleckner140c4a72013-05-17 14:04:52 +00004615// Check if MS extensions or some other language extensions are enabled. If
4616// not, issue a diagnostic that the given attribute is unused.
4617static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4618 bool OtherExtension = false) {
4619 if (S.LangOpts.MicrosoftExt || OtherExtension)
4620 return true;
4621 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4622 return false;
4623}
4624
Chandler Carruthedc2c642011-07-02 00:01:44 +00004625static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004626 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4627 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004628
Reid Kleckner140c4a72013-05-17 14:04:52 +00004629 // check the attribute arguments.
4630 if (!checkAttributeNumArgs(S, Attr, 1))
4631 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004632
Reid Kleckner140c4a72013-05-17 14:04:52 +00004633 Expr *Arg = Attr.getArg(0);
4634 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4635 if (!Str || !Str->isAscii()) {
4636 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4637 << "uuid" << 1;
4638 return;
4639 }
Francois Pichet7da11662010-12-20 01:41:49 +00004640
Reid Kleckner140c4a72013-05-17 14:04:52 +00004641 StringRef StrRef = Str->getString();
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004642
Reid Kleckner140c4a72013-05-17 14:04:52 +00004643 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4644 StrRef.back() == '}';
Francois Pichet7da11662010-12-20 01:41:49 +00004645
Reid Kleckner140c4a72013-05-17 14:04:52 +00004646 // Validate GUID length.
4647 if (IsCurly && StrRef.size() != 38) {
4648 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4649 return;
4650 }
4651 if (!IsCurly && StrRef.size() != 36) {
4652 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4653 return;
4654 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004655
Reid Kleckner140c4a72013-05-17 14:04:52 +00004656 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4657 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4658 StringRef::iterator I = StrRef.begin();
4659 if (IsCurly) // Skip the optional '{'
4660 ++I;
4661
4662 for (int i = 0; i < 36; ++i) {
4663 if (i == 8 || i == 13 || i == 18 || i == 23) {
4664 if (*I != '-') {
Francois Pichet7da11662010-12-20 01:41:49 +00004665 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4666 return;
4667 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004668 } else if (!isHexDigit(*I)) {
4669 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4670 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004671 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004672 I++;
4673 }
Francois Picheta83957a2010-12-19 06:50:37 +00004674
Reid Kleckner140c4a72013-05-17 14:04:52 +00004675 D->addAttr(::new (S.Context)
4676 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4677 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004678}
4679
John McCall8d32c052012-05-22 21:28:12 +00004680static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004681 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004682 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004683
4684 AttributeList::Kind Kind = Attr.getKind();
4685 if (Kind == AttributeList::AT_SingleInheritance)
4686 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004687 ::new (S.Context)
4688 SingleInheritanceAttr(Attr.getRange(), S.Context,
4689 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004690 else if (Kind == AttributeList::AT_MultipleInheritance)
4691 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004692 ::new (S.Context)
4693 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4694 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004695 else if (Kind == AttributeList::AT_VirtualInheritance)
4696 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004697 ::new (S.Context)
4698 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4699 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004700}
4701
4702static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004703 if (!checkMicrosoftExt(S, Attr))
4704 return;
4705
4706 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballman317a77f2013-05-22 23:25:32 +00004707 if (Kind == AttributeList::AT_Win64)
Reid Kleckner140c4a72013-05-17 14:04:52 +00004708 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
Reid Klecknerb144d362013-05-20 14:02:37 +00004721static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4722 if (!checkMicrosoftExt(S, Attr))
4723 return;
4724 // Check linkage after possibly merging declaratinos. See
4725 // checkAttributesAfterMerging().
4726 D->addAttr(::new (S.Context)
4727 SelectAnyAttr(Attr.getRange(), S.Context,
4728 Attr.getAttributeSpellingListIndex()));
4729}
4730
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004731//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004732// Top Level Sema Entry Points
4733//===----------------------------------------------------------------------===//
4734
Chandler Carruthedc2c642011-07-02 00:01:44 +00004735static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4736 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004737 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004738 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4739 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4740 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004741 default:
4742 break;
4743 }
4744}
Abramo Bagnara50099372010-04-30 13:10:51 +00004745
Chandler Carruthedc2c642011-07-02 00:01:44 +00004746static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4747 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004748 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004749 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4750 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4751 case AttributeList::AT_IBOutletCollection:
4752 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004753 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004754 case AttributeList::AT_ObjCGC:
4755 case AttributeList::AT_VectorSize:
4756 case AttributeList::AT_NeonVectorType:
4757 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004758 case AttributeList::AT_Ptr32:
4759 case AttributeList::AT_Ptr64:
4760 case AttributeList::AT_SPtr:
4761 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004762 // Ignore these, these are type attributes, handled by
4763 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004764 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004765 case AttributeList::AT_CUDADevice:
4766 case AttributeList::AT_CUDAHost:
4767 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004768 // Ignore, this is a non-inheritable attribute, handled
4769 // by ProcessNonInheritableDeclAttr.
4770 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004771 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4772 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4773 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4774 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004775 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004776 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004777 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004778 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004779 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4780 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4781 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004782 handleDependencyAttr(S, scope, D, Attr);
4783 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004784 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4785 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4786 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004787 case AttributeList::AT_CXX11NoReturn:
4788 handleCXX11NoReturnAttr(S, D, Attr);
4789 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004790 case AttributeList::AT_Deprecated:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004791 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4792 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004793 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4794 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004795 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004796 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004797 case AttributeList::AT_MinSize:
4798 handleMinSizeAttr(S, D, Attr);
4799 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004800 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4801 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4802 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4803 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4804 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004805 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004806 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004807 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4808 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4809 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4810 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4811 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004812 case AttributeList::AT_ownership_returns:
4813 case AttributeList::AT_ownership_takes:
4814 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004815 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004816 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4817 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4818 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4819 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4820 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4821 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4822 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004823
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004824 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004825 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004826 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004827 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004828
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004829 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004830 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4831
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004832 case AttributeList::AT_ObjCRequiresSuper:
4833 handleObjCRequiresSuperAttr(S, D, Attr); break;
4834
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004835 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004836 handleNSBridgedAttr(S, scope, D, Attr); break;
4837
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004838 case AttributeList::AT_CFAuditedTransfer:
4839 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004840 handleCFTransferAttr(S, D, Attr); break;
4841
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004842 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004843 case AttributeList::AT_CFConsumed:
4844 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4845 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004846 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004847
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004848 case AttributeList::AT_NSReturnsAutoreleased:
4849 case AttributeList::AT_NSReturnsNotRetained:
4850 case AttributeList::AT_CFReturnsNotRetained:
4851 case AttributeList::AT_NSReturnsRetained:
4852 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004853 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004854
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004855 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004856 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004857 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004858
Joey Goulyaba589c2013-03-08 09:42:32 +00004859 case AttributeList::AT_VecTypeHint:
4860 handleVecTypeHint(S, D, Attr); break;
4861
Joey Gouly0608ae82013-03-14 09:54:43 +00004862 case AttributeList::AT_Endian:
4863 handleEndianAttr(S, D, Attr);
4864 break;
4865
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004866 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004867 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004868
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004869 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4870 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4871 case AttributeList::AT_Unavailable:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004872 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4873 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004874 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004875 handleArcWeakrefUnavailableAttr (S, D, Attr);
4876 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004877 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004878 handleObjCRootClassAttr(S, D, Attr);
4879 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004880 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004881 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004882 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004883 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4884 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004885 handleReturnsTwiceAttr(S, D, Attr);
4886 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004887 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004888 case AttributeList::AT_Visibility:
4889 handleVisibilityAttr(S, D, Attr, false);
4890 break;
4891 case AttributeList::AT_TypeVisibility:
4892 handleVisibilityAttr(S, D, Attr, true);
4893 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004894 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004895 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004896 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4897 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4898 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4899 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004900 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004901 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004902 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004903 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004904 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004905 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004906 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004907 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004908 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4909 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4910 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4911 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4912 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4913 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4914 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4915 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4916 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004917 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004918 // Just ignore
4919 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004920 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004921 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004922 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004923 case AttributeList::AT_StdCall:
4924 case AttributeList::AT_CDecl:
4925 case AttributeList::AT_FastCall:
4926 case AttributeList::AT_ThisCall:
4927 case AttributeList::AT_Pascal:
4928 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004929 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004930 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004931 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004932 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004933 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004934 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004935 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004936 case AttributeList::AT_OpenCLImageAccess:
4937 handleOpenCLImageAccessAttr(S, D, Attr);
4938 break;
John McCall8d32c052012-05-22 21:28:12 +00004939
4940 // Microsoft attributes:
John McCall5e77d762013-04-16 07:28:30 +00004941 case AttributeList::AT_MsProperty: break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004942 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004943 handleMsStructAttr(S, D, Attr);
4944 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004945 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004946 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004947 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004948 case AttributeList::AT_SingleInheritance:
4949 case AttributeList::AT_MultipleInheritance:
4950 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004951 handleInheritanceAttr(S, D, Attr);
4952 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004953 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004954 handlePortabilityAttr(S, D, Attr);
4955 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004956 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004957 handleForceInlineAttr(S, D, Attr);
4958 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004959 case AttributeList::AT_SelectAny:
4960 handleSelectAnyAttr(S, D, Attr);
4961 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004962
4963 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004964 case AttributeList::AT_AssertExclusiveLock:
4965 handleAssertExclusiveLockAttr(S, D, Attr);
4966 break;
4967 case AttributeList::AT_AssertSharedLock:
4968 handleAssertSharedLockAttr(S, D, Attr);
4969 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004970 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004971 handleGuardedVarAttr(S, D, Attr);
4972 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004973 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004974 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004975 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004976 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004977 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004978 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004979 case AttributeList::AT_NoSanitizeAddress:
4980 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004981 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004982 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004983 handleNoThreadSafetyAnalysis(S, D, Attr);
4984 break;
4985 case AttributeList::AT_NoSanitizeThread:
4986 handleNoSanitizeThread(S, D, Attr);
4987 break;
4988 case AttributeList::AT_NoSanitizeMemory:
4989 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004990 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004991 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004992 handleLockableAttr(S, D, Attr);
4993 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004994 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004995 handleGuardedByAttr(S, D, Attr);
4996 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004997 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004998 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004999 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005000 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005001 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005002 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005003 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00005004 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005005 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005006 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005007 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005008 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005009 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005010 handleLockReturnedAttr(S, D, Attr);
5011 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005012 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005013 handleLocksExcludedAttr(S, D, Attr);
5014 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005015 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005016 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005017 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005018 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00005019 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005020 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005021 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005022 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005023 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005024 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005025 handleUnlockFunAttr(S, D, Attr);
5026 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005027 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005028 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005029 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005030 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005031 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005032 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005033
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005034 // Type safety attributes.
5035 case AttributeList::AT_ArgumentWithTypeTag:
5036 handleArgumentWithTypeTagAttr(S, D, Attr);
5037 break;
5038 case AttributeList::AT_TypeTagForDatatype:
5039 handleTypeTagForDatatypeAttr(S, D, Attr);
5040 break;
5041
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005042 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005043 // Ask target about the attribute.
5044 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5045 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00005046 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5047 diag::warn_unhandled_ms_attribute_ignored :
5048 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005049 break;
5050 }
5051}
5052
Peter Collingbourneb331b262011-01-21 02:08:45 +00005053/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5054/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smith10876ef2013-01-17 01:30:42 +00005055/// silently ignore it if a GNU attribute.
Chandler Carruthedc2c642011-07-02 00:01:44 +00005056static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5057 const AttributeList &Attr,
Richard Smith10876ef2013-01-17 01:30:42 +00005058 bool NonInheritable, bool Inheritable,
5059 bool IncludeCXX11Attributes) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00005060 if (Attr.isInvalid())
5061 return;
5062
Richard Smith10876ef2013-01-17 01:30:42 +00005063 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5064 // instead.
5065 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5066 return;
5067
Peter Collingbourneb331b262011-01-21 02:08:45 +00005068 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00005069 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00005070
5071 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00005072 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00005073}
5074
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005075/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5076/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005077void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005078 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005079 bool NonInheritable, bool Inheritable,
5080 bool IncludeCXX11Attributes) {
5081 for (const AttributeList* l = AttrList; l; l = l->getNext())
5082 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5083 IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005084
5085 // GCC accepts
5086 // static int a9 __attribute__((weakref));
5087 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005088 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00005089 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00005090 cast<NamedDecl>(D)->getNameAsString();
5091 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005092 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005093 }
5094}
5095
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005096// Annotation attributes are the only attributes allowed after an access
5097// specifier.
5098bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5099 const AttributeList *AttrList) {
5100 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005101 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005102 handleAnnotateAttr(*this, ASDecl, *l);
5103 } else {
5104 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5105 return true;
5106 }
5107 }
5108
5109 return false;
5110}
5111
John McCall42856de2011-10-01 05:17:03 +00005112/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5113/// contains any decl attributes that we should warn about.
5114static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5115 for ( ; A; A = A->getNext()) {
5116 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005117 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005118 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5119
5120 if (A->getKind() == AttributeList::UnknownAttribute) {
5121 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5122 << A->getName() << A->getRange();
5123 } else {
5124 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5125 << A->getName() << A->getRange();
5126 }
5127 }
5128}
5129
5130/// checkUnusedDeclAttributes - Given a declarator which is not being
5131/// used to build a declaration, complain about any decl attributes
5132/// which might be lying around on it.
5133void Sema::checkUnusedDeclAttributes(Declarator &D) {
5134 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5135 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5136 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5137 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5138}
5139
Ryan Flynn7d470f32009-07-30 03:15:39 +00005140/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005141/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005142NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5143 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005144 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005145 NamedDecl *NewD = 0;
5146 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005147 FunctionDecl *NewFD;
5148 // FIXME: Missing call to CheckFunctionDeclaration().
5149 // FIXME: Mangling?
5150 // FIXME: Is the qualifier info correct?
5151 // FIXME: Is the DeclContext correct?
5152 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5153 Loc, Loc, DeclarationName(II),
5154 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005155 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005156 FD->hasPrototype(),
5157 false/*isConstexprSpecified*/);
5158 NewD = NewFD;
5159
5160 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005161 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005162
5163 // Fake up parameter variables; they are declared as if this were
5164 // a typedef.
5165 QualType FDTy = FD->getType();
5166 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5167 SmallVector<ParmVarDecl*, 16> Params;
5168 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5169 AE = FT->arg_type_end(); AI != AE; ++AI) {
5170 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5171 Param->setScopeInfo(0, Params.size());
5172 Params.push_back(Param);
5173 }
David Blaikie9c70e042011-09-21 18:16:56 +00005174 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005175 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005176 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5177 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005178 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005179 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005180 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005181 if (VD->getQualifier()) {
5182 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005183 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005184 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005185 }
5186 return NewD;
5187}
5188
James Dennett634962f2012-06-14 21:40:34 +00005189/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005190/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005191void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005192 if (W.getUsed()) return; // only do this once
5193 W.setUsed(true);
5194 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5195 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005196 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005197 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5198 NDId->getName()));
5199 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005200 WeakTopLevelDecl.push_back(NewD);
5201 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5202 // to insert Decl at TU scope, sorry.
5203 DeclContext *SavedContext = CurContext;
5204 CurContext = Context.getTranslationUnitDecl();
5205 PushOnScopeChains(NewD, S);
5206 CurContext = SavedContext;
5207 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005208 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005209 }
5210}
5211
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005212void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5213 // It's valid to "forward-declare" #pragma weak, in which case we
5214 // have to do this.
5215 LoadExternalWeakUndeclaredIdentifiers();
5216 if (!WeakUndeclaredIdentifiers.empty()) {
5217 NamedDecl *ND = NULL;
5218 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5219 if (VD->isExternC())
5220 ND = VD;
5221 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5222 if (FD->isExternC())
5223 ND = FD;
5224 if (ND) {
5225 if (IdentifierInfo *Id = ND->getIdentifier()) {
5226 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5227 = WeakUndeclaredIdentifiers.find(Id);
5228 if (I != WeakUndeclaredIdentifiers.end()) {
5229 WeakInfo W = I->second;
5230 DeclApplyPragmaWeak(S, ND, W);
5231 WeakUndeclaredIdentifiers[Id] = W;
5232 }
5233 }
5234 }
5235 }
5236}
5237
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005238/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5239/// it, apply them to D. This is a bit tricky because PD can have attributes
5240/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005241void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5242 bool NonInheritable, bool Inheritable) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005243 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005244 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005245 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005246
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005247 // Walk the declarator structure, applying decl attributes that were in a type
5248 // position to the decl itself. This handles cases like:
5249 // int *__attr__(x)** D;
5250 // when X is a decl attribute.
5251 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5252 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith10876ef2013-01-17 01:30:42 +00005253 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5254 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005255
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005256 // Finally, apply any attributes on the decl itself.
5257 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005258 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005259}
John McCall28a6aea2009-11-04 02:18:39 +00005260
John McCall31168b02011-06-15 23:02:42 +00005261/// Is the given declaration allowed to use a forbidden type?
5262static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5263 // Private ivars are always okay. Unfortunately, people don't
5264 // always properly make their ivars private, even in system headers.
5265 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005266 // Function declarations in sys headers will be marked unavailable.
5267 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5268 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005269 return false;
5270
5271 // Require it to be declared in a system header.
5272 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5273}
5274
5275/// Handle a delayed forbidden-type diagnostic.
5276static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5277 Decl *decl) {
5278 if (decl && isForbiddenTypeAllowed(S, decl)) {
5279 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5280 "this system declaration uses an unsupported type"));
5281 return;
5282 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005283 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005284 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005285 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005286 // kind of forbidden type messages on unavailable functions.
5287 if (FD->hasAttr<UnavailableAttr>() &&
5288 diag.getForbiddenTypeDiagnostic() ==
5289 diag::err_arc_array_param_no_ownership) {
5290 diag.Triggered = true;
5291 return;
5292 }
5293 }
John McCall31168b02011-06-15 23:02:42 +00005294
5295 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5296 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5297 diag.Triggered = true;
5298}
5299
John McCall2ec85372012-05-07 06:16:41 +00005300void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5301 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005302 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005303 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005304
John McCall2ec85372012-05-07 06:16:41 +00005305 // When delaying diagnostics to run in the context of a parsed
5306 // declaration, we only want to actually emit anything if parsing
5307 // succeeds.
5308 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005309
John McCall2ec85372012-05-07 06:16:41 +00005310 // We emit all the active diagnostics in this pool or any of its
5311 // parents. In general, we'll get one pool for the decl spec
5312 // and a child pool for each declarator; in a decl group like:
5313 // deprecated_typedef foo, *bar, baz();
5314 // only the declarator pops will be passed decls. This is correct;
5315 // we really do need to consider delayed diagnostics from the decl spec
5316 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005317 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005318 do {
John McCall6347b682012-05-07 06:16:58 +00005319 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005320 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5321 // This const_cast is a bit lame. Really, Triggered should be mutable.
5322 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005323 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005324 continue;
5325
John McCallc1465822011-02-14 07:13:47 +00005326 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005327 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005328 // Don't bother giving deprecation diagnostics if the decl is invalid.
5329 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005330 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005331 break;
5332
5333 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005334 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005335 break;
John McCall31168b02011-06-15 23:02:42 +00005336
5337 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005338 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005339 break;
John McCall86121512010-01-27 03:50:35 +00005340 }
5341 }
John McCall2ec85372012-05-07 06:16:41 +00005342 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005343}
5344
John McCall6347b682012-05-07 06:16:58 +00005345/// Given a set of delayed diagnostics, re-emit them as if they had
5346/// been delayed in the current context instead of in the given pool.
5347/// Essentially, this just moves them to the current pool.
5348void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5349 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5350 assert(curPool && "re-emitting in undelayed context not supported");
5351 curPool->steal(pool);
5352}
5353
John McCall28a6aea2009-11-04 02:18:39 +00005354static bool isDeclDeprecated(Decl *D) {
5355 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005356 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005357 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005358 // A category implicitly has the availability of the interface.
5359 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5360 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005361 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5362 return false;
5363}
5364
Eli Friedman971bfa12012-08-08 21:52:41 +00005365static void
5366DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5367 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005368 const ObjCInterfaceDecl *UnknownObjCClass,
5369 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005370 DeclarationName Name = D->getDeclName();
5371 if (!Message.empty()) {
5372 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5373 S.Diag(D->getLocation(),
5374 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5375 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005376 if (ObjCPropery)
5377 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5378 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005379 } else if (!UnknownObjCClass) {
5380 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5381 S.Diag(D->getLocation(),
5382 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5383 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005384 if (ObjCPropery)
5385 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5386 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005387 } else {
5388 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5389 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5390 }
5391}
5392
John McCallb45a1e72010-08-26 02:13:20 +00005393void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005394 Decl *Ctx) {
5395 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005396 return;
5397
John McCall86121512010-01-27 03:50:35 +00005398 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005399 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5400 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005401 DD.getUnknownObjCClass(),
5402 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005403}
5404
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005405void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005406 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005407 const ObjCInterfaceDecl *UnknownObjCClass,
5408 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005409 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005410 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005411 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5412 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005413 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005414 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005415 return;
5416 }
5417
5418 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005419 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005420 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005421 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005422}