blob: ef401c3a4da6233412822a6c3715b5fc30eb9c72 [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) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002502 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002503 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002504 return;
2505 }
Richard Smithdda56e42011-04-15 14:24:37 +00002506 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002507 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002508 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002509 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2510 return;
2511 }
2512 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002513 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2514 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002515 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002516 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2517 return;
2518 }
2519 }
2520 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002521 // It is okay to include this attribute on properties, e.g.:
2522 //
2523 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2524 //
2525 // In this case it follows tradition and suppresses an error in the above
2526 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002527 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002528 }
Michael Han99315932013-01-24 16:46:58 +00002529 D->addAttr(::new (S.Context)
2530 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2531 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002532}
2533
Mike Stumpd3bb5572009-07-24 19:02:52 +00002534static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002535handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002536 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002537 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002538 return;
2539 }
2540
2541 if (!isa<FunctionDecl>(D)) {
2542 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2543 return;
2544 }
2545
Michael Han99315932013-01-24 16:46:58 +00002546 D->addAttr(::new (S.Context)
2547 OverloadableAttr(Attr.getRange(), S.Context,
2548 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002549}
2550
Chandler Carruthedc2c642011-07-02 00:01:44 +00002551static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002552 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002553 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002554 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002555 return;
2556 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002557
Steve Naroff3405a732008-09-18 16:44:58 +00002558 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002559 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002560 return;
2561 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002562
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002563 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002564 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002565 type = BlocksAttr::ByRef;
2566 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002567 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002568 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002569 return;
2570 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002571
Michael Han99315932013-01-24 16:46:58 +00002572 D->addAttr(::new (S.Context)
2573 BlocksAttr(Attr.getRange(), S.Context, type,
2574 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002575}
2576
Chandler Carruthedc2c642011-07-02 00:01:44 +00002577static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002578 // check the attribute arguments.
2579 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002580 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002581 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002582 }
2583
John McCallb46f2872011-09-09 07:56:05 +00002584 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002585 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002586 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002587 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002588 if (E->isTypeDependent() || E->isValueDependent() ||
2589 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002590 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002591 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002592 return;
2593 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002594
John McCallb46f2872011-09-09 07:56:05 +00002595 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002596 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2597 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002598 return;
2599 }
John McCallb46f2872011-09-09 07:56:05 +00002600
2601 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002602 }
2603
John McCallb46f2872011-09-09 07:56:05 +00002604 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002605 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002606 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002607 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002608 if (E->isTypeDependent() || E->isValueDependent() ||
2609 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002610 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002611 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002612 return;
2613 }
2614 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002615
John McCallb46f2872011-09-09 07:56:05 +00002616 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002617 // FIXME: This error message could be improved, it would be nice
2618 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002619 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2620 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002621 return;
2622 }
2623 }
2624
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002625 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002626 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002627 if (isa<FunctionNoProtoType>(FT)) {
2628 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2629 return;
2630 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002631
Chris Lattner9363e312009-03-17 23:03:47 +00002632 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002633 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002634 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002635 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002636 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002637 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002638 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002639 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002640 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002641 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2642 if (!BD->isVariadic()) {
2643 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2644 return;
2645 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002646 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002647 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002648 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002649 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002650 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002651 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002652 int m = Ty->isFunctionPointerType() ? 0 : 1;
2653 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002654 return;
2655 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002656 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002657 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002658 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002659 return;
2660 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002661 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002662 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002663 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002664 return;
2665 }
Michael Han99315932013-01-24 16:46:58 +00002666 D->addAttr(::new (S.Context)
2667 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2668 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002669}
2670
Chandler Carruthedc2c642011-07-02 00:01:44 +00002671static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002672 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002673 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002674 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002675
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002676 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002678 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002679 return;
2680 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002681
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002682 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2683 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2684 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002685 return;
2686 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002687 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2688 if (MD->getResultType()->isVoidType()) {
2689 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2690 << Attr.getName() << 1;
2691 return;
2692 }
2693
Michael Han99315932013-01-24 16:46:58 +00002694 D->addAttr(::new (S.Context)
2695 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2696 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002697}
2698
Chandler Carruthedc2c642011-07-02 00:01:44 +00002699static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002700 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002701 if (Attr.hasParameterOrArguments()) {
2702 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002703 return;
2704 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002705
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002706 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002707 if (isa<CXXRecordDecl>(D)) {
2708 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2709 return;
2710 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002711 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2712 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002713 return;
2714 }
2715
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002716 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002717
Michael Han99315932013-01-24 16:46:58 +00002718 nd->addAttr(::new (S.Context)
2719 WeakAttr(Attr.getRange(), S.Context,
2720 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002721}
2722
Chandler Carruthedc2c642011-07-02 00:01:44 +00002723static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002724 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002725 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002726 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002727
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002728
2729 // weak_import only applies to variable & function declarations.
2730 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002731 if (!D->canBeWeakImported(isDef)) {
2732 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002733 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2734 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002735 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002736 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002737 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002738 // Nothing to warn about here.
2739 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002741 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002742
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002743 return;
2744 }
2745
Michael Han99315932013-01-24 16:46:58 +00002746 D->addAttr(::new (S.Context)
2747 WeakImportAttr(Attr.getRange(), S.Context,
2748 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002749}
2750
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002751// Handles reqd_work_group_size and work_group_size_hint.
2752static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002753 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002754 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2755 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2756
Nate Begemanf2758702009-06-26 06:32:41 +00002757 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002758 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002759
2760 unsigned WGSize[3];
2761 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002762 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002763 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002764 if (E->isTypeDependent() || E->isValueDependent() ||
2765 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002766 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002767 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002768 return;
2769 }
2770 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2771 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002772
2773 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2774 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2775 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2776 if (!(A->getXDim() == WGSize[0] &&
2777 A->getYDim() == WGSize[1] &&
2778 A->getZDim() == WGSize[2])) {
2779 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2780 Attr.getName();
2781 }
2782 }
2783
2784 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2785 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2786 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2787 if (!(A->getXDim() == WGSize[0] &&
2788 A->getYDim() == WGSize[1] &&
2789 A->getZDim() == WGSize[2])) {
2790 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2791 Attr.getName();
2792 }
2793 }
2794
2795 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2796 D->addAttr(::new (S.Context)
2797 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002798 WGSize[0], WGSize[1], WGSize[2],
2799 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002800 else
2801 D->addAttr(::new (S.Context)
2802 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002803 WGSize[0], WGSize[1], WGSize[2],
2804 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002805}
2806
Joey Goulyaba589c2013-03-08 09:42:32 +00002807static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2808 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2809
2810 // Attribute has 1 argument.
2811 if (!checkAttributeNumArgs(S, Attr, 1))
2812 return;
2813
2814 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2815
2816 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2817 (ParmType->isBooleanType() ||
2818 !ParmType->isIntegralType(S.getASTContext()))) {
2819 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2820 << ParmType;
2821 return;
2822 }
2823
2824 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2825 D->hasAttr<VecTypeHintAttr>()) {
2826 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2827 if (A->getTypeHint() != ParmType) {
2828 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2829 return;
2830 }
2831 }
2832
2833 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2834 ParmType, Attr.getLoc()));
2835}
2836
Joey Gouly0608ae82013-03-14 09:54:43 +00002837static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2838 if (!dyn_cast<VarDecl>(D))
2839 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2840 << 9;
2841 StringRef EndianType = Attr.getParameterName()->getName();
2842 if (EndianType != "host" && EndianType != "device")
2843 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2844}
2845
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002846SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002847 StringRef Name,
2848 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002849 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2850 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002851 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002852 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2853 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002854 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002855 }
Michael Han99315932013-01-24 16:46:58 +00002856 return ::new (Context) SectionAttr(Range, Context, Name,
2857 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002858}
2859
Chandler Carruthedc2c642011-07-02 00:01:44 +00002860static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002861 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002862 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002863 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002864
2865 // Make sure that there is a string literal as the sections's single
2866 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002867 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002868 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002869 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002870 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002871 return;
2872 }
Mike Stump11289f42009-09-09 15:08:12 +00002873
Chris Lattner30ba6742009-08-10 19:03:04 +00002874 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002875 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002876 if (!Error.empty()) {
2877 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2878 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002879 return;
2880 }
Mike Stump11289f42009-09-09 15:08:12 +00002881
Chris Lattner20aee9b2010-01-12 20:58:53 +00002882 // This attribute cannot be applied to local variables.
2883 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2884 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2885 return;
2886 }
Michael Han99315932013-01-24 16:46:58 +00002887
2888 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002889 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han99315932013-01-24 16:46:58 +00002890 SE->getString(), Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002891 if (NewAttr)
2892 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002893}
2894
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002895
Chandler Carruthedc2c642011-07-02 00:01:44 +00002896static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002897 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002898 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002899 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002900 return;
2901 }
Douglas Gregor88336832011-06-15 05:45:11 +00002902
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002903 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002904 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002905 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002906 } else {
Michael Han99315932013-01-24 16:46:58 +00002907 D->addAttr(::new (S.Context)
2908 NoThrowAttr(Attr.getRange(), S.Context,
2909 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002910 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002911}
2912
Chandler Carruthedc2c642011-07-02 00:01:44 +00002913static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002914 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002915 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002916 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002917 return;
2918 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002919
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002920 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002921 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002922 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002923 } else {
Michael Han99315932013-01-24 16:46:58 +00002924 D->addAttr(::new (S.Context)
2925 ConstAttr(Attr.getRange(), S.Context,
2926 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002927 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002928}
2929
Chandler Carruthedc2c642011-07-02 00:01:44 +00002930static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002931 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002932 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002933 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002934
Michael Han99315932013-01-24 16:46:58 +00002935 D->addAttr(::new (S.Context)
2936 PureAttr(Attr.getRange(), S.Context,
2937 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002938}
2939
Chandler Carruthedc2c642011-07-02 00:01:44 +00002940static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002941 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002942 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2943 return;
2944 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002945
Anders Carlssond277d792009-01-31 01:16:18 +00002946 if (Attr.getNumArgs() != 0) {
2947 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2948 return;
2949 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002950
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002951 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002952
Anders Carlssond277d792009-01-31 01:16:18 +00002953 if (!VD || !VD->hasLocalStorage()) {
2954 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2955 return;
2956 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002957
Anders Carlssond277d792009-01-31 01:16:18 +00002958 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002959 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002960 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002961 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2962 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002963 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002964 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002965 Attr.getParameterName();
2966 return;
2967 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002968
Anders Carlssond277d792009-01-31 01:16:18 +00002969 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2970 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002971 S.Diag(Attr.getParameterLoc(),
2972 diag::err_attribute_cleanup_arg_not_function)
2973 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002974 return;
2975 }
2976
Anders Carlssond277d792009-01-31 01:16:18 +00002977 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002978 S.Diag(Attr.getParameterLoc(),
2979 diag::err_attribute_cleanup_func_must_take_one_arg)
2980 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002981 return;
2982 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002983
Anders Carlsson723f55d2009-02-07 23:16:50 +00002984 // We're currently more strict than GCC about what function types we accept.
2985 // If this ever proves to be a problem it should be easy to fix.
2986 QualType Ty = S.Context.getPointerType(VD->getType());
2987 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002988 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2989 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002990 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002991 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2992 Attr.getParameterName() << ParamTy << Ty;
2993 return;
2994 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002995
Michael Han99315932013-01-24 16:46:58 +00002996 D->addAttr(::new (S.Context)
2997 CleanupAttr(Attr.getRange(), S.Context, FD,
2998 Attr.getAttributeSpellingListIndex()));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002999 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewyckya096b142013-02-12 08:08:54 +00003000 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssond277d792009-01-31 01:16:18 +00003001}
3002
Mike Stumpd3bb5572009-07-24 19:02:52 +00003003/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003004/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003005static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003006 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003007 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003008
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003009 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003010 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003011 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003012 return;
3013 }
Chandler Carruth743682b2010-11-16 08:35:43 +00003014
3015 // In C++ the implicit 'this' function parameter also counts, and they are
3016 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003017 bool HasImplicitThisParam = isInstanceMethod(D);
3018 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003019 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00003020
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003021 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003022 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003023 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003024 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3025 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3027 << "format" << 2 << IdxExpr->getSourceRange();
3028 return;
3029 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003030
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003031 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
3032 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3033 << "format" << 2 << IdxExpr->getSourceRange();
3034 return;
3035 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003036
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003037 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003038
Chandler Carruth743682b2010-11-16 08:35:43 +00003039 if (HasImplicitThisParam) {
3040 if (ArgIdx == 0) {
3041 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3042 << "format_arg" << IdxExpr->getSourceRange();
3043 return;
3044 }
3045 ArgIdx--;
3046 }
3047
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003048 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003049 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003050
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003051 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3052 if (not_nsstring_type &&
3053 !isCFStringType(Ty, S.Context) &&
3054 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003055 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003056 // FIXME: Should highlight the actual expression that has the wrong type.
3057 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003058 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003059 << IdxExpr->getSourceRange();
3060 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003061 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003062 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003063 if (!isNSStringType(Ty, S.Context) &&
3064 !isCFStringType(Ty, S.Context) &&
3065 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003066 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003067 // FIXME: Should highlight the actual expression that has the wrong type.
3068 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003069 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003070 << IdxExpr->getSourceRange();
3071 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003072 }
3073
Michael Han99315932013-01-24 16:46:58 +00003074 D->addAttr(::new (S.Context)
3075 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3076 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003077}
3078
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003079enum FormatAttrKind {
3080 CFStringFormat,
3081 NSStringFormat,
3082 StrftimeFormat,
3083 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003084 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003085 InvalidFormat
3086};
3087
3088/// getFormatAttrKind - Map from format attribute names to supported format
3089/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003090static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003091 return llvm::StringSwitch<FormatAttrKind>(Format)
3092 // Check for formats that get handled specially.
3093 .Case("NSString", NSStringFormat)
3094 .Case("CFString", CFStringFormat)
3095 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003096
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003097 // Otherwise, check for supported formats.
3098 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3099 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3100 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003101
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003102 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3103 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003104}
3105
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003106/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003107/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003108static void handleInitPriorityAttr(Sema &S, Decl *D,
3109 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003110 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003111 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3112 return;
3113 }
3114
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003115 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003116 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3117 Attr.setInvalid();
3118 return;
3119 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003120 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003121 if (S.Context.getAsArrayType(T))
3122 T = S.Context.getBaseElementType(T);
3123 if (!T->getAs<RecordType>()) {
3124 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3125 Attr.setInvalid();
3126 return;
3127 }
3128
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003129 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003130 Attr.setInvalid();
3131 return;
3132 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003133 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003134
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003135 llvm::APSInt priority(32);
3136 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3137 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3138 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3139 << "init_priority" << priorityExpr->getSourceRange();
3140 Attr.setInvalid();
3141 return;
3142 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00003143 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003144 if (prioritynum < 101 || prioritynum > 65535) {
3145 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3146 << priorityExpr->getSourceRange();
3147 Attr.setInvalid();
3148 return;
3149 }
Michael Han99315932013-01-24 16:46:58 +00003150 D->addAttr(::new (S.Context)
3151 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3152 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003153}
3154
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003155FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han99315932013-01-24 16:46:58 +00003156 int FormatIdx, int FirstArg,
3157 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003158 // Check whether we already have an equivalent format attribute.
3159 for (specific_attr_iterator<FormatAttr>
3160 i = D->specific_attr_begin<FormatAttr>(),
3161 e = D->specific_attr_end<FormatAttr>();
3162 i != e ; ++i) {
3163 FormatAttr *f = *i;
3164 if (f->getType() == Format &&
3165 f->getFormatIdx() == FormatIdx &&
3166 f->getFirstArg() == FirstArg) {
3167 // If we don't have a valid location for this attribute, adopt the
3168 // location.
3169 if (f->getLocation().isInvalid())
3170 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003171 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003172 }
3173 }
3174
Michael Han99315932013-01-24 16:46:58 +00003175 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3176 AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003177}
3178
Mike Stumpd3bb5572009-07-24 19:02:52 +00003179/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003180/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003181static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003182
Chris Lattner4a927cb2008-06-28 23:36:30 +00003183 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003184 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003185 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003186 return;
3187 }
3188
Chris Lattner4a927cb2008-06-28 23:36:30 +00003189 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003190 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003191 return;
3192 }
3193
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003194 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003195 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003196 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003197 return;
3198 }
3199
Chandler Carruth743682b2010-11-16 08:35:43 +00003200 // In C++ the implicit 'this' function parameter also counts, and they are
3201 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003202 bool HasImplicitThisParam = isInstanceMethod(D);
3203 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003204 unsigned FirstIdx = 1;
3205
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003206 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003207
3208 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003209 if (Format.startswith("__") && Format.endswith("__"))
3210 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003211
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003212 // Check for supported formats.
3213 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003214
3215 if (Kind == IgnoredFormat)
3216 return;
3217
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003218 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003219 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003220 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003221 return;
3222 }
3223
3224 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003225 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003226 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003227 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3228 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003230 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003231 return;
3232 }
3233
3234 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003235 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003236 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003237 return;
3238 }
3239
3240 // FIXME: Do we need to bounds check?
3241 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003242
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003243 if (HasImplicitThisParam) {
3244 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003245 S.Diag(Attr.getLoc(),
3246 diag::err_format_attribute_implicit_this_format_string)
3247 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003248 return;
3249 }
3250 ArgIdx--;
3251 }
Mike Stump11289f42009-09-09 15:08:12 +00003252
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003253 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003254 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003255
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003256 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003257 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003258 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3259 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003260 return;
3261 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003262 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003263 // FIXME: do we need to check if the type is NSString*? What are the
3264 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003265 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003266 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003267 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3268 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003269 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003270 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003271 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003272 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
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 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003276 return;
3277 }
3278
3279 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003280 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003281 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003282 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3283 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003284 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003285 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003286 return;
3287 }
3288
3289 // check if the function is variadic if the 3rd argument non-zero
3290 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003291 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003292 ++NumArgs; // +1 for ...
3293 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003294 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003295 return;
3296 }
3297 }
3298
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003299 // strftime requires FirstArg to be 0 because it doesn't read from any
3300 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003301 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003302 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003303 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3304 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003305 return;
3306 }
3307 // if 0 it disables parameter checking (to use with e.g. va_list)
3308 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003309 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003310 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003311 return;
3312 }
3313
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003314 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3315 Idx.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003316 FirstArg.getZExtValue(),
3317 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003318 if (NewAttr)
3319 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003320}
3321
Chandler Carruthedc2c642011-07-02 00:01:44 +00003322static void handleTransparentUnionAttr(Sema &S, Decl *D,
3323 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003324 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003325 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003326 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003327
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003328
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003329 // Try to find the underlying union declaration.
3330 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003331 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003332 if (TD && TD->getUnderlyingType()->isUnionType())
3333 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3334 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003335 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003336
3337 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003338 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003339 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003340 return;
3341 }
3342
John McCallf937c022011-10-07 06:10:15 +00003343 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003344 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003345 diag::warn_transparent_union_attribute_not_definition);
3346 return;
3347 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003348
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003349 RecordDecl::field_iterator Field = RD->field_begin(),
3350 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003351 if (Field == FieldEnd) {
3352 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3353 return;
3354 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003355
David Blaikie40ed2972012-06-06 20:45:41 +00003356 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003357 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003358 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003359 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003360 diag::warn_transparent_union_attribute_floating)
3361 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003362 return;
3363 }
3364
3365 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3366 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3367 for (; Field != FieldEnd; ++Field) {
3368 QualType FieldType = Field->getType();
3369 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3370 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3371 // Warn if we drop the attribute.
3372 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003373 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003374 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003375 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003376 diag::warn_transparent_union_attribute_field_size_align)
3377 << isSize << Field->getDeclName() << FieldBits;
3378 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003379 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003380 diag::note_transparent_union_first_field_size_align)
3381 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003382 return;
3383 }
3384 }
3385
Michael Han99315932013-01-24 16:46:58 +00003386 RD->addAttr(::new (S.Context)
3387 TransparentUnionAttr(Attr.getRange(), S.Context,
3388 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003389}
3390
Chandler Carruthedc2c642011-07-02 00:01:44 +00003391static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003392 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003393 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003394 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003395
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003396 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003397 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003398
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003399 // Make sure that there is a string literal as the annotation's single
3400 // argument.
3401 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003402 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003403 return;
3404 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003405
3406 // Don't duplicate annotations that are already set.
3407 for (specific_attr_iterator<AnnotateAttr>
3408 i = D->specific_attr_begin<AnnotateAttr>(),
3409 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3410 if ((*i)->getAnnotation() == SE->getString())
3411 return;
3412 }
Michael Han99315932013-01-24 16:46:58 +00003413
3414 D->addAttr(::new (S.Context)
3415 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3416 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003417}
3418
Chandler Carruthedc2c642011-07-02 00:01:44 +00003419static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003420 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003421 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003422 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003423 return;
3424 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003425
Richard Smith848e1f12013-02-01 08:12:08 +00003426 if (Attr.getNumArgs() == 0) {
3427 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3428 true, 0, Attr.getAttributeSpellingListIndex()));
3429 return;
3430 }
3431
Richard Smith44c247f2013-02-22 08:32:16 +00003432 Expr *E = Attr.getArg(0);
3433 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3434 S.Diag(Attr.getEllipsisLoc(),
3435 diag::err_pack_expansion_without_parameter_packs);
3436 return;
3437 }
3438
3439 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3440 return;
3441
3442 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3443 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003444}
3445
3446void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003447 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003448 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3449 SourceLocation AttrLoc = AttrRange.getBegin();
3450
Richard Smith1dba27c2013-01-29 09:02:09 +00003451 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003452 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003453 // C++11 [dcl.align]p1:
3454 // An alignment-specifier may be applied to a variable or to a class
3455 // data member, but it shall not be applied to a bit-field, a function
3456 // parameter, the formal parameter of a catch clause, or a variable
3457 // declared with the register storage class specifier. An
3458 // alignment-specifier may also be applied to the declaration of a class
3459 // or enumeration type.
3460 // C11 6.7.5/2:
3461 // An alignment attribute shall not be specified in a declaration of
3462 // a typedef, or a bit-field, or a function, or a parameter, or an
3463 // object declared with the register storage-class specifier.
3464 int DiagKind = -1;
3465 if (isa<ParmVarDecl>(D)) {
3466 DiagKind = 0;
3467 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3468 if (VD->getStorageClass() == SC_Register)
3469 DiagKind = 1;
3470 if (VD->isExceptionVariable())
3471 DiagKind = 2;
3472 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3473 if (FD->isBitField())
3474 DiagKind = 3;
3475 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003476 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3477 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003478 << (TmpAttr.isC11() ? ExpectedVariableOrField
3479 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003480 return;
3481 }
3482 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003483 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003484 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003485 return;
3486 }
3487 }
3488
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003489 if (E->isTypeDependent() || E->isValueDependent()) {
3490 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003491 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3492 AA->setPackExpansion(IsPackExpansion);
3493 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003494 return;
3495 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003496
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003497 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003498 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003499 ExprResult ICE
3500 = VerifyIntegerConstantExpression(E, &Alignment,
3501 diag::err_aligned_attribute_argument_not_int,
3502 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003503 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003504 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003505
3506 // C++11 [dcl.align]p2:
3507 // -- if the constant expression evaluates to zero, the alignment
3508 // specifier shall have no effect
3509 // C11 6.7.5p6:
3510 // An alignment specification of zero has no effect.
3511 if (!(TmpAttr.isAlignas() && !Alignment) &&
3512 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003513 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3514 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003515 return;
3516 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003517
Richard Smith848e1f12013-02-01 08:12:08 +00003518 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003519 // We've already verified it's a power of 2, now let's make sure it's
3520 // 8192 or less.
3521 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003522 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003523 << E->getSourceRange();
3524 return;
3525 }
3526 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003527
Richard Smith44c247f2013-02-22 08:32:16 +00003528 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3529 ICE.take(), SpellingListIndex);
3530 AA->setPackExpansion(IsPackExpansion);
3531 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003532}
3533
Michael Hanaf02bbe2013-02-01 01:19:17 +00003534void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003535 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003536 // FIXME: Cache the number on the Attr object if non-dependent?
3537 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003538 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3539 SpellingListIndex);
3540 AA->setPackExpansion(IsPackExpansion);
3541 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003542}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003543
Richard Smith848e1f12013-02-01 08:12:08 +00003544void Sema::CheckAlignasUnderalignment(Decl *D) {
3545 assert(D->hasAttrs() && "no attributes on decl");
3546
3547 QualType Ty;
3548 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3549 Ty = VD->getType();
3550 else
3551 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003552 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003553 return;
3554
3555 // C++11 [dcl.align]p5, C11 6.7.5/4:
3556 // The combined effect of all alignment attributes in a declaration shall
3557 // not specify an alignment that is less strict than the alignment that
3558 // would otherwise be required for the entity being declared.
3559 AlignedAttr *AlignasAttr = 0;
3560 unsigned Align = 0;
3561 for (specific_attr_iterator<AlignedAttr>
3562 I = D->specific_attr_begin<AlignedAttr>(),
3563 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3564 if (I->isAlignmentDependent())
3565 return;
3566 if (I->isAlignas())
3567 AlignasAttr = *I;
3568 Align = std::max(Align, I->getAlignment(Context));
3569 }
3570
3571 if (AlignasAttr && Align) {
3572 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3573 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3574 if (NaturalAlign > RequestedAlign)
3575 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3576 << Ty << (unsigned)NaturalAlign.getQuantity();
3577 }
3578}
3579
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003580/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003581/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003582///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003583/// Despite what would be logical, the mode attribute is a decl attribute, not a
3584/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3585/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003586static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003587 // This attribute isn't documented, but glibc uses it. It changes
3588 // the width of an int or unsigned int to the specified size.
3589
3590 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003591 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003592 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003593
Chris Lattneracbc2d22008-06-27 22:18:37 +00003594
3595 IdentifierInfo *Name = Attr.getParameterName();
3596 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003597 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003598 return;
3599 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003600
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003601 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003602
3603 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003604 if (Str.startswith("__") && Str.endswith("__"))
3605 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003606
3607 unsigned DestWidth = 0;
3608 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003609 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003610 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003611 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003612 switch (Str[0]) {
3613 case 'Q': DestWidth = 8; break;
3614 case 'H': DestWidth = 16; break;
3615 case 'S': DestWidth = 32; break;
3616 case 'D': DestWidth = 64; break;
3617 case 'X': DestWidth = 96; break;
3618 case 'T': DestWidth = 128; break;
3619 }
3620 if (Str[1] == 'F') {
3621 IntegerMode = false;
3622 } else if (Str[1] == 'C') {
3623 IntegerMode = false;
3624 ComplexMode = true;
3625 } else if (Str[1] != 'I') {
3626 DestWidth = 0;
3627 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003628 break;
3629 case 4:
3630 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3631 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003632 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003633 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003634 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003635 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003636 break;
3637 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003638 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003639 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003640 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003641 case 11:
3642 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003643 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003644 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003645 }
3646
3647 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003648 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003649 OldTy = TD->getUnderlyingType();
3650 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3651 OldTy = VD->getType();
3652 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003653 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003654 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003655 return;
3656 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003657
John McCall9dd450b2009-09-21 23:43:11 +00003658 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003659 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3660 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003661 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003662 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3663 } else if (ComplexMode) {
3664 if (!OldTy->isComplexType())
3665 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3666 } else {
3667 if (!OldTy->isFloatingType())
3668 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3669 }
3670
Mike Stump87c57ac2009-05-16 07:39:55 +00003671 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3672 // and friends, at least with glibc.
3673 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3674 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003675 // FIXME: Make sure floating-point mappings are accurate
3676 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003677 QualType NewTy;
3678 switch (DestWidth) {
3679 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003680 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003681 return;
3682 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003683 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003684 return;
3685 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003686 if (!IntegerMode) {
3687 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3688 return;
3689 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003690 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003691 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003692 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003693 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003694 break;
3695 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003696 if (!IntegerMode) {
3697 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3698 return;
3699 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003700 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003701 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003702 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003703 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003704 break;
3705 case 32:
3706 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003707 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003708 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003709 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003710 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003711 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003712 break;
3713 case 64:
3714 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003715 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003716 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003717 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003718 NewTy = S.Context.LongTy;
3719 else
3720 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003721 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003722 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003723 NewTy = S.Context.UnsignedLongTy;
3724 else
3725 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003726 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003727 case 96:
3728 NewTy = S.Context.LongDoubleTy;
3729 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003730 case 128:
Roman Divackyac6f5f42013-07-03 21:08:41 +00003731 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3732 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedman1efaaea2009-02-13 02:31:07 +00003733 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3734 return;
3735 }
Roman Divackyb6debb62013-07-03 20:48:06 +00003736 if (IntegerMode) {
3737 if (OldTy->isSignedIntegerType())
3738 NewTy = S.Context.Int128Ty;
3739 else
3740 NewTy = S.Context.UnsignedInt128Ty;
3741 } else
3742 NewTy = S.Context.LongDoubleTy;
Eli Friedman4735374e2009-03-03 06:41:03 +00003743 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003744 }
3745
Eli Friedman4735374e2009-03-03 06:41:03 +00003746 if (ComplexMode) {
3747 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003748 }
3749
3750 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003751 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3752 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3753 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003754 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003755
3756 D->addAttr(::new (S.Context)
3757 ModeAttr(Attr.getRange(), S.Context, Name,
3758 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003759}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003760
Chandler Carruthedc2c642011-07-02 00:01:44 +00003761static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003762 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003763 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003764 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003765
Nick Lewycky08597072012-07-24 01:40:49 +00003766 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3767 if (!VD->hasGlobalStorage())
3768 S.Diag(Attr.getLoc(),
3769 diag::warn_attribute_requires_functions_or_static_globals)
3770 << Attr.getName();
3771 } else if (!isFunctionOrMethod(D)) {
3772 S.Diag(Attr.getLoc(),
3773 diag::warn_attribute_requires_functions_or_static_globals)
3774 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003775 return;
3776 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003777
Michael Han99315932013-01-24 16:46:58 +00003778 D->addAttr(::new (S.Context)
3779 NoDebugAttr(Attr.getRange(), S.Context,
3780 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003781}
3782
Chandler Carruthedc2c642011-07-02 00:01:44 +00003783static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003784 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003785 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003786 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003787
Mike Stumpd3bb5572009-07-24 19:02:52 +00003788
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003789 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003790 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003791 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003792 return;
3793 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003794
Michael Han99315932013-01-24 16:46:58 +00003795 D->addAttr(::new (S.Context)
3796 NoInlineAttr(Attr.getRange(), S.Context,
3797 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003798}
3799
Chandler Carruthedc2c642011-07-02 00:01:44 +00003800static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3801 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003802 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003803 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003804 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003805
Chris Lattner3c77a352010-06-22 00:03:40 +00003806
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003807 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003808 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003809 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003810 return;
3811 }
3812
Michael Han99315932013-01-24 16:46:58 +00003813 D->addAttr(::new (S.Context)
3814 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3815 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003816}
3817
Chandler Carruthedc2c642011-07-02 00:01:44 +00003818static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003819 if (S.LangOpts.CUDA) {
3820 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003821 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003822 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3823 return;
3824 }
3825
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003826 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003827 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003828 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003829 return;
3830 }
3831
Michael Han99315932013-01-24 16:46:58 +00003832 D->addAttr(::new (S.Context)
3833 CUDAConstantAttr(Attr.getRange(), S.Context,
3834 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003835 } else {
3836 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3837 }
3838}
3839
Chandler Carruthedc2c642011-07-02 00:01:44 +00003840static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003841 if (S.LangOpts.CUDA) {
3842 // check the attribute arguments.
3843 if (Attr.getNumArgs() != 0) {
3844 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3845 return;
3846 }
3847
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003848 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003849 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003850 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003851 return;
3852 }
3853
Michael Han99315932013-01-24 16:46:58 +00003854 D->addAttr(::new (S.Context)
3855 CUDADeviceAttr(Attr.getRange(), S.Context,
3856 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003857 } else {
3858 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3859 }
3860}
3861
Chandler Carruthedc2c642011-07-02 00:01:44 +00003862static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003863 if (S.LangOpts.CUDA) {
3864 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003865 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003866 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003867
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003868 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003869 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003870 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003871 return;
3872 }
3873
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003874 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003875 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003876 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003877 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003878 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3879 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003880 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003881 "void");
3882 } else {
3883 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3884 << FD->getType();
3885 }
3886 return;
3887 }
3888
Michael Han99315932013-01-24 16:46:58 +00003889 D->addAttr(::new (S.Context)
3890 CUDAGlobalAttr(Attr.getRange(), S.Context,
3891 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003892 } else {
3893 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3894 }
3895}
3896
Chandler Carruthedc2c642011-07-02 00:01:44 +00003897static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003898 if (S.LangOpts.CUDA) {
3899 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003900 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003901 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003902
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003903
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003904 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003905 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003906 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003907 return;
3908 }
3909
Michael Han99315932013-01-24 16:46:58 +00003910 D->addAttr(::new (S.Context)
3911 CUDAHostAttr(Attr.getRange(), S.Context,
3912 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003913 } else {
3914 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3915 }
3916}
3917
Chandler Carruthedc2c642011-07-02 00:01:44 +00003918static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003919 if (S.LangOpts.CUDA) {
3920 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003921 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003922 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003923
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003924 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003925 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003926 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003927 return;
3928 }
3929
Michael Han99315932013-01-24 16:46:58 +00003930 D->addAttr(::new (S.Context)
3931 CUDASharedAttr(Attr.getRange(), S.Context,
3932 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003933 } else {
3934 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3935 }
3936}
3937
Chandler Carruthedc2c642011-07-02 00:01:44 +00003938static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003939 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003940 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003941 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003942
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003943 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003944 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003946 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003947 return;
3948 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003949
Douglas Gregor35b57532009-10-27 21:01:01 +00003950 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003951 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003952 return;
3953 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003954
Michael Han99315932013-01-24 16:46:58 +00003955 D->addAttr(::new (S.Context)
3956 GNUInlineAttr(Attr.getRange(), S.Context,
3957 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003958}
3959
Chandler Carruthedc2c642011-07-02 00:01:44 +00003960static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003961 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003962
Aaron Ballman02df2e02012-12-09 17:45:41 +00003963 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003964 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003965 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3966 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003967 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003968 return;
3969
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003970 if (!isa<ObjCMethodDecl>(D)) {
3971 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3972 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003973 return;
3974 }
3975
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003976 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003977 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003978 D->addAttr(::new (S.Context)
3979 FastCallAttr(Attr.getRange(), S.Context,
3980 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003981 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003982 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003983 D->addAttr(::new (S.Context)
3984 StdCallAttr(Attr.getRange(), S.Context,
3985 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003986 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003987 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003988 D->addAttr(::new (S.Context)
3989 ThisCallAttr(Attr.getRange(), S.Context,
3990 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003991 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003992 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003993 D->addAttr(::new (S.Context)
3994 CDeclAttr(Attr.getRange(), S.Context,
3995 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003996 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003997 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003998 D->addAttr(::new (S.Context)
3999 PascalAttr(Attr.getRange(), S.Context,
4000 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004001 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004002 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004003 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004004 switch (CC) {
4005 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004006 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004007 break;
4008 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004009 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004010 break;
4011 default:
4012 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004013 }
4014
Michael Han99315932013-01-24 16:46:58 +00004015 D->addAttr(::new (S.Context)
4016 PcsAttr(Attr.getRange(), S.Context, PCS,
4017 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004018 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004019 }
Derek Schuffa2020962012-10-16 22:30:41 +00004020 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00004021 D->addAttr(::new (S.Context)
4022 PnaclCallAttr(Attr.getRange(), S.Context,
4023 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004024 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00004025 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004026 D->addAttr(::new (S.Context)
4027 IntelOclBiccAttr(Attr.getRange(), S.Context,
4028 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004029 return;
Derek Schuffa2020962012-10-16 22:30:41 +00004030
Abramo Bagnara50099372010-04-30 13:10:51 +00004031 default:
4032 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004033 }
4034}
4035
Chandler Carruthedc2c642011-07-02 00:01:44 +00004036static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00004037 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004038 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004039}
4040
Guy Benyeifb36ede2013-03-24 13:58:12 +00004041static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4042 assert(!Attr.isInvalid());
4043
4044 Expr *E = Attr.getArg(0);
4045 llvm::APSInt ArgNum(32);
4046 if (E->isTypeDependent() || E->isValueDependent() ||
4047 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4048 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4049 << Attr.getName()->getName() << E->getSourceRange();
4050 return;
4051 }
4052
4053 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4054 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4055}
4056
Aaron Ballman02df2e02012-12-09 17:45:41 +00004057bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4058 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00004059 if (attr.isInvalid())
4060 return true;
4061
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004062 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4063 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
4064 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00004065 attr.setInvalid();
4066 return true;
4067 }
4068
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004069 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4070 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00004071 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004072 case AttributeList::AT_CDecl: CC = CC_C; break;
4073 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4074 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4075 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4076 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4077 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004078 Expr *Arg = attr.getArg(0);
4079 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004080 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004081 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
4082 << "pcs" << 1;
4083 attr.setInvalid();
4084 return true;
4085 }
4086
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004087 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004088 if (StrRef == "aapcs") {
4089 CC = CC_AAPCS;
4090 break;
4091 } else if (StrRef == "aapcs-vfp") {
4092 CC = CC_AAPCS_VFP;
4093 break;
4094 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004095
4096 attr.setInvalid();
4097 Diag(attr.getLoc(), diag::err_invalid_pcs);
4098 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004099 }
Derek Schuffa2020962012-10-16 22:30:41 +00004100 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00004101 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00004102 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004103 }
4104
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004105 const TargetInfo &TI = Context.getTargetInfo();
4106 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4107 if (A == TargetInfo::CCCR_Warning) {
4108 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00004109
4110 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4111 if (FD)
4112 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4113 TargetInfo::CCMT_NonMember;
4114 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004115 }
4116
John McCall3882ace2011-01-05 12:14:39 +00004117 return false;
4118}
4119
Chandler Carruthedc2c642011-07-02 00:01:44 +00004120static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004121 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00004122
4123 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004124 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00004125 return;
4126
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004127 if (!isa<ObjCMethodDecl>(D)) {
4128 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4129 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004130 return;
4131 }
Eli Friedman7044b762009-03-27 21:06:47 +00004132
Michael Han99315932013-01-24 16:46:58 +00004133 D->addAttr(::new (S.Context)
4134 RegparmAttr(Attr.getRange(), S.Context, numParams,
4135 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00004136}
4137
4138/// Checks a regparm attribute, returning true if it is ill-formed and
4139/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004140bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4141 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004142 return true;
4143
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00004144 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004145 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004146 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004147 }
Eli Friedman7044b762009-03-27 21:06:47 +00004148
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004149 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00004150 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00004151 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00004152 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004153 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00004154 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004155 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004156 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004157 }
4158
Douglas Gregore8bbc122011-09-02 00:18:52 +00004159 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004160 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004161 << 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
John McCall3882ace2011-01-05 12:14:39 +00004166 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00004167 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004168 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004169 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004170 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004171 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004172 }
4173
John McCall3882ace2011-01-05 12:14:39 +00004174 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004175}
4176
Chandler Carruthedc2c642011-07-02 00:01:44 +00004177static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004178 if (S.LangOpts.CUDA) {
4179 // check the attribute arguments.
4180 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004181 // FIXME: 0 is not okay.
4182 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004183 return;
4184 }
4185
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004186 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004187 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004188 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004189 return;
4190 }
4191
4192 Expr *MaxThreadsExpr = Attr.getArg(0);
4193 llvm::APSInt MaxThreads(32);
4194 if (MaxThreadsExpr->isTypeDependent() ||
4195 MaxThreadsExpr->isValueDependent() ||
4196 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4197 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4198 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4199 return;
4200 }
4201
4202 llvm::APSInt MinBlocks(32);
4203 if (Attr.getNumArgs() > 1) {
4204 Expr *MinBlocksExpr = Attr.getArg(1);
4205 if (MinBlocksExpr->isTypeDependent() ||
4206 MinBlocksExpr->isValueDependent() ||
4207 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4209 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4210 return;
4211 }
4212 }
4213
Michael Han99315932013-01-24 16:46:58 +00004214 D->addAttr(::new (S.Context)
4215 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4216 MaxThreads.getZExtValue(),
4217 MinBlocks.getZExtValue(),
4218 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004219 } else {
4220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4221 }
4222}
4223
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004224static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4225 const AttributeList &Attr) {
4226 StringRef AttrName = Attr.getName()->getName();
4227 if (!Attr.getParameterName()) {
4228 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4229 << Attr.getName() << /* arg num = */ 1;
4230 return;
4231 }
4232
4233 if (Attr.getNumArgs() != 2) {
4234 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4235 << /* required args = */ 3;
4236 return;
4237 }
4238
4239 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4240
4241 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4242 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4243 << Attr.getName() << ExpectedFunctionOrMethod;
4244 return;
4245 }
4246
4247 uint64_t ArgumentIdx;
4248 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4249 Attr.getLoc(), 2,
4250 Attr.getArg(0), ArgumentIdx))
4251 return;
4252
4253 uint64_t TypeTagIdx;
4254 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4255 Attr.getLoc(), 3,
4256 Attr.getArg(1), TypeTagIdx))
4257 return;
4258
4259 bool IsPointer = (AttrName == "pointer_with_type_tag");
4260 if (IsPointer) {
4261 // Ensure that buffer has a pointer type.
4262 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4263 if (!BufferTy->isPointerType()) {
4264 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004265 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004266 }
4267 }
4268
Michael Han99315932013-01-24 16:46:58 +00004269 D->addAttr(::new (S.Context)
4270 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4271 ArgumentIdx, TypeTagIdx, IsPointer,
4272 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004273}
4274
4275static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4276 const AttributeList &Attr) {
4277 IdentifierInfo *PointerKind = Attr.getParameterName();
4278 if (!PointerKind) {
4279 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4280 << "type_tag_for_datatype" << 1;
4281 return;
4282 }
4283
4284 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4285
Michael Han99315932013-01-24 16:46:58 +00004286 D->addAttr(::new (S.Context)
4287 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4288 MatchingCType,
4289 Attr.getLayoutCompatible(),
4290 Attr.getMustBeNull(),
4291 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004292}
4293
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004294//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004295// Checker-specific attribute handlers.
4296//===----------------------------------------------------------------------===//
4297
John McCalled433932011-01-25 03:31:58 +00004298static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004299 return type->isDependentType() ||
4300 type->isObjCObjectPointerType() ||
4301 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004302}
4303static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004304 return type->isDependentType() ||
4305 type->isPointerType() ||
4306 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004307}
4308
Chandler Carruthedc2c642011-07-02 00:01:44 +00004309static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004310 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004311 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004312 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004313 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004314 return;
4315 }
4316
4317 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004318 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004319 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4320 cf = false;
4321 } else {
4322 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4323 cf = true;
4324 }
4325
4326 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004327 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004328 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004329 return;
4330 }
4331
4332 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004333 param->addAttr(::new (S.Context)
4334 CFConsumedAttr(Attr.getRange(), S.Context,
4335 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004336 else
Michael Han99315932013-01-24 16:46:58 +00004337 param->addAttr(::new (S.Context)
4338 NSConsumedAttr(Attr.getRange(), S.Context,
4339 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004340}
4341
Chandler Carruthedc2c642011-07-02 00:01:44 +00004342static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4343 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004344 if (!isa<ObjCMethodDecl>(D)) {
4345 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004346 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004347 return;
4348 }
4349
Michael Han99315932013-01-24 16:46:58 +00004350 D->addAttr(::new (S.Context)
4351 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4352 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004353}
4354
Chandler Carruthedc2c642011-07-02 00:01:44 +00004355static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4356 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004357
John McCalled433932011-01-25 03:31:58 +00004358 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004359
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004360 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004361 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004362 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004363 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004364 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004365 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4366 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004367 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004368 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004369 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004370 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004371 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004372 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004373 return;
4374 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004375
John McCalled433932011-01-25 03:31:58 +00004376 bool typeOK;
4377 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004378 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004379 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004380 case AttributeList::AT_NSReturnsAutoreleased:
4381 case AttributeList::AT_NSReturnsRetained:
4382 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004383 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4384 cf = false;
4385 break;
4386
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004387 case AttributeList::AT_CFReturnsRetained:
4388 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004389 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4390 cf = true;
4391 break;
4392 }
4393
4394 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004395 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004396 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004397 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004398 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004399
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004400 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004401 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004402 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004403 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004404 D->addAttr(::new (S.Context)
4405 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4406 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004407 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004408 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004409 D->addAttr(::new (S.Context)
4410 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4411 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004412 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004413 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004414 D->addAttr(::new (S.Context)
4415 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4416 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004417 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004418 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004419 D->addAttr(::new (S.Context)
4420 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4421 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004422 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004423 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004424 D->addAttr(::new (S.Context)
4425 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4426 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004427 return;
4428 };
4429}
4430
John McCallcf166702011-07-22 08:53:00 +00004431static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4432 const AttributeList &attr) {
4433 SourceLocation loc = attr.getLoc();
4434
4435 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4436
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004437 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00004438 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004439 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00004440 return;
4441 }
4442
4443 // Check that the method returns a normal pointer.
4444 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004445
4446 if (!resultType->isReferenceType() &&
4447 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00004448 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4449 << SourceRange(loc)
4450 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4451
4452 // Drop the attribute.
4453 return;
4454 }
4455
Michael Han99315932013-01-24 16:46:58 +00004456 method->addAttr(::new (S.Context)
4457 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4458 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004459}
4460
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004461static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4462 const AttributeList &attr) {
4463 SourceLocation loc = attr.getLoc();
4464 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4465
4466 if (!method) {
4467 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4468 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4469 return;
4470 }
4471 DeclContext *DC = method->getDeclContext();
4472 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4473 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4474 << attr.getName() << 0;
4475 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4476 return;
4477 }
4478 if (method->getMethodFamily() == OMF_dealloc) {
4479 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4480 << attr.getName() << 1;
4481 return;
4482 }
4483
Michael Han99315932013-01-24 16:46:58 +00004484 method->addAttr(::new (S.Context)
4485 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4486 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004487}
4488
John McCall32f5fe12011-09-30 05:12:12 +00004489/// Handle cf_audited_transfer and cf_unknown_transfer.
4490static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4491 if (!isa<FunctionDecl>(D)) {
4492 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004493 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004494 return;
4495 }
4496
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004497 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004498
4499 // Check whether there's a conflicting attribute already present.
4500 Attr *Existing;
4501 if (IsAudited) {
4502 Existing = D->getAttr<CFUnknownTransferAttr>();
4503 } else {
4504 Existing = D->getAttr<CFAuditedTransferAttr>();
4505 }
4506 if (Existing) {
4507 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4508 << A.getName()
4509 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4510 << A.getRange() << Existing->getRange();
4511 return;
4512 }
4513
4514 // All clear; add the attribute.
4515 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004516 D->addAttr(::new (S.Context)
4517 CFAuditedTransferAttr(A.getRange(), S.Context,
4518 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004519 } else {
Michael Han99315932013-01-24 16:46:58 +00004520 D->addAttr(::new (S.Context)
4521 CFUnknownTransferAttr(A.getRange(), S.Context,
4522 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004523 }
4524}
4525
John McCallf1e8b342011-09-29 07:17:38 +00004526static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4527 const AttributeList &Attr) {
4528 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4529 if (!RD || RD->isUnion()) {
4530 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004531 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004532 }
4533
4534 IdentifierInfo *ParmName = Attr.getParameterName();
4535
4536 // In Objective-C, verify that the type names an Objective-C type.
4537 // We don't want to check this outside of ObjC because people sometimes
4538 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004539 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004540 // Check for an existing type with this name.
4541 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4542 Sema::LookupOrdinaryName);
4543 if (S.LookupName(R, Sc)) {
4544 NamedDecl *Target = R.getFoundDecl();
4545 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4546 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4547 S.Diag(Target->getLocStart(), diag::note_declared_at);
4548 }
4549 }
4550 }
4551
Michael Han99315932013-01-24 16:46:58 +00004552 D->addAttr(::new (S.Context)
4553 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4554 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004555}
4556
Chandler Carruthedc2c642011-07-02 00:01:44 +00004557static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4558 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004559 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004560
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004561 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004562 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004563}
4564
Chandler Carruthedc2c642011-07-02 00:01:44 +00004565static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4566 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004567 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
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 return;
4571 }
4572
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004573 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004574 QualType type = vd->getType();
4575
4576 if (!type->isDependentType() &&
4577 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004578 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004579 << type;
4580 return;
4581 }
4582
4583 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4584
4585 // If we have no lifetime yet, check the lifetime we're presumably
4586 // going to infer.
4587 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4588 lifetime = type->getObjCARCImplicitLifetime();
4589
4590 switch (lifetime) {
4591 case Qualifiers::OCL_None:
4592 assert(type->isDependentType() &&
4593 "didn't infer lifetime for non-dependent type?");
4594 break;
4595
4596 case Qualifiers::OCL_Weak: // meaningful
4597 case Qualifiers::OCL_Strong: // meaningful
4598 break;
4599
4600 case Qualifiers::OCL_ExplicitNone:
4601 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004602 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004603 << (lifetime == Qualifiers::OCL_Autoreleasing);
4604 break;
4605 }
4606
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004607 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004608 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4609 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004610}
4611
Francois Picheta83957a2010-12-19 06:50:37 +00004612//===----------------------------------------------------------------------===//
4613// Microsoft specific attribute handlers.
4614//===----------------------------------------------------------------------===//
4615
Reid Kleckner140c4a72013-05-17 14:04:52 +00004616// Check if MS extensions or some other language extensions are enabled. If
4617// not, issue a diagnostic that the given attribute is unused.
4618static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4619 bool OtherExtension = false) {
4620 if (S.LangOpts.MicrosoftExt || OtherExtension)
4621 return true;
4622 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4623 return false;
4624}
4625
Chandler Carruthedc2c642011-07-02 00:01:44 +00004626static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004627 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4628 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004629
Reid Kleckner140c4a72013-05-17 14:04:52 +00004630 // check the attribute arguments.
4631 if (!checkAttributeNumArgs(S, Attr, 1))
4632 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004633
Reid Kleckner140c4a72013-05-17 14:04:52 +00004634 Expr *Arg = Attr.getArg(0);
4635 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4636 if (!Str || !Str->isAscii()) {
4637 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4638 << "uuid" << 1;
4639 return;
4640 }
Francois Pichet7da11662010-12-20 01:41:49 +00004641
Reid Kleckner140c4a72013-05-17 14:04:52 +00004642 StringRef StrRef = Str->getString();
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004643
Reid Kleckner140c4a72013-05-17 14:04:52 +00004644 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4645 StrRef.back() == '}';
Francois Pichet7da11662010-12-20 01:41:49 +00004646
Reid Kleckner140c4a72013-05-17 14:04:52 +00004647 // Validate GUID length.
4648 if (IsCurly && StrRef.size() != 38) {
4649 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4650 return;
4651 }
4652 if (!IsCurly && StrRef.size() != 36) {
4653 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4654 return;
4655 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004656
Reid Kleckner140c4a72013-05-17 14:04:52 +00004657 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4658 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4659 StringRef::iterator I = StrRef.begin();
4660 if (IsCurly) // Skip the optional '{'
4661 ++I;
4662
4663 for (int i = 0; i < 36; ++i) {
4664 if (i == 8 || i == 13 || i == 18 || i == 23) {
4665 if (*I != '-') {
Francois Pichet7da11662010-12-20 01:41:49 +00004666 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4667 return;
4668 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004669 } else if (!isHexDigit(*I)) {
4670 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4671 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004672 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004673 I++;
4674 }
Francois Picheta83957a2010-12-19 06:50:37 +00004675
Reid Kleckner140c4a72013-05-17 14:04:52 +00004676 D->addAttr(::new (S.Context)
4677 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4678 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004679}
4680
John McCall8d32c052012-05-22 21:28:12 +00004681static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004682 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004683 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004684
4685 AttributeList::Kind Kind = Attr.getKind();
4686 if (Kind == AttributeList::AT_SingleInheritance)
4687 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004688 ::new (S.Context)
4689 SingleInheritanceAttr(Attr.getRange(), S.Context,
4690 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004691 else if (Kind == AttributeList::AT_MultipleInheritance)
4692 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004693 ::new (S.Context)
4694 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4695 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004696 else if (Kind == AttributeList::AT_VirtualInheritance)
4697 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004698 ::new (S.Context)
4699 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4700 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004701}
4702
4703static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004704 if (!checkMicrosoftExt(S, Attr))
4705 return;
4706
4707 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballman317a77f2013-05-22 23:25:32 +00004708 if (Kind == AttributeList::AT_Win64)
Reid Kleckner140c4a72013-05-17 14:04:52 +00004709 D->addAttr(
4710 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4711 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004712}
4713
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004714static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004715 if (!checkMicrosoftExt(S, Attr))
4716 return;
4717 D->addAttr(::new (S.Context)
4718 ForceInlineAttr(Attr.getRange(), S.Context,
4719 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004720}
4721
Reid Klecknerb144d362013-05-20 14:02:37 +00004722static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4723 if (!checkMicrosoftExt(S, Attr))
4724 return;
4725 // Check linkage after possibly merging declaratinos. See
4726 // checkAttributesAfterMerging().
4727 D->addAttr(::new (S.Context)
4728 SelectAnyAttr(Attr.getRange(), S.Context,
4729 Attr.getAttributeSpellingListIndex()));
4730}
4731
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004732//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004733// Top Level Sema Entry Points
4734//===----------------------------------------------------------------------===//
4735
Chandler Carruthedc2c642011-07-02 00:01:44 +00004736static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4737 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004738 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004739 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4740 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4741 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004742 default:
4743 break;
4744 }
4745}
Abramo Bagnara50099372010-04-30 13:10:51 +00004746
Chandler Carruthedc2c642011-07-02 00:01:44 +00004747static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4748 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004749 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004750 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4751 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4752 case AttributeList::AT_IBOutletCollection:
4753 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004754 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004755 case AttributeList::AT_ObjCGC:
4756 case AttributeList::AT_VectorSize:
4757 case AttributeList::AT_NeonVectorType:
4758 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004759 case AttributeList::AT_Ptr32:
4760 case AttributeList::AT_Ptr64:
4761 case AttributeList::AT_SPtr:
4762 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004763 // Ignore these, these are type attributes, handled by
4764 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004765 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004766 case AttributeList::AT_CUDADevice:
4767 case AttributeList::AT_CUDAHost:
4768 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004769 // Ignore, this is a non-inheritable attribute, handled
4770 // by ProcessNonInheritableDeclAttr.
4771 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004772 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4773 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4774 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4775 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004776 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004777 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004778 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004779 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004780 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4781 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4782 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004783 handleDependencyAttr(S, scope, D, Attr);
4784 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004785 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4786 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4787 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004788 case AttributeList::AT_CXX11NoReturn:
4789 handleCXX11NoReturnAttr(S, D, Attr);
4790 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004791 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004792 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004793 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004794 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4795 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004796 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004797 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004798 case AttributeList::AT_MinSize:
4799 handleMinSizeAttr(S, D, Attr);
4800 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004801 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4802 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4803 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4804 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4805 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004806 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004807 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004808 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4809 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4810 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4811 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4812 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004813 case AttributeList::AT_ownership_returns:
4814 case AttributeList::AT_ownership_takes:
4815 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004816 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004817 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4818 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4819 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4820 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4821 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4822 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4823 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004824
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004825 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004826 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004827 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004828 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004829
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004830 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004831 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4832
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004833 case AttributeList::AT_ObjCRequiresSuper:
4834 handleObjCRequiresSuperAttr(S, D, Attr); break;
4835
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004836 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004837 handleNSBridgedAttr(S, scope, D, Attr); break;
4838
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004839 case AttributeList::AT_CFAuditedTransfer:
4840 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004841 handleCFTransferAttr(S, D, Attr); break;
4842
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004843 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004844 case AttributeList::AT_CFConsumed:
4845 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4846 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004847 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004848
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004849 case AttributeList::AT_NSReturnsAutoreleased:
4850 case AttributeList::AT_NSReturnsNotRetained:
4851 case AttributeList::AT_CFReturnsNotRetained:
4852 case AttributeList::AT_NSReturnsRetained:
4853 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004854 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004855
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004856 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004857 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004858 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004859
Joey Goulyaba589c2013-03-08 09:42:32 +00004860 case AttributeList::AT_VecTypeHint:
4861 handleVecTypeHint(S, D, Attr); break;
4862
Joey Gouly0608ae82013-03-14 09:54:43 +00004863 case AttributeList::AT_Endian:
4864 handleEndianAttr(S, D, Attr);
4865 break;
4866
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004867 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004868 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004869
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004870 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4871 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4872 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004873 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004874 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004875 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004876 handleArcWeakrefUnavailableAttr (S, D, Attr);
4877 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004878 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004879 handleObjCRootClassAttr(S, D, Attr);
4880 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004881 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004882 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004883 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004884 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4885 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004886 handleReturnsTwiceAttr(S, D, Attr);
4887 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004888 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004889 case AttributeList::AT_Visibility:
4890 handleVisibilityAttr(S, D, Attr, false);
4891 break;
4892 case AttributeList::AT_TypeVisibility:
4893 handleVisibilityAttr(S, D, Attr, true);
4894 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004895 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004896 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004897 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4898 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4899 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4900 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004901 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004902 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004903 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004904 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004905 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004906 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004907 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004908 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004909 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4910 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4911 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4912 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4913 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4914 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4915 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4916 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4917 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004918 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004919 // Just ignore
4920 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004921 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004922 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004923 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004924 case AttributeList::AT_StdCall:
4925 case AttributeList::AT_CDecl:
4926 case AttributeList::AT_FastCall:
4927 case AttributeList::AT_ThisCall:
4928 case AttributeList::AT_Pascal:
4929 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004930 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004931 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004932 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004933 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004934 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004935 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004936 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004937 case AttributeList::AT_OpenCLImageAccess:
4938 handleOpenCLImageAccessAttr(S, D, Attr);
4939 break;
John McCall8d32c052012-05-22 21:28:12 +00004940
4941 // Microsoft attributes:
John McCall5e77d762013-04-16 07:28:30 +00004942 case AttributeList::AT_MsProperty: break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004943 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004944 handleMsStructAttr(S, D, Attr);
4945 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004946 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004947 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004948 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004949 case AttributeList::AT_SingleInheritance:
4950 case AttributeList::AT_MultipleInheritance:
4951 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004952 handleInheritanceAttr(S, D, Attr);
4953 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004954 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004955 handlePortabilityAttr(S, D, Attr);
4956 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004957 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004958 handleForceInlineAttr(S, D, Attr);
4959 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004960 case AttributeList::AT_SelectAny:
4961 handleSelectAnyAttr(S, D, Attr);
4962 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004963
4964 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004965 case AttributeList::AT_AssertExclusiveLock:
4966 handleAssertExclusiveLockAttr(S, D, Attr);
4967 break;
4968 case AttributeList::AT_AssertSharedLock:
4969 handleAssertSharedLockAttr(S, D, Attr);
4970 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004971 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004972 handleGuardedVarAttr(S, D, Attr);
4973 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004974 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004975 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004976 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004977 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004978 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004979 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004980 case AttributeList::AT_NoSanitizeAddress:
4981 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004982 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004983 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004984 handleNoThreadSafetyAnalysis(S, D, Attr);
4985 break;
4986 case AttributeList::AT_NoSanitizeThread:
4987 handleNoSanitizeThread(S, D, Attr);
4988 break;
4989 case AttributeList::AT_NoSanitizeMemory:
4990 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004991 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004992 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004993 handleLockableAttr(S, D, Attr);
4994 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004995 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004996 handleGuardedByAttr(S, D, Attr);
4997 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004998 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004999 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005000 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005001 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005002 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005003 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005004 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00005005 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005006 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005007 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005008 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005009 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005010 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005011 handleLockReturnedAttr(S, D, Attr);
5012 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005013 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005014 handleLocksExcludedAttr(S, D, Attr);
5015 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005016 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005017 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005018 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005019 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00005020 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005021 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005022 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005023 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005024 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005025 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005026 handleUnlockFunAttr(S, D, Attr);
5027 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005028 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005029 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005030 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005031 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005032 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005033 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005034
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005035 // Type safety attributes.
5036 case AttributeList::AT_ArgumentWithTypeTag:
5037 handleArgumentWithTypeTagAttr(S, D, Attr);
5038 break;
5039 case AttributeList::AT_TypeTagForDatatype:
5040 handleTypeTagForDatatypeAttr(S, D, Attr);
5041 break;
5042
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005043 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005044 // Ask target about the attribute.
5045 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5046 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00005047 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5048 diag::warn_unhandled_ms_attribute_ignored :
5049 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005050 break;
5051 }
5052}
5053
Peter Collingbourneb331b262011-01-21 02:08:45 +00005054/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5055/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smith10876ef2013-01-17 01:30:42 +00005056/// silently ignore it if a GNU attribute.
Chandler Carruthedc2c642011-07-02 00:01:44 +00005057static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5058 const AttributeList &Attr,
Richard Smith10876ef2013-01-17 01:30:42 +00005059 bool NonInheritable, bool Inheritable,
5060 bool IncludeCXX11Attributes) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00005061 if (Attr.isInvalid())
5062 return;
5063
Richard Smith10876ef2013-01-17 01:30:42 +00005064 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5065 // instead.
5066 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5067 return;
5068
Peter Collingbourneb331b262011-01-21 02:08:45 +00005069 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00005070 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00005071
5072 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00005073 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00005074}
5075
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005076/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5077/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005078void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005079 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005080 bool NonInheritable, bool Inheritable,
5081 bool IncludeCXX11Attributes) {
5082 for (const AttributeList* l = AttrList; l; l = l->getNext())
5083 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5084 IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005085
5086 // GCC accepts
5087 // static int a9 __attribute__((weakref));
5088 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005089 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00005090 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00005091 cast<NamedDecl>(D)->getNameAsString();
5092 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005093 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005094 }
5095}
5096
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005097// Annotation attributes are the only attributes allowed after an access
5098// specifier.
5099bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5100 const AttributeList *AttrList) {
5101 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005102 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005103 handleAnnotateAttr(*this, ASDecl, *l);
5104 } else {
5105 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5106 return true;
5107 }
5108 }
5109
5110 return false;
5111}
5112
John McCall42856de2011-10-01 05:17:03 +00005113/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5114/// contains any decl attributes that we should warn about.
5115static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5116 for ( ; A; A = A->getNext()) {
5117 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005118 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005119 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5120
5121 if (A->getKind() == AttributeList::UnknownAttribute) {
5122 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5123 << A->getName() << A->getRange();
5124 } else {
5125 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5126 << A->getName() << A->getRange();
5127 }
5128 }
5129}
5130
5131/// checkUnusedDeclAttributes - Given a declarator which is not being
5132/// used to build a declaration, complain about any decl attributes
5133/// which might be lying around on it.
5134void Sema::checkUnusedDeclAttributes(Declarator &D) {
5135 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5136 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5137 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5138 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5139}
5140
Ryan Flynn7d470f32009-07-30 03:15:39 +00005141/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005142/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005143NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5144 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005145 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005146 NamedDecl *NewD = 0;
5147 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005148 FunctionDecl *NewFD;
5149 // FIXME: Missing call to CheckFunctionDeclaration().
5150 // FIXME: Mangling?
5151 // FIXME: Is the qualifier info correct?
5152 // FIXME: Is the DeclContext correct?
5153 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5154 Loc, Loc, DeclarationName(II),
5155 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005156 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005157 FD->hasPrototype(),
5158 false/*isConstexprSpecified*/);
5159 NewD = NewFD;
5160
5161 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005162 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005163
5164 // Fake up parameter variables; they are declared as if this were
5165 // a typedef.
5166 QualType FDTy = FD->getType();
5167 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5168 SmallVector<ParmVarDecl*, 16> Params;
5169 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5170 AE = FT->arg_type_end(); AI != AE; ++AI) {
5171 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5172 Param->setScopeInfo(0, Params.size());
5173 Params.push_back(Param);
5174 }
David Blaikie9c70e042011-09-21 18:16:56 +00005175 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005176 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005177 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5178 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005179 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005180 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005181 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005182 if (VD->getQualifier()) {
5183 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005184 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005185 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005186 }
5187 return NewD;
5188}
5189
James Dennett634962f2012-06-14 21:40:34 +00005190/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005191/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005192void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005193 if (W.getUsed()) return; // only do this once
5194 W.setUsed(true);
5195 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5196 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005197 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005198 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5199 NDId->getName()));
5200 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005201 WeakTopLevelDecl.push_back(NewD);
5202 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5203 // to insert Decl at TU scope, sorry.
5204 DeclContext *SavedContext = CurContext;
5205 CurContext = Context.getTranslationUnitDecl();
5206 PushOnScopeChains(NewD, S);
5207 CurContext = SavedContext;
5208 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005209 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005210 }
5211}
5212
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005213void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5214 // It's valid to "forward-declare" #pragma weak, in which case we
5215 // have to do this.
5216 LoadExternalWeakUndeclaredIdentifiers();
5217 if (!WeakUndeclaredIdentifiers.empty()) {
5218 NamedDecl *ND = NULL;
5219 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5220 if (VD->isExternC())
5221 ND = VD;
5222 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5223 if (FD->isExternC())
5224 ND = FD;
5225 if (ND) {
5226 if (IdentifierInfo *Id = ND->getIdentifier()) {
5227 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5228 = WeakUndeclaredIdentifiers.find(Id);
5229 if (I != WeakUndeclaredIdentifiers.end()) {
5230 WeakInfo W = I->second;
5231 DeclApplyPragmaWeak(S, ND, W);
5232 WeakUndeclaredIdentifiers[Id] = W;
5233 }
5234 }
5235 }
5236 }
5237}
5238
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005239/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5240/// it, apply them to D. This is a bit tricky because PD can have attributes
5241/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005242void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5243 bool NonInheritable, bool Inheritable) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005244 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005245 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005246 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005247
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005248 // Walk the declarator structure, applying decl attributes that were in a type
5249 // position to the decl itself. This handles cases like:
5250 // int *__attr__(x)** D;
5251 // when X is a decl attribute.
5252 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5253 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith10876ef2013-01-17 01:30:42 +00005254 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5255 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005256
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005257 // Finally, apply any attributes on the decl itself.
5258 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005259 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005260}
John McCall28a6aea2009-11-04 02:18:39 +00005261
John McCall31168b02011-06-15 23:02:42 +00005262/// Is the given declaration allowed to use a forbidden type?
5263static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5264 // Private ivars are always okay. Unfortunately, people don't
5265 // always properly make their ivars private, even in system headers.
5266 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005267 // Function declarations in sys headers will be marked unavailable.
5268 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5269 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005270 return false;
5271
5272 // Require it to be declared in a system header.
5273 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5274}
5275
5276/// Handle a delayed forbidden-type diagnostic.
5277static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5278 Decl *decl) {
5279 if (decl && isForbiddenTypeAllowed(S, decl)) {
5280 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5281 "this system declaration uses an unsupported type"));
5282 return;
5283 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005284 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005285 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005286 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005287 // kind of forbidden type messages on unavailable functions.
5288 if (FD->hasAttr<UnavailableAttr>() &&
5289 diag.getForbiddenTypeDiagnostic() ==
5290 diag::err_arc_array_param_no_ownership) {
5291 diag.Triggered = true;
5292 return;
5293 }
5294 }
John McCall31168b02011-06-15 23:02:42 +00005295
5296 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5297 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5298 diag.Triggered = true;
5299}
5300
John McCall2ec85372012-05-07 06:16:41 +00005301void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5302 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005303 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005304 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005305
John McCall2ec85372012-05-07 06:16:41 +00005306 // When delaying diagnostics to run in the context of a parsed
5307 // declaration, we only want to actually emit anything if parsing
5308 // succeeds.
5309 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005310
John McCall2ec85372012-05-07 06:16:41 +00005311 // We emit all the active diagnostics in this pool or any of its
5312 // parents. In general, we'll get one pool for the decl spec
5313 // and a child pool for each declarator; in a decl group like:
5314 // deprecated_typedef foo, *bar, baz();
5315 // only the declarator pops will be passed decls. This is correct;
5316 // we really do need to consider delayed diagnostics from the decl spec
5317 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005318 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005319 do {
John McCall6347b682012-05-07 06:16:58 +00005320 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005321 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5322 // This const_cast is a bit lame. Really, Triggered should be mutable.
5323 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005324 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005325 continue;
5326
John McCallc1465822011-02-14 07:13:47 +00005327 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005328 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005329 // Don't bother giving deprecation diagnostics if the decl is invalid.
5330 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005331 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005332 break;
5333
5334 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005335 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005336 break;
John McCall31168b02011-06-15 23:02:42 +00005337
5338 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005339 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005340 break;
John McCall86121512010-01-27 03:50:35 +00005341 }
5342 }
John McCall2ec85372012-05-07 06:16:41 +00005343 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005344}
5345
John McCall6347b682012-05-07 06:16:58 +00005346/// Given a set of delayed diagnostics, re-emit them as if they had
5347/// been delayed in the current context instead of in the given pool.
5348/// Essentially, this just moves them to the current pool.
5349void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5350 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5351 assert(curPool && "re-emitting in undelayed context not supported");
5352 curPool->steal(pool);
5353}
5354
John McCall28a6aea2009-11-04 02:18:39 +00005355static bool isDeclDeprecated(Decl *D) {
5356 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005357 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005358 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005359 // A category implicitly has the availability of the interface.
5360 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5361 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005362 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5363 return false;
5364}
5365
Eli Friedman971bfa12012-08-08 21:52:41 +00005366static void
5367DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5368 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005369 const ObjCInterfaceDecl *UnknownObjCClass,
5370 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005371 DeclarationName Name = D->getDeclName();
5372 if (!Message.empty()) {
5373 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5374 S.Diag(D->getLocation(),
5375 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5376 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005377 if (ObjCPropery)
5378 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5379 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005380 } else if (!UnknownObjCClass) {
5381 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5382 S.Diag(D->getLocation(),
5383 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5384 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005385 if (ObjCPropery)
5386 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5387 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005388 } else {
5389 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5390 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5391 }
5392}
5393
John McCallb45a1e72010-08-26 02:13:20 +00005394void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005395 Decl *Ctx) {
5396 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005397 return;
5398
John McCall86121512010-01-27 03:50:35 +00005399 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005400 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5401 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005402 DD.getUnknownObjCClass(),
5403 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005404}
5405
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005406void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005407 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005408 const ObjCInterfaceDecl *UnknownObjCClass,
5409 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005410 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005411 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005412 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5413 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005414 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005415 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005416 return;
5417 }
5418
5419 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005420 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005421 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005422 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005423}