blob: 807978726c237a0febb5b3be967048f0b93cfee6 [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,
Aaron Ballmanf90ccb02013-07-18 14:56:42 +000055 ExpectedTypeOrNamespace,
56 ExpectedObjectiveCInterface
John McCall5fca7ea2011-03-02 12:29:23 +000057};
58
Chris Lattner58418ff2008-06-29 00:16:31 +000059//===----------------------------------------------------------------------===//
60// Helper functions
61//===----------------------------------------------------------------------===//
62
Chandler Carruthff4c4f02011-07-01 23:49:12 +000063static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000064 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000065 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000066 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000067 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000068 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000069 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000070 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000071 Ty = decl->getUnderlyingType();
72 else
73 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000074
Chris Lattner2c6fcf52008-06-26 18:38:35 +000075 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000076 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000077 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000078 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000079
John McCall9dd450b2009-09-21 23:43:11 +000080 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000081}
82
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000083// FIXME: We should provide an abstraction around a method or function
84// to provide the following bits of information.
85
Nuno Lopes518e3702009-12-20 23:11:08 +000086/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000087/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000088static bool isFunction(const Decl *D) {
89 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000090}
91
92/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000093/// type (function or function-typed variable) or an Objective-C
94/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000095static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000096 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000097}
98
Fariborz Jahanian4447e172009-05-15 23:15:03 +000099/// isFunctionOrMethodOrBlock - Return true if the given decl has function
100/// type (function or function-typed variable) or an Objective-C
101/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000102static bool isFunctionOrMethodOrBlock(const Decl *D) {
103 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000104 return true;
105 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000106 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000107 QualType Ty = V->getType();
108 return Ty->isBlockPointerType();
109 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000110 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000111}
112
John McCall3882ace2011-01-05 12:14:39 +0000113/// Return true if the given decl has a declarator that should have
114/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000116 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000117 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
118 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000119}
120
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000121/// hasFunctionProto - Return true if the given decl has a argument
122/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000123/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000124static bool hasFunctionProto(const Decl *D) {
125 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000126 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000127 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000129 return true;
130 }
131}
132
133/// getFunctionOrMethodNumArgs - Return number of function or method
134/// arguments. It is an error to call this on a K&R function (use
135/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
137 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000138 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000139 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000140 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000142}
143
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000146 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000147 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000148 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000149
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000150 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000151}
152
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000153static QualType getFunctionOrMethodResultType(const Decl *D) {
154 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000155 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000156 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000157}
158
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000159static bool isFunctionOrMethodVariadic(const Decl *D) {
160 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000161 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000162 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000163 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000164 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000165 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000166 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000167 }
168}
169
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000170static bool isInstanceMethod(const Decl *D) {
171 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000172 return MethodDecl->isInstance();
173 return false;
174}
175
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000176static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000177 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000178 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000179 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000180
John McCall96fa4842010-05-17 21:00:27 +0000181 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
182 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000183 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000184
John McCall96fa4842010-05-17 21:00:27 +0000185 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000186
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000187 // FIXME: Should we walk the chain of classes?
188 return ClsName == &Ctx.Idents.get("NSString") ||
189 ClsName == &Ctx.Idents.get("NSMutableString");
190}
191
Daniel Dunbar980c6692008-09-26 03:32:58 +0000192static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000193 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000194 if (!PT)
195 return false;
196
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000197 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000198 if (!RT)
199 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000200
Daniel Dunbar980c6692008-09-26 03:32:58 +0000201 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000202 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000203 return false;
204
205 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
206}
207
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000208/// \brief Check if the attribute has exactly as many args as Num. May
209/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000210static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
211 unsigned int Num) {
212 if (Attr.getNumArgs() != Num) {
213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
214 return false;
215 }
216
217 return true;
218}
219
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000220
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000221/// \brief Check if the attribute has at least as many args as Num. May
222/// output an error.
223static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
224 unsigned int Num) {
225 if (Attr.getNumArgs() < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000226 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
227 return false;
228 }
229
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000230 return true;
231}
232
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000233/// \brief Check if IdxExpr is a valid argument index for a function or
234/// instance method D. May output an error.
235///
236/// \returns true if IdxExpr is a valid index.
237static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
238 StringRef AttrName,
239 SourceLocation AttrLoc,
240 unsigned AttrArgNum,
241 const Expr *IdxExpr,
242 uint64_t &Idx)
243{
244 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
245
246 // In C++ the implicit 'this' function parameter also counts.
247 // Parameters are counted from one.
248 const bool HasImplicitThisParam = isInstanceMethod(D);
249 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
250 const unsigned FirstIdx = 1;
251
252 llvm::APSInt IdxInt;
253 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
254 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
255 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
256 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
257 return false;
258 }
259
260 Idx = IdxInt.getLimitedValue();
261 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
262 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
263 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
264 return false;
265 }
266 Idx--; // Convert to zero-based.
267 if (HasImplicitThisParam) {
268 if (Idx == 0) {
269 S.Diag(AttrLoc,
270 diag::err_attribute_invalid_implicit_this_argument)
271 << AttrName << IdxExpr->getSourceRange();
272 return false;
273 }
274 --Idx;
275 }
276
277 return true;
278}
279
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000280///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000281/// \brief Check if passed in Decl is a field or potentially shared global var
282/// \return true if the Decl is a field or potentially shared global variable
283///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000284static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000285 if (isa<FieldDecl>(D))
286 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000287 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smithfd3834f2013-04-13 02:43:54 +0000288 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000289
290 return false;
291}
292
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000293/// \brief Check if the passed-in expression is of type int or bool.
294static bool isIntOrBool(Expr *Exp) {
295 QualType QT = Exp->getType();
296 return QT->isBooleanType() || QT->isIntegerType();
297}
298
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000299
300// Check to see if the type is a smart pointer of some kind. We assume
301// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000302static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
303 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
304 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000305 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000306 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000307
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000308 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
309 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000310 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000311 return false;
312
313 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000314}
315
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000316/// \brief Check if passed in Decl is a pointer type.
317/// Note that this function may produce an error message.
318/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000319static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
320 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000321 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000322 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000323 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000324 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000325
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000326 if (const RecordType *RT = QT->getAs<RecordType>()) {
327 // If it's an incomplete type, it could be a smart pointer; skip it.
328 // (We don't want to force template instantiation if we can avoid it,
329 // since that would alter the order in which templates are instantiated.)
330 if (RT->isIncompleteType())
331 return true;
332
333 if (threadSafetyCheckIsSmartPointer(S, RT))
334 return true;
335 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000336
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000337 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000338 << Attr.getName()->getName() << QT;
339 } else {
340 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
341 << Attr.getName();
342 }
343 return false;
344}
345
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000346/// \brief Checks that the passed in QualType either is of RecordType or points
347/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000348static const RecordType *getRecordType(QualType QT) {
349 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000350 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000351
352 // Now check if we point to record type.
353 if (const PointerType *PT = QT->getAs<PointerType>())
354 return PT->getPointeeType()->getAs<RecordType>();
355
356 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000357}
358
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000359
Jordy Rose740b0c22012-05-08 03:27:22 +0000360static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
361 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000362 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
363 if (RT->getDecl()->getAttr<LockableAttr>())
364 return true;
365 return false;
366}
367
368
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000369/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000370/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000371static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
372 QualType Ty) {
373 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000374
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000375 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000376 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000377 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000378 << Attr.getName() << Ty.getAsString();
379 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000380 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000381
Michael Hana9171bc2012-08-03 17:40:43 +0000382 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000383 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000384 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000385
386 // Allow smart pointers to be used as lockable objects.
387 // FIXME -- Check the type that the smart pointer points to.
388 if (threadSafetyCheckIsSmartPointer(S, RT))
389 return;
390
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000391 // Check if the type is lockable.
392 RecordDecl *RD = RT->getDecl();
393 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000394 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000395
396 // Else check if any base classes are lockable.
397 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
398 CXXBasePaths BPaths(false, false);
399 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
400 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000401 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000402
403 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
404 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000405}
406
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000407/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000408/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000409/// \param Sidx The attribute argument index to start checking with.
410/// \param ParamIdxOk Whether an argument can be indexing into a function
411/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000412static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000413 const AttributeList &Attr,
414 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000415 int Sidx = 0,
416 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000417 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000418 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000419
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000420 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000421 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000422 Args.push_back(ArgExp);
423 continue;
424 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000425
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000426 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000427 if (StrLit->getLength() == 0 ||
428 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000429 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000430 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000431 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000432 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000433 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000434
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000435 // We allow constant strings to be used as a placeholder for expressions
436 // that are not valid C++ syntax, but warn that they are ignored.
437 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
438 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000439 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000440 continue;
441 }
442
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000443 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000444
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000445 // A pointer to member expression of the form &MyClass::mu is treated
446 // specially -- we need to look at the type of the member.
447 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
448 if (UOp->getOpcode() == UO_AddrOf)
449 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
450 if (DRE->getDecl()->isCXXInstanceMember())
451 ArgTy = DRE->getDecl()->getType();
452
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000453 // First see if we can just cast to record type, or point to record type.
454 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000455
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000456 // Now check if we index into a record type function param.
457 if(!RT && ParamIdxOk) {
458 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000459 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
460 if(FD && IL) {
461 unsigned int NumParams = FD->getNumParams();
462 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
464 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
465 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000466 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
467 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000468 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000469 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000470 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000471 }
472 }
473
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000474 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000476 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000477 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000478}
479
Chris Lattner58418ff2008-06-29 00:16:31 +0000480//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000481// Attribute Implementations
482//===----------------------------------------------------------------------===//
483
Daniel Dunbar032db472008-07-31 22:40:48 +0000484// FIXME: All this manual attribute parsing code is gross. At the
485// least add some helper functions to check most argument patterns (#
486// and types of args).
487
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000488enum ThreadAttributeDeclKind {
489 ThreadExpectedFieldOrGlobalVar,
490 ThreadExpectedFunctionOrMethod,
491 ThreadExpectedClassOrStruct
492};
493
Michael Hana9171bc2012-08-03 17:40:43 +0000494static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000495 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000496 assert(!Attr.isInvalid());
497
498 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000499 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000500
501 // D must be either a member field or global (potentially shared) variable.
502 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000503 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
504 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000505 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000506 }
507
Michael Han3be3b442012-07-23 18:48:41 +0000508 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000509}
510
Michael Han3be3b442012-07-23 18:48:41 +0000511static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
512 if (!checkGuardedVarAttrCommon(S, D, Attr))
513 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000514
Michael Han99315932013-01-24 16:46:58 +0000515 D->addAttr(::new (S.Context)
516 GuardedVarAttr(Attr.getRange(), S.Context,
517 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000518}
519
Michael Hana9171bc2012-08-03 17:40:43 +0000520static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000521 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000522 if (!checkGuardedVarAttrCommon(S, D, Attr))
523 return;
524
525 if (!threadSafetyCheckIsPointer(S, D, Attr))
526 return;
527
Michael Han99315932013-01-24 16:46:58 +0000528 D->addAttr(::new (S.Context)
529 PtGuardedVarAttr(Attr.getRange(), S.Context,
530 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000531}
532
Michael Hana9171bc2012-08-03 17:40:43 +0000533static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
534 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000535 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000536 assert(!Attr.isInvalid());
537
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000538 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000539 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000540
541 // D must be either a member field or global (potentially shared) variable.
542 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000543 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
544 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000545 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000546 }
547
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000548 SmallVector<Expr*, 1> Args;
549 // check that all arguments are lockable objects
550 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
551 unsigned Size = Args.size();
552 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000553 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000554
Michael Han3be3b442012-07-23 18:48:41 +0000555 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000556
Michael Han3be3b442012-07-23 18:48:41 +0000557 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000558}
559
Michael Han3be3b442012-07-23 18:48:41 +0000560static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
561 Expr *Arg = 0;
562 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
563 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000564
Michael Han3be3b442012-07-23 18:48:41 +0000565 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
566}
567
Michael Hana9171bc2012-08-03 17:40:43 +0000568static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000569 const AttributeList &Attr) {
570 Expr *Arg = 0;
571 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
572 return;
573
574 if (!threadSafetyCheckIsPointer(S, D, Attr))
575 return;
576
577 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
578 S.Context, Arg));
579}
580
Michael Hana9171bc2012-08-03 17:40:43 +0000581static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000582 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000583 assert(!Attr.isInvalid());
584
585 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000586 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000587
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000588 // FIXME: Lockable structs for C code.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000589 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000590 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
591 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Han3be3b442012-07-23 18:48:41 +0000592 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000593 }
594
Michael Han3be3b442012-07-23 18:48:41 +0000595 return true;
596}
597
598static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
599 if (!checkLockableAttrCommon(S, D, Attr))
600 return;
601
602 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
603}
604
Michael Hana9171bc2012-08-03 17:40:43 +0000605static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000606 const AttributeList &Attr) {
607 if (!checkLockableAttrCommon(S, D, Attr))
608 return;
609
Michael Han99315932013-01-24 16:46:58 +0000610 D->addAttr(::new (S.Context)
611 ScopedLockableAttr(Attr.getRange(), S.Context,
612 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000613}
614
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000615static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
616 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000617 assert(!Attr.isInvalid());
618
619 if (!checkAttributeNumArgs(S, Attr, 0))
620 return;
621
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000622 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000623 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
624 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000625 return;
626 }
627
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000628 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000629 S.Context));
630}
631
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000632static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000633 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000634 assert(!Attr.isInvalid());
635
636 if (!checkAttributeNumArgs(S, Attr, 0))
637 return;
638
639 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000641 << Attr.getName() << ExpectedFunctionOrMethod;
642 return;
643 }
644
Michael Han99315932013-01-24 16:46:58 +0000645 D->addAttr(::new (S.Context)
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000646 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
647 Attr.getAttributeSpellingListIndex()));
648}
649
650static void handleNoSanitizeMemory(Sema &S, Decl *D,
651 const AttributeList &Attr) {
652 assert(!Attr.isInvalid());
653
654 if (!checkAttributeNumArgs(S, Attr, 0))
655 return;
656
657 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
658 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
659 << Attr.getName() << ExpectedFunctionOrMethod;
660 return;
661 }
662
663 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
664 S.Context));
665}
666
667static void handleNoSanitizeThread(Sema &S, Decl *D,
668 const AttributeList &Attr) {
669 assert(!Attr.isInvalid());
670
671 if (!checkAttributeNumArgs(S, Attr, 0))
672 return;
673
674 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
675 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
676 << Attr.getName() << ExpectedFunctionOrMethod;
677 return;
678 }
679
680 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
681 S.Context));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000682}
683
Michael Hana9171bc2012-08-03 17:40:43 +0000684static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
685 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000686 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000687 assert(!Attr.isInvalid());
688
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000689 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000690 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000691
692 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000693 ValueDecl *VD = dyn_cast<ValueDecl>(D);
694 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000695 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
696 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000697 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000698 }
699
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000700 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000701 QualType QT = VD->getType();
702 if (!QT->isDependentType()) {
703 const RecordType *RT = getRecordType(QT);
704 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000705 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000706 << Attr.getName();
707 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000708 }
709 }
710
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000711 // Check that all arguments are lockable objects.
712 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000713 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000714 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000715
Michael Han3be3b442012-07-23 18:48:41 +0000716 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000717}
718
Michael Hana9171bc2012-08-03 17:40:43 +0000719static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000720 const AttributeList &Attr) {
721 SmallVector<Expr*, 1> Args;
722 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
723 return;
724
725 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000726 D->addAttr(::new (S.Context)
727 AcquiredAfterAttr(Attr.getRange(), S.Context,
728 StartArg, Args.size(),
729 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000730}
731
Michael Hana9171bc2012-08-03 17:40:43 +0000732static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000733 const AttributeList &Attr) {
734 SmallVector<Expr*, 1> Args;
735 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
736 return;
737
738 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000739 D->addAttr(::new (S.Context)
740 AcquiredBeforeAttr(Attr.getRange(), S.Context,
741 StartArg, Args.size(),
742 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000743}
744
Michael Hana9171bc2012-08-03 17:40:43 +0000745static bool checkLockFunAttrCommon(Sema &S, Decl *D,
746 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000747 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000748 assert(!Attr.isInvalid());
749
750 // zero or more arguments ok
751
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000752 // check that the attribute is applied to a function
753 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000754 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
755 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000756 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000757 }
758
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000759 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000760 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000761
Michael Han3be3b442012-07-23 18:48:41 +0000762 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000763}
764
Michael Hana9171bc2012-08-03 17:40:43 +0000765static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000766 const AttributeList &Attr) {
767 SmallVector<Expr*, 1> Args;
768 if (!checkLockFunAttrCommon(S, D, Attr, Args))
769 return;
770
771 unsigned Size = Args.size();
772 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000773 D->addAttr(::new (S.Context)
774 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
775 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000776}
777
Michael Hana9171bc2012-08-03 17:40:43 +0000778static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000779 const AttributeList &Attr) {
780 SmallVector<Expr*, 1> Args;
781 if (!checkLockFunAttrCommon(S, D, Attr, Args))
782 return;
783
784 unsigned Size = Args.size();
785 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000786 D->addAttr(::new (S.Context)
787 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
788 StartArg, Size,
789 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000790}
791
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000792static void handleAssertSharedLockAttr(Sema &S, Decl *D,
793 const AttributeList &Attr) {
794 SmallVector<Expr*, 1> Args;
795 if (!checkLockFunAttrCommon(S, D, Attr, Args))
796 return;
797
798 unsigned Size = Args.size();
799 Expr **StartArg = Size == 0 ? 0 : &Args[0];
800 D->addAttr(::new (S.Context)
801 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
802 Attr.getAttributeSpellingListIndex()));
803}
804
805static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
806 const AttributeList &Attr) {
807 SmallVector<Expr*, 1> Args;
808 if (!checkLockFunAttrCommon(S, D, Attr, Args))
809 return;
810
811 unsigned Size = Args.size();
812 Expr **StartArg = Size == 0 ? 0 : &Args[0];
813 D->addAttr(::new (S.Context)
814 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
815 StartArg, Size,
816 Attr.getAttributeSpellingListIndex()));
817}
818
819
Michael Hana9171bc2012-08-03 17:40:43 +0000820static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
821 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000822 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000823 assert(!Attr.isInvalid());
824
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000825 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000826 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000827
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000828 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000829 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
830 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000831 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000832 }
833
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000834 if (!isIntOrBool(Attr.getArg(0))) {
835 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Han3be3b442012-07-23 18:48:41 +0000836 << Attr.getName();
837 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000838 }
839
840 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000841 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000842
Michael Han3be3b442012-07-23 18:48:41 +0000843 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000844}
845
Michael Hana9171bc2012-08-03 17:40:43 +0000846static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000847 const AttributeList &Attr) {
848 SmallVector<Expr*, 2> Args;
849 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
850 return;
851
852 unsigned Size = Args.size();
853 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000854 D->addAttr(::new (S.Context)
855 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
856 Attr.getArg(0), StartArg, Size,
857 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000858}
859
Michael Hana9171bc2012-08-03 17:40:43 +0000860static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000861 const AttributeList &Attr) {
862 SmallVector<Expr*, 2> Args;
863 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
864 return;
865
866 unsigned Size = Args.size();
867 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000868 D->addAttr(::new (S.Context)
869 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
870 Attr.getArg(0), StartArg, Size,
871 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000872}
873
Michael Hana9171bc2012-08-03 17:40:43 +0000874static bool checkLocksRequiredCommon(Sema &S, Decl *D,
875 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000876 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000877 assert(!Attr.isInvalid());
878
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000879 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000880 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000881
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000882 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000883 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
884 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000885 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000886 }
887
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000888 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000889 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000890 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000891 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000892
Michael Han3be3b442012-07-23 18:48:41 +0000893 return true;
894}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000895
Michael Hana9171bc2012-08-03 17:40:43 +0000896static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000897 const AttributeList &Attr) {
898 SmallVector<Expr*, 1> Args;
899 if (!checkLocksRequiredCommon(S, D, Attr, Args))
900 return;
901
902 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000903 D->addAttr(::new (S.Context)
904 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
905 StartArg, Args.size(),
906 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000907}
908
Michael Hana9171bc2012-08-03 17:40:43 +0000909static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000910 const AttributeList &Attr) {
911 SmallVector<Expr*, 1> Args;
912 if (!checkLocksRequiredCommon(S, D, Attr, Args))
913 return;
914
915 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000916 D->addAttr(::new (S.Context)
917 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
918 StartArg, Args.size(),
919 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000920}
921
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000922static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000923 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000924 assert(!Attr.isInvalid());
925
926 // zero or more arguments ok
927
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000928 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000929 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
930 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000931 return;
932 }
933
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000934 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000935 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000936 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000937 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000938 Expr **StartArg = Size == 0 ? 0 : &Args[0];
939
Michael Han99315932013-01-24 16:46:58 +0000940 D->addAttr(::new (S.Context)
941 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
942 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000943}
944
945static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000946 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000947 assert(!Attr.isInvalid());
948
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000949 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000950 return;
951
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000952 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000953 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
954 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000955 return;
956 }
957
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000958 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000959 SmallVector<Expr*, 1> Args;
960 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
961 unsigned Size = Args.size();
962 if (Size == 0)
963 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000964
Michael Han99315932013-01-24 16:46:58 +0000965 D->addAttr(::new (S.Context)
966 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
967 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000968}
969
970static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000971 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000972 assert(!Attr.isInvalid());
973
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000974 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000975 return;
976
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000977 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000978 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
979 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000980 return;
981 }
982
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000983 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000984 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000985 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000986 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000987 if (Size == 0)
988 return;
989 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000990
Michael Han99315932013-01-24 16:46:58 +0000991 D->addAttr(::new (S.Context)
992 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
993 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000994}
995
996
Chandler Carruthedc2c642011-07-02 00:01:44 +0000997static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
998 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +0000999 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1000 if (TD == 0) {
1001 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1002 // and type-ids.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001003 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +00001004 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001005 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001006
Richard Smith1f5a4322013-01-13 02:11:23 +00001007 // Remember this typedef decl, we will need it later for diagnostics.
1008 S.ExtVectorDecls.push_back(TD);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001009}
1010
Chandler Carruthedc2c642011-07-02 00:01:44 +00001011static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001012 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001013 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001014 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001015
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001016 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001017 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001018 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001019 // If the alignment is less than or equal to 8 bits, the packed attribute
1020 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001021 if (!FD->getType()->isDependentType() &&
1022 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001023 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001024 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001025 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001026 else
Michael Han99315932013-01-24 16:46:58 +00001027 FD->addAttr(::new (S.Context)
1028 PackedAttr(Attr.getRange(), S.Context,
1029 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001030 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001031 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001032}
1033
Chandler Carruthedc2c642011-07-02 00:01:44 +00001034static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +00001035 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001036 RD->addAttr(::new (S.Context)
1037 MsStructAttr(Attr.getRange(), S.Context,
1038 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001039 else
1040 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1041}
1042
Chandler Carruthedc2c642011-07-02 00:01:44 +00001043static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001044 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001045 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001046 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001047
Ted Kremenek1f672822010-02-18 03:08:58 +00001048 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001049 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +00001050 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +00001051 D->addAttr(::new (S.Context)
1052 IBActionAttr(Attr.getRange(), S.Context,
1053 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +00001054 return;
1055 }
1056
Ted Kremenekd68ec812011-02-04 06:54:16 +00001057 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +00001058}
1059
Ted Kremenek7fd17232011-09-29 07:02:25 +00001060static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1061 // The IBOutlet/IBOutletCollection attributes only apply to instance
1062 // variables or properties of Objective-C classes. The outlet must also
1063 // have an object reference type.
1064 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1065 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001066 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001067 << Attr.getName() << VD->getType() << 0;
1068 return false;
1069 }
1070 }
1071 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1072 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001073 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001074 << Attr.getName() << PD->getType() << 1;
1075 return false;
1076 }
1077 }
1078 else {
1079 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1080 return false;
1081 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001082
Ted Kremenek7fd17232011-09-29 07:02:25 +00001083 return true;
1084}
1085
Chandler Carruthedc2c642011-07-02 00:01:44 +00001086static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001087 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001088 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +00001089 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001090
1091 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001092 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001093
Michael Han99315932013-01-24 16:46:58 +00001094 D->addAttr(::new (S.Context)
1095 IBOutletAttr(Attr.getRange(), S.Context,
1096 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001097}
1098
Chandler Carruthedc2c642011-07-02 00:01:44 +00001099static void handleIBOutletCollection(Sema &S, Decl *D,
1100 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001101
1102 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001103 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001104 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1105 return;
1106 }
1107
Ted Kremenek7fd17232011-09-29 07:02:25 +00001108 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001109 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001110
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001111 IdentifierInfo *II = Attr.getParameterName();
1112 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001113 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +00001114
John McCallba7bf592010-08-24 05:47:05 +00001115 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001116 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001117 if (!TypeRep) {
1118 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1119 return;
1120 }
John McCallba7bf592010-08-24 05:47:05 +00001121 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001122 // Diagnose use of non-object type in iboutletcollection attribute.
1123 // FIXME. Gnu attribute extension ignores use of builtin types in
1124 // attributes. So, __attribute__((iboutletcollection(char))) will be
1125 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001126 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001127 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1128 return;
1129 }
Michael Han99315932013-01-24 16:46:58 +00001130 D->addAttr(::new (S.Context)
1131 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1132 QT, Attr.getParameterLoc(),
1133 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001134}
1135
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001136static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001137 if (const RecordType *UT = T->getAsUnionType())
1138 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1139 RecordDecl *UD = UT->getDecl();
1140 for (RecordDecl::field_iterator it = UD->field_begin(),
1141 itend = UD->field_end(); it != itend; ++it) {
1142 QualType QT = it->getType();
1143 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1144 T = QT;
1145 return;
1146 }
1147 }
1148 }
1149}
1150
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001151static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001152 if (!isFunctionOrMethod(D)) {
1153 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1154 << "alloc_size" << ExpectedFunctionOrMethod;
1155 return;
1156 }
1157
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001158 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1159 return;
1160
1161 // In C++ the implicit 'this' function parameter also counts, and they are
1162 // counted from one.
1163 bool HasImplicitThisParam = isInstanceMethod(D);
Argyrios Kyrtzidise9365052013-02-22 06:58:28 +00001164 unsigned NumArgs;
1165 if (hasFunctionProto(D))
1166 NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1167 else
1168 NumArgs = 0;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001169
1170 SmallVector<unsigned, 8> SizeArgs;
1171
1172 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1173 E = Attr.arg_end(); I!=E; ++I) {
1174 // The argument must be an integer constant expression.
1175 Expr *Ex = *I;
1176 llvm::APSInt ArgNum;
1177 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1178 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1179 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1180 << "alloc_size" << Ex->getSourceRange();
1181 return;
1182 }
1183
1184 uint64_t x = ArgNum.getZExtValue();
1185
1186 if (x < 1 || x > NumArgs) {
1187 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1188 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1189 return;
1190 }
1191
1192 --x;
1193 if (HasImplicitThisParam) {
1194 if (x == 0) {
1195 S.Diag(Attr.getLoc(),
1196 diag::err_attribute_invalid_implicit_this_argument)
1197 << "alloc_size" << Ex->getSourceRange();
1198 return;
1199 }
1200 --x;
1201 }
1202
1203 // check if the function argument is of an integer type
1204 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1205 if (!T->isIntegerType()) {
1206 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1207 << "alloc_size" << Ex->getSourceRange();
1208 return;
1209 }
1210
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001211 SizeArgs.push_back(x);
1212 }
1213
1214 // check if the function returns a pointer
1215 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1216 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1217 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1218 }
1219
Michael Han99315932013-01-24 16:46:58 +00001220 D->addAttr(::new (S.Context)
1221 AllocSizeAttr(Attr.getRange(), S.Context,
1222 SizeArgs.data(), SizeArgs.size(),
1223 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001224}
1225
Chandler Carruthedc2c642011-07-02 00:01:44 +00001226static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001227 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1228 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001229 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001230 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001231 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001232 return;
1233 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001234
Chandler Carruth743682b2010-11-16 08:35:43 +00001235 // In C++ the implicit 'this' function parameter also counts, and they are
1236 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001237 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewyckye1121512013-01-24 01:12:16 +00001238 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001239
1240 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001241 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001242
Nick Lewyckye1121512013-01-24 01:12:16 +00001243 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1244 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001245 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001246 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001247 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001248 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1249 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001250 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1251 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001252 return;
1253 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001254
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001255 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001256
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001257 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001258 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +00001259 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001260 return;
1261 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001262
Ted Kremenek5224e6a2008-07-21 22:09:15 +00001263 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001264 if (HasImplicitThisParam) {
1265 if (x == 0) {
1266 S.Diag(Attr.getLoc(),
1267 diag::err_attribute_invalid_implicit_this_argument)
1268 << "nonnull" << Ex->getSourceRange();
1269 return;
1270 }
1271 --x;
1272 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001273
1274 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001275 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001276 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001277
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001278 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001279 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001280 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001281 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001282 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001283 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001284
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001285 NonNullArgs.push_back(x);
1286 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001287
1288 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1289 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001290 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001291 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1292 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001293 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001294 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001295 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001296 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001297
Ted Kremenek22813f42010-10-21 18:49:36 +00001298 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001299 if (NonNullArgs.empty()) {
1300 // Warn the trivial case only if attribute is not coming from a
1301 // macro instantiation.
1302 if (Attr.getLoc().isFileID())
1303 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001304 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001305 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001306 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001307
Nick Lewyckye1121512013-01-24 01:12:16 +00001308 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001309 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001310 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001311 D->addAttr(::new (S.Context)
1312 NonNullAttr(Attr.getRange(), S.Context, start, size,
1313 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001314}
1315
Chandler Carruthedc2c642011-07-02 00:01:44 +00001316static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001317 // This attribute must be applied to a function declaration.
1318 // The first argument to the attribute must be a string,
1319 // the name of the resource, for example "malloc".
1320 // The following arguments must be argument indexes, the arguments must be
1321 // of integer type for Returns, otherwise of pointer type.
1322 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001323 // after being held. free() should be __attribute((ownership_takes)), whereas
1324 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001325
1326 if (!AL.getParameterName()) {
1327 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1328 << AL.getName()->getName() << 1;
1329 return;
1330 }
1331 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001332 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001333 switch (AL.getKind()) {
1334 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001335 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001336 if (AL.getNumArgs() < 1) {
1337 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1338 return;
1339 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001340 break;
1341 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001342 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001343 if (AL.getNumArgs() < 1) {
1344 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1345 return;
1346 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001347 break;
1348 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001349 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001350 if (AL.getNumArgs() > 1) {
1351 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1352 << AL.getNumArgs() + 1;
1353 return;
1354 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001355 break;
1356 default:
1357 // This should never happen given how we are called.
1358 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001359 }
1360
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001361 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001362 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1363 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001364 return;
1365 }
1366
Chandler Carruth743682b2010-11-16 08:35:43 +00001367 // In C++ the implicit 'this' function parameter also counts, and they are
1368 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001369 bool HasImplicitThisParam = isInstanceMethod(D);
1370 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001371
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001372 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001373
1374 // Normalize the argument, __foo__ becomes foo.
1375 if (Module.startswith("__") && Module.endswith("__"))
1376 Module = Module.substr(2, Module.size() - 4);
1377
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001378 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001379
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001380 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1381 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001382
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001383 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384 llvm::APSInt ArgNum(32);
1385 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1386 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1387 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1388 << AL.getName()->getName() << IdxExpr->getSourceRange();
1389 continue;
1390 }
1391
1392 unsigned x = (unsigned) ArgNum.getZExtValue();
1393
1394 if (x > NumArgs || x < 1) {
1395 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1396 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1397 continue;
1398 }
1399 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001400 if (HasImplicitThisParam) {
1401 if (x == 0) {
1402 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1403 << "ownership" << IdxExpr->getSourceRange();
1404 return;
1405 }
1406 --x;
1407 }
1408
Ted Kremenekd21139a2010-07-31 01:52:11 +00001409 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001410 case OwnershipAttr::Takes:
1411 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001412 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001413 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001414 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1415 // FIXME: Should also highlight argument in decl.
1416 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001417 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001418 << "pointer"
1419 << IdxExpr->getSourceRange();
1420 continue;
1421 }
1422 break;
1423 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001424 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001425 if (AL.getNumArgs() > 1) {
1426 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001427 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001428 llvm::APSInt ArgNum(32);
1429 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1430 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1431 S.Diag(AL.getLoc(), diag::err_ownership_type)
1432 << "ownership_returns" << "integer"
1433 << IdxExpr->getSourceRange();
1434 return;
1435 }
1436 }
1437 break;
1438 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001439 } // switch
1440
1441 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001442 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001443 i = D->specific_attr_begin<OwnershipAttr>(),
1444 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001445 i != e; ++i) {
1446 if ((*i)->getOwnKind() != K) {
1447 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1448 I!=E; ++I) {
1449 if (x == *I) {
1450 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1451 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001452 }
1453 }
1454 }
1455 }
1456 OwnershipArgs.push_back(x);
1457 }
1458
1459 unsigned* start = OwnershipArgs.data();
1460 unsigned size = OwnershipArgs.size();
1461 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001462
1463 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1464 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1465 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001466 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001467
Michael Han99315932013-01-24 16:46:58 +00001468 D->addAttr(::new (S.Context)
1469 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1470 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001471}
1472
Chandler Carruthedc2c642011-07-02 00:01:44 +00001473static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001474 // Check the attribute arguments.
1475 if (Attr.getNumArgs() > 1) {
1476 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1477 return;
1478 }
1479
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001480 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001481 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001482 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001483 return;
1484 }
1485
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001486 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001487
Rafael Espindolac18086a2010-02-23 22:00:30 +00001488 // gcc rejects
1489 // class c {
1490 // static int a __attribute__((weakref ("v2")));
1491 // static int b() __attribute__((weakref ("f3")));
1492 // };
1493 // and ignores the attributes of
1494 // void f(void) {
1495 // static int a __attribute__((weakref ("v2")));
1496 // }
1497 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001498 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001499 if (!Ctx->isFileContext()) {
1500 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001501 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001502 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001503 }
1504
1505 // The GCC manual says
1506 //
1507 // At present, a declaration to which `weakref' is attached can only
1508 // be `static'.
1509 //
1510 // It also says
1511 //
1512 // Without a TARGET,
1513 // given as an argument to `weakref' or to `alias', `weakref' is
1514 // equivalent to `weak'.
1515 //
1516 // gcc 4.4.1 will accept
1517 // int a7 __attribute__((weakref));
1518 // as
1519 // int a7 __attribute__((weak));
1520 // This looks like a bug in gcc. We reject that for now. We should revisit
1521 // it if this behaviour is actually used.
1522
Rafael Espindolac18086a2010-02-23 22:00:30 +00001523 // GCC rejects
1524 // static ((alias ("y"), weakref)).
1525 // Should we? How to check that weakref is before or after alias?
1526
1527 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001528 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001529 Arg = Arg->IgnoreParenCasts();
1530 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1531
Douglas Gregorfb65e592011-07-27 05:40:30 +00001532 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001533 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1534 << "weakref" << 1;
1535 return;
1536 }
1537 // GCC will accept anything as the argument of weakref. Should we
1538 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001539 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001540 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001541 }
1542
Michael Han99315932013-01-24 16:46:58 +00001543 D->addAttr(::new (S.Context)
1544 WeakRefAttr(Attr.getRange(), S.Context,
1545 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001546}
1547
Chandler Carruthedc2c642011-07-02 00:01:44 +00001548static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001549 // check the attribute arguments.
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00001550 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001551 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001552
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001553 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001554 Arg = Arg->IgnoreParenCasts();
1555 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001556
Douglas Gregorfb65e592011-07-27 05:40:30 +00001557 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001558 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001559 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001560 return;
1561 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001562
Douglas Gregore8bbc122011-09-02 00:18:52 +00001563 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001564 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1565 return;
1566 }
1567
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001568 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001569
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001570 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00001571 Str->getString(),
1572 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001573}
1574
Quentin Colombet4e172062012-11-01 23:55:47 +00001575static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1576 // Check the attribute arguments.
1577 if (!checkAttributeNumArgs(S, Attr, 0))
1578 return;
1579
1580 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1581 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1582 << Attr.getName() << ExpectedFunctionOrMethod;
1583 return;
1584 }
1585
Michael Han99315932013-01-24 16:46:58 +00001586 D->addAttr(::new (S.Context)
1587 MinSizeAttr(Attr.getRange(), S.Context,
1588 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001589}
1590
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001591static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1592 // Check the attribute arguments.
1593 if (!checkAttributeNumArgs(S, Attr, 0))
1594 return;
1595
1596 if (!isa<FunctionDecl>(D)) {
1597 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1598 << Attr.getName() << ExpectedFunction;
1599 return;
1600 }
1601
1602 if (D->hasAttr<HotAttr>()) {
1603 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1604 << Attr.getName() << "hot";
1605 return;
1606 }
1607
Michael Han99315932013-01-24 16:46:58 +00001608 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1609 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001610}
1611
1612static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1613 // Check the attribute arguments.
1614 if (!checkAttributeNumArgs(S, Attr, 0))
1615 return;
1616
1617 if (!isa<FunctionDecl>(D)) {
1618 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1619 << Attr.getName() << ExpectedFunction;
1620 return;
1621 }
1622
1623 if (D->hasAttr<ColdAttr>()) {
1624 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1625 << Attr.getName() << "cold";
1626 return;
1627 }
1628
Michael Han99315932013-01-24 16:46:58 +00001629 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1630 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001631}
1632
Chandler Carruthedc2c642011-07-02 00:01:44 +00001633static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001634 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001635 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001636 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001637
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001638 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001639 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001640 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001641 return;
1642 }
1643
Michael Han99315932013-01-24 16:46:58 +00001644 D->addAttr(::new (S.Context)
1645 NakedAttr(Attr.getRange(), S.Context,
1646 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001647}
1648
Chandler Carruthedc2c642011-07-02 00:01:44 +00001649static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1650 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001651 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001652 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1654 return;
1655 }
1656
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001657 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001658 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001659 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001660 return;
1661 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001662
Michael Han99315932013-01-24 16:46:58 +00001663 D->addAttr(::new (S.Context)
1664 AlwaysInlineAttr(Attr.getRange(), S.Context,
1665 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001666}
1667
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001668static void handleTLSModelAttr(Sema &S, Decl *D,
1669 const AttributeList &Attr) {
1670 // Check the attribute arguments.
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00001671 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001672 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001673
1674 Expr *Arg = Attr.getArg(0);
1675 Arg = Arg->IgnoreParenCasts();
1676 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1677
1678 // Check that it is a string.
1679 if (!Str) {
1680 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1681 return;
1682 }
1683
Richard Smithfd3834f2013-04-13 02:43:54 +00001684 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1686 << Attr.getName() << ExpectedTLSVar;
1687 return;
1688 }
1689
1690 // Check that the value.
1691 StringRef Model = Str->getString();
1692 if (Model != "global-dynamic" && Model != "local-dynamic"
1693 && Model != "initial-exec" && Model != "local-exec") {
1694 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1695 return;
1696 }
1697
Michael Han99315932013-01-24 16:46:58 +00001698 D->addAttr(::new (S.Context)
1699 TLSModelAttr(Attr.getRange(), S.Context, Model,
1700 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001701}
1702
Chandler Carruthedc2c642011-07-02 00:01:44 +00001703static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001704 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001705 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001706 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1707 return;
1708 }
Mike Stump11289f42009-09-09 15:08:12 +00001709
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001710 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001711 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001712 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001713 D->addAttr(::new (S.Context)
1714 MallocAttr(Attr.getRange(), S.Context,
1715 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001716 return;
1717 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001718 }
1719
Ted Kremenek08479ae2009-08-15 00:51:46 +00001720 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001721}
1722
Chandler Carruthedc2c642011-07-02 00:01:44 +00001723static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001724 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001725 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001726 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001727
Michael Han99315932013-01-24 16:46:58 +00001728 D->addAttr(::new (S.Context)
1729 MayAliasAttr(Attr.getRange(), S.Context,
1730 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001731}
1732
Chandler Carruthedc2c642011-07-02 00:01:44 +00001733static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001734 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001735 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001736 D->addAttr(::new (S.Context)
1737 NoCommonAttr(Attr.getRange(), S.Context,
1738 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001739 else
1740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001741 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001742}
1743
Chandler Carruthedc2c642011-07-02 00:01:44 +00001744static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001745 assert(!Attr.isInvalid());
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001746
1747 if (S.LangOpts.CPlusPlus) {
1748 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1749 return;
1750 }
1751
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001752 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001753 D->addAttr(::new (S.Context)
1754 CommonAttr(Attr.getRange(), S.Context,
1755 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001756 else
1757 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001758 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001759}
1760
Chandler Carruthedc2c642011-07-02 00:01:44 +00001761static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001762 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001763
1764 if (S.CheckNoReturnAttr(attr)) return;
1765
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001766 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001767 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001768 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001769 return;
1770 }
1771
Michael Han99315932013-01-24 16:46:58 +00001772 D->addAttr(::new (S.Context)
1773 NoReturnAttr(attr.getRange(), S.Context,
1774 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001775}
1776
1777bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001778 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001779 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1780 attr.setInvalid();
1781 return true;
1782 }
1783
1784 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001785}
1786
Chandler Carruthedc2c642011-07-02 00:01:44 +00001787static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1788 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001789
1790 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1791 // because 'analyzer_noreturn' does not impact the type.
1792
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001793 if(!checkAttributeNumArgs(S, Attr, 0))
1794 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001795
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001796 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1797 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001798 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1799 && !VD->getType()->isFunctionPointerType())) {
1800 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001801 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001802 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001803 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001804 return;
1805 }
1806 }
1807
Michael Han99315932013-01-24 16:46:58 +00001808 D->addAttr(::new (S.Context)
1809 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1810 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001811}
1812
Richard Smith10876ef2013-01-17 01:30:42 +00001813static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1814 const AttributeList &Attr) {
1815 // C++11 [dcl.attr.noreturn]p1:
1816 // The attribute may be applied to the declarator-id in a function
1817 // declaration.
1818 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1819 if (!FD) {
1820 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1821 << Attr.getName() << ExpectedFunctionOrMethod;
1822 return;
1823 }
1824
Michael Han99315932013-01-24 16:46:58 +00001825 D->addAttr(::new (S.Context)
1826 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1827 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001828}
1829
John Thompsoncdb847ba2010-08-09 21:53:52 +00001830// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001831static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001832/*
1833 Returning a Vector Class in Registers
1834
Eric Christopherbc638a82010-12-01 22:13:54 +00001835 According to the PPU ABI specifications, a class with a single member of
1836 vector type is returned in memory when used as the return value of a function.
1837 This results in inefficient code when implementing vector classes. To return
1838 the value in a single vector register, add the vecreturn attribute to the
1839 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001840
1841 Example:
1842
1843 struct Vector
1844 {
1845 __vector float xyzw;
1846 } __attribute__((vecreturn));
1847
1848 Vector Add(Vector lhs, Vector rhs)
1849 {
1850 Vector result;
1851 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1852 return result; // This will be returned in a register
1853 }
1854*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001855 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001856 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001857 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001858 return;
1859 }
1860
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001861 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001862 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1863 return;
1864 }
1865
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001866 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001867 int count = 0;
1868
1869 if (!isa<CXXRecordDecl>(record)) {
1870 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1871 return;
1872 }
1873
1874 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1875 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1876 return;
1877 }
1878
Eric Christopherbc638a82010-12-01 22:13:54 +00001879 for (RecordDecl::field_iterator iter = record->field_begin();
1880 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001881 if ((count == 1) || !iter->getType()->isVectorType()) {
1882 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1883 return;
1884 }
1885 count++;
1886 }
1887
Michael Han99315932013-01-24 16:46:58 +00001888 D->addAttr(::new (S.Context)
1889 VecReturnAttr(Attr.getRange(), S.Context,
1890 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001891}
1892
Richard Smithe233fbf2013-01-28 22:42:45 +00001893static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1894 const AttributeList &Attr) {
1895 if (isa<ParmVarDecl>(D)) {
1896 // [[carries_dependency]] can only be applied to a parameter if it is a
1897 // parameter of a function declaration or lambda.
1898 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1899 S.Diag(Attr.getLoc(),
1900 diag::err_carries_dependency_param_not_function_decl);
1901 return;
1902 }
1903 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001904 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001905 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001906 return;
1907 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001908
1909 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1910 Attr.getRange(), S.Context,
1911 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001912}
1913
Chandler Carruthedc2c642011-07-02 00:01:44 +00001914static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001915 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001916 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001917 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001918 return;
1919 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001920
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001921 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001922 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001923 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001924 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001925 return;
1926 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001927
Michael Han99315932013-01-24 16:46:58 +00001928 D->addAttr(::new (S.Context)
1929 UnusedAttr(Attr.getRange(), S.Context,
1930 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001931}
1932
Rafael Espindola70107f92011-10-03 14:59:42 +00001933static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1934 const AttributeList &Attr) {
1935 // check the attribute arguments.
1936 if (Attr.hasParameterOrArguments()) {
1937 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1938 return;
1939 }
1940
1941 if (!isa<FunctionDecl>(D)) {
1942 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1943 << Attr.getName() << ExpectedFunction;
1944 return;
1945 }
1946
Michael Han99315932013-01-24 16:46:58 +00001947 D->addAttr(::new (S.Context)
1948 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1949 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00001950}
1951
Chandler Carruthedc2c642011-07-02 00:01:44 +00001952static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001953 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001954 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001955 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1956 return;
1957 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001958
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001959 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001960 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001961 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1962 return;
1963 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001964 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001965 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001966 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001967 return;
1968 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001969
Michael Han99315932013-01-24 16:46:58 +00001970 D->addAttr(::new (S.Context)
1971 UsedAttr(Attr.getRange(), S.Context,
1972 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001973}
1974
Chandler Carruthedc2c642011-07-02 00:01:44 +00001975static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001976 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001977 if (Attr.getNumArgs() > 1) {
1978 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001979 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001980 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001981
1982 int priority = 65535; // FIXME: Do not hardcode such constants.
1983 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001984 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001985 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001986 if (E->isTypeDependent() || E->isValueDependent() ||
1987 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001988 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001989 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001990 return;
1991 }
1992 priority = Idx.getZExtValue();
1993 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001994
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001995 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001996 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001997 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001998 return;
1999 }
2000
Michael Han99315932013-01-24 16:46:58 +00002001 D->addAttr(::new (S.Context)
2002 ConstructorAttr(Attr.getRange(), S.Context, priority,
2003 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002004}
2005
Chandler Carruthedc2c642011-07-02 00:01:44 +00002006static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002007 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002008 if (Attr.getNumArgs() > 1) {
2009 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002010 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002011 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002012
2013 int priority = 65535; // FIXME: Do not hardcode such constants.
2014 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002015 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00002016 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002017 if (E->isTypeDependent() || E->isValueDependent() ||
2018 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002019 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002020 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00002021 return;
2022 }
2023 priority = Idx.getZExtValue();
2024 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002025
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002026 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002027 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002028 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002029 return;
2030 }
2031
Michael Han99315932013-01-24 16:46:58 +00002032 D->addAttr(::new (S.Context)
2033 DestructorAttr(Attr.getRange(), S.Context, priority,
2034 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002035}
2036
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002037template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002038static void handleAttrWithMessage(Sema &S, Decl *D,
2039 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00002040 unsigned NumArgs = Attr.getNumArgs();
2041 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00002042 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002043 return;
2044 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002045
2046 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002047 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00002048 if (NumArgs == 1) {
2049 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00002050 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00002051 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002052 << Attr.getName();
Fariborz Jahanian551063102010-10-06 21:18:44 +00002053 return;
2054 }
Chris Lattner190aa102011-02-24 05:42:24 +00002055 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00002056 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002057
Michael Han99315932013-01-24 16:46:58 +00002058 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2059 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002060}
2061
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002062static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2063 const AttributeList &Attr) {
2064 unsigned NumArgs = Attr.getNumArgs();
2065 if (NumArgs > 0) {
2066 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2067 return;
2068 }
2069
Michael Han99315932013-01-24 16:46:58 +00002070 D->addAttr(::new (S.Context)
2071 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2072 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002073}
2074
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002075static void handleObjCRootClassAttr(Sema &S, Decl *D,
2076 const AttributeList &Attr) {
2077 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002078 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2079 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002080 return;
2081 }
2082
2083 unsigned NumArgs = Attr.getNumArgs();
2084 if (NumArgs > 0) {
2085 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2086 return;
2087 }
2088
Michael Han99315932013-01-24 16:46:58 +00002089 D->addAttr(::new (S.Context)
2090 ObjCRootClassAttr(Attr.getRange(), S.Context,
2091 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002092}
2093
Michael Han99315932013-01-24 16:46:58 +00002094static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2095 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002096 if (!isa<ObjCInterfaceDecl>(D)) {
2097 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2098 return;
2099 }
2100
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002101 unsigned NumArgs = Attr.getNumArgs();
2102 if (NumArgs > 0) {
2103 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2104 return;
2105 }
2106
Michael Han99315932013-01-24 16:46:58 +00002107 D->addAttr(::new (S.Context)
2108 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2109 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002110}
2111
Jordy Rose740b0c22012-05-08 03:27:22 +00002112static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2113 IdentifierInfo *Platform,
2114 VersionTuple Introduced,
2115 VersionTuple Deprecated,
2116 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002117 StringRef PlatformName
2118 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2119 if (PlatformName.empty())
2120 PlatformName = Platform->getName();
2121
2122 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2123 // of these steps are needed).
2124 if (!Introduced.empty() && !Deprecated.empty() &&
2125 !(Introduced <= Deprecated)) {
2126 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2127 << 1 << PlatformName << Deprecated.getAsString()
2128 << 0 << Introduced.getAsString();
2129 return true;
2130 }
2131
2132 if (!Introduced.empty() && !Obsoleted.empty() &&
2133 !(Introduced <= Obsoleted)) {
2134 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2135 << 2 << PlatformName << Obsoleted.getAsString()
2136 << 0 << Introduced.getAsString();
2137 return true;
2138 }
2139
2140 if (!Deprecated.empty() && !Obsoleted.empty() &&
2141 !(Deprecated <= Obsoleted)) {
2142 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2143 << 2 << PlatformName << Obsoleted.getAsString()
2144 << 1 << Deprecated.getAsString();
2145 return true;
2146 }
2147
2148 return false;
2149}
2150
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002151/// \brief Check whether the two versions match.
2152///
2153/// If either version tuple is empty, then they are assumed to match. If
2154/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2155static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2156 bool BeforeIsOkay) {
2157 if (X.empty() || Y.empty())
2158 return true;
2159
2160 if (X == Y)
2161 return true;
2162
2163 if (BeforeIsOkay && X < Y)
2164 return true;
2165
2166 return false;
2167}
2168
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002169AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002170 IdentifierInfo *Platform,
2171 VersionTuple Introduced,
2172 VersionTuple Deprecated,
2173 VersionTuple Obsoleted,
2174 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002175 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002176 bool Override,
2177 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002178 VersionTuple MergedIntroduced = Introduced;
2179 VersionTuple MergedDeprecated = Deprecated;
2180 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002181 bool FoundAny = false;
2182
Rafael Espindolac67f2232012-05-10 02:50:16 +00002183 if (D->hasAttrs()) {
2184 AttrVec &Attrs = D->getAttrs();
2185 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2186 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2187 if (!OldAA) {
2188 ++i;
2189 continue;
2190 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002191
Rafael Espindolac67f2232012-05-10 02:50:16 +00002192 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2193 if (OldPlatform != Platform) {
2194 ++i;
2195 continue;
2196 }
2197
2198 FoundAny = true;
2199 VersionTuple OldIntroduced = OldAA->getIntroduced();
2200 VersionTuple OldDeprecated = OldAA->getDeprecated();
2201 VersionTuple OldObsoleted = OldAA->getObsoleted();
2202 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002203
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002204 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2205 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2206 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2207 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002208 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002209 if (Override) {
2210 int Which = -1;
2211 VersionTuple FirstVersion;
2212 VersionTuple SecondVersion;
2213 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2214 Which = 0;
2215 FirstVersion = OldIntroduced;
2216 SecondVersion = Introduced;
2217 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2218 Which = 1;
2219 FirstVersion = Deprecated;
2220 SecondVersion = OldDeprecated;
2221 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2222 Which = 2;
2223 FirstVersion = Obsoleted;
2224 SecondVersion = OldObsoleted;
2225 }
2226
2227 if (Which == -1) {
2228 Diag(OldAA->getLocation(),
2229 diag::warn_mismatched_availability_override_unavail)
2230 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2231 } else {
2232 Diag(OldAA->getLocation(),
2233 diag::warn_mismatched_availability_override)
2234 << Which
2235 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2236 << FirstVersion.getAsString() << SecondVersion.getAsString();
2237 }
2238 Diag(Range.getBegin(), diag::note_overridden_method);
2239 } else {
2240 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2241 Diag(Range.getBegin(), diag::note_previous_attribute);
2242 }
2243
Rafael Espindolac67f2232012-05-10 02:50:16 +00002244 Attrs.erase(Attrs.begin() + i);
2245 --e;
2246 continue;
2247 }
2248
2249 VersionTuple MergedIntroduced2 = MergedIntroduced;
2250 VersionTuple MergedDeprecated2 = MergedDeprecated;
2251 VersionTuple MergedObsoleted2 = MergedObsoleted;
2252
2253 if (MergedIntroduced2.empty())
2254 MergedIntroduced2 = OldIntroduced;
2255 if (MergedDeprecated2.empty())
2256 MergedDeprecated2 = OldDeprecated;
2257 if (MergedObsoleted2.empty())
2258 MergedObsoleted2 = OldObsoleted;
2259
2260 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2261 MergedIntroduced2, MergedDeprecated2,
2262 MergedObsoleted2)) {
2263 Attrs.erase(Attrs.begin() + i);
2264 --e;
2265 continue;
2266 }
2267
2268 MergedIntroduced = MergedIntroduced2;
2269 MergedDeprecated = MergedDeprecated2;
2270 MergedObsoleted = MergedObsoleted2;
2271 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002272 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002273 }
2274
2275 if (FoundAny &&
2276 MergedIntroduced == Introduced &&
2277 MergedDeprecated == Deprecated &&
2278 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002279 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002280
Ted Kremenekb5445722013-04-06 00:34:27 +00002281 // Only create a new attribute if !Override, but we want to do
2282 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002283 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002284 MergedDeprecated, MergedObsoleted) &&
2285 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002286 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2287 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002288 Obsoleted, IsUnavailable, Message,
2289 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002290 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002291 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002292}
2293
Chandler Carruthedc2c642011-07-02 00:01:44 +00002294static void handleAvailabilityAttr(Sema &S, Decl *D,
2295 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002296 IdentifierInfo *Platform = Attr.getParameterName();
2297 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han99315932013-01-24 16:46:58 +00002298 unsigned Index = Attr.getAttributeSpellingListIndex();
2299
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002300 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002301 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2302 << Platform;
2303
Rafael Espindolac231fab2013-01-08 21:30:32 +00002304 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2305 if (!ND) {
2306 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2307 return;
2308 }
2309
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002310 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2311 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2312 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002313 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002314 StringRef Str;
2315 const StringLiteral *SE =
2316 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2317 if (SE)
2318 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002319
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002320 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002321 Platform,
2322 Introduced.Version,
2323 Deprecated.Version,
2324 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002325 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002326 /*Override=*/false,
2327 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002328 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002329 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002330}
2331
John McCalld041a9b2013-02-20 01:54:26 +00002332template <class T>
2333static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2334 typename T::VisibilityType value,
2335 unsigned attrSpellingListIndex) {
2336 T *existingAttr = D->getAttr<T>();
2337 if (existingAttr) {
2338 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2339 if (existingValue == value)
2340 return NULL;
2341 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2342 S.Diag(range.getBegin(), diag::note_previous_attribute);
2343 D->dropAttr<T>();
2344 }
2345 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2346}
2347
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002348VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002349 VisibilityAttr::VisibilityType Vis,
2350 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002351 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2352 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002353}
2354
John McCalld041a9b2013-02-20 01:54:26 +00002355TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2356 TypeVisibilityAttr::VisibilityType Vis,
2357 unsigned AttrSpellingListIndex) {
2358 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2359 AttrSpellingListIndex);
2360}
2361
2362static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2363 bool isTypeVisibility) {
2364 // Visibility attributes don't mean anything on a typedef.
2365 if (isa<TypedefNameDecl>(D)) {
2366 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2367 << Attr.getName();
2368 return;
2369 }
2370
2371 // 'type_visibility' can only go on a type or namespace.
2372 if (isTypeVisibility &&
2373 !(isa<TagDecl>(D) ||
2374 isa<ObjCInterfaceDecl>(D) ||
2375 isa<NamespaceDecl>(D))) {
2376 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2377 << Attr.getName() << ExpectedTypeOrNamespace;
2378 return;
2379 }
2380
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002381 // check the attribute arguments.
John McCalld041a9b2013-02-20 01:54:26 +00002382 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002383 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002384
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002385 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002386 Arg = Arg->IgnoreParenCasts();
2387 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002388
Douglas Gregorfb65e592011-07-27 05:40:30 +00002389 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002390 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld041a9b2013-02-20 01:54:26 +00002391 << (isTypeVisibility ? "type_visibility" : "visibility") << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002392 return;
2393 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002394
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002395 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002396 VisibilityAttr::VisibilityType type;
Michael Han99315932013-01-24 16:46:58 +00002397
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002398 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002399 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002400 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002401 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002402 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002403 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00002404 else if (TypeStr == "protected") {
2405 // Complain about attempts to use protected visibility on targets
2406 // (like Darwin) that don't support it.
2407 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2408 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2409 type = VisibilityAttr::Default;
2410 } else {
2411 type = VisibilityAttr::Protected;
2412 }
2413 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002414 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002415 return;
2416 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002417
Michael Han99315932013-01-24 16:46:58 +00002418 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002419 clang::Attr *newAttr;
2420 if (isTypeVisibility) {
2421 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2422 (TypeVisibilityAttr::VisibilityType) type,
2423 Index);
2424 } else {
2425 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2426 }
2427 if (newAttr)
2428 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002429}
2430
Chandler Carruthedc2c642011-07-02 00:01:44 +00002431static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2432 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002433 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2434 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002435 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002436 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002437 return;
2438 }
2439
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002440 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2441 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2442 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00002443 << "objc_method_family" << 1;
2444 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002445 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002446 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002447 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002448 return;
2449 }
2450
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002451 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002452 ObjCMethodFamilyAttr::FamilyKind family;
2453 if (param == "none")
2454 family = ObjCMethodFamilyAttr::OMF_None;
2455 else if (param == "alloc")
2456 family = ObjCMethodFamilyAttr::OMF_alloc;
2457 else if (param == "copy")
2458 family = ObjCMethodFamilyAttr::OMF_copy;
2459 else if (param == "init")
2460 family = ObjCMethodFamilyAttr::OMF_init;
2461 else if (param == "mutableCopy")
2462 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2463 else if (param == "new")
2464 family = ObjCMethodFamilyAttr::OMF_new;
2465 else {
2466 // Just warn and ignore it. This is future-proof against new
2467 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002468 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002469 return;
2470 }
2471
John McCall31168b02011-06-15 23:02:42 +00002472 if (family == ObjCMethodFamilyAttr::OMF_init &&
2473 !method->getResultType()->isObjCObjectPointerType()) {
2474 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2475 << method->getResultType();
2476 // Ignore the attribute.
2477 return;
2478 }
2479
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002480 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002481 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002482}
2483
Chandler Carruthedc2c642011-07-02 00:01:44 +00002484static void handleObjCExceptionAttr(Sema &S, Decl *D,
2485 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002486 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002487 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002488
Chris Lattner677a3582009-02-14 08:09:34 +00002489 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2490 if (OCI == 0) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2492 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner677a3582009-02-14 08:09:34 +00002493 return;
2494 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002495
Michael Han99315932013-01-24 16:46:58 +00002496 D->addAttr(::new (S.Context)
2497 ObjCExceptionAttr(Attr.getRange(), S.Context,
2498 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002499}
2500
Chandler Carruthedc2c642011-07-02 00:01:44 +00002501static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman7ced1672013-07-23 12:13:14 +00002502 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002503 return;
Richard Smithdda56e42011-04-15 14:24:37 +00002504 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002505 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002506 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002507 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2508 return;
2509 }
2510 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002511 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2512 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002513 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002514 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2515 return;
2516 }
2517 }
2518 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002519 // It is okay to include this attribute on properties, e.g.:
2520 //
2521 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2522 //
2523 // In this case it follows tradition and suppresses an error in the above
2524 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002525 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002526 }
Michael Han99315932013-01-24 16:46:58 +00002527 D->addAttr(::new (S.Context)
2528 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2529 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002530}
2531
Mike Stumpd3bb5572009-07-24 19:02:52 +00002532static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002533handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman7ced1672013-07-23 12:13:14 +00002534 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002535 return;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002536
2537 if (!isa<FunctionDecl>(D)) {
2538 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2539 return;
2540 }
2541
Michael Han99315932013-01-24 16:46:58 +00002542 D->addAttr(::new (S.Context)
2543 OverloadableAttr(Attr.getRange(), S.Context,
2544 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002545}
2546
Chandler Carruthedc2c642011-07-02 00:01:44 +00002547static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002548 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002549 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002550 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002551 return;
2552 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002553
Steve Naroff3405a732008-09-18 16:44:58 +00002554 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002555 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002556 return;
2557 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002558
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002559 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002560 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002561 type = BlocksAttr::ByRef;
2562 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002563 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002564 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002565 return;
2566 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002567
Michael Han99315932013-01-24 16:46:58 +00002568 D->addAttr(::new (S.Context)
2569 BlocksAttr(Attr.getRange(), S.Context, type,
2570 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002571}
2572
Chandler Carruthedc2c642011-07-02 00:01:44 +00002573static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002574 // check the attribute arguments.
2575 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002576 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002577 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002578 }
2579
John McCallb46f2872011-09-09 07:56:05 +00002580 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002581 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002582 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002583 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002584 if (E->isTypeDependent() || E->isValueDependent() ||
2585 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002586 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002587 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002588 return;
2589 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002590
John McCallb46f2872011-09-09 07:56:05 +00002591 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002592 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2593 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002594 return;
2595 }
John McCallb46f2872011-09-09 07:56:05 +00002596
2597 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002598 }
2599
John McCallb46f2872011-09-09 07:56:05 +00002600 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002601 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002602 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002603 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002604 if (E->isTypeDependent() || E->isValueDependent() ||
2605 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002606 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002607 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002608 return;
2609 }
2610 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002611
John McCallb46f2872011-09-09 07:56:05 +00002612 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002613 // FIXME: This error message could be improved, it would be nice
2614 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002615 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2616 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002617 return;
2618 }
2619 }
2620
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002621 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002622 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002623 if (isa<FunctionNoProtoType>(FT)) {
2624 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2625 return;
2626 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002627
Chris Lattner9363e312009-03-17 23:03:47 +00002628 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002629 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002630 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002631 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002632 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002633 if (!MD->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;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002636 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002637 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2638 if (!BD->isVariadic()) {
2639 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2640 return;
2641 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002642 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002643 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002644 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002645 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002646 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002647 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002648 int m = Ty->isFunctionPointerType() ? 0 : 1;
2649 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002650 return;
2651 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002652 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002653 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002654 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002655 return;
2656 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002657 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002658 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002659 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002660 return;
2661 }
Michael Han99315932013-01-24 16:46:58 +00002662 D->addAttr(::new (S.Context)
2663 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2664 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002665}
2666
Lubos Lunakedc13882013-07-20 15:05:36 +00002667static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2668 // Check the attribute arguments.
2669 if (!checkAttributeNumArgs(S, Attr, 0))
2670 return;
2671
2672 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2673 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2674 else
2675 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2676}
2677
Chandler Carruthedc2c642011-07-02 00:01:44 +00002678static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002679 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002680 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002681 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002682
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002683 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002684 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002685 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002686 return;
2687 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002688
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002689 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2690 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2691 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002692 return;
2693 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002694 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2695 if (MD->getResultType()->isVoidType()) {
2696 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2697 << Attr.getName() << 1;
2698 return;
2699 }
2700
Michael Han99315932013-01-24 16:46:58 +00002701 D->addAttr(::new (S.Context)
2702 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2703 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002704}
2705
Chandler Carruthedc2c642011-07-02 00:01:44 +00002706static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002707 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002708 if (Attr.hasParameterOrArguments()) {
2709 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002710 return;
2711 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002712
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002713 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002714 if (isa<CXXRecordDecl>(D)) {
2715 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2716 return;
2717 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002718 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2719 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002720 return;
2721 }
2722
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002723 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002724
Michael Han99315932013-01-24 16:46:58 +00002725 nd->addAttr(::new (S.Context)
2726 WeakAttr(Attr.getRange(), S.Context,
2727 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002728}
2729
Chandler Carruthedc2c642011-07-02 00:01:44 +00002730static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002731 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002732 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002733 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002734
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002735
2736 // weak_import only applies to variable & function declarations.
2737 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002738 if (!D->canBeWeakImported(isDef)) {
2739 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002740 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2741 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002742 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002743 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002744 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002745 // Nothing to warn about here.
2746 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002747 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002748 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002749
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002750 return;
2751 }
2752
Michael Han99315932013-01-24 16:46:58 +00002753 D->addAttr(::new (S.Context)
2754 WeakImportAttr(Attr.getRange(), S.Context,
2755 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002756}
2757
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002758// Handles reqd_work_group_size and work_group_size_hint.
2759static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002760 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002761 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2762 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2763
Nate Begemanf2758702009-06-26 06:32:41 +00002764 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002765 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002766
2767 unsigned WGSize[3];
2768 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002769 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002770 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002771 if (E->isTypeDependent() || E->isValueDependent() ||
2772 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002773 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002774 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002775 return;
2776 }
2777 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2778 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002779
2780 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2781 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2782 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2783 if (!(A->getXDim() == WGSize[0] &&
2784 A->getYDim() == WGSize[1] &&
2785 A->getZDim() == WGSize[2])) {
2786 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2787 Attr.getName();
2788 }
2789 }
2790
2791 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2792 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2793 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2794 if (!(A->getXDim() == WGSize[0] &&
2795 A->getYDim() == WGSize[1] &&
2796 A->getZDim() == WGSize[2])) {
2797 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2798 Attr.getName();
2799 }
2800 }
2801
2802 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2803 D->addAttr(::new (S.Context)
2804 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002805 WGSize[0], WGSize[1], WGSize[2],
2806 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002807 else
2808 D->addAttr(::new (S.Context)
2809 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002810 WGSize[0], WGSize[1], WGSize[2],
2811 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002812}
2813
Joey Goulyaba589c2013-03-08 09:42:32 +00002814static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2815 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2816
2817 // Attribute has 1 argument.
2818 if (!checkAttributeNumArgs(S, Attr, 1))
2819 return;
2820
2821 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2822
2823 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2824 (ParmType->isBooleanType() ||
2825 !ParmType->isIntegralType(S.getASTContext()))) {
2826 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2827 << ParmType;
2828 return;
2829 }
2830
2831 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2832 D->hasAttr<VecTypeHintAttr>()) {
2833 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2834 if (A->getTypeHint() != ParmType) {
2835 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2836 return;
2837 }
2838 }
2839
2840 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2841 ParmType, Attr.getLoc()));
2842}
2843
Joey Gouly0608ae82013-03-14 09:54:43 +00002844static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2845 if (!dyn_cast<VarDecl>(D))
2846 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2847 << 9;
2848 StringRef EndianType = Attr.getParameterName()->getName();
2849 if (EndianType != "host" && EndianType != "device")
2850 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2851}
2852
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002853SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002854 StringRef Name,
2855 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002856 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2857 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002858 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002859 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2860 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002861 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002862 }
Michael Han99315932013-01-24 16:46:58 +00002863 return ::new (Context) SectionAttr(Range, Context, Name,
2864 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002865}
2866
Chandler Carruthedc2c642011-07-02 00:01:44 +00002867static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002868 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002869 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002870 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002871
2872 // Make sure that there is a string literal as the sections's single
2873 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002874 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002875 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002876 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002877 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002878 return;
2879 }
Mike Stump11289f42009-09-09 15:08:12 +00002880
Chris Lattner30ba6742009-08-10 19:03:04 +00002881 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002882 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002883 if (!Error.empty()) {
2884 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2885 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002886 return;
2887 }
Mike Stump11289f42009-09-09 15:08:12 +00002888
Chris Lattner20aee9b2010-01-12 20:58:53 +00002889 // This attribute cannot be applied to local variables.
2890 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2891 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2892 return;
2893 }
Michael Han99315932013-01-24 16:46:58 +00002894
2895 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002896 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han99315932013-01-24 16:46:58 +00002897 SE->getString(), Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002898 if (NewAttr)
2899 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002900}
2901
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002902
Chandler Carruthedc2c642011-07-02 00:01:44 +00002903static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002904 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002905 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002907 return;
2908 }
Douglas Gregor88336832011-06-15 05:45:11 +00002909
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002910 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002911 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002912 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002913 } else {
Michael Han99315932013-01-24 16:46:58 +00002914 D->addAttr(::new (S.Context)
2915 NoThrowAttr(Attr.getRange(), S.Context,
2916 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002917 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002918}
2919
Chandler Carruthedc2c642011-07-02 00:01:44 +00002920static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002921 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002922 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002923 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002924 return;
2925 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002926
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002927 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002928 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002929 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002930 } else {
Michael Han99315932013-01-24 16:46:58 +00002931 D->addAttr(::new (S.Context)
2932 ConstAttr(Attr.getRange(), S.Context,
2933 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002934 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002935}
2936
Chandler Carruthedc2c642011-07-02 00:01:44 +00002937static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002938 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002939 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002940 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002941
Michael Han99315932013-01-24 16:46:58 +00002942 D->addAttr(::new (S.Context)
2943 PureAttr(Attr.getRange(), S.Context,
2944 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002945}
2946
Chandler Carruthedc2c642011-07-02 00:01:44 +00002947static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002948 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002949 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2950 return;
2951 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002952
Anders Carlssond277d792009-01-31 01:16:18 +00002953 if (Attr.getNumArgs() != 0) {
2954 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2955 return;
2956 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002957
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002958 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002959
Anders Carlssond277d792009-01-31 01:16:18 +00002960 if (!VD || !VD->hasLocalStorage()) {
2961 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2962 return;
2963 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002964
Anders Carlssond277d792009-01-31 01:16:18 +00002965 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002966 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002967 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002968 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2969 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002970 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002971 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002972 Attr.getParameterName();
2973 return;
2974 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002975
Anders Carlssond277d792009-01-31 01:16:18 +00002976 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2977 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002978 S.Diag(Attr.getParameterLoc(),
2979 diag::err_attribute_cleanup_arg_not_function)
2980 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002981 return;
2982 }
2983
Anders Carlssond277d792009-01-31 01:16:18 +00002984 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002985 S.Diag(Attr.getParameterLoc(),
2986 diag::err_attribute_cleanup_func_must_take_one_arg)
2987 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002988 return;
2989 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002990
Anders Carlsson723f55d2009-02-07 23:16:50 +00002991 // We're currently more strict than GCC about what function types we accept.
2992 // If this ever proves to be a problem it should be easy to fix.
2993 QualType Ty = S.Context.getPointerType(VD->getType());
2994 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002995 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2996 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002997 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002998 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2999 Attr.getParameterName() << ParamTy << Ty;
3000 return;
3001 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003002
Michael Han99315932013-01-24 16:46:58 +00003003 D->addAttr(::new (S.Context)
3004 CleanupAttr(Attr.getRange(), S.Context, FD,
3005 Attr.getAttributeSpellingListIndex()));
Eli Friedmanfa0df832012-02-02 03:46:19 +00003006 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewyckya096b142013-02-12 08:08:54 +00003007 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssond277d792009-01-31 01:16:18 +00003008}
3009
Mike Stumpd3bb5572009-07-24 19:02:52 +00003010/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003011/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003012static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003013 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003014 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003015
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003016 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003017 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003018 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003019 return;
3020 }
Chandler Carruth743682b2010-11-16 08:35:43 +00003021
3022 // In C++ the implicit 'this' function parameter also counts, and they are
3023 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003024 bool HasImplicitThisParam = isInstanceMethod(D);
3025 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003026 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00003027
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003028 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003029 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003030 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003031 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3032 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003033 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3034 << "format" << 2 << IdxExpr->getSourceRange();
3035 return;
3036 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003037
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003038 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
3039 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3040 << "format" << 2 << IdxExpr->getSourceRange();
3041 return;
3042 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003043
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003044 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003045
Chandler Carruth743682b2010-11-16 08:35:43 +00003046 if (HasImplicitThisParam) {
3047 if (ArgIdx == 0) {
3048 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3049 << "format_arg" << IdxExpr->getSourceRange();
3050 return;
3051 }
3052 ArgIdx--;
3053 }
3054
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003055 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003056 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003057
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003058 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3059 if (not_nsstring_type &&
3060 !isCFStringType(Ty, S.Context) &&
3061 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003062 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003063 // FIXME: Should highlight the actual expression that has the wrong type.
3064 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003065 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003066 << IdxExpr->getSourceRange();
3067 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003068 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003069 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003070 if (!isNSStringType(Ty, S.Context) &&
3071 !isCFStringType(Ty, S.Context) &&
3072 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003073 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003074 // FIXME: Should highlight the actual expression that has the wrong type.
3075 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003076 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003077 << IdxExpr->getSourceRange();
3078 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003079 }
3080
Michael Han99315932013-01-24 16:46:58 +00003081 D->addAttr(::new (S.Context)
3082 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3083 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003084}
3085
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003086enum FormatAttrKind {
3087 CFStringFormat,
3088 NSStringFormat,
3089 StrftimeFormat,
3090 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003091 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003092 InvalidFormat
3093};
3094
3095/// getFormatAttrKind - Map from format attribute names to supported format
3096/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003097static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003098 return llvm::StringSwitch<FormatAttrKind>(Format)
3099 // Check for formats that get handled specially.
3100 .Case("NSString", NSStringFormat)
3101 .Case("CFString", CFStringFormat)
3102 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003103
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003104 // Otherwise, check for supported formats.
3105 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3106 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3107 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003108
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003109 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3110 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003111}
3112
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003113/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003114/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003115static void handleInitPriorityAttr(Sema &S, Decl *D,
3116 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003117 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003118 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3119 return;
3120 }
3121
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003122 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003123 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3124 Attr.setInvalid();
3125 return;
3126 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003127 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003128 if (S.Context.getAsArrayType(T))
3129 T = S.Context.getBaseElementType(T);
3130 if (!T->getAs<RecordType>()) {
3131 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3132 Attr.setInvalid();
3133 return;
3134 }
3135
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003136 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003137 Attr.setInvalid();
3138 return;
3139 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003140 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003141
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003142 llvm::APSInt priority(32);
3143 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3144 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3145 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3146 << "init_priority" << priorityExpr->getSourceRange();
3147 Attr.setInvalid();
3148 return;
3149 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00003150 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003151 if (prioritynum < 101 || prioritynum > 65535) {
3152 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3153 << priorityExpr->getSourceRange();
3154 Attr.setInvalid();
3155 return;
3156 }
Michael Han99315932013-01-24 16:46:58 +00003157 D->addAttr(::new (S.Context)
3158 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3159 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003160}
3161
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003162FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han99315932013-01-24 16:46:58 +00003163 int FormatIdx, int FirstArg,
3164 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003165 // Check whether we already have an equivalent format attribute.
3166 for (specific_attr_iterator<FormatAttr>
3167 i = D->specific_attr_begin<FormatAttr>(),
3168 e = D->specific_attr_end<FormatAttr>();
3169 i != e ; ++i) {
3170 FormatAttr *f = *i;
3171 if (f->getType() == Format &&
3172 f->getFormatIdx() == FormatIdx &&
3173 f->getFirstArg() == FirstArg) {
3174 // If we don't have a valid location for this attribute, adopt the
3175 // location.
3176 if (f->getLocation().isInvalid())
3177 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003178 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003179 }
3180 }
3181
Michael Han99315932013-01-24 16:46:58 +00003182 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3183 AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003184}
3185
Mike Stumpd3bb5572009-07-24 19:02:52 +00003186/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003187/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003188static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003189
Chris Lattner4a927cb2008-06-28 23:36:30 +00003190 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003191 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003192 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003193 return;
3194 }
3195
Chris Lattner4a927cb2008-06-28 23:36:30 +00003196 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003197 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003198 return;
3199 }
3200
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003201 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003202 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003203 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003204 return;
3205 }
3206
Chandler Carruth743682b2010-11-16 08:35:43 +00003207 // In C++ the implicit 'this' function parameter also counts, and they are
3208 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003209 bool HasImplicitThisParam = isInstanceMethod(D);
3210 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003211 unsigned FirstIdx = 1;
3212
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003213 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003214
3215 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003216 if (Format.startswith("__") && Format.endswith("__"))
3217 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003218
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003219 // Check for supported formats.
3220 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003221
3222 if (Kind == IgnoredFormat)
3223 return;
3224
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003225 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003226 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003227 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003228 return;
3229 }
3230
3231 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003232 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003233 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003234 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3235 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003236 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003237 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003238 return;
3239 }
3240
3241 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003242 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003243 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003244 return;
3245 }
3246
3247 // FIXME: Do we need to bounds check?
3248 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003249
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003250 if (HasImplicitThisParam) {
3251 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003252 S.Diag(Attr.getLoc(),
3253 diag::err_format_attribute_implicit_this_format_string)
3254 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003255 return;
3256 }
3257 ArgIdx--;
3258 }
Mike Stump11289f42009-09-09 15:08:12 +00003259
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003260 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003261 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003262
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003263 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003264 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003265 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3266 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003267 return;
3268 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003269 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003270 // FIXME: do we need to check if the type is NSString*? What are the
3271 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003272 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003273 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003274 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3275 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003276 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003277 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003278 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003279 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003280 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003281 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3282 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003283 return;
3284 }
3285
3286 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003287 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003288 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003289 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3290 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003291 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003292 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003293 return;
3294 }
3295
3296 // check if the function is variadic if the 3rd argument non-zero
3297 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003298 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003299 ++NumArgs; // +1 for ...
3300 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003301 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003302 return;
3303 }
3304 }
3305
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003306 // strftime requires FirstArg to be 0 because it doesn't read from any
3307 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003308 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003309 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003310 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3311 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003312 return;
3313 }
3314 // if 0 it disables parameter checking (to use with e.g. va_list)
3315 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003316 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003317 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003318 return;
3319 }
3320
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003321 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3322 Idx.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003323 FirstArg.getZExtValue(),
3324 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003325 if (NewAttr)
3326 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003327}
3328
Chandler Carruthedc2c642011-07-02 00:01:44 +00003329static void handleTransparentUnionAttr(Sema &S, Decl *D,
3330 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003331 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003332 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003333 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003334
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003335
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003336 // Try to find the underlying union declaration.
3337 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003338 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003339 if (TD && TD->getUnderlyingType()->isUnionType())
3340 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3341 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003342 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003343
3344 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003345 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003346 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003347 return;
3348 }
3349
John McCallf937c022011-10-07 06:10:15 +00003350 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003351 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003352 diag::warn_transparent_union_attribute_not_definition);
3353 return;
3354 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003355
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003356 RecordDecl::field_iterator Field = RD->field_begin(),
3357 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003358 if (Field == FieldEnd) {
3359 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3360 return;
3361 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003362
David Blaikie40ed2972012-06-06 20:45:41 +00003363 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003364 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003365 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003366 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003367 diag::warn_transparent_union_attribute_floating)
3368 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003369 return;
3370 }
3371
3372 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3373 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3374 for (; Field != FieldEnd; ++Field) {
3375 QualType FieldType = Field->getType();
3376 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3377 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3378 // Warn if we drop the attribute.
3379 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003380 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003381 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003382 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003383 diag::warn_transparent_union_attribute_field_size_align)
3384 << isSize << Field->getDeclName() << FieldBits;
3385 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003386 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003387 diag::note_transparent_union_first_field_size_align)
3388 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003389 return;
3390 }
3391 }
3392
Michael Han99315932013-01-24 16:46:58 +00003393 RD->addAttr(::new (S.Context)
3394 TransparentUnionAttr(Attr.getRange(), S.Context,
3395 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003396}
3397
Chandler Carruthedc2c642011-07-02 00:01:44 +00003398static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003399 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003400 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003401 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003402
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003403 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003404 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003405
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003406 // Make sure that there is a string literal as the annotation's single
3407 // argument.
3408 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003409 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003410 return;
3411 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003412
3413 // Don't duplicate annotations that are already set.
3414 for (specific_attr_iterator<AnnotateAttr>
3415 i = D->specific_attr_begin<AnnotateAttr>(),
3416 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3417 if ((*i)->getAnnotation() == SE->getString())
3418 return;
3419 }
Michael Han99315932013-01-24 16:46:58 +00003420
3421 D->addAttr(::new (S.Context)
3422 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3423 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003424}
3425
Chandler Carruthedc2c642011-07-02 00:01:44 +00003426static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003427 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003428 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003430 return;
3431 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003432
Richard Smith848e1f12013-02-01 08:12:08 +00003433 if (Attr.getNumArgs() == 0) {
3434 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3435 true, 0, Attr.getAttributeSpellingListIndex()));
3436 return;
3437 }
3438
Richard Smith44c247f2013-02-22 08:32:16 +00003439 Expr *E = Attr.getArg(0);
3440 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3441 S.Diag(Attr.getEllipsisLoc(),
3442 diag::err_pack_expansion_without_parameter_packs);
3443 return;
3444 }
3445
3446 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3447 return;
3448
3449 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3450 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003451}
3452
3453void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003454 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003455 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3456 SourceLocation AttrLoc = AttrRange.getBegin();
3457
Richard Smith1dba27c2013-01-29 09:02:09 +00003458 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003459 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003460 // C++11 [dcl.align]p1:
3461 // An alignment-specifier may be applied to a variable or to a class
3462 // data member, but it shall not be applied to a bit-field, a function
3463 // parameter, the formal parameter of a catch clause, or a variable
3464 // declared with the register storage class specifier. An
3465 // alignment-specifier may also be applied to the declaration of a class
3466 // or enumeration type.
3467 // C11 6.7.5/2:
3468 // An alignment attribute shall not be specified in a declaration of
3469 // a typedef, or a bit-field, or a function, or a parameter, or an
3470 // object declared with the register storage-class specifier.
3471 int DiagKind = -1;
3472 if (isa<ParmVarDecl>(D)) {
3473 DiagKind = 0;
3474 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3475 if (VD->getStorageClass() == SC_Register)
3476 DiagKind = 1;
3477 if (VD->isExceptionVariable())
3478 DiagKind = 2;
3479 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3480 if (FD->isBitField())
3481 DiagKind = 3;
3482 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003483 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3484 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003485 << (TmpAttr.isC11() ? ExpectedVariableOrField
3486 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003487 return;
3488 }
3489 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003490 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003491 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003492 return;
3493 }
3494 }
3495
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003496 if (E->isTypeDependent() || E->isValueDependent()) {
3497 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003498 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3499 AA->setPackExpansion(IsPackExpansion);
3500 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003501 return;
3502 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003503
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003504 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003505 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003506 ExprResult ICE
3507 = VerifyIntegerConstantExpression(E, &Alignment,
3508 diag::err_aligned_attribute_argument_not_int,
3509 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003510 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003511 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003512
3513 // C++11 [dcl.align]p2:
3514 // -- if the constant expression evaluates to zero, the alignment
3515 // specifier shall have no effect
3516 // C11 6.7.5p6:
3517 // An alignment specification of zero has no effect.
3518 if (!(TmpAttr.isAlignas() && !Alignment) &&
3519 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003520 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3521 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003522 return;
3523 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003524
Richard Smith848e1f12013-02-01 08:12:08 +00003525 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003526 // We've already verified it's a power of 2, now let's make sure it's
3527 // 8192 or less.
3528 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003529 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003530 << E->getSourceRange();
3531 return;
3532 }
3533 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003534
Richard Smith44c247f2013-02-22 08:32:16 +00003535 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3536 ICE.take(), SpellingListIndex);
3537 AA->setPackExpansion(IsPackExpansion);
3538 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003539}
3540
Michael Hanaf02bbe2013-02-01 01:19:17 +00003541void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003542 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003543 // FIXME: Cache the number on the Attr object if non-dependent?
3544 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003545 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3546 SpellingListIndex);
3547 AA->setPackExpansion(IsPackExpansion);
3548 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003549}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003550
Richard Smith848e1f12013-02-01 08:12:08 +00003551void Sema::CheckAlignasUnderalignment(Decl *D) {
3552 assert(D->hasAttrs() && "no attributes on decl");
3553
3554 QualType Ty;
3555 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3556 Ty = VD->getType();
3557 else
3558 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003559 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003560 return;
3561
3562 // C++11 [dcl.align]p5, C11 6.7.5/4:
3563 // The combined effect of all alignment attributes in a declaration shall
3564 // not specify an alignment that is less strict than the alignment that
3565 // would otherwise be required for the entity being declared.
3566 AlignedAttr *AlignasAttr = 0;
3567 unsigned Align = 0;
3568 for (specific_attr_iterator<AlignedAttr>
3569 I = D->specific_attr_begin<AlignedAttr>(),
3570 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3571 if (I->isAlignmentDependent())
3572 return;
3573 if (I->isAlignas())
3574 AlignasAttr = *I;
3575 Align = std::max(Align, I->getAlignment(Context));
3576 }
3577
3578 if (AlignasAttr && Align) {
3579 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3580 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3581 if (NaturalAlign > RequestedAlign)
3582 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3583 << Ty << (unsigned)NaturalAlign.getQuantity();
3584 }
3585}
3586
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003587/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003588/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003589///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003590/// Despite what would be logical, the mode attribute is a decl attribute, not a
3591/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3592/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003593static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003594 // This attribute isn't documented, but glibc uses it. It changes
3595 // the width of an int or unsigned int to the specified size.
3596
3597 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003598 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003599 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003600
Chris Lattneracbc2d22008-06-27 22:18:37 +00003601
3602 IdentifierInfo *Name = Attr.getParameterName();
3603 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003604 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003605 return;
3606 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003607
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003608 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003609
3610 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003611 if (Str.startswith("__") && Str.endswith("__"))
3612 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003613
3614 unsigned DestWidth = 0;
3615 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003616 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003617 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003618 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003619 switch (Str[0]) {
3620 case 'Q': DestWidth = 8; break;
3621 case 'H': DestWidth = 16; break;
3622 case 'S': DestWidth = 32; break;
3623 case 'D': DestWidth = 64; break;
3624 case 'X': DestWidth = 96; break;
3625 case 'T': DestWidth = 128; break;
3626 }
3627 if (Str[1] == 'F') {
3628 IntegerMode = false;
3629 } else if (Str[1] == 'C') {
3630 IntegerMode = false;
3631 ComplexMode = true;
3632 } else if (Str[1] != 'I') {
3633 DestWidth = 0;
3634 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003635 break;
3636 case 4:
3637 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3638 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003639 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003640 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003641 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003642 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003643 break;
3644 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003645 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003646 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003647 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003648 case 11:
3649 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003650 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003651 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003652 }
3653
3654 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003655 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003656 OldTy = TD->getUnderlyingType();
3657 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3658 OldTy = VD->getType();
3659 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003660 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003661 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003662 return;
3663 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003664
John McCall9dd450b2009-09-21 23:43:11 +00003665 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003666 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3667 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003668 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003669 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3670 } else if (ComplexMode) {
3671 if (!OldTy->isComplexType())
3672 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3673 } else {
3674 if (!OldTy->isFloatingType())
3675 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3676 }
3677
Mike Stump87c57ac2009-05-16 07:39:55 +00003678 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3679 // and friends, at least with glibc.
3680 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3681 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003682 // FIXME: Make sure floating-point mappings are accurate
3683 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003684 QualType NewTy;
3685 switch (DestWidth) {
3686 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003687 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003688 return;
3689 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003690 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003691 return;
3692 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003693 if (!IntegerMode) {
3694 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3695 return;
3696 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003697 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003698 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003699 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003700 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003701 break;
3702 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003703 if (!IntegerMode) {
3704 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3705 return;
3706 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003707 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003708 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003709 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003710 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003711 break;
3712 case 32:
3713 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003714 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003715 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003716 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003717 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003718 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003719 break;
3720 case 64:
3721 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003722 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003723 else if (OldTy->isSignedIntegerType())
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.LongTy;
3726 else
3727 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003728 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003729 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003730 NewTy = S.Context.UnsignedLongTy;
3731 else
3732 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003733 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003734 case 96:
3735 NewTy = S.Context.LongDoubleTy;
3736 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003737 case 128:
Roman Divackyac6f5f42013-07-03 21:08:41 +00003738 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3739 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedman1efaaea2009-02-13 02:31:07 +00003740 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3741 return;
3742 }
Roman Divackyb6debb62013-07-03 20:48:06 +00003743 if (IntegerMode) {
3744 if (OldTy->isSignedIntegerType())
3745 NewTy = S.Context.Int128Ty;
3746 else
3747 NewTy = S.Context.UnsignedInt128Ty;
3748 } else
3749 NewTy = S.Context.LongDoubleTy;
Eli Friedman4735374e2009-03-03 06:41:03 +00003750 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003751 }
3752
Eli Friedman4735374e2009-03-03 06:41:03 +00003753 if (ComplexMode) {
3754 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003755 }
3756
3757 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003758 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3759 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3760 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003761 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003762
3763 D->addAttr(::new (S.Context)
3764 ModeAttr(Attr.getRange(), S.Context, Name,
3765 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003766}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003767
Chandler Carruthedc2c642011-07-02 00:01:44 +00003768static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003769 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003770 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003771 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003772
Nick Lewycky08597072012-07-24 01:40:49 +00003773 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3774 if (!VD->hasGlobalStorage())
3775 S.Diag(Attr.getLoc(),
3776 diag::warn_attribute_requires_functions_or_static_globals)
3777 << Attr.getName();
3778 } else if (!isFunctionOrMethod(D)) {
3779 S.Diag(Attr.getLoc(),
3780 diag::warn_attribute_requires_functions_or_static_globals)
3781 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003782 return;
3783 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003784
Michael Han99315932013-01-24 16:46:58 +00003785 D->addAttr(::new (S.Context)
3786 NoDebugAttr(Attr.getRange(), S.Context,
3787 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003788}
3789
Chandler Carruthedc2c642011-07-02 00:01:44 +00003790static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003791 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003792 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003793 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003794
Mike Stumpd3bb5572009-07-24 19:02:52 +00003795
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003796 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003797 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003798 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003799 return;
3800 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003801
Michael Han99315932013-01-24 16:46:58 +00003802 D->addAttr(::new (S.Context)
3803 NoInlineAttr(Attr.getRange(), S.Context,
3804 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003805}
3806
Chandler Carruthedc2c642011-07-02 00:01:44 +00003807static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3808 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003809 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003810 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003811 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003812
Chris Lattner3c77a352010-06-22 00:03:40 +00003813
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003814 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003815 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003816 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003817 return;
3818 }
3819
Michael Han99315932013-01-24 16:46:58 +00003820 D->addAttr(::new (S.Context)
3821 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3822 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003823}
3824
Chandler Carruthedc2c642011-07-02 00:01:44 +00003825static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003826 if (S.LangOpts.CUDA) {
3827 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003828 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003829 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3830 return;
3831 }
3832
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003833 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003834 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003835 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003836 return;
3837 }
3838
Michael Han99315932013-01-24 16:46:58 +00003839 D->addAttr(::new (S.Context)
3840 CUDAConstantAttr(Attr.getRange(), S.Context,
3841 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003842 } else {
3843 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3844 }
3845}
3846
Chandler Carruthedc2c642011-07-02 00:01:44 +00003847static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003848 if (S.LangOpts.CUDA) {
3849 // check the attribute arguments.
3850 if (Attr.getNumArgs() != 0) {
3851 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3852 return;
3853 }
3854
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003855 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003856 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003857 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003858 return;
3859 }
3860
Michael Han99315932013-01-24 16:46:58 +00003861 D->addAttr(::new (S.Context)
3862 CUDADeviceAttr(Attr.getRange(), S.Context,
3863 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003864 } else {
3865 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3866 }
3867}
3868
Chandler Carruthedc2c642011-07-02 00:01:44 +00003869static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003870 if (S.LangOpts.CUDA) {
3871 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003872 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003873 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003874
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003875 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003877 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003878 return;
3879 }
3880
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003881 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003882 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003883 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003884 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003885 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3886 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003887 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003888 "void");
3889 } else {
3890 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3891 << FD->getType();
3892 }
3893 return;
3894 }
3895
Michael Han99315932013-01-24 16:46:58 +00003896 D->addAttr(::new (S.Context)
3897 CUDAGlobalAttr(Attr.getRange(), S.Context,
3898 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003899 } else {
3900 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3901 }
3902}
3903
Chandler Carruthedc2c642011-07-02 00:01:44 +00003904static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003905 if (S.LangOpts.CUDA) {
3906 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003907 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003908 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003909
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003910
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003911 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003912 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003913 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003914 return;
3915 }
3916
Michael Han99315932013-01-24 16:46:58 +00003917 D->addAttr(::new (S.Context)
3918 CUDAHostAttr(Attr.getRange(), S.Context,
3919 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003920 } else {
3921 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3922 }
3923}
3924
Chandler Carruthedc2c642011-07-02 00:01:44 +00003925static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003926 if (S.LangOpts.CUDA) {
3927 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003928 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003929 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003930
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003931 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003932 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003933 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003934 return;
3935 }
3936
Michael Han99315932013-01-24 16:46:58 +00003937 D->addAttr(::new (S.Context)
3938 CUDASharedAttr(Attr.getRange(), S.Context,
3939 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003940 } else {
3941 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3942 }
3943}
3944
Chandler Carruthedc2c642011-07-02 00:01:44 +00003945static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003946 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003947 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003948 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003949
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003950 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003951 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003953 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003954 return;
3955 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003956
Douglas Gregor35b57532009-10-27 21:01:01 +00003957 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003958 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003959 return;
3960 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003961
Michael Han99315932013-01-24 16:46:58 +00003962 D->addAttr(::new (S.Context)
3963 GNUInlineAttr(Attr.getRange(), S.Context,
3964 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003965}
3966
Chandler Carruthedc2c642011-07-02 00:01:44 +00003967static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003968 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003969
Aaron Ballman02df2e02012-12-09 17:45:41 +00003970 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003971 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003972 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3973 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003974 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003975 return;
3976
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003977 if (!isa<ObjCMethodDecl>(D)) {
3978 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3979 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003980 return;
3981 }
3982
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003983 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003984 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003985 D->addAttr(::new (S.Context)
3986 FastCallAttr(Attr.getRange(), S.Context,
3987 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003988 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003989 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003990 D->addAttr(::new (S.Context)
3991 StdCallAttr(Attr.getRange(), S.Context,
3992 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003993 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003994 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003995 D->addAttr(::new (S.Context)
3996 ThisCallAttr(Attr.getRange(), S.Context,
3997 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003998 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003999 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004000 D->addAttr(::new (S.Context)
4001 CDeclAttr(Attr.getRange(), S.Context,
4002 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004003 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004004 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004005 D->addAttr(::new (S.Context)
4006 PascalAttr(Attr.getRange(), S.Context,
4007 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004008 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004009 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004010 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004011 switch (CC) {
4012 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004013 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004014 break;
4015 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004016 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004017 break;
4018 default:
4019 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004020 }
4021
Michael Han99315932013-01-24 16:46:58 +00004022 D->addAttr(::new (S.Context)
4023 PcsAttr(Attr.getRange(), S.Context, PCS,
4024 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004025 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004026 }
Derek Schuffa2020962012-10-16 22:30:41 +00004027 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00004028 D->addAttr(::new (S.Context)
4029 PnaclCallAttr(Attr.getRange(), S.Context,
4030 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004031 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00004032 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004033 D->addAttr(::new (S.Context)
4034 IntelOclBiccAttr(Attr.getRange(), S.Context,
4035 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004036 return;
Derek Schuffa2020962012-10-16 22:30:41 +00004037
Abramo Bagnara50099372010-04-30 13:10:51 +00004038 default:
4039 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004040 }
4041}
4042
Chandler Carruthedc2c642011-07-02 00:01:44 +00004043static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00004044 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004045 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004046}
4047
Guy Benyeifb36ede2013-03-24 13:58:12 +00004048static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4049 assert(!Attr.isInvalid());
4050
4051 Expr *E = Attr.getArg(0);
4052 llvm::APSInt ArgNum(32);
4053 if (E->isTypeDependent() || E->isValueDependent() ||
4054 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4055 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4056 << Attr.getName()->getName() << E->getSourceRange();
4057 return;
4058 }
4059
4060 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4061 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4062}
4063
Aaron Ballman02df2e02012-12-09 17:45:41 +00004064bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4065 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00004066 if (attr.isInvalid())
4067 return true;
4068
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004069 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4070 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
4071 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00004072 attr.setInvalid();
4073 return true;
4074 }
4075
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004076 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4077 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00004078 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004079 case AttributeList::AT_CDecl: CC = CC_C; break;
4080 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4081 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4082 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4083 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4084 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004085 Expr *Arg = attr.getArg(0);
4086 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004087 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004088 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
4089 << "pcs" << 1;
4090 attr.setInvalid();
4091 return true;
4092 }
4093
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004094 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004095 if (StrRef == "aapcs") {
4096 CC = CC_AAPCS;
4097 break;
4098 } else if (StrRef == "aapcs-vfp") {
4099 CC = CC_AAPCS_VFP;
4100 break;
4101 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004102
4103 attr.setInvalid();
4104 Diag(attr.getLoc(), diag::err_invalid_pcs);
4105 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004106 }
Derek Schuffa2020962012-10-16 22:30:41 +00004107 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00004108 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00004109 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004110 }
4111
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004112 const TargetInfo &TI = Context.getTargetInfo();
4113 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4114 if (A == TargetInfo::CCCR_Warning) {
4115 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00004116
4117 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4118 if (FD)
4119 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4120 TargetInfo::CCMT_NonMember;
4121 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004122 }
4123
John McCall3882ace2011-01-05 12:14:39 +00004124 return false;
4125}
4126
Chandler Carruthedc2c642011-07-02 00:01:44 +00004127static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004128 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00004129
4130 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004131 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00004132 return;
4133
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004134 if (!isa<ObjCMethodDecl>(D)) {
4135 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4136 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004137 return;
4138 }
Eli Friedman7044b762009-03-27 21:06:47 +00004139
Michael Han99315932013-01-24 16:46:58 +00004140 D->addAttr(::new (S.Context)
4141 RegparmAttr(Attr.getRange(), S.Context, numParams,
4142 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00004143}
4144
4145/// Checks a regparm attribute, returning true if it is ill-formed and
4146/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004147bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4148 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004149 return true;
4150
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00004151 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004152 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004153 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004154 }
Eli Friedman7044b762009-03-27 21:06:47 +00004155
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004156 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00004157 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00004158 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00004159 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004160 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00004161 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004162 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004163 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004164 }
4165
Douglas Gregore8bbc122011-09-02 00:18:52 +00004166 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004167 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004168 << 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 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00004174 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004175 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004176 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004177 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004178 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004179 }
4180
John McCall3882ace2011-01-05 12:14:39 +00004181 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004182}
4183
Chandler Carruthedc2c642011-07-02 00:01:44 +00004184static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004185 if (S.LangOpts.CUDA) {
4186 // check the attribute arguments.
4187 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004188 // FIXME: 0 is not okay.
4189 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004190 return;
4191 }
4192
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004193 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004194 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004195 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004196 return;
4197 }
4198
4199 Expr *MaxThreadsExpr = Attr.getArg(0);
4200 llvm::APSInt MaxThreads(32);
4201 if (MaxThreadsExpr->isTypeDependent() ||
4202 MaxThreadsExpr->isValueDependent() ||
4203 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4204 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4205 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4206 return;
4207 }
4208
4209 llvm::APSInt MinBlocks(32);
4210 if (Attr.getNumArgs() > 1) {
4211 Expr *MinBlocksExpr = Attr.getArg(1);
4212 if (MinBlocksExpr->isTypeDependent() ||
4213 MinBlocksExpr->isValueDependent() ||
4214 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4215 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4216 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4217 return;
4218 }
4219 }
4220
Michael Han99315932013-01-24 16:46:58 +00004221 D->addAttr(::new (S.Context)
4222 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4223 MaxThreads.getZExtValue(),
4224 MinBlocks.getZExtValue(),
4225 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004226 } else {
4227 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4228 }
4229}
4230
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004231static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4232 const AttributeList &Attr) {
4233 StringRef AttrName = Attr.getName()->getName();
4234 if (!Attr.getParameterName()) {
4235 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4236 << Attr.getName() << /* arg num = */ 1;
4237 return;
4238 }
4239
4240 if (Attr.getNumArgs() != 2) {
4241 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4242 << /* required args = */ 3;
4243 return;
4244 }
4245
4246 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4247
4248 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4249 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4250 << Attr.getName() << ExpectedFunctionOrMethod;
4251 return;
4252 }
4253
4254 uint64_t ArgumentIdx;
4255 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4256 Attr.getLoc(), 2,
4257 Attr.getArg(0), ArgumentIdx))
4258 return;
4259
4260 uint64_t TypeTagIdx;
4261 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4262 Attr.getLoc(), 3,
4263 Attr.getArg(1), TypeTagIdx))
4264 return;
4265
4266 bool IsPointer = (AttrName == "pointer_with_type_tag");
4267 if (IsPointer) {
4268 // Ensure that buffer has a pointer type.
4269 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4270 if (!BufferTy->isPointerType()) {
4271 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004272 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004273 }
4274 }
4275
Michael Han99315932013-01-24 16:46:58 +00004276 D->addAttr(::new (S.Context)
4277 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4278 ArgumentIdx, TypeTagIdx, IsPointer,
4279 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004280}
4281
4282static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4283 const AttributeList &Attr) {
4284 IdentifierInfo *PointerKind = Attr.getParameterName();
4285 if (!PointerKind) {
4286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4287 << "type_tag_for_datatype" << 1;
4288 return;
4289 }
4290
4291 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4292
Michael Han99315932013-01-24 16:46:58 +00004293 D->addAttr(::new (S.Context)
4294 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4295 MatchingCType,
4296 Attr.getLayoutCompatible(),
4297 Attr.getMustBeNull(),
4298 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004299}
4300
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004301//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004302// Checker-specific attribute handlers.
4303//===----------------------------------------------------------------------===//
4304
John McCalled433932011-01-25 03:31:58 +00004305static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004306 return type->isDependentType() ||
4307 type->isObjCObjectPointerType() ||
4308 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004309}
4310static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004311 return type->isDependentType() ||
4312 type->isPointerType() ||
4313 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004314}
4315
Chandler Carruthedc2c642011-07-02 00:01:44 +00004316static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004317 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004318 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004319 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004320 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004321 return;
4322 }
4323
4324 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004325 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004326 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4327 cf = false;
4328 } else {
4329 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4330 cf = true;
4331 }
4332
4333 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004334 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004335 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004336 return;
4337 }
4338
4339 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004340 param->addAttr(::new (S.Context)
4341 CFConsumedAttr(Attr.getRange(), S.Context,
4342 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004343 else
Michael Han99315932013-01-24 16:46:58 +00004344 param->addAttr(::new (S.Context)
4345 NSConsumedAttr(Attr.getRange(), S.Context,
4346 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004347}
4348
Chandler Carruthedc2c642011-07-02 00:01:44 +00004349static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4350 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004351 if (!isa<ObjCMethodDecl>(D)) {
4352 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004353 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004354 return;
4355 }
4356
Michael Han99315932013-01-24 16:46:58 +00004357 D->addAttr(::new (S.Context)
4358 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4359 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004360}
4361
Chandler Carruthedc2c642011-07-02 00:01:44 +00004362static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4363 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004364
John McCalled433932011-01-25 03:31:58 +00004365 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004366
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004367 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004368 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004369 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004370 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004371 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004372 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4373 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004374 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004375 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004376 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004377 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004378 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004379 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004380 return;
4381 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004382
John McCalled433932011-01-25 03:31:58 +00004383 bool typeOK;
4384 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004385 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004386 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004387 case AttributeList::AT_NSReturnsAutoreleased:
4388 case AttributeList::AT_NSReturnsRetained:
4389 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004390 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4391 cf = false;
4392 break;
4393
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004394 case AttributeList::AT_CFReturnsRetained:
4395 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004396 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4397 cf = true;
4398 break;
4399 }
4400
4401 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004402 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004403 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004404 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004405 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004406
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004407 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004408 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004409 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004410 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004411 D->addAttr(::new (S.Context)
4412 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4413 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004414 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004415 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004416 D->addAttr(::new (S.Context)
4417 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4418 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004419 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004420 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004421 D->addAttr(::new (S.Context)
4422 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4423 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004424 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004425 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004426 D->addAttr(::new (S.Context)
4427 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4428 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004429 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004430 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004431 D->addAttr(::new (S.Context)
4432 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4433 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004434 return;
4435 };
4436}
4437
John McCallcf166702011-07-22 08:53:00 +00004438static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4439 const AttributeList &attr) {
4440 SourceLocation loc = attr.getLoc();
4441
4442 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4443
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004444 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00004445 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004446 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00004447 return;
4448 }
4449
4450 // Check that the method returns a normal pointer.
4451 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004452
4453 if (!resultType->isReferenceType() &&
4454 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00004455 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4456 << SourceRange(loc)
4457 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4458
4459 // Drop the attribute.
4460 return;
4461 }
4462
Michael Han99315932013-01-24 16:46:58 +00004463 method->addAttr(::new (S.Context)
4464 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4465 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004466}
4467
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004468static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4469 const AttributeList &attr) {
4470 SourceLocation loc = attr.getLoc();
4471 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4472
4473 if (!method) {
4474 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4475 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4476 return;
4477 }
4478 DeclContext *DC = method->getDeclContext();
4479 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4480 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4481 << attr.getName() << 0;
4482 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4483 return;
4484 }
4485 if (method->getMethodFamily() == OMF_dealloc) {
4486 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4487 << attr.getName() << 1;
4488 return;
4489 }
4490
Michael Han99315932013-01-24 16:46:58 +00004491 method->addAttr(::new (S.Context)
4492 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4493 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004494}
4495
John McCall32f5fe12011-09-30 05:12:12 +00004496/// Handle cf_audited_transfer and cf_unknown_transfer.
4497static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4498 if (!isa<FunctionDecl>(D)) {
4499 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004500 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004501 return;
4502 }
4503
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004504 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004505
4506 // Check whether there's a conflicting attribute already present.
4507 Attr *Existing;
4508 if (IsAudited) {
4509 Existing = D->getAttr<CFUnknownTransferAttr>();
4510 } else {
4511 Existing = D->getAttr<CFAuditedTransferAttr>();
4512 }
4513 if (Existing) {
4514 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4515 << A.getName()
4516 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4517 << A.getRange() << Existing->getRange();
4518 return;
4519 }
4520
4521 // All clear; add the attribute.
4522 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004523 D->addAttr(::new (S.Context)
4524 CFAuditedTransferAttr(A.getRange(), S.Context,
4525 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004526 } else {
Michael Han99315932013-01-24 16:46:58 +00004527 D->addAttr(::new (S.Context)
4528 CFUnknownTransferAttr(A.getRange(), S.Context,
4529 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004530 }
4531}
4532
John McCallf1e8b342011-09-29 07:17:38 +00004533static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4534 const AttributeList &Attr) {
4535 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4536 if (!RD || RD->isUnion()) {
4537 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004538 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004539 }
4540
4541 IdentifierInfo *ParmName = Attr.getParameterName();
4542
4543 // In Objective-C, verify that the type names an Objective-C type.
4544 // We don't want to check this outside of ObjC because people sometimes
4545 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004546 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004547 // Check for an existing type with this name.
4548 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4549 Sema::LookupOrdinaryName);
4550 if (S.LookupName(R, Sc)) {
4551 NamedDecl *Target = R.getFoundDecl();
4552 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4553 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4554 S.Diag(Target->getLocStart(), diag::note_declared_at);
4555 }
4556 }
4557 }
4558
Michael Han99315932013-01-24 16:46:58 +00004559 D->addAttr(::new (S.Context)
4560 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4561 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004562}
4563
Chandler Carruthedc2c642011-07-02 00:01:44 +00004564static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4565 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004566 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004567
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004568 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004569 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004570}
4571
Chandler Carruthedc2c642011-07-02 00:01:44 +00004572static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4573 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004574 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004575 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004576 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004577 return;
4578 }
4579
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004580 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004581 QualType type = vd->getType();
4582
4583 if (!type->isDependentType() &&
4584 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004585 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004586 << type;
4587 return;
4588 }
4589
4590 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4591
4592 // If we have no lifetime yet, check the lifetime we're presumably
4593 // going to infer.
4594 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4595 lifetime = type->getObjCARCImplicitLifetime();
4596
4597 switch (lifetime) {
4598 case Qualifiers::OCL_None:
4599 assert(type->isDependentType() &&
4600 "didn't infer lifetime for non-dependent type?");
4601 break;
4602
4603 case Qualifiers::OCL_Weak: // meaningful
4604 case Qualifiers::OCL_Strong: // meaningful
4605 break;
4606
4607 case Qualifiers::OCL_ExplicitNone:
4608 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004609 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004610 << (lifetime == Qualifiers::OCL_Autoreleasing);
4611 break;
4612 }
4613
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004614 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004615 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4616 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004617}
4618
Francois Picheta83957a2010-12-19 06:50:37 +00004619//===----------------------------------------------------------------------===//
4620// Microsoft specific attribute handlers.
4621//===----------------------------------------------------------------------===//
4622
Reid Kleckner140c4a72013-05-17 14:04:52 +00004623// Check if MS extensions or some other language extensions are enabled. If
4624// not, issue a diagnostic that the given attribute is unused.
4625static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4626 bool OtherExtension = false) {
4627 if (S.LangOpts.MicrosoftExt || OtherExtension)
4628 return true;
4629 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4630 return false;
4631}
4632
Chandler Carruthedc2c642011-07-02 00:01:44 +00004633static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004634 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4635 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004636
Reid Kleckner140c4a72013-05-17 14:04:52 +00004637 // check the attribute arguments.
4638 if (!checkAttributeNumArgs(S, Attr, 1))
4639 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004640
Reid Kleckner140c4a72013-05-17 14:04:52 +00004641 Expr *Arg = Attr.getArg(0);
4642 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4643 if (!Str || !Str->isAscii()) {
4644 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4645 << "uuid" << 1;
4646 return;
4647 }
Francois Pichet7da11662010-12-20 01:41:49 +00004648
Reid Kleckner140c4a72013-05-17 14:04:52 +00004649 StringRef StrRef = Str->getString();
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004650
Reid Kleckner140c4a72013-05-17 14:04:52 +00004651 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4652 StrRef.back() == '}';
Francois Pichet7da11662010-12-20 01:41:49 +00004653
Reid Kleckner140c4a72013-05-17 14:04:52 +00004654 // Validate GUID length.
4655 if (IsCurly && StrRef.size() != 38) {
4656 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4657 return;
4658 }
4659 if (!IsCurly && StrRef.size() != 36) {
4660 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4661 return;
4662 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004663
Reid Kleckner140c4a72013-05-17 14:04:52 +00004664 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4665 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4666 StringRef::iterator I = StrRef.begin();
4667 if (IsCurly) // Skip the optional '{'
4668 ++I;
4669
4670 for (int i = 0; i < 36; ++i) {
4671 if (i == 8 || i == 13 || i == 18 || i == 23) {
4672 if (*I != '-') {
Francois Pichet7da11662010-12-20 01:41:49 +00004673 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4674 return;
4675 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004676 } else if (!isHexDigit(*I)) {
4677 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4678 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004679 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004680 I++;
4681 }
Francois Picheta83957a2010-12-19 06:50:37 +00004682
Reid Kleckner140c4a72013-05-17 14:04:52 +00004683 D->addAttr(::new (S.Context)
4684 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4685 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004686}
4687
John McCall8d32c052012-05-22 21:28:12 +00004688static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004689 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004690 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004691
4692 AttributeList::Kind Kind = Attr.getKind();
4693 if (Kind == AttributeList::AT_SingleInheritance)
4694 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004695 ::new (S.Context)
4696 SingleInheritanceAttr(Attr.getRange(), S.Context,
4697 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004698 else if (Kind == AttributeList::AT_MultipleInheritance)
4699 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004700 ::new (S.Context)
4701 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4702 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004703 else if (Kind == AttributeList::AT_VirtualInheritance)
4704 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004705 ::new (S.Context)
4706 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4707 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004708}
4709
4710static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004711 if (!checkMicrosoftExt(S, Attr))
4712 return;
4713
4714 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballman317a77f2013-05-22 23:25:32 +00004715 if (Kind == AttributeList::AT_Win64)
Reid Kleckner140c4a72013-05-17 14:04:52 +00004716 D->addAttr(
4717 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4718 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004719}
4720
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004721static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004722 if (!checkMicrosoftExt(S, Attr))
4723 return;
4724 D->addAttr(::new (S.Context)
4725 ForceInlineAttr(Attr.getRange(), S.Context,
4726 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004727}
4728
Reid Klecknerb144d362013-05-20 14:02:37 +00004729static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4730 if (!checkMicrosoftExt(S, Attr))
4731 return;
4732 // Check linkage after possibly merging declaratinos. See
4733 // checkAttributesAfterMerging().
4734 D->addAttr(::new (S.Context)
4735 SelectAnyAttr(Attr.getRange(), S.Context,
4736 Attr.getAttributeSpellingListIndex()));
4737}
4738
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004739//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004740// Top Level Sema Entry Points
4741//===----------------------------------------------------------------------===//
4742
Chandler Carruthedc2c642011-07-02 00:01:44 +00004743static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4744 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004745 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004746 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4747 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4748 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004749 default:
4750 break;
4751 }
4752}
Abramo Bagnara50099372010-04-30 13:10:51 +00004753
Chandler Carruthedc2c642011-07-02 00:01:44 +00004754static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4755 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004756 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004757 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4758 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4759 case AttributeList::AT_IBOutletCollection:
4760 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004761 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004762 case AttributeList::AT_ObjCGC:
4763 case AttributeList::AT_VectorSize:
4764 case AttributeList::AT_NeonVectorType:
4765 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004766 case AttributeList::AT_Ptr32:
4767 case AttributeList::AT_Ptr64:
4768 case AttributeList::AT_SPtr:
4769 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004770 // Ignore these, these are type attributes, handled by
4771 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004772 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004773 case AttributeList::AT_CUDADevice:
4774 case AttributeList::AT_CUDAHost:
4775 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004776 // Ignore, this is a non-inheritable attribute, handled
4777 // by ProcessNonInheritableDeclAttr.
4778 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004779 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4780 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4781 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4782 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004783 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004784 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004785 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004786 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004787 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4788 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4789 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004790 handleDependencyAttr(S, scope, D, Attr);
4791 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004792 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4793 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4794 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004795 case AttributeList::AT_CXX11NoReturn:
4796 handleCXX11NoReturnAttr(S, D, Attr);
4797 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004798 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004799 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004800 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004801 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4802 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004803 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004804 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004805 case AttributeList::AT_MinSize:
4806 handleMinSizeAttr(S, D, Attr);
4807 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004808 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4809 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4810 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4811 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4812 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004813 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004814 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004815 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4816 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4817 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4818 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4819 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004820 case AttributeList::AT_ownership_returns:
4821 case AttributeList::AT_ownership_takes:
4822 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004823 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004824 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4825 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4826 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4827 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4828 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4829 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4830 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004831
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004832 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004833 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004834 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004835 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004836
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004837 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004838 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4839
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004840 case AttributeList::AT_ObjCRequiresSuper:
4841 handleObjCRequiresSuperAttr(S, D, Attr); break;
4842
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004843 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004844 handleNSBridgedAttr(S, scope, D, Attr); break;
4845
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004846 case AttributeList::AT_CFAuditedTransfer:
4847 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004848 handleCFTransferAttr(S, D, Attr); break;
4849
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004850 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004851 case AttributeList::AT_CFConsumed:
4852 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4853 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004854 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004855
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004856 case AttributeList::AT_NSReturnsAutoreleased:
4857 case AttributeList::AT_NSReturnsNotRetained:
4858 case AttributeList::AT_CFReturnsNotRetained:
4859 case AttributeList::AT_NSReturnsRetained:
4860 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004861 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004862
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004863 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004864 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004865 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004866
Joey Goulyaba589c2013-03-08 09:42:32 +00004867 case AttributeList::AT_VecTypeHint:
4868 handleVecTypeHint(S, D, Attr); break;
4869
Joey Gouly0608ae82013-03-14 09:54:43 +00004870 case AttributeList::AT_Endian:
4871 handleEndianAttr(S, D, Attr);
4872 break;
4873
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004874 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004875 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004876
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004877 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4878 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4879 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004880 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004881 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004882 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004883 handleArcWeakrefUnavailableAttr (S, D, Attr);
4884 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004885 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004886 handleObjCRootClassAttr(S, D, Attr);
4887 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004888 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004889 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004890 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004891 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4892 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004893 handleReturnsTwiceAttr(S, D, Attr);
4894 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004895 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004896 case AttributeList::AT_Visibility:
4897 handleVisibilityAttr(S, D, Attr, false);
4898 break;
4899 case AttributeList::AT_TypeVisibility:
4900 handleVisibilityAttr(S, D, Attr, true);
4901 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004902 case AttributeList::AT_WarnUnused:
4903 handleWarnUnusedAttr(S, D, Attr);
4904 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004905 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004906 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004907 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4908 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4909 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4910 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004911 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004912 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004913 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004914 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004915 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004916 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004917 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004918 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004919 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4920 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4921 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4922 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4923 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4924 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4925 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4926 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4927 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004928 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004929 // Just ignore
4930 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004931 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004932 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004933 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004934 case AttributeList::AT_StdCall:
4935 case AttributeList::AT_CDecl:
4936 case AttributeList::AT_FastCall:
4937 case AttributeList::AT_ThisCall:
4938 case AttributeList::AT_Pascal:
4939 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004940 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004941 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004942 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004943 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004944 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004945 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004946 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004947 case AttributeList::AT_OpenCLImageAccess:
4948 handleOpenCLImageAccessAttr(S, D, Attr);
4949 break;
John McCall8d32c052012-05-22 21:28:12 +00004950
4951 // Microsoft attributes:
John McCall5e77d762013-04-16 07:28:30 +00004952 case AttributeList::AT_MsProperty: break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004953 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004954 handleMsStructAttr(S, D, Attr);
4955 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004956 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004957 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004958 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004959 case AttributeList::AT_SingleInheritance:
4960 case AttributeList::AT_MultipleInheritance:
4961 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004962 handleInheritanceAttr(S, D, Attr);
4963 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004964 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004965 handlePortabilityAttr(S, D, Attr);
4966 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004967 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004968 handleForceInlineAttr(S, D, Attr);
4969 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004970 case AttributeList::AT_SelectAny:
4971 handleSelectAnyAttr(S, D, Attr);
4972 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004973
4974 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004975 case AttributeList::AT_AssertExclusiveLock:
4976 handleAssertExclusiveLockAttr(S, D, Attr);
4977 break;
4978 case AttributeList::AT_AssertSharedLock:
4979 handleAssertSharedLockAttr(S, D, Attr);
4980 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004981 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004982 handleGuardedVarAttr(S, D, Attr);
4983 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004984 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004985 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004986 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004987 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004988 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004989 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004990 case AttributeList::AT_NoSanitizeAddress:
4991 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004992 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004993 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004994 handleNoThreadSafetyAnalysis(S, D, Attr);
4995 break;
4996 case AttributeList::AT_NoSanitizeThread:
4997 handleNoSanitizeThread(S, D, Attr);
4998 break;
4999 case AttributeList::AT_NoSanitizeMemory:
5000 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005001 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005002 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005003 handleLockableAttr(S, D, Attr);
5004 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005005 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005006 handleGuardedByAttr(S, D, Attr);
5007 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005008 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005009 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005010 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005011 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005012 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005013 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005014 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00005015 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005016 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005017 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005018 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005019 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005020 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005021 handleLockReturnedAttr(S, D, Attr);
5022 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005023 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005024 handleLocksExcludedAttr(S, D, Attr);
5025 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005026 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005027 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005028 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005029 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00005030 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005031 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005032 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005033 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005034 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005035 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005036 handleUnlockFunAttr(S, D, Attr);
5037 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005038 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005039 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005040 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005041 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005042 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005043 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005044
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005045 // Type safety attributes.
5046 case AttributeList::AT_ArgumentWithTypeTag:
5047 handleArgumentWithTypeTagAttr(S, D, Attr);
5048 break;
5049 case AttributeList::AT_TypeTagForDatatype:
5050 handleTypeTagForDatatypeAttr(S, D, Attr);
5051 break;
5052
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005053 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005054 // Ask target about the attribute.
5055 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5056 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00005057 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5058 diag::warn_unhandled_ms_attribute_ignored :
5059 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005060 break;
5061 }
5062}
5063
Peter Collingbourneb331b262011-01-21 02:08:45 +00005064/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5065/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smith10876ef2013-01-17 01:30:42 +00005066/// silently ignore it if a GNU attribute.
Chandler Carruthedc2c642011-07-02 00:01:44 +00005067static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5068 const AttributeList &Attr,
Richard Smith10876ef2013-01-17 01:30:42 +00005069 bool NonInheritable, bool Inheritable,
5070 bool IncludeCXX11Attributes) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00005071 if (Attr.isInvalid())
5072 return;
5073
Richard Smith10876ef2013-01-17 01:30:42 +00005074 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5075 // instead.
5076 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5077 return;
5078
Peter Collingbourneb331b262011-01-21 02:08:45 +00005079 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00005080 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00005081
5082 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00005083 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00005084}
5085
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005086/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5087/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005088void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005089 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005090 bool NonInheritable, bool Inheritable,
5091 bool IncludeCXX11Attributes) {
5092 for (const AttributeList* l = AttrList; l; l = l->getNext())
5093 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5094 IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005095
5096 // GCC accepts
5097 // static int a9 __attribute__((weakref));
5098 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005099 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00005100 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00005101 cast<NamedDecl>(D)->getNameAsString();
5102 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005103 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005104 }
5105}
5106
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005107// Annotation attributes are the only attributes allowed after an access
5108// specifier.
5109bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5110 const AttributeList *AttrList) {
5111 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005112 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005113 handleAnnotateAttr(*this, ASDecl, *l);
5114 } else {
5115 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5116 return true;
5117 }
5118 }
5119
5120 return false;
5121}
5122
John McCall42856de2011-10-01 05:17:03 +00005123/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5124/// contains any decl attributes that we should warn about.
5125static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5126 for ( ; A; A = A->getNext()) {
5127 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005128 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005129 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5130
5131 if (A->getKind() == AttributeList::UnknownAttribute) {
5132 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5133 << A->getName() << A->getRange();
5134 } else {
5135 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5136 << A->getName() << A->getRange();
5137 }
5138 }
5139}
5140
5141/// checkUnusedDeclAttributes - Given a declarator which is not being
5142/// used to build a declaration, complain about any decl attributes
5143/// which might be lying around on it.
5144void Sema::checkUnusedDeclAttributes(Declarator &D) {
5145 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5146 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5147 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5148 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5149}
5150
Ryan Flynn7d470f32009-07-30 03:15:39 +00005151/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005152/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005153NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5154 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005155 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005156 NamedDecl *NewD = 0;
5157 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005158 FunctionDecl *NewFD;
5159 // FIXME: Missing call to CheckFunctionDeclaration().
5160 // FIXME: Mangling?
5161 // FIXME: Is the qualifier info correct?
5162 // FIXME: Is the DeclContext correct?
5163 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5164 Loc, Loc, DeclarationName(II),
5165 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005166 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005167 FD->hasPrototype(),
5168 false/*isConstexprSpecified*/);
5169 NewD = NewFD;
5170
5171 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005172 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005173
5174 // Fake up parameter variables; they are declared as if this were
5175 // a typedef.
5176 QualType FDTy = FD->getType();
5177 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5178 SmallVector<ParmVarDecl*, 16> Params;
5179 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5180 AE = FT->arg_type_end(); AI != AE; ++AI) {
5181 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5182 Param->setScopeInfo(0, Params.size());
5183 Params.push_back(Param);
5184 }
David Blaikie9c70e042011-09-21 18:16:56 +00005185 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005186 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005187 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5188 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005189 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005190 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005191 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005192 if (VD->getQualifier()) {
5193 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005194 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005195 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005196 }
5197 return NewD;
5198}
5199
James Dennett634962f2012-06-14 21:40:34 +00005200/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005201/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005202void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005203 if (W.getUsed()) return; // only do this once
5204 W.setUsed(true);
5205 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5206 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005207 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005208 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5209 NDId->getName()));
5210 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005211 WeakTopLevelDecl.push_back(NewD);
5212 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5213 // to insert Decl at TU scope, sorry.
5214 DeclContext *SavedContext = CurContext;
5215 CurContext = Context.getTranslationUnitDecl();
5216 PushOnScopeChains(NewD, S);
5217 CurContext = SavedContext;
5218 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005219 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005220 }
5221}
5222
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005223void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5224 // It's valid to "forward-declare" #pragma weak, in which case we
5225 // have to do this.
5226 LoadExternalWeakUndeclaredIdentifiers();
5227 if (!WeakUndeclaredIdentifiers.empty()) {
5228 NamedDecl *ND = NULL;
5229 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5230 if (VD->isExternC())
5231 ND = VD;
5232 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5233 if (FD->isExternC())
5234 ND = FD;
5235 if (ND) {
5236 if (IdentifierInfo *Id = ND->getIdentifier()) {
5237 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5238 = WeakUndeclaredIdentifiers.find(Id);
5239 if (I != WeakUndeclaredIdentifiers.end()) {
5240 WeakInfo W = I->second;
5241 DeclApplyPragmaWeak(S, ND, W);
5242 WeakUndeclaredIdentifiers[Id] = W;
5243 }
5244 }
5245 }
5246 }
5247}
5248
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005249/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5250/// it, apply them to D. This is a bit tricky because PD can have attributes
5251/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005252void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5253 bool NonInheritable, bool Inheritable) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005254 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005255 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005256 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005257
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005258 // Walk the declarator structure, applying decl attributes that were in a type
5259 // position to the decl itself. This handles cases like:
5260 // int *__attr__(x)** D;
5261 // when X is a decl attribute.
5262 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5263 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith10876ef2013-01-17 01:30:42 +00005264 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5265 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005266
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005267 // Finally, apply any attributes on the decl itself.
5268 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005269 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005270}
John McCall28a6aea2009-11-04 02:18:39 +00005271
John McCall31168b02011-06-15 23:02:42 +00005272/// Is the given declaration allowed to use a forbidden type?
5273static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5274 // Private ivars are always okay. Unfortunately, people don't
5275 // always properly make their ivars private, even in system headers.
5276 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005277 // Function declarations in sys headers will be marked unavailable.
5278 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5279 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005280 return false;
5281
5282 // Require it to be declared in a system header.
5283 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5284}
5285
5286/// Handle a delayed forbidden-type diagnostic.
5287static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5288 Decl *decl) {
5289 if (decl && isForbiddenTypeAllowed(S, decl)) {
5290 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5291 "this system declaration uses an unsupported type"));
5292 return;
5293 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005294 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005295 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005296 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005297 // kind of forbidden type messages on unavailable functions.
5298 if (FD->hasAttr<UnavailableAttr>() &&
5299 diag.getForbiddenTypeDiagnostic() ==
5300 diag::err_arc_array_param_no_ownership) {
5301 diag.Triggered = true;
5302 return;
5303 }
5304 }
John McCall31168b02011-06-15 23:02:42 +00005305
5306 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5307 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5308 diag.Triggered = true;
5309}
5310
John McCall2ec85372012-05-07 06:16:41 +00005311void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5312 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005313 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005314 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005315
John McCall2ec85372012-05-07 06:16:41 +00005316 // When delaying diagnostics to run in the context of a parsed
5317 // declaration, we only want to actually emit anything if parsing
5318 // succeeds.
5319 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005320
John McCall2ec85372012-05-07 06:16:41 +00005321 // We emit all the active diagnostics in this pool or any of its
5322 // parents. In general, we'll get one pool for the decl spec
5323 // and a child pool for each declarator; in a decl group like:
5324 // deprecated_typedef foo, *bar, baz();
5325 // only the declarator pops will be passed decls. This is correct;
5326 // we really do need to consider delayed diagnostics from the decl spec
5327 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005328 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005329 do {
John McCall6347b682012-05-07 06:16:58 +00005330 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005331 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5332 // This const_cast is a bit lame. Really, Triggered should be mutable.
5333 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005334 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005335 continue;
5336
John McCallc1465822011-02-14 07:13:47 +00005337 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005338 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005339 // Don't bother giving deprecation diagnostics if the decl is invalid.
5340 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005341 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005342 break;
5343
5344 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005345 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005346 break;
John McCall31168b02011-06-15 23:02:42 +00005347
5348 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005349 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005350 break;
John McCall86121512010-01-27 03:50:35 +00005351 }
5352 }
John McCall2ec85372012-05-07 06:16:41 +00005353 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005354}
5355
John McCall6347b682012-05-07 06:16:58 +00005356/// Given a set of delayed diagnostics, re-emit them as if they had
5357/// been delayed in the current context instead of in the given pool.
5358/// Essentially, this just moves them to the current pool.
5359void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5360 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5361 assert(curPool && "re-emitting in undelayed context not supported");
5362 curPool->steal(pool);
5363}
5364
John McCall28a6aea2009-11-04 02:18:39 +00005365static bool isDeclDeprecated(Decl *D) {
5366 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005367 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005368 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005369 // A category implicitly has the availability of the interface.
5370 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5371 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005372 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5373 return false;
5374}
5375
Eli Friedman971bfa12012-08-08 21:52:41 +00005376static void
5377DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5378 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005379 const ObjCInterfaceDecl *UnknownObjCClass,
5380 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005381 DeclarationName Name = D->getDeclName();
5382 if (!Message.empty()) {
5383 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5384 S.Diag(D->getLocation(),
5385 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5386 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005387 if (ObjCPropery)
5388 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5389 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005390 } else if (!UnknownObjCClass) {
5391 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5392 S.Diag(D->getLocation(),
5393 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5394 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005395 if (ObjCPropery)
5396 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5397 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005398 } else {
5399 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5400 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5401 }
5402}
5403
John McCallb45a1e72010-08-26 02:13:20 +00005404void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005405 Decl *Ctx) {
5406 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005407 return;
5408
John McCall86121512010-01-27 03:50:35 +00005409 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005410 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5411 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005412 DD.getUnknownObjCClass(),
5413 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005414}
5415
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005416void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005417 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005418 const ObjCInterfaceDecl *UnknownObjCClass,
5419 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005420 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005421 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005422 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5423 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005424 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005425 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005426 return;
5427 }
5428
5429 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005430 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005431 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005432 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005433}