blob: 00379b604ec33612c5558c5c9c1fe1bf7a8f3864 [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
Aaron Ballman29982272013-07-23 14:03:57 +000059/// These constants match the enumerated choices of
60/// err_attribute_argument_n_type.
61enum AttributeArgumentNType {
62 ArgumentIntOrBool,
63 ArgumentIntegerConstant,
64 ArgumentString,
65 ArgumentIdentifier
66};
67
Chris Lattner58418ff2008-06-29 00:16:31 +000068//===----------------------------------------------------------------------===//
69// Helper functions
70//===----------------------------------------------------------------------===//
71
Chandler Carruthff4c4f02011-07-01 23:49:12 +000072static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000073 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000074 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000075 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000076 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000077 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000078 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000080 Ty = decl->getUnderlyingType();
81 else
82 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000083
Chris Lattner2c6fcf52008-06-26 18:38:35 +000084 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000085 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000086 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000087 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000088
John McCall9dd450b2009-09-21 23:43:11 +000089 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000090}
91
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000092// FIXME: We should provide an abstraction around a method or function
93// to provide the following bits of information.
94
Nuno Lopes518e3702009-12-20 23:11:08 +000095/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000096/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000097static bool isFunction(const Decl *D) {
98 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000099}
100
101/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000102/// type (function or function-typed variable) or an Objective-C
103/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000104static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +0000105 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000106}
107
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000108/// isFunctionOrMethodOrBlock - Return true if the given decl has function
109/// type (function or function-typed variable) or an Objective-C
110/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000111static bool isFunctionOrMethodOrBlock(const Decl *D) {
112 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000113 return true;
114 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000116 QualType Ty = V->getType();
117 return Ty->isBlockPointerType();
118 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000119 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000120}
121
John McCall3882ace2011-01-05 12:14:39 +0000122/// Return true if the given decl has a declarator that should have
123/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000124static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000125 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000126 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
127 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000128}
129
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000130/// hasFunctionProto - Return true if the given decl has a argument
131/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000132/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000133static bool hasFunctionProto(const Decl *D) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000135 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000136 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000138 return true;
139 }
140}
141
142/// getFunctionOrMethodNumArgs - Return number of function or method
143/// arguments. It is an error to call this on a K&R function (use
144/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000145static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
146 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000147 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000148 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000149 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000150 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000151}
152
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000153static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
154 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000155 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000156 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000157 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000158
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000159 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000160}
161
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000162static QualType getFunctionOrMethodResultType(const Decl *D) {
163 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000164 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000165 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000166}
167
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000168static bool isFunctionOrMethodVariadic(const Decl *D) {
169 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000170 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000171 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000172 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000173 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000174 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000175 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000176 }
177}
178
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000179static bool isInstanceMethod(const Decl *D) {
180 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000181 return MethodDecl->isInstance();
182 return false;
183}
184
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000185static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000186 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000187 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000188 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000189
John McCall96fa4842010-05-17 21:00:27 +0000190 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
191 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000192 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000193
John McCall96fa4842010-05-17 21:00:27 +0000194 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000195
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000196 // FIXME: Should we walk the chain of classes?
197 return ClsName == &Ctx.Idents.get("NSString") ||
198 ClsName == &Ctx.Idents.get("NSMutableString");
199}
200
Daniel Dunbar980c6692008-09-26 03:32:58 +0000201static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000202 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000203 if (!PT)
204 return false;
205
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000206 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000207 if (!RT)
208 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000209
Daniel Dunbar980c6692008-09-26 03:32:58 +0000210 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000211 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000212 return false;
213
214 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
215}
216
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000217/// \brief Check if the attribute has exactly as many args as Num. May
218/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000219static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
220 unsigned int Num) {
221 if (Attr.getNumArgs() != Num) {
222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
223 return false;
224 }
225
226 return true;
227}
228
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000229
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000230/// \brief Check if the attribute has at least as many args as Num. May
231/// output an error.
232static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
233 unsigned int Num) {
234 if (Attr.getNumArgs() < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000235 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
236 return false;
237 }
238
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000239 return true;
240}
241
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000242/// \brief Check if IdxExpr is a valid argument index for a function or
243/// instance method D. May output an error.
244///
245/// \returns true if IdxExpr is a valid index.
246static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
247 StringRef AttrName,
248 SourceLocation AttrLoc,
249 unsigned AttrArgNum,
250 const Expr *IdxExpr,
251 uint64_t &Idx)
252{
253 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
254
255 // In C++ the implicit 'this' function parameter also counts.
256 // Parameters are counted from one.
257 const bool HasImplicitThisParam = isInstanceMethod(D);
258 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
259 const unsigned FirstIdx = 1;
260
261 llvm::APSInt IdxInt;
262 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
263 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +0000264 std::string Name = std::string("'") + AttrName.str() + std::string("'");
265 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
266 << AttrArgNum << ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000267 return false;
268 }
269
270 Idx = IdxInt.getLimitedValue();
271 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
272 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
273 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
274 return false;
275 }
276 Idx--; // Convert to zero-based.
277 if (HasImplicitThisParam) {
278 if (Idx == 0) {
279 S.Diag(AttrLoc,
280 diag::err_attribute_invalid_implicit_this_argument)
281 << AttrName << IdxExpr->getSourceRange();
282 return false;
283 }
284 --Idx;
285 }
286
287 return true;
288}
289
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000290///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000291/// \brief Check if passed in Decl is a field or potentially shared global var
292/// \return true if the Decl is a field or potentially shared global variable
293///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000294static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000295 if (isa<FieldDecl>(D))
296 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000297 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smithfd3834f2013-04-13 02:43:54 +0000298 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000299
300 return false;
301}
302
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000303/// \brief Check if the passed-in expression is of type int or bool.
304static bool isIntOrBool(Expr *Exp) {
305 QualType QT = Exp->getType();
306 return QT->isBooleanType() || QT->isIntegerType();
307}
308
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000309
310// Check to see if the type is a smart pointer of some kind. We assume
311// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000312static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
313 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
314 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000315 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000316 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000317
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000318 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
319 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000320 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000321 return false;
322
323 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000324}
325
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000326/// \brief Check if passed in Decl is a pointer type.
327/// Note that this function may produce an error message.
328/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000329static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
330 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000331 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000332 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000333 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000334 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000335
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000336 if (const RecordType *RT = QT->getAs<RecordType>()) {
337 // If it's an incomplete type, it could be a smart pointer; skip it.
338 // (We don't want to force template instantiation if we can avoid it,
339 // since that would alter the order in which templates are instantiated.)
340 if (RT->isIncompleteType())
341 return true;
342
343 if (threadSafetyCheckIsSmartPointer(S, RT))
344 return true;
345 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000346
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000347 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000348 << Attr.getName()->getName() << QT;
349 } else {
350 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
351 << Attr.getName();
352 }
353 return false;
354}
355
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000356/// \brief Checks that the passed in QualType either is of RecordType or points
357/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000358static const RecordType *getRecordType(QualType QT) {
359 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000360 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000361
362 // Now check if we point to record type.
363 if (const PointerType *PT = QT->getAs<PointerType>())
364 return PT->getPointeeType()->getAs<RecordType>();
365
366 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000367}
368
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000369
Jordy Rose740b0c22012-05-08 03:27:22 +0000370static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
371 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000372 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
373 if (RT->getDecl()->getAttr<LockableAttr>())
374 return true;
375 return false;
376}
377
378
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000379/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000380/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000381static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
382 QualType Ty) {
383 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000384
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000385 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000386 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000387 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000388 << Attr.getName() << Ty.getAsString();
389 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000390 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000391
Michael Hana9171bc2012-08-03 17:40:43 +0000392 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000393 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000394 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000395
396 // Allow smart pointers to be used as lockable objects.
397 // FIXME -- Check the type that the smart pointer points to.
398 if (threadSafetyCheckIsSmartPointer(S, RT))
399 return;
400
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000401 // Check if the type is lockable.
402 RecordDecl *RD = RT->getDecl();
403 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000404 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000405
406 // Else check if any base classes are lockable.
407 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
408 CXXBasePaths BPaths(false, false);
409 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
410 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000411 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000412
413 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
414 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000415}
416
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000417/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000418/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000419/// \param Sidx The attribute argument index to start checking with.
420/// \param ParamIdxOk Whether an argument can be indexing into a function
421/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000422static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000423 const AttributeList &Attr,
424 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000425 int Sidx = 0,
426 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000427 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000428 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000429
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000430 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000431 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000432 Args.push_back(ArgExp);
433 continue;
434 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000435
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000436 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000437 if (StrLit->getLength() == 0 ||
438 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000439 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000440 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000441 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000442 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000443 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000444
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000445 // We allow constant strings to be used as a placeholder for expressions
446 // that are not valid C++ syntax, but warn that they are ignored.
447 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
448 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000449 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000450 continue;
451 }
452
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000453 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000454
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000455 // A pointer to member expression of the form &MyClass::mu is treated
456 // specially -- we need to look at the type of the member.
457 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
458 if (UOp->getOpcode() == UO_AddrOf)
459 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
460 if (DRE->getDecl()->isCXXInstanceMember())
461 ArgTy = DRE->getDecl()->getType();
462
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463 // First see if we can just cast to record type, or point to record type.
464 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000465
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000466 // Now check if we index into a record type function param.
467 if(!RT && ParamIdxOk) {
468 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000469 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
470 if(FD && IL) {
471 unsigned int NumParams = FD->getNumParams();
472 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000473 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
474 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
475 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000476 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
477 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000478 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000479 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000480 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000481 }
482 }
483
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000484 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000485
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000486 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000487 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000488}
489
Chris Lattner58418ff2008-06-29 00:16:31 +0000490//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000491// Attribute Implementations
492//===----------------------------------------------------------------------===//
493
Daniel Dunbar032db472008-07-31 22:40:48 +0000494// FIXME: All this manual attribute parsing code is gross. At the
495// least add some helper functions to check most argument patterns (#
496// and types of args).
497
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000498enum ThreadAttributeDeclKind {
499 ThreadExpectedFieldOrGlobalVar,
500 ThreadExpectedFunctionOrMethod,
501 ThreadExpectedClassOrStruct
502};
503
Michael Hana9171bc2012-08-03 17:40:43 +0000504static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000505 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000506 assert(!Attr.isInvalid());
507
508 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000509 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000510
511 // D must be either a member field or global (potentially shared) variable.
512 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000513 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
514 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000515 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000516 }
517
Michael Han3be3b442012-07-23 18:48:41 +0000518 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000519}
520
Michael Han3be3b442012-07-23 18:48:41 +0000521static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
522 if (!checkGuardedVarAttrCommon(S, D, Attr))
523 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000524
Michael Han99315932013-01-24 16:46:58 +0000525 D->addAttr(::new (S.Context)
526 GuardedVarAttr(Attr.getRange(), S.Context,
527 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000528}
529
Michael Hana9171bc2012-08-03 17:40:43 +0000530static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000531 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000532 if (!checkGuardedVarAttrCommon(S, D, Attr))
533 return;
534
535 if (!threadSafetyCheckIsPointer(S, D, Attr))
536 return;
537
Michael Han99315932013-01-24 16:46:58 +0000538 D->addAttr(::new (S.Context)
539 PtGuardedVarAttr(Attr.getRange(), S.Context,
540 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000541}
542
Michael Hana9171bc2012-08-03 17:40:43 +0000543static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
544 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000545 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000546 assert(!Attr.isInvalid());
547
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000548 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000549 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000550
551 // D must be either a member field or global (potentially shared) variable.
552 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000553 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
554 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000555 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000556 }
557
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000558 SmallVector<Expr*, 1> Args;
559 // check that all arguments are lockable objects
560 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
561 unsigned Size = Args.size();
562 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000563 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000564
Michael Han3be3b442012-07-23 18:48:41 +0000565 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000566
Michael Han3be3b442012-07-23 18:48:41 +0000567 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000568}
569
Michael Han3be3b442012-07-23 18:48:41 +0000570static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
571 Expr *Arg = 0;
572 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
573 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000574
Michael Han3be3b442012-07-23 18:48:41 +0000575 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
576}
577
Michael Hana9171bc2012-08-03 17:40:43 +0000578static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000579 const AttributeList &Attr) {
580 Expr *Arg = 0;
581 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
582 return;
583
584 if (!threadSafetyCheckIsPointer(S, D, Attr))
585 return;
586
587 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
588 S.Context, Arg));
589}
590
Michael Hana9171bc2012-08-03 17:40:43 +0000591static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000592 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000593 assert(!Attr.isInvalid());
594
595 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000596 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000597
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000598 // FIXME: Lockable structs for C code.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000599 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000600 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
601 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Han3be3b442012-07-23 18:48:41 +0000602 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000603 }
604
Michael Han3be3b442012-07-23 18:48:41 +0000605 return true;
606}
607
608static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
609 if (!checkLockableAttrCommon(S, D, Attr))
610 return;
611
612 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
613}
614
Michael Hana9171bc2012-08-03 17:40:43 +0000615static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000616 const AttributeList &Attr) {
617 if (!checkLockableAttrCommon(S, D, Attr))
618 return;
619
Michael Han99315932013-01-24 16:46:58 +0000620 D->addAttr(::new (S.Context)
621 ScopedLockableAttr(Attr.getRange(), S.Context,
622 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000623}
624
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000625static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
626 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000627 assert(!Attr.isInvalid());
628
629 if (!checkAttributeNumArgs(S, Attr, 0))
630 return;
631
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000632 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000633 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
634 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000635 return;
636 }
637
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000638 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000639 S.Context));
640}
641
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000642static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000643 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000644 assert(!Attr.isInvalid());
645
646 if (!checkAttributeNumArgs(S, Attr, 0))
647 return;
648
649 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000650 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000651 << Attr.getName() << ExpectedFunctionOrMethod;
652 return;
653 }
654
Michael Han99315932013-01-24 16:46:58 +0000655 D->addAttr(::new (S.Context)
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000656 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
657 Attr.getAttributeSpellingListIndex()));
658}
659
660static void handleNoSanitizeMemory(Sema &S, Decl *D,
661 const AttributeList &Attr) {
662 assert(!Attr.isInvalid());
663
664 if (!checkAttributeNumArgs(S, Attr, 0))
665 return;
666
667 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
669 << Attr.getName() << ExpectedFunctionOrMethod;
670 return;
671 }
672
673 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
674 S.Context));
675}
676
677static void handleNoSanitizeThread(Sema &S, Decl *D,
678 const AttributeList &Attr) {
679 assert(!Attr.isInvalid());
680
681 if (!checkAttributeNumArgs(S, Attr, 0))
682 return;
683
684 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
686 << Attr.getName() << ExpectedFunctionOrMethod;
687 return;
688 }
689
690 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
691 S.Context));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000692}
693
Michael Hana9171bc2012-08-03 17:40:43 +0000694static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
695 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000696 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000697 assert(!Attr.isInvalid());
698
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000699 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000700 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000701
702 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000703 ValueDecl *VD = dyn_cast<ValueDecl>(D);
704 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000705 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
706 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000707 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000708 }
709
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000710 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000711 QualType QT = VD->getType();
712 if (!QT->isDependentType()) {
713 const RecordType *RT = getRecordType(QT);
714 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000715 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000716 << Attr.getName();
717 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000718 }
719 }
720
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000721 // Check that all arguments are lockable objects.
722 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000723 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000724 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000725
Michael Han3be3b442012-07-23 18:48:41 +0000726 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000727}
728
Michael Hana9171bc2012-08-03 17:40:43 +0000729static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000730 const AttributeList &Attr) {
731 SmallVector<Expr*, 1> Args;
732 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
733 return;
734
735 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000736 D->addAttr(::new (S.Context)
737 AcquiredAfterAttr(Attr.getRange(), S.Context,
738 StartArg, Args.size(),
739 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000740}
741
Michael Hana9171bc2012-08-03 17:40:43 +0000742static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000743 const AttributeList &Attr) {
744 SmallVector<Expr*, 1> Args;
745 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
746 return;
747
748 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000749 D->addAttr(::new (S.Context)
750 AcquiredBeforeAttr(Attr.getRange(), S.Context,
751 StartArg, Args.size(),
752 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000753}
754
Michael Hana9171bc2012-08-03 17:40:43 +0000755static bool checkLockFunAttrCommon(Sema &S, Decl *D,
756 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000757 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000758 assert(!Attr.isInvalid());
759
760 // zero or more arguments ok
761
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000762 // check that the attribute is applied to a function
763 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000764 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
765 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000766 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000767 }
768
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000769 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000770 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000771
Michael Han3be3b442012-07-23 18:48:41 +0000772 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000773}
774
Michael Hana9171bc2012-08-03 17:40:43 +0000775static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000776 const AttributeList &Attr) {
777 SmallVector<Expr*, 1> Args;
778 if (!checkLockFunAttrCommon(S, D, Attr, Args))
779 return;
780
781 unsigned Size = Args.size();
782 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000783 D->addAttr(::new (S.Context)
784 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
785 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000786}
787
Michael Hana9171bc2012-08-03 17:40:43 +0000788static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000789 const AttributeList &Attr) {
790 SmallVector<Expr*, 1> Args;
791 if (!checkLockFunAttrCommon(S, D, Attr, Args))
792 return;
793
794 unsigned Size = Args.size();
795 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000796 D->addAttr(::new (S.Context)
797 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
798 StartArg, Size,
799 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000800}
801
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000802static void handleAssertSharedLockAttr(Sema &S, Decl *D,
803 const AttributeList &Attr) {
804 SmallVector<Expr*, 1> Args;
805 if (!checkLockFunAttrCommon(S, D, Attr, Args))
806 return;
807
808 unsigned Size = Args.size();
809 Expr **StartArg = Size == 0 ? 0 : &Args[0];
810 D->addAttr(::new (S.Context)
811 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
812 Attr.getAttributeSpellingListIndex()));
813}
814
815static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
816 const AttributeList &Attr) {
817 SmallVector<Expr*, 1> Args;
818 if (!checkLockFunAttrCommon(S, D, Attr, Args))
819 return;
820
821 unsigned Size = Args.size();
822 Expr **StartArg = Size == 0 ? 0 : &Args[0];
823 D->addAttr(::new (S.Context)
824 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
825 StartArg, Size,
826 Attr.getAttributeSpellingListIndex()));
827}
828
829
Michael Hana9171bc2012-08-03 17:40:43 +0000830static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
831 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000832 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000833 assert(!Attr.isInvalid());
834
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000835 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000836 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000837
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000838 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000839 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
840 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000841 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000842 }
843
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000844 if (!isIntOrBool(Attr.getArg(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000845 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
846 << Attr.getName() << 1 << ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000847 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000848 }
849
850 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000851 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000852
Michael Han3be3b442012-07-23 18:48:41 +0000853 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000854}
855
Michael Hana9171bc2012-08-03 17:40:43 +0000856static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000857 const AttributeList &Attr) {
858 SmallVector<Expr*, 2> Args;
859 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
860 return;
861
862 unsigned Size = Args.size();
863 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000864 D->addAttr(::new (S.Context)
865 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
866 Attr.getArg(0), StartArg, Size,
867 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000868}
869
Michael Hana9171bc2012-08-03 17:40:43 +0000870static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000871 const AttributeList &Attr) {
872 SmallVector<Expr*, 2> Args;
873 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
874 return;
875
876 unsigned Size = Args.size();
877 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000878 D->addAttr(::new (S.Context)
879 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
880 Attr.getArg(0), StartArg, Size,
881 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000882}
883
Michael Hana9171bc2012-08-03 17:40:43 +0000884static bool checkLocksRequiredCommon(Sema &S, Decl *D,
885 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000886 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000887 assert(!Attr.isInvalid());
888
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000889 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000890 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000891
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000892 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000893 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
894 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000895 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000896 }
897
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000898 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000899 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000900 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000901 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000902
Michael Han3be3b442012-07-23 18:48:41 +0000903 return true;
904}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000905
Michael Hana9171bc2012-08-03 17:40:43 +0000906static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000907 const AttributeList &Attr) {
908 SmallVector<Expr*, 1> Args;
909 if (!checkLocksRequiredCommon(S, D, Attr, Args))
910 return;
911
912 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000913 D->addAttr(::new (S.Context)
914 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
915 StartArg, Args.size(),
916 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000917}
918
Michael Hana9171bc2012-08-03 17:40:43 +0000919static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000920 const AttributeList &Attr) {
921 SmallVector<Expr*, 1> Args;
922 if (!checkLocksRequiredCommon(S, D, Attr, Args))
923 return;
924
925 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000926 D->addAttr(::new (S.Context)
927 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
928 StartArg, Args.size(),
929 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000930}
931
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000932static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000933 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000934 assert(!Attr.isInvalid());
935
936 // zero or more arguments ok
937
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000938 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000939 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
940 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000941 return;
942 }
943
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000944 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000945 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000946 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000947 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000948 Expr **StartArg = Size == 0 ? 0 : &Args[0];
949
Michael Han99315932013-01-24 16:46:58 +0000950 D->addAttr(::new (S.Context)
951 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
952 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000953}
954
955static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000956 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000957 assert(!Attr.isInvalid());
958
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000959 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000960 return;
961
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000962 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000963 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
964 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000965 return;
966 }
967
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000968 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000969 SmallVector<Expr*, 1> Args;
970 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
971 unsigned Size = Args.size();
972 if (Size == 0)
973 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000974
Michael Han99315932013-01-24 16:46:58 +0000975 D->addAttr(::new (S.Context)
976 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
977 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000978}
979
980static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000981 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000982 assert(!Attr.isInvalid());
983
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000984 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000985 return;
986
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000987 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000988 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
989 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000990 return;
991 }
992
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000993 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000994 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000995 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000996 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000997 if (Size == 0)
998 return;
999 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +00001000
Michael Han99315932013-01-24 16:46:58 +00001001 D->addAttr(::new (S.Context)
1002 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
1003 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00001004}
1005
1006
Chandler Carruthedc2c642011-07-02 00:01:44 +00001007static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1008 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001009 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1010 if (TD == 0) {
1011 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1012 // and type-ids.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001013 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +00001014 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001015 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001016
Richard Smith1f5a4322013-01-13 02:11:23 +00001017 // Remember this typedef decl, we will need it later for diagnostics.
1018 S.ExtVectorDecls.push_back(TD);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001019}
1020
Chandler Carruthedc2c642011-07-02 00:01:44 +00001021static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001022 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001023 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001024 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001025
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001026 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001027 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001028 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001029 // If the alignment is less than or equal to 8 bits, the packed attribute
1030 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001031 if (!FD->getType()->isDependentType() &&
1032 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001033 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001034 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001035 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001036 else
Michael Han99315932013-01-24 16:46:58 +00001037 FD->addAttr(::new (S.Context)
1038 PackedAttr(Attr.getRange(), S.Context,
1039 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001040 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001041 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001042}
1043
Chandler Carruthedc2c642011-07-02 00:01:44 +00001044static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +00001045 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001046 RD->addAttr(::new (S.Context)
1047 MsStructAttr(Attr.getRange(), S.Context,
1048 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001049 else
1050 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1051}
1052
Chandler Carruthedc2c642011-07-02 00:01:44 +00001053static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001054 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001055 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001056 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001057
Ted Kremenek1f672822010-02-18 03:08:58 +00001058 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001059 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +00001060 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +00001061 D->addAttr(::new (S.Context)
1062 IBActionAttr(Attr.getRange(), S.Context,
1063 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +00001064 return;
1065 }
1066
Ted Kremenekd68ec812011-02-04 06:54:16 +00001067 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +00001068}
1069
Ted Kremenek7fd17232011-09-29 07:02:25 +00001070static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1071 // The IBOutlet/IBOutletCollection attributes only apply to instance
1072 // variables or properties of Objective-C classes. The outlet must also
1073 // have an object reference type.
1074 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1075 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001076 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001077 << Attr.getName() << VD->getType() << 0;
1078 return false;
1079 }
1080 }
1081 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1082 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001083 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001084 << Attr.getName() << PD->getType() << 1;
1085 return false;
1086 }
1087 }
1088 else {
1089 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1090 return false;
1091 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001092
Ted Kremenek7fd17232011-09-29 07:02:25 +00001093 return true;
1094}
1095
Chandler Carruthedc2c642011-07-02 00:01:44 +00001096static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001097 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001098 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +00001099 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001100
1101 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001102 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001103
Michael Han99315932013-01-24 16:46:58 +00001104 D->addAttr(::new (S.Context)
1105 IBOutletAttr(Attr.getRange(), S.Context,
1106 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001107}
1108
Chandler Carruthedc2c642011-07-02 00:01:44 +00001109static void handleIBOutletCollection(Sema &S, Decl *D,
1110 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001111
1112 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001113 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001114 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1115 return;
1116 }
1117
Ted Kremenek7fd17232011-09-29 07:02:25 +00001118 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001119 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001120
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001121 IdentifierInfo *II = Attr.getParameterName();
1122 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001123 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +00001124
John McCallba7bf592010-08-24 05:47:05 +00001125 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001126 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001127 if (!TypeRep) {
1128 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1129 return;
1130 }
John McCallba7bf592010-08-24 05:47:05 +00001131 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001132 // Diagnose use of non-object type in iboutletcollection attribute.
1133 // FIXME. Gnu attribute extension ignores use of builtin types in
1134 // attributes. So, __attribute__((iboutletcollection(char))) will be
1135 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001136 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001137 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1138 return;
1139 }
Michael Han99315932013-01-24 16:46:58 +00001140 D->addAttr(::new (S.Context)
1141 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1142 QT, Attr.getParameterLoc(),
1143 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001144}
1145
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001146static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001147 if (const RecordType *UT = T->getAsUnionType())
1148 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1149 RecordDecl *UD = UT->getDecl();
1150 for (RecordDecl::field_iterator it = UD->field_begin(),
1151 itend = UD->field_end(); it != itend; ++it) {
1152 QualType QT = it->getType();
1153 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1154 T = QT;
1155 return;
1156 }
1157 }
1158 }
1159}
1160
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001161static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001162 if (!isFunctionOrMethod(D)) {
1163 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1164 << "alloc_size" << ExpectedFunctionOrMethod;
1165 return;
1166 }
1167
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001168 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1169 return;
1170
1171 // In C++ the implicit 'this' function parameter also counts, and they are
1172 // counted from one.
1173 bool HasImplicitThisParam = isInstanceMethod(D);
Argyrios Kyrtzidise9365052013-02-22 06:58:28 +00001174 unsigned NumArgs;
1175 if (hasFunctionProto(D))
1176 NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1177 else
1178 NumArgs = 0;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001179
1180 SmallVector<unsigned, 8> SizeArgs;
1181
1182 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1183 E = Attr.arg_end(); I!=E; ++I) {
1184 // The argument must be an integer constant expression.
1185 Expr *Ex = *I;
1186 llvm::APSInt ArgNum;
1187 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1188 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1190 << "alloc_size" << Ex->getSourceRange();
1191 return;
1192 }
1193
1194 uint64_t x = ArgNum.getZExtValue();
1195
1196 if (x < 1 || x > NumArgs) {
1197 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1198 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1199 return;
1200 }
1201
1202 --x;
1203 if (HasImplicitThisParam) {
1204 if (x == 0) {
1205 S.Diag(Attr.getLoc(),
1206 diag::err_attribute_invalid_implicit_this_argument)
1207 << "alloc_size" << Ex->getSourceRange();
1208 return;
1209 }
1210 --x;
1211 }
1212
1213 // check if the function argument is of an integer type
1214 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1215 if (!T->isIntegerType()) {
1216 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1217 << "alloc_size" << Ex->getSourceRange();
1218 return;
1219 }
1220
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001221 SizeArgs.push_back(x);
1222 }
1223
1224 // check if the function returns a pointer
1225 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1226 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1227 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1228 }
1229
Michael Han99315932013-01-24 16:46:58 +00001230 D->addAttr(::new (S.Context)
1231 AllocSizeAttr(Attr.getRange(), S.Context,
1232 SizeArgs.data(), SizeArgs.size(),
1233 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001234}
1235
Chandler Carruthedc2c642011-07-02 00:01:44 +00001236static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001237 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1238 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001239 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001240 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001241 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001242 return;
1243 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001244
Chandler Carruth743682b2010-11-16 08:35:43 +00001245 // In C++ the implicit 'this' function parameter also counts, and they are
1246 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001247 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewyckye1121512013-01-24 01:12:16 +00001248 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001249
1250 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001251 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001252
Nick Lewyckye1121512013-01-24 01:12:16 +00001253 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1254 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001255 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001256 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001257 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001258 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1259 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001260 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1261 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001262 return;
1263 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001264
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001265 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001266
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001267 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001268 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +00001269 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001270 return;
1271 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001272
Ted Kremenek5224e6a2008-07-21 22:09:15 +00001273 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001274 if (HasImplicitThisParam) {
1275 if (x == 0) {
1276 S.Diag(Attr.getLoc(),
1277 diag::err_attribute_invalid_implicit_this_argument)
1278 << "nonnull" << Ex->getSourceRange();
1279 return;
1280 }
1281 --x;
1282 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001283
1284 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001285 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001286 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001287
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001288 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001289 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001290 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001291 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001292 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001293 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001294
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001295 NonNullArgs.push_back(x);
1296 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001297
1298 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1299 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001300 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001301 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1302 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001303 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001304 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001305 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001306 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001307
Ted Kremenek22813f42010-10-21 18:49:36 +00001308 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001309 if (NonNullArgs.empty()) {
1310 // Warn the trivial case only if attribute is not coming from a
1311 // macro instantiation.
1312 if (Attr.getLoc().isFileID())
1313 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001314 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001315 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001316 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001317
Nick Lewyckye1121512013-01-24 01:12:16 +00001318 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001319 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001320 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001321 D->addAttr(::new (S.Context)
1322 NonNullAttr(Attr.getRange(), S.Context, start, size,
1323 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001324}
1325
Chandler Carruthedc2c642011-07-02 00:01:44 +00001326static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001327 // This attribute must be applied to a function declaration.
1328 // The first argument to the attribute must be a string,
1329 // the name of the resource, for example "malloc".
1330 // The following arguments must be argument indexes, the arguments must be
1331 // of integer type for Returns, otherwise of pointer type.
1332 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001333 // after being held. free() should be __attribute((ownership_takes)), whereas
1334 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001335
1336 if (!AL.getParameterName()) {
Aaron Ballman29982272013-07-23 14:03:57 +00001337 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
1338 << AL.getName()->getName() << 1 << ArgumentString;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001339 return;
1340 }
1341 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001342 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001343 switch (AL.getKind()) {
1344 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001345 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001346 if (AL.getNumArgs() < 1) {
1347 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1348 return;
1349 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001350 break;
1351 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001352 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001353 if (AL.getNumArgs() < 1) {
1354 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1355 return;
1356 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001357 break;
1358 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001359 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001360 if (AL.getNumArgs() > 1) {
1361 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1362 << AL.getNumArgs() + 1;
1363 return;
1364 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001365 break;
1366 default:
1367 // This should never happen given how we are called.
1368 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001369 }
1370
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001371 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001372 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1373 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001374 return;
1375 }
1376
Chandler Carruth743682b2010-11-16 08:35:43 +00001377 // In C++ the implicit 'this' function parameter also counts, and they are
1378 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001379 bool HasImplicitThisParam = isInstanceMethod(D);
1380 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001381
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001382 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001383
1384 // Normalize the argument, __foo__ becomes foo.
1385 if (Module.startswith("__") && Module.endswith("__"))
1386 Module = Module.substr(2, Module.size() - 4);
1387
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001388 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001389
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001390 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1391 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001392
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001393 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001394 llvm::APSInt ArgNum(32);
1395 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1396 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1397 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1398 << AL.getName()->getName() << IdxExpr->getSourceRange();
1399 continue;
1400 }
1401
1402 unsigned x = (unsigned) ArgNum.getZExtValue();
1403
1404 if (x > NumArgs || x < 1) {
1405 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1406 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1407 continue;
1408 }
1409 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001410 if (HasImplicitThisParam) {
1411 if (x == 0) {
1412 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1413 << "ownership" << IdxExpr->getSourceRange();
1414 return;
1415 }
1416 --x;
1417 }
1418
Ted Kremenekd21139a2010-07-31 01:52:11 +00001419 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001420 case OwnershipAttr::Takes:
1421 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001422 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001423 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001424 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1425 // FIXME: Should also highlight argument in decl.
1426 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001427 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001428 << "pointer"
1429 << IdxExpr->getSourceRange();
1430 continue;
1431 }
1432 break;
1433 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001434 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001435 if (AL.getNumArgs() > 1) {
1436 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001437 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001438 llvm::APSInt ArgNum(32);
1439 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1440 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1441 S.Diag(AL.getLoc(), diag::err_ownership_type)
1442 << "ownership_returns" << "integer"
1443 << IdxExpr->getSourceRange();
1444 return;
1445 }
1446 }
1447 break;
1448 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001449 } // switch
1450
1451 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001452 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001453 i = D->specific_attr_begin<OwnershipAttr>(),
1454 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001455 i != e; ++i) {
1456 if ((*i)->getOwnKind() != K) {
1457 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1458 I!=E; ++I) {
1459 if (x == *I) {
1460 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1461 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001462 }
1463 }
1464 }
1465 }
1466 OwnershipArgs.push_back(x);
1467 }
1468
1469 unsigned* start = OwnershipArgs.data();
1470 unsigned size = OwnershipArgs.size();
1471 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001472
1473 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1474 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1475 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001476 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001477
Michael Han99315932013-01-24 16:46:58 +00001478 D->addAttr(::new (S.Context)
1479 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1480 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001481}
1482
Chandler Carruthedc2c642011-07-02 00:01:44 +00001483static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001484 // Check the attribute arguments.
1485 if (Attr.getNumArgs() > 1) {
1486 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1487 return;
1488 }
1489
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001490 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001492 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001493 return;
1494 }
1495
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001496 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001497
Rafael Espindolac18086a2010-02-23 22:00:30 +00001498 // gcc rejects
1499 // class c {
1500 // static int a __attribute__((weakref ("v2")));
1501 // static int b() __attribute__((weakref ("f3")));
1502 // };
1503 // and ignores the attributes of
1504 // void f(void) {
1505 // static int a __attribute__((weakref ("v2")));
1506 // }
1507 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001508 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001509 if (!Ctx->isFileContext()) {
1510 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001511 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001512 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001513 }
1514
1515 // The GCC manual says
1516 //
1517 // At present, a declaration to which `weakref' is attached can only
1518 // be `static'.
1519 //
1520 // It also says
1521 //
1522 // Without a TARGET,
1523 // given as an argument to `weakref' or to `alias', `weakref' is
1524 // equivalent to `weak'.
1525 //
1526 // gcc 4.4.1 will accept
1527 // int a7 __attribute__((weakref));
1528 // as
1529 // int a7 __attribute__((weak));
1530 // This looks like a bug in gcc. We reject that for now. We should revisit
1531 // it if this behaviour is actually used.
1532
Rafael Espindolac18086a2010-02-23 22:00:30 +00001533 // GCC rejects
1534 // static ((alias ("y"), weakref)).
1535 // Should we? How to check that weakref is before or after alias?
1536
1537 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001538 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001539 Arg = Arg->IgnoreParenCasts();
1540 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1541
Douglas Gregorfb65e592011-07-27 05:40:30 +00001542 if (!Str || !Str->isAscii()) {
Aaron Ballman29982272013-07-23 14:03:57 +00001543 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1544 << Attr.getName() << 1 << ArgumentString;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001545 return;
1546 }
1547 // GCC will accept anything as the argument of weakref. Should we
1548 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001549 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001550 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001551 }
1552
Michael Han99315932013-01-24 16:46:58 +00001553 D->addAttr(::new (S.Context)
1554 WeakRefAttr(Attr.getRange(), S.Context,
1555 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001556}
1557
Chandler Carruthedc2c642011-07-02 00:01:44 +00001558static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001559 // check the attribute arguments.
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00001560 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001561 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001562
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001563 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001564 Arg = Arg->IgnoreParenCasts();
1565 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001566
Douglas Gregorfb65e592011-07-27 05:40:30 +00001567 if (!Str || !Str->isAscii()) {
Aaron Ballman29982272013-07-23 14:03:57 +00001568 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1569 << Attr.getName() << 1 << ArgumentString;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001570 return;
1571 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001572
Douglas Gregore8bbc122011-09-02 00:18:52 +00001573 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001574 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1575 return;
1576 }
1577
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001578 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001579
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001580 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00001581 Str->getString(),
1582 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001583}
1584
Quentin Colombet4e172062012-11-01 23:55:47 +00001585static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1586 // Check the attribute arguments.
1587 if (!checkAttributeNumArgs(S, Attr, 0))
1588 return;
1589
1590 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1591 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1592 << Attr.getName() << ExpectedFunctionOrMethod;
1593 return;
1594 }
1595
Michael Han99315932013-01-24 16:46:58 +00001596 D->addAttr(::new (S.Context)
1597 MinSizeAttr(Attr.getRange(), S.Context,
1598 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001599}
1600
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001601static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1602 // Check the attribute arguments.
1603 if (!checkAttributeNumArgs(S, Attr, 0))
1604 return;
1605
1606 if (!isa<FunctionDecl>(D)) {
1607 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1608 << Attr.getName() << ExpectedFunction;
1609 return;
1610 }
1611
1612 if (D->hasAttr<HotAttr>()) {
1613 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1614 << Attr.getName() << "hot";
1615 return;
1616 }
1617
Michael Han99315932013-01-24 16:46:58 +00001618 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1619 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001620}
1621
1622static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1623 // Check the attribute arguments.
1624 if (!checkAttributeNumArgs(S, Attr, 0))
1625 return;
1626
1627 if (!isa<FunctionDecl>(D)) {
1628 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1629 << Attr.getName() << ExpectedFunction;
1630 return;
1631 }
1632
1633 if (D->hasAttr<ColdAttr>()) {
1634 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1635 << Attr.getName() << "cold";
1636 return;
1637 }
1638
Michael Han99315932013-01-24 16:46:58 +00001639 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1640 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001641}
1642
Chandler Carruthedc2c642011-07-02 00:01:44 +00001643static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001644 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001645 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001646 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001647
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001648 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001649 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001650 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001651 return;
1652 }
1653
Michael Han99315932013-01-24 16:46:58 +00001654 D->addAttr(::new (S.Context)
1655 NakedAttr(Attr.getRange(), S.Context,
1656 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001657}
1658
Chandler Carruthedc2c642011-07-02 00:01:44 +00001659static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1660 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001661 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001662 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001663 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1664 return;
1665 }
1666
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001667 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001669 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001670 return;
1671 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001672
Michael Han99315932013-01-24 16:46:58 +00001673 D->addAttr(::new (S.Context)
1674 AlwaysInlineAttr(Attr.getRange(), S.Context,
1675 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001676}
1677
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001678static void handleTLSModelAttr(Sema &S, Decl *D,
1679 const AttributeList &Attr) {
1680 // Check the attribute arguments.
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00001681 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001682 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001683
1684 Expr *Arg = Attr.getArg(0);
1685 Arg = Arg->IgnoreParenCasts();
1686 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1687
1688 // Check that it is a string.
1689 if (!Str) {
1690 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1691 return;
1692 }
1693
Richard Smithfd3834f2013-04-13 02:43:54 +00001694 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001695 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1696 << Attr.getName() << ExpectedTLSVar;
1697 return;
1698 }
1699
1700 // Check that the value.
1701 StringRef Model = Str->getString();
1702 if (Model != "global-dynamic" && Model != "local-dynamic"
1703 && Model != "initial-exec" && Model != "local-exec") {
1704 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1705 return;
1706 }
1707
Michael Han99315932013-01-24 16:46:58 +00001708 D->addAttr(::new (S.Context)
1709 TLSModelAttr(Attr.getRange(), S.Context, Model,
1710 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001711}
1712
Chandler Carruthedc2c642011-07-02 00:01:44 +00001713static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001714 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001715 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001716 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1717 return;
1718 }
Mike Stump11289f42009-09-09 15:08:12 +00001719
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001720 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001721 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001722 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001723 D->addAttr(::new (S.Context)
1724 MallocAttr(Attr.getRange(), S.Context,
1725 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001726 return;
1727 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001728 }
1729
Ted Kremenek08479ae2009-08-15 00:51:46 +00001730 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001731}
1732
Chandler Carruthedc2c642011-07-02 00:01:44 +00001733static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001734 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001735 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001736 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001737
Michael Han99315932013-01-24 16:46:58 +00001738 D->addAttr(::new (S.Context)
1739 MayAliasAttr(Attr.getRange(), S.Context,
1740 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001741}
1742
Chandler Carruthedc2c642011-07-02 00:01:44 +00001743static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001744 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001745 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001746 D->addAttr(::new (S.Context)
1747 NoCommonAttr(Attr.getRange(), S.Context,
1748 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001749 else
1750 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001751 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001752}
1753
Chandler Carruthedc2c642011-07-02 00:01:44 +00001754static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001755 assert(!Attr.isInvalid());
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001756
1757 if (S.LangOpts.CPlusPlus) {
1758 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1759 return;
1760 }
1761
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001762 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001763 D->addAttr(::new (S.Context)
1764 CommonAttr(Attr.getRange(), S.Context,
1765 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001766 else
1767 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001768 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001769}
1770
Chandler Carruthedc2c642011-07-02 00:01:44 +00001771static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001772 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001773
1774 if (S.CheckNoReturnAttr(attr)) return;
1775
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001776 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001777 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001778 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001779 return;
1780 }
1781
Michael Han99315932013-01-24 16:46:58 +00001782 D->addAttr(::new (S.Context)
1783 NoReturnAttr(attr.getRange(), S.Context,
1784 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001785}
1786
1787bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001788 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001789 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1790 attr.setInvalid();
1791 return true;
1792 }
1793
1794 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001795}
1796
Chandler Carruthedc2c642011-07-02 00:01:44 +00001797static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1798 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001799
1800 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1801 // because 'analyzer_noreturn' does not impact the type.
1802
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001803 if(!checkAttributeNumArgs(S, Attr, 0))
1804 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001805
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001806 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1807 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001808 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1809 && !VD->getType()->isFunctionPointerType())) {
1810 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001811 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001812 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001813 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001814 return;
1815 }
1816 }
1817
Michael Han99315932013-01-24 16:46:58 +00001818 D->addAttr(::new (S.Context)
1819 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1820 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001821}
1822
Richard Smith10876ef2013-01-17 01:30:42 +00001823static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1824 const AttributeList &Attr) {
1825 // C++11 [dcl.attr.noreturn]p1:
1826 // The attribute may be applied to the declarator-id in a function
1827 // declaration.
1828 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1829 if (!FD) {
1830 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1831 << Attr.getName() << ExpectedFunctionOrMethod;
1832 return;
1833 }
1834
Michael Han99315932013-01-24 16:46:58 +00001835 D->addAttr(::new (S.Context)
1836 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1837 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001838}
1839
John Thompsoncdb847ba2010-08-09 21:53:52 +00001840// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001841static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001842/*
1843 Returning a Vector Class in Registers
1844
Eric Christopherbc638a82010-12-01 22:13:54 +00001845 According to the PPU ABI specifications, a class with a single member of
1846 vector type is returned in memory when used as the return value of a function.
1847 This results in inefficient code when implementing vector classes. To return
1848 the value in a single vector register, add the vecreturn attribute to the
1849 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001850
1851 Example:
1852
1853 struct Vector
1854 {
1855 __vector float xyzw;
1856 } __attribute__((vecreturn));
1857
1858 Vector Add(Vector lhs, Vector rhs)
1859 {
1860 Vector result;
1861 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1862 return result; // This will be returned in a register
1863 }
1864*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001865 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001866 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001867 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001868 return;
1869 }
1870
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001871 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001872 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1873 return;
1874 }
1875
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001876 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001877 int count = 0;
1878
1879 if (!isa<CXXRecordDecl>(record)) {
1880 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1881 return;
1882 }
1883
1884 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1885 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1886 return;
1887 }
1888
Eric Christopherbc638a82010-12-01 22:13:54 +00001889 for (RecordDecl::field_iterator iter = record->field_begin();
1890 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001891 if ((count == 1) || !iter->getType()->isVectorType()) {
1892 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1893 return;
1894 }
1895 count++;
1896 }
1897
Michael Han99315932013-01-24 16:46:58 +00001898 D->addAttr(::new (S.Context)
1899 VecReturnAttr(Attr.getRange(), S.Context,
1900 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001901}
1902
Richard Smithe233fbf2013-01-28 22:42:45 +00001903static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1904 const AttributeList &Attr) {
1905 if (isa<ParmVarDecl>(D)) {
1906 // [[carries_dependency]] can only be applied to a parameter if it is a
1907 // parameter of a function declaration or lambda.
1908 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1909 S.Diag(Attr.getLoc(),
1910 diag::err_carries_dependency_param_not_function_decl);
1911 return;
1912 }
1913 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001914 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001915 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001916 return;
1917 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001918
1919 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1920 Attr.getRange(), S.Context,
1921 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001922}
1923
Chandler Carruthedc2c642011-07-02 00:01:44 +00001924static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001925 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001926 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001927 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001928 return;
1929 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001930
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001931 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001932 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001933 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001934 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001935 return;
1936 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001937
Michael Han99315932013-01-24 16:46:58 +00001938 D->addAttr(::new (S.Context)
1939 UnusedAttr(Attr.getRange(), S.Context,
1940 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001941}
1942
Rafael Espindola70107f92011-10-03 14:59:42 +00001943static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1944 const AttributeList &Attr) {
1945 // check the attribute arguments.
1946 if (Attr.hasParameterOrArguments()) {
1947 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1948 return;
1949 }
1950
1951 if (!isa<FunctionDecl>(D)) {
1952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1953 << Attr.getName() << ExpectedFunction;
1954 return;
1955 }
1956
Michael Han99315932013-01-24 16:46:58 +00001957 D->addAttr(::new (S.Context)
1958 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1959 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00001960}
1961
Chandler Carruthedc2c642011-07-02 00:01:44 +00001962static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001963 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001964 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1966 return;
1967 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001968
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001969 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001970 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001971 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1972 return;
1973 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001974 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001975 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001976 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001977 return;
1978 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001979
Michael Han99315932013-01-24 16:46:58 +00001980 D->addAttr(::new (S.Context)
1981 UsedAttr(Attr.getRange(), S.Context,
1982 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001983}
1984
Chandler Carruthedc2c642011-07-02 00:01:44 +00001985static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001986 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001987 if (Attr.getNumArgs() > 1) {
1988 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001989 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001990 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001991
1992 int priority = 65535; // FIXME: Do not hardcode such constants.
1993 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001994 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001995 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001996 if (E->isTypeDependent() || E->isValueDependent() ||
1997 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001998 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1999 << Attr.getName() << 1 << ArgumentIntegerConstant
2000 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00002001 return;
2002 }
2003 priority = Idx.getZExtValue();
2004 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002005
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002006 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002007 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002008 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002009 return;
2010 }
2011
Michael Han99315932013-01-24 16:46:58 +00002012 D->addAttr(::new (S.Context)
2013 ConstructorAttr(Attr.getRange(), S.Context, priority,
2014 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002015}
2016
Chandler Carruthedc2c642011-07-02 00:01:44 +00002017static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002018 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002019 if (Attr.getNumArgs() > 1) {
2020 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002021 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002022 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002023
2024 int priority = 65535; // FIXME: Do not hardcode such constants.
2025 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002026 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00002027 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002028 if (E->isTypeDependent() || E->isValueDependent() ||
2029 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002030 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2031 << Attr.getName() << 1 << ArgumentIntegerConstant
2032 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00002033 return;
2034 }
2035 priority = Idx.getZExtValue();
2036 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002037
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002038 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002039 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002040 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002041 return;
2042 }
2043
Michael Han99315932013-01-24 16:46:58 +00002044 D->addAttr(::new (S.Context)
2045 DestructorAttr(Attr.getRange(), S.Context, priority,
2046 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002047}
2048
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002049template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002050static void handleAttrWithMessage(Sema &S, Decl *D,
2051 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00002052 unsigned NumArgs = Attr.getNumArgs();
2053 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00002054 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002055 return;
2056 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002057
2058 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002059 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00002060 if (NumArgs == 1) {
2061 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00002062 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00002063 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002064 << Attr.getName();
Fariborz Jahanian551063102010-10-06 21:18:44 +00002065 return;
2066 }
Chris Lattner190aa102011-02-24 05:42:24 +00002067 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00002068 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002069
Michael Han99315932013-01-24 16:46:58 +00002070 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2071 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002072}
2073
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002074static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2075 const AttributeList &Attr) {
2076 unsigned NumArgs = Attr.getNumArgs();
2077 if (NumArgs > 0) {
2078 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2079 return;
2080 }
2081
Michael Han99315932013-01-24 16:46:58 +00002082 D->addAttr(::new (S.Context)
2083 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2084 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002085}
2086
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002087static void handleObjCRootClassAttr(Sema &S, Decl *D,
2088 const AttributeList &Attr) {
2089 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002090 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2091 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002092 return;
2093 }
2094
2095 unsigned NumArgs = Attr.getNumArgs();
2096 if (NumArgs > 0) {
2097 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2098 return;
2099 }
2100
Michael Han99315932013-01-24 16:46:58 +00002101 D->addAttr(::new (S.Context)
2102 ObjCRootClassAttr(Attr.getRange(), S.Context,
2103 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002104}
2105
Michael Han99315932013-01-24 16:46:58 +00002106static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2107 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002108 if (!isa<ObjCInterfaceDecl>(D)) {
2109 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2110 return;
2111 }
2112
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002113 unsigned NumArgs = Attr.getNumArgs();
2114 if (NumArgs > 0) {
2115 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2116 return;
2117 }
2118
Michael Han99315932013-01-24 16:46:58 +00002119 D->addAttr(::new (S.Context)
2120 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2121 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002122}
2123
Jordy Rose740b0c22012-05-08 03:27:22 +00002124static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2125 IdentifierInfo *Platform,
2126 VersionTuple Introduced,
2127 VersionTuple Deprecated,
2128 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002129 StringRef PlatformName
2130 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2131 if (PlatformName.empty())
2132 PlatformName = Platform->getName();
2133
2134 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2135 // of these steps are needed).
2136 if (!Introduced.empty() && !Deprecated.empty() &&
2137 !(Introduced <= Deprecated)) {
2138 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2139 << 1 << PlatformName << Deprecated.getAsString()
2140 << 0 << Introduced.getAsString();
2141 return true;
2142 }
2143
2144 if (!Introduced.empty() && !Obsoleted.empty() &&
2145 !(Introduced <= Obsoleted)) {
2146 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2147 << 2 << PlatformName << Obsoleted.getAsString()
2148 << 0 << Introduced.getAsString();
2149 return true;
2150 }
2151
2152 if (!Deprecated.empty() && !Obsoleted.empty() &&
2153 !(Deprecated <= Obsoleted)) {
2154 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2155 << 2 << PlatformName << Obsoleted.getAsString()
2156 << 1 << Deprecated.getAsString();
2157 return true;
2158 }
2159
2160 return false;
2161}
2162
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002163/// \brief Check whether the two versions match.
2164///
2165/// If either version tuple is empty, then they are assumed to match. If
2166/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2167static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2168 bool BeforeIsOkay) {
2169 if (X.empty() || Y.empty())
2170 return true;
2171
2172 if (X == Y)
2173 return true;
2174
2175 if (BeforeIsOkay && X < Y)
2176 return true;
2177
2178 return false;
2179}
2180
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002181AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002182 IdentifierInfo *Platform,
2183 VersionTuple Introduced,
2184 VersionTuple Deprecated,
2185 VersionTuple Obsoleted,
2186 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002187 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002188 bool Override,
2189 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002190 VersionTuple MergedIntroduced = Introduced;
2191 VersionTuple MergedDeprecated = Deprecated;
2192 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002193 bool FoundAny = false;
2194
Rafael Espindolac67f2232012-05-10 02:50:16 +00002195 if (D->hasAttrs()) {
2196 AttrVec &Attrs = D->getAttrs();
2197 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2198 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2199 if (!OldAA) {
2200 ++i;
2201 continue;
2202 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002203
Rafael Espindolac67f2232012-05-10 02:50:16 +00002204 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2205 if (OldPlatform != Platform) {
2206 ++i;
2207 continue;
2208 }
2209
2210 FoundAny = true;
2211 VersionTuple OldIntroduced = OldAA->getIntroduced();
2212 VersionTuple OldDeprecated = OldAA->getDeprecated();
2213 VersionTuple OldObsoleted = OldAA->getObsoleted();
2214 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002215
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002216 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2217 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2218 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2219 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002220 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002221 if (Override) {
2222 int Which = -1;
2223 VersionTuple FirstVersion;
2224 VersionTuple SecondVersion;
2225 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2226 Which = 0;
2227 FirstVersion = OldIntroduced;
2228 SecondVersion = Introduced;
2229 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2230 Which = 1;
2231 FirstVersion = Deprecated;
2232 SecondVersion = OldDeprecated;
2233 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2234 Which = 2;
2235 FirstVersion = Obsoleted;
2236 SecondVersion = OldObsoleted;
2237 }
2238
2239 if (Which == -1) {
2240 Diag(OldAA->getLocation(),
2241 diag::warn_mismatched_availability_override_unavail)
2242 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2243 } else {
2244 Diag(OldAA->getLocation(),
2245 diag::warn_mismatched_availability_override)
2246 << Which
2247 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2248 << FirstVersion.getAsString() << SecondVersion.getAsString();
2249 }
2250 Diag(Range.getBegin(), diag::note_overridden_method);
2251 } else {
2252 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2253 Diag(Range.getBegin(), diag::note_previous_attribute);
2254 }
2255
Rafael Espindolac67f2232012-05-10 02:50:16 +00002256 Attrs.erase(Attrs.begin() + i);
2257 --e;
2258 continue;
2259 }
2260
2261 VersionTuple MergedIntroduced2 = MergedIntroduced;
2262 VersionTuple MergedDeprecated2 = MergedDeprecated;
2263 VersionTuple MergedObsoleted2 = MergedObsoleted;
2264
2265 if (MergedIntroduced2.empty())
2266 MergedIntroduced2 = OldIntroduced;
2267 if (MergedDeprecated2.empty())
2268 MergedDeprecated2 = OldDeprecated;
2269 if (MergedObsoleted2.empty())
2270 MergedObsoleted2 = OldObsoleted;
2271
2272 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2273 MergedIntroduced2, MergedDeprecated2,
2274 MergedObsoleted2)) {
2275 Attrs.erase(Attrs.begin() + i);
2276 --e;
2277 continue;
2278 }
2279
2280 MergedIntroduced = MergedIntroduced2;
2281 MergedDeprecated = MergedDeprecated2;
2282 MergedObsoleted = MergedObsoleted2;
2283 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002284 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002285 }
2286
2287 if (FoundAny &&
2288 MergedIntroduced == Introduced &&
2289 MergedDeprecated == Deprecated &&
2290 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002291 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002292
Ted Kremenekb5445722013-04-06 00:34:27 +00002293 // Only create a new attribute if !Override, but we want to do
2294 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002295 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002296 MergedDeprecated, MergedObsoleted) &&
2297 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002298 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2299 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002300 Obsoleted, IsUnavailable, Message,
2301 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002302 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002303 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002304}
2305
Chandler Carruthedc2c642011-07-02 00:01:44 +00002306static void handleAvailabilityAttr(Sema &S, Decl *D,
2307 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002308 IdentifierInfo *Platform = Attr.getParameterName();
2309 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han99315932013-01-24 16:46:58 +00002310 unsigned Index = Attr.getAttributeSpellingListIndex();
2311
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002312 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002313 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2314 << Platform;
2315
Rafael Espindolac231fab2013-01-08 21:30:32 +00002316 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2317 if (!ND) {
2318 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2319 return;
2320 }
2321
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002322 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2323 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2324 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002325 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002326 StringRef Str;
2327 const StringLiteral *SE =
2328 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2329 if (SE)
2330 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002331
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002332 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002333 Platform,
2334 Introduced.Version,
2335 Deprecated.Version,
2336 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002337 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002338 /*Override=*/false,
2339 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002340 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002341 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002342}
2343
John McCalld041a9b2013-02-20 01:54:26 +00002344template <class T>
2345static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2346 typename T::VisibilityType value,
2347 unsigned attrSpellingListIndex) {
2348 T *existingAttr = D->getAttr<T>();
2349 if (existingAttr) {
2350 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2351 if (existingValue == value)
2352 return NULL;
2353 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2354 S.Diag(range.getBegin(), diag::note_previous_attribute);
2355 D->dropAttr<T>();
2356 }
2357 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2358}
2359
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002360VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002361 VisibilityAttr::VisibilityType Vis,
2362 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002363 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2364 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002365}
2366
John McCalld041a9b2013-02-20 01:54:26 +00002367TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2368 TypeVisibilityAttr::VisibilityType Vis,
2369 unsigned AttrSpellingListIndex) {
2370 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2371 AttrSpellingListIndex);
2372}
2373
2374static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2375 bool isTypeVisibility) {
2376 // Visibility attributes don't mean anything on a typedef.
2377 if (isa<TypedefNameDecl>(D)) {
2378 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2379 << Attr.getName();
2380 return;
2381 }
2382
2383 // 'type_visibility' can only go on a type or namespace.
2384 if (isTypeVisibility &&
2385 !(isa<TagDecl>(D) ||
2386 isa<ObjCInterfaceDecl>(D) ||
2387 isa<NamespaceDecl>(D))) {
2388 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2389 << Attr.getName() << ExpectedTypeOrNamespace;
2390 return;
2391 }
2392
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002393 // check the attribute arguments.
John McCalld041a9b2013-02-20 01:54:26 +00002394 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002395 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002396
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002397 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002398 Arg = Arg->IgnoreParenCasts();
2399 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002400
Douglas Gregorfb65e592011-07-27 05:40:30 +00002401 if (!Str || !Str->isAscii()) {
Aaron Ballman29982272013-07-23 14:03:57 +00002402 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2403 << Attr.getName() << 1 << ArgumentString;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002404 return;
2405 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002406
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002407 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002408 VisibilityAttr::VisibilityType type;
Michael Han99315932013-01-24 16:46:58 +00002409
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002410 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002411 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002412 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002413 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002414 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002415 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00002416 else if (TypeStr == "protected") {
2417 // Complain about attempts to use protected visibility on targets
2418 // (like Darwin) that don't support it.
2419 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2420 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2421 type = VisibilityAttr::Default;
2422 } else {
2423 type = VisibilityAttr::Protected;
2424 }
2425 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002426 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002427 return;
2428 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002429
Michael Han99315932013-01-24 16:46:58 +00002430 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002431 clang::Attr *newAttr;
2432 if (isTypeVisibility) {
2433 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2434 (TypeVisibilityAttr::VisibilityType) type,
2435 Index);
2436 } else {
2437 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2438 }
2439 if (newAttr)
2440 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002441}
2442
Chandler Carruthedc2c642011-07-02 00:01:44 +00002443static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2444 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002445 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2446 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002448 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002449 return;
2450 }
2451
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002452 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2453 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
Aaron Ballman29982272013-07-23 14:03:57 +00002454 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2455 << Attr.getName() << 1 << ArgumentString;
John McCall86bc21f2011-03-02 11:33:24 +00002456 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002457 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002458 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002459 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002460 return;
2461 }
2462
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002463 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002464 ObjCMethodFamilyAttr::FamilyKind family;
2465 if (param == "none")
2466 family = ObjCMethodFamilyAttr::OMF_None;
2467 else if (param == "alloc")
2468 family = ObjCMethodFamilyAttr::OMF_alloc;
2469 else if (param == "copy")
2470 family = ObjCMethodFamilyAttr::OMF_copy;
2471 else if (param == "init")
2472 family = ObjCMethodFamilyAttr::OMF_init;
2473 else if (param == "mutableCopy")
2474 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2475 else if (param == "new")
2476 family = ObjCMethodFamilyAttr::OMF_new;
2477 else {
2478 // Just warn and ignore it. This is future-proof against new
2479 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002480 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002481 return;
2482 }
2483
John McCall31168b02011-06-15 23:02:42 +00002484 if (family == ObjCMethodFamilyAttr::OMF_init &&
2485 !method->getResultType()->isObjCObjectPointerType()) {
2486 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2487 << method->getResultType();
2488 // Ignore the attribute.
2489 return;
2490 }
2491
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002492 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002493 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002494}
2495
Chandler Carruthedc2c642011-07-02 00:01:44 +00002496static void handleObjCExceptionAttr(Sema &S, Decl *D,
2497 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002498 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002499 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002500
Chris Lattner677a3582009-02-14 08:09:34 +00002501 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2502 if (OCI == 0) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002503 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2504 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner677a3582009-02-14 08:09:34 +00002505 return;
2506 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002507
Michael Han99315932013-01-24 16:46:58 +00002508 D->addAttr(::new (S.Context)
2509 ObjCExceptionAttr(Attr.getRange(), S.Context,
2510 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002511}
2512
Chandler Carruthedc2c642011-07-02 00:01:44 +00002513static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman7ced1672013-07-23 12:13:14 +00002514 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002515 return;
Richard Smithdda56e42011-04-15 14:24:37 +00002516 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002517 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002518 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002519 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2520 return;
2521 }
2522 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002523 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2524 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002525 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002526 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2527 return;
2528 }
2529 }
2530 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002531 // It is okay to include this attribute on properties, e.g.:
2532 //
2533 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2534 //
2535 // In this case it follows tradition and suppresses an error in the above
2536 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002537 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002538 }
Michael Han99315932013-01-24 16:46:58 +00002539 D->addAttr(::new (S.Context)
2540 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2541 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002542}
2543
Mike Stumpd3bb5572009-07-24 19:02:52 +00002544static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002545handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman7ced1672013-07-23 12:13:14 +00002546 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002547 return;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002548
2549 if (!isa<FunctionDecl>(D)) {
2550 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2551 return;
2552 }
2553
Michael Han99315932013-01-24 16:46:58 +00002554 D->addAttr(::new (S.Context)
2555 OverloadableAttr(Attr.getRange(), S.Context,
2556 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002557}
2558
Chandler Carruthedc2c642011-07-02 00:01:44 +00002559static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002560 if (!Attr.getParameterName()) {
Aaron Ballman29982272013-07-23 14:03:57 +00002561 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2562 << Attr.getName() << 1 << ArgumentString;
Steve Naroff3405a732008-09-18 16:44:58 +00002563 return;
2564 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002565
Steve Naroff3405a732008-09-18 16:44:58 +00002566 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002567 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002568 return;
2569 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002570
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002571 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002572 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002573 type = BlocksAttr::ByRef;
2574 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002575 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002576 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002577 return;
2578 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002579
Michael Han99315932013-01-24 16:46:58 +00002580 D->addAttr(::new (S.Context)
2581 BlocksAttr(Attr.getRange(), S.Context, type,
2582 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002583}
2584
Chandler Carruthedc2c642011-07-02 00:01:44 +00002585static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002586 // check the attribute arguments.
2587 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002588 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002589 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002590 }
2591
Aaron Ballman29982272013-07-23 14:03:57 +00002592 // Normalize the attribute name, __foo__ becomes foo.
2593 StringRef AttrName = Attr.getName()->getName();
2594 if (AttrName.startswith("__") && AttrName.endswith("__"))
2595 AttrName = AttrName.substr(2, AttrName.size() - 4);
2596
John McCallb46f2872011-09-09 07:56:05 +00002597 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002598 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002599 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002600 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002601 if (E->isTypeDependent() || E->isValueDependent() ||
2602 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002603 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2604 << "'" + AttrName.str() + "'" << 1 << ArgumentIntegerConstant
2605 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002606 return;
2607 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002608
John McCallb46f2872011-09-09 07:56:05 +00002609 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002610 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2611 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002612 return;
2613 }
John McCallb46f2872011-09-09 07:56:05 +00002614
2615 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002616 }
2617
John McCallb46f2872011-09-09 07:56:05 +00002618 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002619 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002620 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002621 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002622 if (E->isTypeDependent() || E->isValueDependent() ||
2623 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002624 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2625 << "'" + AttrName.str() + "'" << 2 << ArgumentIntegerConstant
2626 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002627 return;
2628 }
2629 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002630
John McCallb46f2872011-09-09 07:56:05 +00002631 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002632 // FIXME: This error message could be improved, it would be nice
2633 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002634 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2635 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002636 return;
2637 }
2638 }
2639
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002640 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002641 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002642 if (isa<FunctionNoProtoType>(FT)) {
2643 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2644 return;
2645 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002646
Chris Lattner9363e312009-03-17 23:03:47 +00002647 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002648 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002649 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002650 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002651 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002652 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002653 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002654 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002655 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002656 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2657 if (!BD->isVariadic()) {
2658 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2659 return;
2660 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002661 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002662 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002663 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002664 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002665 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002666 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002667 int m = Ty->isFunctionPointerType() ? 0 : 1;
2668 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002669 return;
2670 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002671 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002672 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002673 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002674 return;
2675 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002676 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002678 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002679 return;
2680 }
Michael Han99315932013-01-24 16:46:58 +00002681 D->addAttr(::new (S.Context)
2682 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2683 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002684}
2685
Lubos Lunakedc13882013-07-20 15:05:36 +00002686static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2687 // Check the attribute arguments.
2688 if (!checkAttributeNumArgs(S, Attr, 0))
2689 return;
2690
2691 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2692 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2693 else
2694 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2695}
2696
Chandler Carruthedc2c642011-07-02 00:01:44 +00002697static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002698 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002699 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002700 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002701
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002702 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002703 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002704 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002705 return;
2706 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002707
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002708 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2709 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2710 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002711 return;
2712 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002713 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2714 if (MD->getResultType()->isVoidType()) {
2715 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2716 << Attr.getName() << 1;
2717 return;
2718 }
2719
Michael Han99315932013-01-24 16:46:58 +00002720 D->addAttr(::new (S.Context)
2721 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2722 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002723}
2724
Chandler Carruthedc2c642011-07-02 00:01:44 +00002725static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002726 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002727 if (Attr.hasParameterOrArguments()) {
2728 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002729 return;
2730 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002731
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002732 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002733 if (isa<CXXRecordDecl>(D)) {
2734 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2735 return;
2736 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002737 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2738 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002739 return;
2740 }
2741
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002742 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002743
Michael Han99315932013-01-24 16:46:58 +00002744 nd->addAttr(::new (S.Context)
2745 WeakAttr(Attr.getRange(), S.Context,
2746 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002747}
2748
Chandler Carruthedc2c642011-07-02 00:01:44 +00002749static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002750 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002751 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002752 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002753
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002754
2755 // weak_import only applies to variable & function declarations.
2756 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002757 if (!D->canBeWeakImported(isDef)) {
2758 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002759 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2760 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002761 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002762 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002763 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002764 // Nothing to warn about here.
2765 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002766 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002767 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002768
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002769 return;
2770 }
2771
Michael Han99315932013-01-24 16:46:58 +00002772 D->addAttr(::new (S.Context)
2773 WeakImportAttr(Attr.getRange(), S.Context,
2774 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002775}
2776
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002777// Handles reqd_work_group_size and work_group_size_hint.
2778static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002779 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002780 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2781 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2782
Nate Begemanf2758702009-06-26 06:32:41 +00002783 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002784 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002785
2786 unsigned WGSize[3];
2787 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002788 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002789 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002790 if (E->isTypeDependent() || E->isValueDependent() ||
2791 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002792 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002793 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002794 return;
2795 }
2796 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2797 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002798
2799 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2800 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2801 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2802 if (!(A->getXDim() == WGSize[0] &&
2803 A->getYDim() == WGSize[1] &&
2804 A->getZDim() == WGSize[2])) {
2805 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2806 Attr.getName();
2807 }
2808 }
2809
2810 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2811 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2812 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2813 if (!(A->getXDim() == WGSize[0] &&
2814 A->getYDim() == WGSize[1] &&
2815 A->getZDim() == WGSize[2])) {
2816 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2817 Attr.getName();
2818 }
2819 }
2820
2821 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2822 D->addAttr(::new (S.Context)
2823 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002824 WGSize[0], WGSize[1], WGSize[2],
2825 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002826 else
2827 D->addAttr(::new (S.Context)
2828 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002829 WGSize[0], WGSize[1], WGSize[2],
2830 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002831}
2832
Joey Goulyaba589c2013-03-08 09:42:32 +00002833static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2834 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2835
2836 // Attribute has 1 argument.
2837 if (!checkAttributeNumArgs(S, Attr, 1))
2838 return;
2839
2840 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2841
2842 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2843 (ParmType->isBooleanType() ||
2844 !ParmType->isIntegralType(S.getASTContext()))) {
2845 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2846 << ParmType;
2847 return;
2848 }
2849
2850 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2851 D->hasAttr<VecTypeHintAttr>()) {
2852 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2853 if (A->getTypeHint() != ParmType) {
2854 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2855 return;
2856 }
2857 }
2858
2859 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2860 ParmType, Attr.getLoc()));
2861}
2862
Joey Gouly0608ae82013-03-14 09:54:43 +00002863static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2864 if (!dyn_cast<VarDecl>(D))
2865 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2866 << 9;
2867 StringRef EndianType = Attr.getParameterName()->getName();
2868 if (EndianType != "host" && EndianType != "device")
2869 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2870}
2871
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002872SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002873 StringRef Name,
2874 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002875 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2876 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002877 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002878 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2879 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002880 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002881 }
Michael Han99315932013-01-24 16:46:58 +00002882 return ::new (Context) SectionAttr(Range, Context, Name,
2883 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002884}
2885
Chandler Carruthedc2c642011-07-02 00:01:44 +00002886static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002887 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002888 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002889 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002890
2891 // Make sure that there is a string literal as the sections's single
2892 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002893 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002894 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002895 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002896 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002897 return;
2898 }
Mike Stump11289f42009-09-09 15:08:12 +00002899
Chris Lattner30ba6742009-08-10 19:03:04 +00002900 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002901 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002902 if (!Error.empty()) {
2903 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2904 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002905 return;
2906 }
Mike Stump11289f42009-09-09 15:08:12 +00002907
Chris Lattner20aee9b2010-01-12 20:58:53 +00002908 // This attribute cannot be applied to local variables.
2909 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2910 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2911 return;
2912 }
Michael Han99315932013-01-24 16:46:58 +00002913
2914 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002915 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han99315932013-01-24 16:46:58 +00002916 SE->getString(), Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002917 if (NewAttr)
2918 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002919}
2920
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002921
Chandler Carruthedc2c642011-07-02 00:01:44 +00002922static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002923 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002924 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002925 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002926 return;
2927 }
Douglas Gregor88336832011-06-15 05:45:11 +00002928
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002929 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002930 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002931 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002932 } else {
Michael Han99315932013-01-24 16:46:58 +00002933 D->addAttr(::new (S.Context)
2934 NoThrowAttr(Attr.getRange(), S.Context,
2935 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002936 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002937}
2938
Chandler Carruthedc2c642011-07-02 00:01:44 +00002939static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002940 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002941 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002942 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002943 return;
2944 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002945
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002946 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002947 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002948 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002949 } else {
Michael Han99315932013-01-24 16:46:58 +00002950 D->addAttr(::new (S.Context)
2951 ConstAttr(Attr.getRange(), S.Context,
2952 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002953 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002954}
2955
Chandler Carruthedc2c642011-07-02 00:01:44 +00002956static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002957 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002958 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002959 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002960
Michael Han99315932013-01-24 16:46:58 +00002961 D->addAttr(::new (S.Context)
2962 PureAttr(Attr.getRange(), S.Context,
2963 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002964}
2965
Chandler Carruthedc2c642011-07-02 00:01:44 +00002966static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002967 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002968 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2969 return;
2970 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002971
Anders Carlssond277d792009-01-31 01:16:18 +00002972 if (Attr.getNumArgs() != 0) {
2973 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2974 return;
2975 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002976
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002977 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002978
Anders Carlssond277d792009-01-31 01:16:18 +00002979 if (!VD || !VD->hasLocalStorage()) {
2980 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2981 return;
2982 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002983
Anders Carlssond277d792009-01-31 01:16:18 +00002984 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002985 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002986 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002987 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2988 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002989 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002990 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002991 Attr.getParameterName();
2992 return;
2993 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002994
Anders Carlssond277d792009-01-31 01:16:18 +00002995 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2996 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002997 S.Diag(Attr.getParameterLoc(),
2998 diag::err_attribute_cleanup_arg_not_function)
2999 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00003000 return;
3001 }
3002
Anders Carlssond277d792009-01-31 01:16:18 +00003003 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00003004 S.Diag(Attr.getParameterLoc(),
3005 diag::err_attribute_cleanup_func_must_take_one_arg)
3006 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00003007 return;
3008 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003009
Anders Carlsson723f55d2009-02-07 23:16:50 +00003010 // We're currently more strict than GCC about what function types we accept.
3011 // If this ever proves to be a problem it should be easy to fix.
3012 QualType Ty = S.Context.getPointerType(VD->getType());
3013 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003014 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3015 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00003016 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00003017 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
3018 Attr.getParameterName() << ParamTy << Ty;
3019 return;
3020 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003021
Michael Han99315932013-01-24 16:46:58 +00003022 D->addAttr(::new (S.Context)
3023 CleanupAttr(Attr.getRange(), S.Context, FD,
3024 Attr.getAttributeSpellingListIndex()));
Eli Friedmanfa0df832012-02-02 03:46:19 +00003025 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewyckya096b142013-02-12 08:08:54 +00003026 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssond277d792009-01-31 01:16:18 +00003027}
3028
Mike Stumpd3bb5572009-07-24 19:02:52 +00003029/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003030/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003031static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003032 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003033 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003034
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003035 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003036 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003037 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003038 return;
3039 }
Chandler Carruth743682b2010-11-16 08:35:43 +00003040
3041 // In C++ the implicit 'this' function parameter also counts, and they are
3042 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003043 bool HasImplicitThisParam = isInstanceMethod(D);
3044 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003045 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00003046
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003047 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003048 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003049 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003050 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3051 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003052 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3053 << Attr.getName() << 2 << ArgumentIntegerConstant
3054 << IdxExpr->getSourceRange();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003055 return;
3056 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003057
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003058 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
3059 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3060 << "format" << 2 << IdxExpr->getSourceRange();
3061 return;
3062 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003063
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003064 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003065
Chandler Carruth743682b2010-11-16 08:35:43 +00003066 if (HasImplicitThisParam) {
3067 if (ArgIdx == 0) {
3068 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3069 << "format_arg" << IdxExpr->getSourceRange();
3070 return;
3071 }
3072 ArgIdx--;
3073 }
3074
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003075 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003076 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003077
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003078 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3079 if (not_nsstring_type &&
3080 !isCFStringType(Ty, S.Context) &&
3081 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003082 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003083 // FIXME: Should highlight the actual expression that has the wrong type.
3084 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003085 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003086 << IdxExpr->getSourceRange();
3087 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003088 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003089 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003090 if (!isNSStringType(Ty, S.Context) &&
3091 !isCFStringType(Ty, S.Context) &&
3092 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003093 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003094 // FIXME: Should highlight the actual expression that has the wrong type.
3095 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003096 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003097 << IdxExpr->getSourceRange();
3098 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003099 }
3100
Michael Han99315932013-01-24 16:46:58 +00003101 D->addAttr(::new (S.Context)
3102 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3103 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003104}
3105
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003106enum FormatAttrKind {
3107 CFStringFormat,
3108 NSStringFormat,
3109 StrftimeFormat,
3110 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003111 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003112 InvalidFormat
3113};
3114
3115/// getFormatAttrKind - Map from format attribute names to supported format
3116/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003117static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003118 return llvm::StringSwitch<FormatAttrKind>(Format)
3119 // Check for formats that get handled specially.
3120 .Case("NSString", NSStringFormat)
3121 .Case("CFString", CFStringFormat)
3122 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003123
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003124 // Otherwise, check for supported formats.
3125 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3126 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3127 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003128
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003129 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3130 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003131}
3132
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003133/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003134/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003135static void handleInitPriorityAttr(Sema &S, Decl *D,
3136 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003137 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003138 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3139 return;
3140 }
3141
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003142 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003143 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3144 Attr.setInvalid();
3145 return;
3146 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003147 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003148 if (S.Context.getAsArrayType(T))
3149 T = S.Context.getBaseElementType(T);
3150 if (!T->getAs<RecordType>()) {
3151 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3152 Attr.setInvalid();
3153 return;
3154 }
3155
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003156 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003157 Attr.setInvalid();
3158 return;
3159 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003160 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003161
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003162 llvm::APSInt priority(32);
3163 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3164 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3165 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3166 << "init_priority" << priorityExpr->getSourceRange();
3167 Attr.setInvalid();
3168 return;
3169 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00003170 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003171 if (prioritynum < 101 || prioritynum > 65535) {
3172 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3173 << priorityExpr->getSourceRange();
3174 Attr.setInvalid();
3175 return;
3176 }
Michael Han99315932013-01-24 16:46:58 +00003177 D->addAttr(::new (S.Context)
3178 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3179 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003180}
3181
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003182FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han99315932013-01-24 16:46:58 +00003183 int FormatIdx, int FirstArg,
3184 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003185 // Check whether we already have an equivalent format attribute.
3186 for (specific_attr_iterator<FormatAttr>
3187 i = D->specific_attr_begin<FormatAttr>(),
3188 e = D->specific_attr_end<FormatAttr>();
3189 i != e ; ++i) {
3190 FormatAttr *f = *i;
3191 if (f->getType() == Format &&
3192 f->getFormatIdx() == FormatIdx &&
3193 f->getFirstArg() == FirstArg) {
3194 // If we don't have a valid location for this attribute, adopt the
3195 // location.
3196 if (f->getLocation().isInvalid())
3197 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003198 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003199 }
3200 }
3201
Michael Han99315932013-01-24 16:46:58 +00003202 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3203 AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003204}
3205
Mike Stumpd3bb5572009-07-24 19:02:52 +00003206/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003207/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003208static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003209
Chris Lattner4a927cb2008-06-28 23:36:30 +00003210 if (!Attr.getParameterName()) {
Aaron Ballman29982272013-07-23 14:03:57 +00003211 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3212 << Attr.getName() << 1 << ArgumentString;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003213 return;
3214 }
3215
Chris Lattner4a927cb2008-06-28 23:36:30 +00003216 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003217 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003218 return;
3219 }
3220
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003221 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003222 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003223 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003224 return;
3225 }
3226
Chandler Carruth743682b2010-11-16 08:35:43 +00003227 // In C++ the implicit 'this' function parameter also counts, and they are
3228 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003229 bool HasImplicitThisParam = isInstanceMethod(D);
3230 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003231 unsigned FirstIdx = 1;
3232
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003233 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003234
3235 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003236 if (Format.startswith("__") && Format.endswith("__"))
3237 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003238
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003239 // Check for supported formats.
3240 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003241
3242 if (Kind == IgnoredFormat)
3243 return;
3244
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003245 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003246 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003247 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003248 return;
3249 }
3250
3251 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003252 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003253 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003254 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3255 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003256 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3257 << Attr.getName() << 2 << ArgumentIntegerConstant
3258 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003259 return;
3260 }
3261
3262 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003263 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003264 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003265 return;
3266 }
3267
3268 // FIXME: Do we need to bounds check?
3269 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003270
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003271 if (HasImplicitThisParam) {
3272 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003273 S.Diag(Attr.getLoc(),
3274 diag::err_format_attribute_implicit_this_format_string)
3275 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003276 return;
3277 }
3278 ArgIdx--;
3279 }
Mike Stump11289f42009-09-09 15:08:12 +00003280
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003281 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003282 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003283
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003284 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003285 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003286 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3287 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003288 return;
3289 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003290 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003291 // FIXME: do we need to check if the type is NSString*? What are the
3292 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003293 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003294 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003295 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3296 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003297 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003298 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003299 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003300 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003301 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003302 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3303 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003304 return;
3305 }
3306
3307 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003308 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003309 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003310 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3311 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003312 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3313 << Attr.getName() << 3 << ArgumentIntegerConstant
3314 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003315 return;
3316 }
3317
3318 // check if the function is variadic if the 3rd argument non-zero
3319 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003320 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003321 ++NumArgs; // +1 for ...
3322 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003323 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003324 return;
3325 }
3326 }
3327
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003328 // strftime requires FirstArg to be 0 because it doesn't read from any
3329 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003330 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003331 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003332 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3333 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003334 return;
3335 }
3336 // if 0 it disables parameter checking (to use with e.g. va_list)
3337 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003338 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003339 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003340 return;
3341 }
3342
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003343 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3344 Idx.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003345 FirstArg.getZExtValue(),
3346 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003347 if (NewAttr)
3348 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003349}
3350
Chandler Carruthedc2c642011-07-02 00:01:44 +00003351static void handleTransparentUnionAttr(Sema &S, Decl *D,
3352 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003353 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003354 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003355 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003356
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003357
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003358 // Try to find the underlying union declaration.
3359 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003360 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003361 if (TD && TD->getUnderlyingType()->isUnionType())
3362 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3363 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003364 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003365
3366 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003367 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003368 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003369 return;
3370 }
3371
John McCallf937c022011-10-07 06:10:15 +00003372 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003373 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003374 diag::warn_transparent_union_attribute_not_definition);
3375 return;
3376 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003377
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003378 RecordDecl::field_iterator Field = RD->field_begin(),
3379 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003380 if (Field == FieldEnd) {
3381 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3382 return;
3383 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003384
David Blaikie40ed2972012-06-06 20:45:41 +00003385 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003386 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003387 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003388 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003389 diag::warn_transparent_union_attribute_floating)
3390 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003391 return;
3392 }
3393
3394 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3395 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3396 for (; Field != FieldEnd; ++Field) {
3397 QualType FieldType = Field->getType();
3398 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3399 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3400 // Warn if we drop the attribute.
3401 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003402 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003403 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003404 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003405 diag::warn_transparent_union_attribute_field_size_align)
3406 << isSize << Field->getDeclName() << FieldBits;
3407 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003408 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003409 diag::note_transparent_union_first_field_size_align)
3410 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003411 return;
3412 }
3413 }
3414
Michael Han99315932013-01-24 16:46:58 +00003415 RD->addAttr(::new (S.Context)
3416 TransparentUnionAttr(Attr.getRange(), S.Context,
3417 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003418}
3419
Chandler Carruthedc2c642011-07-02 00:01:44 +00003420static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003421 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003422 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003423 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003424
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003425 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003426 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003427
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003428 // Make sure that there is a string literal as the annotation's single
3429 // argument.
3430 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003431 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003432 return;
3433 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003434
3435 // Don't duplicate annotations that are already set.
3436 for (specific_attr_iterator<AnnotateAttr>
3437 i = D->specific_attr_begin<AnnotateAttr>(),
3438 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3439 if ((*i)->getAnnotation() == SE->getString())
3440 return;
3441 }
Michael Han99315932013-01-24 16:46:58 +00003442
3443 D->addAttr(::new (S.Context)
3444 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3445 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003446}
3447
Chandler Carruthedc2c642011-07-02 00:01:44 +00003448static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003449 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003450 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003451 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003452 return;
3453 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003454
Richard Smith848e1f12013-02-01 08:12:08 +00003455 if (Attr.getNumArgs() == 0) {
3456 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3457 true, 0, Attr.getAttributeSpellingListIndex()));
3458 return;
3459 }
3460
Richard Smith44c247f2013-02-22 08:32:16 +00003461 Expr *E = Attr.getArg(0);
3462 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3463 S.Diag(Attr.getEllipsisLoc(),
3464 diag::err_pack_expansion_without_parameter_packs);
3465 return;
3466 }
3467
3468 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3469 return;
3470
3471 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3472 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003473}
3474
3475void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003476 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003477 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3478 SourceLocation AttrLoc = AttrRange.getBegin();
3479
Richard Smith1dba27c2013-01-29 09:02:09 +00003480 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003481 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003482 // C++11 [dcl.align]p1:
3483 // An alignment-specifier may be applied to a variable or to a class
3484 // data member, but it shall not be applied to a bit-field, a function
3485 // parameter, the formal parameter of a catch clause, or a variable
3486 // declared with the register storage class specifier. An
3487 // alignment-specifier may also be applied to the declaration of a class
3488 // or enumeration type.
3489 // C11 6.7.5/2:
3490 // An alignment attribute shall not be specified in a declaration of
3491 // a typedef, or a bit-field, or a function, or a parameter, or an
3492 // object declared with the register storage-class specifier.
3493 int DiagKind = -1;
3494 if (isa<ParmVarDecl>(D)) {
3495 DiagKind = 0;
3496 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3497 if (VD->getStorageClass() == SC_Register)
3498 DiagKind = 1;
3499 if (VD->isExceptionVariable())
3500 DiagKind = 2;
3501 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3502 if (FD->isBitField())
3503 DiagKind = 3;
3504 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003505 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3506 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003507 << (TmpAttr.isC11() ? ExpectedVariableOrField
3508 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003509 return;
3510 }
3511 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003512 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003513 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003514 return;
3515 }
3516 }
3517
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003518 if (E->isTypeDependent() || E->isValueDependent()) {
3519 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003520 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3521 AA->setPackExpansion(IsPackExpansion);
3522 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003523 return;
3524 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003525
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003526 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003527 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003528 ExprResult ICE
3529 = VerifyIntegerConstantExpression(E, &Alignment,
3530 diag::err_aligned_attribute_argument_not_int,
3531 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003532 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003533 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003534
3535 // C++11 [dcl.align]p2:
3536 // -- if the constant expression evaluates to zero, the alignment
3537 // specifier shall have no effect
3538 // C11 6.7.5p6:
3539 // An alignment specification of zero has no effect.
3540 if (!(TmpAttr.isAlignas() && !Alignment) &&
3541 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003542 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3543 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003544 return;
3545 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003546
Richard Smith848e1f12013-02-01 08:12:08 +00003547 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003548 // We've already verified it's a power of 2, now let's make sure it's
3549 // 8192 or less.
3550 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003551 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003552 << E->getSourceRange();
3553 return;
3554 }
3555 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003556
Richard Smith44c247f2013-02-22 08:32:16 +00003557 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3558 ICE.take(), SpellingListIndex);
3559 AA->setPackExpansion(IsPackExpansion);
3560 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003561}
3562
Michael Hanaf02bbe2013-02-01 01:19:17 +00003563void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003564 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003565 // FIXME: Cache the number on the Attr object if non-dependent?
3566 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003567 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3568 SpellingListIndex);
3569 AA->setPackExpansion(IsPackExpansion);
3570 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003571}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003572
Richard Smith848e1f12013-02-01 08:12:08 +00003573void Sema::CheckAlignasUnderalignment(Decl *D) {
3574 assert(D->hasAttrs() && "no attributes on decl");
3575
3576 QualType Ty;
3577 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3578 Ty = VD->getType();
3579 else
3580 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003581 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003582 return;
3583
3584 // C++11 [dcl.align]p5, C11 6.7.5/4:
3585 // The combined effect of all alignment attributes in a declaration shall
3586 // not specify an alignment that is less strict than the alignment that
3587 // would otherwise be required for the entity being declared.
3588 AlignedAttr *AlignasAttr = 0;
3589 unsigned Align = 0;
3590 for (specific_attr_iterator<AlignedAttr>
3591 I = D->specific_attr_begin<AlignedAttr>(),
3592 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3593 if (I->isAlignmentDependent())
3594 return;
3595 if (I->isAlignas())
3596 AlignasAttr = *I;
3597 Align = std::max(Align, I->getAlignment(Context));
3598 }
3599
3600 if (AlignasAttr && Align) {
3601 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3602 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3603 if (NaturalAlign > RequestedAlign)
3604 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3605 << Ty << (unsigned)NaturalAlign.getQuantity();
3606 }
3607}
3608
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003609/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003610/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003611///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003612/// Despite what would be logical, the mode attribute is a decl attribute, not a
3613/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3614/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003615static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003616 // This attribute isn't documented, but glibc uses it. It changes
3617 // the width of an int or unsigned int to the specified size.
3618
3619 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003620 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003621 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003622
Chris Lattneracbc2d22008-06-27 22:18:37 +00003623
3624 IdentifierInfo *Name = Attr.getParameterName();
3625 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003626 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003627 return;
3628 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003629
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003630 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003631
3632 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003633 if (Str.startswith("__") && Str.endswith("__"))
3634 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003635
3636 unsigned DestWidth = 0;
3637 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003638 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003639 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003640 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003641 switch (Str[0]) {
3642 case 'Q': DestWidth = 8; break;
3643 case 'H': DestWidth = 16; break;
3644 case 'S': DestWidth = 32; break;
3645 case 'D': DestWidth = 64; break;
3646 case 'X': DestWidth = 96; break;
3647 case 'T': DestWidth = 128; break;
3648 }
3649 if (Str[1] == 'F') {
3650 IntegerMode = false;
3651 } else if (Str[1] == 'C') {
3652 IntegerMode = false;
3653 ComplexMode = true;
3654 } else if (Str[1] != 'I') {
3655 DestWidth = 0;
3656 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003657 break;
3658 case 4:
3659 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3660 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003661 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003662 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003663 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003664 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003665 break;
3666 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003667 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003668 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003669 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003670 case 11:
3671 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003672 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003673 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003674 }
3675
3676 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003677 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003678 OldTy = TD->getUnderlyingType();
3679 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3680 OldTy = VD->getType();
3681 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003682 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003683 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003684 return;
3685 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003686
John McCall9dd450b2009-09-21 23:43:11 +00003687 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003688 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3689 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003690 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003691 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3692 } else if (ComplexMode) {
3693 if (!OldTy->isComplexType())
3694 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3695 } else {
3696 if (!OldTy->isFloatingType())
3697 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3698 }
3699
Mike Stump87c57ac2009-05-16 07:39:55 +00003700 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3701 // and friends, at least with glibc.
3702 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3703 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003704 // FIXME: Make sure floating-point mappings are accurate
3705 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003706 QualType NewTy;
3707 switch (DestWidth) {
3708 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003709 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003710 return;
3711 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003712 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003713 return;
3714 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003715 if (!IntegerMode) {
3716 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3717 return;
3718 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003719 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003720 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003721 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003722 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003723 break;
3724 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003725 if (!IntegerMode) {
3726 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3727 return;
3728 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003729 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003730 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003731 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003732 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003733 break;
3734 case 32:
3735 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003736 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003737 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003738 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003739 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003740 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003741 break;
3742 case 64:
3743 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003744 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003745 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003746 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003747 NewTy = S.Context.LongTy;
3748 else
3749 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003750 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003751 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003752 NewTy = S.Context.UnsignedLongTy;
3753 else
3754 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003755 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003756 case 96:
3757 NewTy = S.Context.LongDoubleTy;
3758 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003759 case 128:
Roman Divackyac6f5f42013-07-03 21:08:41 +00003760 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3761 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedman1efaaea2009-02-13 02:31:07 +00003762 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3763 return;
3764 }
Roman Divackyb6debb62013-07-03 20:48:06 +00003765 if (IntegerMode) {
3766 if (OldTy->isSignedIntegerType())
3767 NewTy = S.Context.Int128Ty;
3768 else
3769 NewTy = S.Context.UnsignedInt128Ty;
3770 } else
3771 NewTy = S.Context.LongDoubleTy;
Eli Friedman4735374e2009-03-03 06:41:03 +00003772 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003773 }
3774
Eli Friedman4735374e2009-03-03 06:41:03 +00003775 if (ComplexMode) {
3776 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003777 }
3778
3779 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003780 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3781 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3782 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003783 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003784
3785 D->addAttr(::new (S.Context)
3786 ModeAttr(Attr.getRange(), S.Context, Name,
3787 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003788}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003789
Chandler Carruthedc2c642011-07-02 00:01:44 +00003790static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003791 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003792 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003793 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003794
Nick Lewycky08597072012-07-24 01:40:49 +00003795 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3796 if (!VD->hasGlobalStorage())
3797 S.Diag(Attr.getLoc(),
3798 diag::warn_attribute_requires_functions_or_static_globals)
3799 << Attr.getName();
3800 } else if (!isFunctionOrMethod(D)) {
3801 S.Diag(Attr.getLoc(),
3802 diag::warn_attribute_requires_functions_or_static_globals)
3803 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003804 return;
3805 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003806
Michael Han99315932013-01-24 16:46:58 +00003807 D->addAttr(::new (S.Context)
3808 NoDebugAttr(Attr.getRange(), S.Context,
3809 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003810}
3811
Chandler Carruthedc2c642011-07-02 00:01:44 +00003812static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003813 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003814 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003815 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003816
Mike Stumpd3bb5572009-07-24 19:02:52 +00003817
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003818 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003820 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003821 return;
3822 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003823
Michael Han99315932013-01-24 16:46:58 +00003824 D->addAttr(::new (S.Context)
3825 NoInlineAttr(Attr.getRange(), S.Context,
3826 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003827}
3828
Chandler Carruthedc2c642011-07-02 00:01:44 +00003829static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3830 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003831 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003832 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003833 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003834
Chris Lattner3c77a352010-06-22 00:03:40 +00003835
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003836 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003837 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003838 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003839 return;
3840 }
3841
Michael Han99315932013-01-24 16:46:58 +00003842 D->addAttr(::new (S.Context)
3843 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3844 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003845}
3846
Chandler Carruthedc2c642011-07-02 00:01:44 +00003847static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003848 if (S.LangOpts.CUDA) {
3849 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003850 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003851 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3852 return;
3853 }
3854
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003855 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003856 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003857 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003858 return;
3859 }
3860
Michael Han99315932013-01-24 16:46:58 +00003861 D->addAttr(::new (S.Context)
3862 CUDAConstantAttr(Attr.getRange(), S.Context,
3863 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003864 } else {
3865 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3866 }
3867}
3868
Chandler Carruthedc2c642011-07-02 00:01:44 +00003869static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003870 if (S.LangOpts.CUDA) {
3871 // check the attribute arguments.
3872 if (Attr.getNumArgs() != 0) {
3873 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3874 return;
3875 }
3876
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003877 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003878 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003879 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003880 return;
3881 }
3882
Michael Han99315932013-01-24 16:46:58 +00003883 D->addAttr(::new (S.Context)
3884 CUDADeviceAttr(Attr.getRange(), S.Context,
3885 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003886 } else {
3887 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3888 }
3889}
3890
Chandler Carruthedc2c642011-07-02 00:01:44 +00003891static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003892 if (S.LangOpts.CUDA) {
3893 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003894 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003895 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003896
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003897 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003898 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003899 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003900 return;
3901 }
3902
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003903 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003904 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003905 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003906 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003907 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3908 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003909 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003910 "void");
3911 } else {
3912 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3913 << FD->getType();
3914 }
3915 return;
3916 }
3917
Michael Han99315932013-01-24 16:46:58 +00003918 D->addAttr(::new (S.Context)
3919 CUDAGlobalAttr(Attr.getRange(), S.Context,
3920 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003921 } else {
3922 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3923 }
3924}
3925
Chandler Carruthedc2c642011-07-02 00:01:44 +00003926static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003927 if (S.LangOpts.CUDA) {
3928 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003929 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003930 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003931
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003932
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003933 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003934 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003935 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003936 return;
3937 }
3938
Michael Han99315932013-01-24 16:46:58 +00003939 D->addAttr(::new (S.Context)
3940 CUDAHostAttr(Attr.getRange(), S.Context,
3941 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003942 } else {
3943 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3944 }
3945}
3946
Chandler Carruthedc2c642011-07-02 00:01:44 +00003947static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003948 if (S.LangOpts.CUDA) {
3949 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003950 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003951 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003952
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003953 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003954 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003955 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003956 return;
3957 }
3958
Michael Han99315932013-01-24 16:46:58 +00003959 D->addAttr(::new (S.Context)
3960 CUDASharedAttr(Attr.getRange(), S.Context,
3961 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003962 } else {
3963 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3964 }
3965}
3966
Chandler Carruthedc2c642011-07-02 00:01:44 +00003967static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003968 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003969 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003970 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003971
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003972 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003973 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003974 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003975 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003976 return;
3977 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003978
Douglas Gregor35b57532009-10-27 21:01:01 +00003979 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003980 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003981 return;
3982 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003983
Michael Han99315932013-01-24 16:46:58 +00003984 D->addAttr(::new (S.Context)
3985 GNUInlineAttr(Attr.getRange(), S.Context,
3986 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003987}
3988
Chandler Carruthedc2c642011-07-02 00:01:44 +00003989static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003990 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003991
Aaron Ballman02df2e02012-12-09 17:45:41 +00003992 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003993 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003994 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3995 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003996 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003997 return;
3998
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003999 if (!isa<ObjCMethodDecl>(D)) {
4000 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4001 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004002 return;
4003 }
4004
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004005 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004006 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004007 D->addAttr(::new (S.Context)
4008 FastCallAttr(Attr.getRange(), S.Context,
4009 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004010 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004011 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004012 D->addAttr(::new (S.Context)
4013 StdCallAttr(Attr.getRange(), S.Context,
4014 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004015 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004016 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004017 D->addAttr(::new (S.Context)
4018 ThisCallAttr(Attr.getRange(), S.Context,
4019 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004020 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004021 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004022 D->addAttr(::new (S.Context)
4023 CDeclAttr(Attr.getRange(), S.Context,
4024 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004025 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004026 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004027 D->addAttr(::new (S.Context)
4028 PascalAttr(Attr.getRange(), S.Context,
4029 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004030 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004031 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004032 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004033 switch (CC) {
4034 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004035 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004036 break;
4037 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004038 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004039 break;
4040 default:
4041 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004042 }
4043
Michael Han99315932013-01-24 16:46:58 +00004044 D->addAttr(::new (S.Context)
4045 PcsAttr(Attr.getRange(), S.Context, PCS,
4046 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004047 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004048 }
Derek Schuffa2020962012-10-16 22:30:41 +00004049 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00004050 D->addAttr(::new (S.Context)
4051 PnaclCallAttr(Attr.getRange(), S.Context,
4052 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004053 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00004054 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004055 D->addAttr(::new (S.Context)
4056 IntelOclBiccAttr(Attr.getRange(), S.Context,
4057 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004058 return;
Derek Schuffa2020962012-10-16 22:30:41 +00004059
Abramo Bagnara50099372010-04-30 13:10:51 +00004060 default:
4061 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004062 }
4063}
4064
Chandler Carruthedc2c642011-07-02 00:01:44 +00004065static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00004066 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004067 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004068}
4069
Guy Benyeifb36ede2013-03-24 13:58:12 +00004070static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4071 assert(!Attr.isInvalid());
4072
4073 Expr *E = Attr.getArg(0);
4074 llvm::APSInt ArgNum(32);
4075 if (E->isTypeDependent() || E->isValueDependent() ||
4076 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4077 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4078 << Attr.getName()->getName() << E->getSourceRange();
4079 return;
4080 }
4081
4082 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4083 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4084}
4085
Aaron Ballman02df2e02012-12-09 17:45:41 +00004086bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4087 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00004088 if (attr.isInvalid())
4089 return true;
4090
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004091 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4092 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
4093 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00004094 attr.setInvalid();
4095 return true;
4096 }
4097
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004098 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4099 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00004100 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004101 case AttributeList::AT_CDecl: CC = CC_C; break;
4102 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4103 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4104 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4105 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4106 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004107 Expr *Arg = attr.getArg(0);
4108 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004109 if (!Str || !Str->isAscii()) {
Aaron Ballman29982272013-07-23 14:03:57 +00004110 Diag(attr.getLoc(), diag::err_attribute_argument_n_type)
4111 << attr.getName() << 1 << ArgumentString;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004112 attr.setInvalid();
4113 return true;
4114 }
4115
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004116 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004117 if (StrRef == "aapcs") {
4118 CC = CC_AAPCS;
4119 break;
4120 } else if (StrRef == "aapcs-vfp") {
4121 CC = CC_AAPCS_VFP;
4122 break;
4123 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004124
4125 attr.setInvalid();
4126 Diag(attr.getLoc(), diag::err_invalid_pcs);
4127 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004128 }
Derek Schuffa2020962012-10-16 22:30:41 +00004129 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00004130 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00004131 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004132 }
4133
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004134 const TargetInfo &TI = Context.getTargetInfo();
4135 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4136 if (A == TargetInfo::CCCR_Warning) {
4137 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00004138
4139 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4140 if (FD)
4141 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4142 TargetInfo::CCMT_NonMember;
4143 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004144 }
4145
John McCall3882ace2011-01-05 12:14:39 +00004146 return false;
4147}
4148
Chandler Carruthedc2c642011-07-02 00:01:44 +00004149static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004150 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00004151
4152 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004153 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00004154 return;
4155
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004156 if (!isa<ObjCMethodDecl>(D)) {
4157 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4158 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004159 return;
4160 }
Eli Friedman7044b762009-03-27 21:06:47 +00004161
Michael Han99315932013-01-24 16:46:58 +00004162 D->addAttr(::new (S.Context)
4163 RegparmAttr(Attr.getRange(), S.Context, numParams,
4164 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00004165}
4166
4167/// Checks a regparm attribute, returning true if it is ill-formed and
4168/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004169bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4170 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004171 return true;
4172
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00004173 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004174 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004175 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004176 }
Eli Friedman7044b762009-03-27 21:06:47 +00004177
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004178 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00004179 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00004180 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00004181 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004182 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00004183 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004184 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004185 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004186 }
4187
Douglas Gregore8bbc122011-09-02 00:18:52 +00004188 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004189 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004190 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004191 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004192 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004193 }
4194
John McCall3882ace2011-01-05 12:14:39 +00004195 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00004196 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004197 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004198 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004199 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004200 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004201 }
4202
John McCall3882ace2011-01-05 12:14:39 +00004203 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004204}
4205
Chandler Carruthedc2c642011-07-02 00:01:44 +00004206static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004207 if (S.LangOpts.CUDA) {
4208 // check the attribute arguments.
4209 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004210 // FIXME: 0 is not okay.
4211 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004212 return;
4213 }
4214
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004215 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004216 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004217 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004218 return;
4219 }
4220
4221 Expr *MaxThreadsExpr = Attr.getArg(0);
4222 llvm::APSInt MaxThreads(32);
4223 if (MaxThreadsExpr->isTypeDependent() ||
4224 MaxThreadsExpr->isValueDependent() ||
4225 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004226 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4227 << Attr.getName() << 1 << ArgumentIntegerConstant
4228 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne827301e2010-12-12 23:03:07 +00004229 return;
4230 }
4231
4232 llvm::APSInt MinBlocks(32);
4233 if (Attr.getNumArgs() > 1) {
4234 Expr *MinBlocksExpr = Attr.getArg(1);
4235 if (MinBlocksExpr->isTypeDependent() ||
4236 MinBlocksExpr->isValueDependent() ||
4237 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004238 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4239 << Attr.getName() << 2 << ArgumentIntegerConstant
4240 << MinBlocksExpr->getSourceRange();
Peter Collingbourne827301e2010-12-12 23:03:07 +00004241 return;
4242 }
4243 }
4244
Michael Han99315932013-01-24 16:46:58 +00004245 D->addAttr(::new (S.Context)
4246 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4247 MaxThreads.getZExtValue(),
4248 MinBlocks.getZExtValue(),
4249 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004250 } else {
4251 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4252 }
4253}
4254
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004255static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4256 const AttributeList &Attr) {
4257 StringRef AttrName = Attr.getName()->getName();
4258 if (!Attr.getParameterName()) {
Aaron Ballman29982272013-07-23 14:03:57 +00004259 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4260 << Attr.getName() << /* arg num = */ 1 << ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004261 return;
4262 }
4263
4264 if (Attr.getNumArgs() != 2) {
4265 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4266 << /* required args = */ 3;
4267 return;
4268 }
4269
4270 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4271
4272 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4273 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4274 << Attr.getName() << ExpectedFunctionOrMethod;
4275 return;
4276 }
4277
4278 uint64_t ArgumentIdx;
4279 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4280 Attr.getLoc(), 2,
4281 Attr.getArg(0), ArgumentIdx))
4282 return;
4283
4284 uint64_t TypeTagIdx;
4285 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4286 Attr.getLoc(), 3,
4287 Attr.getArg(1), TypeTagIdx))
4288 return;
4289
4290 bool IsPointer = (AttrName == "pointer_with_type_tag");
4291 if (IsPointer) {
4292 // Ensure that buffer has a pointer type.
4293 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4294 if (!BufferTy->isPointerType()) {
4295 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004296 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004297 }
4298 }
4299
Michael Han99315932013-01-24 16:46:58 +00004300 D->addAttr(::new (S.Context)
4301 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4302 ArgumentIdx, TypeTagIdx, IsPointer,
4303 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004304}
4305
4306static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4307 const AttributeList &Attr) {
4308 IdentifierInfo *PointerKind = Attr.getParameterName();
4309 if (!PointerKind) {
Aaron Ballman29982272013-07-23 14:03:57 +00004310 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4311 << Attr.getName() << 1 << ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004312 return;
4313 }
4314
4315 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4316
Michael Han99315932013-01-24 16:46:58 +00004317 D->addAttr(::new (S.Context)
4318 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4319 MatchingCType,
4320 Attr.getLayoutCompatible(),
4321 Attr.getMustBeNull(),
4322 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004323}
4324
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004325//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004326// Checker-specific attribute handlers.
4327//===----------------------------------------------------------------------===//
4328
John McCalled433932011-01-25 03:31:58 +00004329static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004330 return type->isDependentType() ||
4331 type->isObjCObjectPointerType() ||
4332 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004333}
4334static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004335 return type->isDependentType() ||
4336 type->isPointerType() ||
4337 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004338}
4339
Chandler Carruthedc2c642011-07-02 00:01:44 +00004340static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004341 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004342 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004343 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004344 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004345 return;
4346 }
4347
4348 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004349 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004350 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4351 cf = false;
4352 } else {
4353 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4354 cf = true;
4355 }
4356
4357 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004358 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004359 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004360 return;
4361 }
4362
4363 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004364 param->addAttr(::new (S.Context)
4365 CFConsumedAttr(Attr.getRange(), S.Context,
4366 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004367 else
Michael Han99315932013-01-24 16:46:58 +00004368 param->addAttr(::new (S.Context)
4369 NSConsumedAttr(Attr.getRange(), S.Context,
4370 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004371}
4372
Chandler Carruthedc2c642011-07-02 00:01:44 +00004373static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4374 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004375 if (!isa<ObjCMethodDecl>(D)) {
4376 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004377 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004378 return;
4379 }
4380
Michael Han99315932013-01-24 16:46:58 +00004381 D->addAttr(::new (S.Context)
4382 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4383 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004384}
4385
Chandler Carruthedc2c642011-07-02 00:01:44 +00004386static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4387 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004388
John McCalled433932011-01-25 03:31:58 +00004389 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004390
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004391 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004392 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004393 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004394 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004395 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004396 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4397 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004398 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004399 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004400 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004401 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004402 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004403 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004404 return;
4405 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004406
John McCalled433932011-01-25 03:31:58 +00004407 bool typeOK;
4408 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004409 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004410 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004411 case AttributeList::AT_NSReturnsAutoreleased:
4412 case AttributeList::AT_NSReturnsRetained:
4413 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004414 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4415 cf = false;
4416 break;
4417
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004418 case AttributeList::AT_CFReturnsRetained:
4419 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004420 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4421 cf = true;
4422 break;
4423 }
4424
4425 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004426 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004427 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004428 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004429 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004430
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004431 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004432 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004433 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004434 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004435 D->addAttr(::new (S.Context)
4436 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4437 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004438 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004439 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004440 D->addAttr(::new (S.Context)
4441 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4442 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004443 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004444 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004445 D->addAttr(::new (S.Context)
4446 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4447 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004448 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004449 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004450 D->addAttr(::new (S.Context)
4451 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4452 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004453 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004454 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004455 D->addAttr(::new (S.Context)
4456 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4457 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004458 return;
4459 };
4460}
4461
John McCallcf166702011-07-22 08:53:00 +00004462static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4463 const AttributeList &attr) {
4464 SourceLocation loc = attr.getLoc();
4465
4466 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4467
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004468 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00004469 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004470 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00004471 return;
4472 }
4473
4474 // Check that the method returns a normal pointer.
4475 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004476
4477 if (!resultType->isReferenceType() &&
4478 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00004479 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4480 << SourceRange(loc)
4481 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4482
4483 // Drop the attribute.
4484 return;
4485 }
4486
Michael Han99315932013-01-24 16:46:58 +00004487 method->addAttr(::new (S.Context)
4488 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4489 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004490}
4491
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004492static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4493 const AttributeList &attr) {
4494 SourceLocation loc = attr.getLoc();
4495 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4496
4497 if (!method) {
4498 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4499 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4500 return;
4501 }
4502 DeclContext *DC = method->getDeclContext();
4503 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4504 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4505 << attr.getName() << 0;
4506 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4507 return;
4508 }
4509 if (method->getMethodFamily() == OMF_dealloc) {
4510 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4511 << attr.getName() << 1;
4512 return;
4513 }
4514
Michael Han99315932013-01-24 16:46:58 +00004515 method->addAttr(::new (S.Context)
4516 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4517 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004518}
4519
John McCall32f5fe12011-09-30 05:12:12 +00004520/// Handle cf_audited_transfer and cf_unknown_transfer.
4521static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4522 if (!isa<FunctionDecl>(D)) {
4523 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004524 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004525 return;
4526 }
4527
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004528 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004529
4530 // Check whether there's a conflicting attribute already present.
4531 Attr *Existing;
4532 if (IsAudited) {
4533 Existing = D->getAttr<CFUnknownTransferAttr>();
4534 } else {
4535 Existing = D->getAttr<CFAuditedTransferAttr>();
4536 }
4537 if (Existing) {
4538 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4539 << A.getName()
4540 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4541 << A.getRange() << Existing->getRange();
4542 return;
4543 }
4544
4545 // All clear; add the attribute.
4546 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004547 D->addAttr(::new (S.Context)
4548 CFAuditedTransferAttr(A.getRange(), S.Context,
4549 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004550 } else {
Michael Han99315932013-01-24 16:46:58 +00004551 D->addAttr(::new (S.Context)
4552 CFUnknownTransferAttr(A.getRange(), S.Context,
4553 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004554 }
4555}
4556
John McCallf1e8b342011-09-29 07:17:38 +00004557static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4558 const AttributeList &Attr) {
4559 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4560 if (!RD || RD->isUnion()) {
4561 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004562 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004563 }
4564
4565 IdentifierInfo *ParmName = Attr.getParameterName();
4566
4567 // In Objective-C, verify that the type names an Objective-C type.
4568 // We don't want to check this outside of ObjC because people sometimes
4569 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004570 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004571 // Check for an existing type with this name.
4572 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4573 Sema::LookupOrdinaryName);
4574 if (S.LookupName(R, Sc)) {
4575 NamedDecl *Target = R.getFoundDecl();
4576 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4577 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4578 S.Diag(Target->getLocStart(), diag::note_declared_at);
4579 }
4580 }
4581 }
4582
Michael Han99315932013-01-24 16:46:58 +00004583 D->addAttr(::new (S.Context)
4584 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4585 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004586}
4587
Chandler Carruthedc2c642011-07-02 00:01:44 +00004588static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4589 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004590 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004591
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004592 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004593 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004594}
4595
Chandler Carruthedc2c642011-07-02 00:01:44 +00004596static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4597 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004598 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004599 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004600 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004601 return;
4602 }
4603
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004604 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004605 QualType type = vd->getType();
4606
4607 if (!type->isDependentType() &&
4608 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004609 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004610 << type;
4611 return;
4612 }
4613
4614 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4615
4616 // If we have no lifetime yet, check the lifetime we're presumably
4617 // going to infer.
4618 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4619 lifetime = type->getObjCARCImplicitLifetime();
4620
4621 switch (lifetime) {
4622 case Qualifiers::OCL_None:
4623 assert(type->isDependentType() &&
4624 "didn't infer lifetime for non-dependent type?");
4625 break;
4626
4627 case Qualifiers::OCL_Weak: // meaningful
4628 case Qualifiers::OCL_Strong: // meaningful
4629 break;
4630
4631 case Qualifiers::OCL_ExplicitNone:
4632 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004633 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004634 << (lifetime == Qualifiers::OCL_Autoreleasing);
4635 break;
4636 }
4637
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004638 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004639 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4640 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004641}
4642
Francois Picheta83957a2010-12-19 06:50:37 +00004643//===----------------------------------------------------------------------===//
4644// Microsoft specific attribute handlers.
4645//===----------------------------------------------------------------------===//
4646
Reid Kleckner140c4a72013-05-17 14:04:52 +00004647// Check if MS extensions or some other language extensions are enabled. If
4648// not, issue a diagnostic that the given attribute is unused.
4649static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4650 bool OtherExtension = false) {
4651 if (S.LangOpts.MicrosoftExt || OtherExtension)
4652 return true;
4653 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4654 return false;
4655}
4656
Chandler Carruthedc2c642011-07-02 00:01:44 +00004657static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004658 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4659 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004660
Reid Kleckner140c4a72013-05-17 14:04:52 +00004661 // check the attribute arguments.
4662 if (!checkAttributeNumArgs(S, Attr, 1))
4663 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004664
Reid Kleckner140c4a72013-05-17 14:04:52 +00004665 Expr *Arg = Attr.getArg(0);
4666 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4667 if (!Str || !Str->isAscii()) {
Aaron Ballman29982272013-07-23 14:03:57 +00004668 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4669 << Attr.getName() << 1 << ArgumentString;
Reid Kleckner140c4a72013-05-17 14:04:52 +00004670 return;
4671 }
Francois Pichet7da11662010-12-20 01:41:49 +00004672
Reid Kleckner140c4a72013-05-17 14:04:52 +00004673 StringRef StrRef = Str->getString();
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004674
Reid Kleckner140c4a72013-05-17 14:04:52 +00004675 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4676 StrRef.back() == '}';
Francois Pichet7da11662010-12-20 01:41:49 +00004677
Reid Kleckner140c4a72013-05-17 14:04:52 +00004678 // Validate GUID length.
4679 if (IsCurly && StrRef.size() != 38) {
4680 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4681 return;
4682 }
4683 if (!IsCurly && StrRef.size() != 36) {
4684 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4685 return;
4686 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004687
Reid Kleckner140c4a72013-05-17 14:04:52 +00004688 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4689 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4690 StringRef::iterator I = StrRef.begin();
4691 if (IsCurly) // Skip the optional '{'
4692 ++I;
4693
4694 for (int i = 0; i < 36; ++i) {
4695 if (i == 8 || i == 13 || i == 18 || i == 23) {
4696 if (*I != '-') {
Francois Pichet7da11662010-12-20 01:41:49 +00004697 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4698 return;
4699 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004700 } else if (!isHexDigit(*I)) {
4701 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4702 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004703 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004704 I++;
4705 }
Francois Picheta83957a2010-12-19 06:50:37 +00004706
Reid Kleckner140c4a72013-05-17 14:04:52 +00004707 D->addAttr(::new (S.Context)
4708 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4709 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004710}
4711
John McCall8d32c052012-05-22 21:28:12 +00004712static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004713 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004714 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004715
4716 AttributeList::Kind Kind = Attr.getKind();
4717 if (Kind == AttributeList::AT_SingleInheritance)
4718 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004719 ::new (S.Context)
4720 SingleInheritanceAttr(Attr.getRange(), S.Context,
4721 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004722 else if (Kind == AttributeList::AT_MultipleInheritance)
4723 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004724 ::new (S.Context)
4725 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4726 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004727 else if (Kind == AttributeList::AT_VirtualInheritance)
4728 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004729 ::new (S.Context)
4730 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4731 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004732}
4733
4734static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004735 if (!checkMicrosoftExt(S, Attr))
4736 return;
4737
4738 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballman317a77f2013-05-22 23:25:32 +00004739 if (Kind == AttributeList::AT_Win64)
Reid Kleckner140c4a72013-05-17 14:04:52 +00004740 D->addAttr(
4741 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4742 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004743}
4744
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004745static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004746 if (!checkMicrosoftExt(S, Attr))
4747 return;
4748 D->addAttr(::new (S.Context)
4749 ForceInlineAttr(Attr.getRange(), S.Context,
4750 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004751}
4752
Reid Klecknerb144d362013-05-20 14:02:37 +00004753static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4754 if (!checkMicrosoftExt(S, Attr))
4755 return;
4756 // Check linkage after possibly merging declaratinos. See
4757 // checkAttributesAfterMerging().
4758 D->addAttr(::new (S.Context)
4759 SelectAnyAttr(Attr.getRange(), S.Context,
4760 Attr.getAttributeSpellingListIndex()));
4761}
4762
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004763//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004764// Top Level Sema Entry Points
4765//===----------------------------------------------------------------------===//
4766
Chandler Carruthedc2c642011-07-02 00:01:44 +00004767static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4768 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004769 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004770 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4771 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4772 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004773 default:
4774 break;
4775 }
4776}
Abramo Bagnara50099372010-04-30 13:10:51 +00004777
Chandler Carruthedc2c642011-07-02 00:01:44 +00004778static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4779 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004780 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004781 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4782 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4783 case AttributeList::AT_IBOutletCollection:
4784 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004785 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004786 case AttributeList::AT_ObjCGC:
4787 case AttributeList::AT_VectorSize:
4788 case AttributeList::AT_NeonVectorType:
4789 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004790 case AttributeList::AT_Ptr32:
4791 case AttributeList::AT_Ptr64:
4792 case AttributeList::AT_SPtr:
4793 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004794 // Ignore these, these are type attributes, handled by
4795 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004796 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004797 case AttributeList::AT_CUDADevice:
4798 case AttributeList::AT_CUDAHost:
4799 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004800 // Ignore, this is a non-inheritable attribute, handled
4801 // by ProcessNonInheritableDeclAttr.
4802 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004803 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4804 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4805 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4806 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004807 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004808 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004809 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004810 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004811 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4812 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4813 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004814 handleDependencyAttr(S, scope, D, Attr);
4815 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004816 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4817 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4818 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004819 case AttributeList::AT_CXX11NoReturn:
4820 handleCXX11NoReturnAttr(S, D, Attr);
4821 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004822 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004823 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004824 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004825 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4826 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004827 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004828 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004829 case AttributeList::AT_MinSize:
4830 handleMinSizeAttr(S, D, Attr);
4831 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004832 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4833 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4834 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4835 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4836 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004837 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004838 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004839 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4840 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4841 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4842 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4843 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004844 case AttributeList::AT_ownership_returns:
4845 case AttributeList::AT_ownership_takes:
4846 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004847 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004848 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4849 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4850 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4851 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4852 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4853 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4854 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004855
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004856 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004857 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004858 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004859 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004860
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004861 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004862 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4863
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004864 case AttributeList::AT_ObjCRequiresSuper:
4865 handleObjCRequiresSuperAttr(S, D, Attr); break;
4866
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004867 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004868 handleNSBridgedAttr(S, scope, D, Attr); break;
4869
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004870 case AttributeList::AT_CFAuditedTransfer:
4871 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004872 handleCFTransferAttr(S, D, Attr); break;
4873
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004874 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004875 case AttributeList::AT_CFConsumed:
4876 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4877 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004878 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004879
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004880 case AttributeList::AT_NSReturnsAutoreleased:
4881 case AttributeList::AT_NSReturnsNotRetained:
4882 case AttributeList::AT_CFReturnsNotRetained:
4883 case AttributeList::AT_NSReturnsRetained:
4884 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004885 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004886
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004887 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004888 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004889 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004890
Joey Goulyaba589c2013-03-08 09:42:32 +00004891 case AttributeList::AT_VecTypeHint:
4892 handleVecTypeHint(S, D, Attr); break;
4893
Joey Gouly0608ae82013-03-14 09:54:43 +00004894 case AttributeList::AT_Endian:
4895 handleEndianAttr(S, D, Attr);
4896 break;
4897
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004898 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004899 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004900
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004901 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4902 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4903 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004904 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004905 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004906 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004907 handleArcWeakrefUnavailableAttr (S, D, Attr);
4908 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004909 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004910 handleObjCRootClassAttr(S, D, Attr);
4911 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004912 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004913 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004914 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004915 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4916 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004917 handleReturnsTwiceAttr(S, D, Attr);
4918 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004919 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004920 case AttributeList::AT_Visibility:
4921 handleVisibilityAttr(S, D, Attr, false);
4922 break;
4923 case AttributeList::AT_TypeVisibility:
4924 handleVisibilityAttr(S, D, Attr, true);
4925 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004926 case AttributeList::AT_WarnUnused:
4927 handleWarnUnusedAttr(S, D, Attr);
4928 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004929 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004930 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004931 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4932 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4933 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4934 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004935 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004936 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004937 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004938 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004939 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004940 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004941 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004942 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004943 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4944 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4945 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4946 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4947 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4948 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4949 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4950 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4951 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004952 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004953 // Just ignore
4954 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004955 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004956 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004957 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004958 case AttributeList::AT_StdCall:
4959 case AttributeList::AT_CDecl:
4960 case AttributeList::AT_FastCall:
4961 case AttributeList::AT_ThisCall:
4962 case AttributeList::AT_Pascal:
4963 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004964 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004965 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004966 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004967 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004968 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004969 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004970 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004971 case AttributeList::AT_OpenCLImageAccess:
4972 handleOpenCLImageAccessAttr(S, D, Attr);
4973 break;
John McCall8d32c052012-05-22 21:28:12 +00004974
4975 // Microsoft attributes:
John McCall5e77d762013-04-16 07:28:30 +00004976 case AttributeList::AT_MsProperty: break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004977 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004978 handleMsStructAttr(S, D, Attr);
4979 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004980 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004981 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004982 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004983 case AttributeList::AT_SingleInheritance:
4984 case AttributeList::AT_MultipleInheritance:
4985 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004986 handleInheritanceAttr(S, D, Attr);
4987 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004988 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004989 handlePortabilityAttr(S, D, Attr);
4990 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004991 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004992 handleForceInlineAttr(S, D, Attr);
4993 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004994 case AttributeList::AT_SelectAny:
4995 handleSelectAnyAttr(S, D, Attr);
4996 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004997
4998 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004999 case AttributeList::AT_AssertExclusiveLock:
5000 handleAssertExclusiveLockAttr(S, D, Attr);
5001 break;
5002 case AttributeList::AT_AssertSharedLock:
5003 handleAssertSharedLockAttr(S, D, Attr);
5004 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005005 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005006 handleGuardedVarAttr(S, D, Attr);
5007 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005008 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005009 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005010 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005011 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00005012 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005013 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005014 case AttributeList::AT_NoSanitizeAddress:
5015 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005016 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005017 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005018 handleNoThreadSafetyAnalysis(S, D, Attr);
5019 break;
5020 case AttributeList::AT_NoSanitizeThread:
5021 handleNoSanitizeThread(S, D, Attr);
5022 break;
5023 case AttributeList::AT_NoSanitizeMemory:
5024 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005025 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005026 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005027 handleLockableAttr(S, D, Attr);
5028 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005029 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005030 handleGuardedByAttr(S, D, Attr);
5031 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005032 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005033 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005034 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005035 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005036 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005037 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005038 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00005039 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005040 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005041 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005042 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005043 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005044 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005045 handleLockReturnedAttr(S, D, Attr);
5046 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005047 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005048 handleLocksExcludedAttr(S, D, Attr);
5049 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005050 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005051 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005052 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005053 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00005054 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005055 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005056 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005057 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005058 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005059 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005060 handleUnlockFunAttr(S, D, Attr);
5061 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005062 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005063 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005064 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005065 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005066 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005067 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005068
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005069 // Type safety attributes.
5070 case AttributeList::AT_ArgumentWithTypeTag:
5071 handleArgumentWithTypeTagAttr(S, D, Attr);
5072 break;
5073 case AttributeList::AT_TypeTagForDatatype:
5074 handleTypeTagForDatatypeAttr(S, D, Attr);
5075 break;
5076
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005077 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005078 // Ask target about the attribute.
5079 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5080 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00005081 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5082 diag::warn_unhandled_ms_attribute_ignored :
5083 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005084 break;
5085 }
5086}
5087
Peter Collingbourneb331b262011-01-21 02:08:45 +00005088/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5089/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smith10876ef2013-01-17 01:30:42 +00005090/// silently ignore it if a GNU attribute.
Chandler Carruthedc2c642011-07-02 00:01:44 +00005091static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5092 const AttributeList &Attr,
Richard Smith10876ef2013-01-17 01:30:42 +00005093 bool NonInheritable, bool Inheritable,
5094 bool IncludeCXX11Attributes) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00005095 if (Attr.isInvalid())
5096 return;
5097
Richard Smith10876ef2013-01-17 01:30:42 +00005098 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5099 // instead.
5100 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5101 return;
5102
Peter Collingbourneb331b262011-01-21 02:08:45 +00005103 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00005104 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00005105
5106 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00005107 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00005108}
5109
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005110/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5111/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005112void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005113 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005114 bool NonInheritable, bool Inheritable,
5115 bool IncludeCXX11Attributes) {
5116 for (const AttributeList* l = AttrList; l; l = l->getNext())
5117 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5118 IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005119
5120 // GCC accepts
5121 // static int a9 __attribute__((weakref));
5122 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005123 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00005124 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00005125 cast<NamedDecl>(D)->getNameAsString();
5126 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005127 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005128 }
5129}
5130
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005131// Annotation attributes are the only attributes allowed after an access
5132// specifier.
5133bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5134 const AttributeList *AttrList) {
5135 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005136 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005137 handleAnnotateAttr(*this, ASDecl, *l);
5138 } else {
5139 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5140 return true;
5141 }
5142 }
5143
5144 return false;
5145}
5146
John McCall42856de2011-10-01 05:17:03 +00005147/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5148/// contains any decl attributes that we should warn about.
5149static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5150 for ( ; A; A = A->getNext()) {
5151 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005152 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005153 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5154
5155 if (A->getKind() == AttributeList::UnknownAttribute) {
5156 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5157 << A->getName() << A->getRange();
5158 } else {
5159 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5160 << A->getName() << A->getRange();
5161 }
5162 }
5163}
5164
5165/// checkUnusedDeclAttributes - Given a declarator which is not being
5166/// used to build a declaration, complain about any decl attributes
5167/// which might be lying around on it.
5168void Sema::checkUnusedDeclAttributes(Declarator &D) {
5169 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5170 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5171 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5172 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5173}
5174
Ryan Flynn7d470f32009-07-30 03:15:39 +00005175/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005176/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005177NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5178 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005179 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005180 NamedDecl *NewD = 0;
5181 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005182 FunctionDecl *NewFD;
5183 // FIXME: Missing call to CheckFunctionDeclaration().
5184 // FIXME: Mangling?
5185 // FIXME: Is the qualifier info correct?
5186 // FIXME: Is the DeclContext correct?
5187 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5188 Loc, Loc, DeclarationName(II),
5189 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005190 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005191 FD->hasPrototype(),
5192 false/*isConstexprSpecified*/);
5193 NewD = NewFD;
5194
5195 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005196 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005197
5198 // Fake up parameter variables; they are declared as if this were
5199 // a typedef.
5200 QualType FDTy = FD->getType();
5201 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5202 SmallVector<ParmVarDecl*, 16> Params;
5203 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5204 AE = FT->arg_type_end(); AI != AE; ++AI) {
5205 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5206 Param->setScopeInfo(0, Params.size());
5207 Params.push_back(Param);
5208 }
David Blaikie9c70e042011-09-21 18:16:56 +00005209 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005210 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005211 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5212 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005213 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005214 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005215 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005216 if (VD->getQualifier()) {
5217 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005218 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005219 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005220 }
5221 return NewD;
5222}
5223
James Dennett634962f2012-06-14 21:40:34 +00005224/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005225/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005226void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005227 if (W.getUsed()) return; // only do this once
5228 W.setUsed(true);
5229 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5230 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005231 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005232 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5233 NDId->getName()));
5234 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005235 WeakTopLevelDecl.push_back(NewD);
5236 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5237 // to insert Decl at TU scope, sorry.
5238 DeclContext *SavedContext = CurContext;
5239 CurContext = Context.getTranslationUnitDecl();
5240 PushOnScopeChains(NewD, S);
5241 CurContext = SavedContext;
5242 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005243 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005244 }
5245}
5246
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005247void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5248 // It's valid to "forward-declare" #pragma weak, in which case we
5249 // have to do this.
5250 LoadExternalWeakUndeclaredIdentifiers();
5251 if (!WeakUndeclaredIdentifiers.empty()) {
5252 NamedDecl *ND = NULL;
5253 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5254 if (VD->isExternC())
5255 ND = VD;
5256 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5257 if (FD->isExternC())
5258 ND = FD;
5259 if (ND) {
5260 if (IdentifierInfo *Id = ND->getIdentifier()) {
5261 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5262 = WeakUndeclaredIdentifiers.find(Id);
5263 if (I != WeakUndeclaredIdentifiers.end()) {
5264 WeakInfo W = I->second;
5265 DeclApplyPragmaWeak(S, ND, W);
5266 WeakUndeclaredIdentifiers[Id] = W;
5267 }
5268 }
5269 }
5270 }
5271}
5272
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005273/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5274/// it, apply them to D. This is a bit tricky because PD can have attributes
5275/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005276void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5277 bool NonInheritable, bool Inheritable) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005278 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005279 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005280 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005281
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005282 // Walk the declarator structure, applying decl attributes that were in a type
5283 // position to the decl itself. This handles cases like:
5284 // int *__attr__(x)** D;
5285 // when X is a decl attribute.
5286 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5287 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith10876ef2013-01-17 01:30:42 +00005288 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5289 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005290
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005291 // Finally, apply any attributes on the decl itself.
5292 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005293 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005294}
John McCall28a6aea2009-11-04 02:18:39 +00005295
John McCall31168b02011-06-15 23:02:42 +00005296/// Is the given declaration allowed to use a forbidden type?
5297static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5298 // Private ivars are always okay. Unfortunately, people don't
5299 // always properly make their ivars private, even in system headers.
5300 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005301 // Function declarations in sys headers will be marked unavailable.
5302 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5303 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005304 return false;
5305
5306 // Require it to be declared in a system header.
5307 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5308}
5309
5310/// Handle a delayed forbidden-type diagnostic.
5311static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5312 Decl *decl) {
5313 if (decl && isForbiddenTypeAllowed(S, decl)) {
5314 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5315 "this system declaration uses an unsupported type"));
5316 return;
5317 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005318 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005319 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005320 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005321 // kind of forbidden type messages on unavailable functions.
5322 if (FD->hasAttr<UnavailableAttr>() &&
5323 diag.getForbiddenTypeDiagnostic() ==
5324 diag::err_arc_array_param_no_ownership) {
5325 diag.Triggered = true;
5326 return;
5327 }
5328 }
John McCall31168b02011-06-15 23:02:42 +00005329
5330 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5331 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5332 diag.Triggered = true;
5333}
5334
John McCall2ec85372012-05-07 06:16:41 +00005335void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5336 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005337 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005338 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005339
John McCall2ec85372012-05-07 06:16:41 +00005340 // When delaying diagnostics to run in the context of a parsed
5341 // declaration, we only want to actually emit anything if parsing
5342 // succeeds.
5343 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005344
John McCall2ec85372012-05-07 06:16:41 +00005345 // We emit all the active diagnostics in this pool or any of its
5346 // parents. In general, we'll get one pool for the decl spec
5347 // and a child pool for each declarator; in a decl group like:
5348 // deprecated_typedef foo, *bar, baz();
5349 // only the declarator pops will be passed decls. This is correct;
5350 // we really do need to consider delayed diagnostics from the decl spec
5351 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005352 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005353 do {
John McCall6347b682012-05-07 06:16:58 +00005354 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005355 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5356 // This const_cast is a bit lame. Really, Triggered should be mutable.
5357 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005358 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005359 continue;
5360
John McCallc1465822011-02-14 07:13:47 +00005361 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005362 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005363 // Don't bother giving deprecation diagnostics if the decl is invalid.
5364 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005365 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005366 break;
5367
5368 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005369 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005370 break;
John McCall31168b02011-06-15 23:02:42 +00005371
5372 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005373 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005374 break;
John McCall86121512010-01-27 03:50:35 +00005375 }
5376 }
John McCall2ec85372012-05-07 06:16:41 +00005377 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005378}
5379
John McCall6347b682012-05-07 06:16:58 +00005380/// Given a set of delayed diagnostics, re-emit them as if they had
5381/// been delayed in the current context instead of in the given pool.
5382/// Essentially, this just moves them to the current pool.
5383void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5384 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5385 assert(curPool && "re-emitting in undelayed context not supported");
5386 curPool->steal(pool);
5387}
5388
John McCall28a6aea2009-11-04 02:18:39 +00005389static bool isDeclDeprecated(Decl *D) {
5390 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005391 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005392 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005393 // A category implicitly has the availability of the interface.
5394 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5395 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005396 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5397 return false;
5398}
5399
Eli Friedman971bfa12012-08-08 21:52:41 +00005400static void
5401DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5402 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005403 const ObjCInterfaceDecl *UnknownObjCClass,
5404 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005405 DeclarationName Name = D->getDeclName();
5406 if (!Message.empty()) {
5407 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5408 S.Diag(D->getLocation(),
5409 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5410 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005411 if (ObjCPropery)
5412 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5413 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005414 } else if (!UnknownObjCClass) {
5415 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5416 S.Diag(D->getLocation(),
5417 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5418 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005419 if (ObjCPropery)
5420 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5421 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005422 } else {
5423 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5424 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5425 }
5426}
5427
John McCallb45a1e72010-08-26 02:13:20 +00005428void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005429 Decl *Ctx) {
5430 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005431 return;
5432
John McCall86121512010-01-27 03:50:35 +00005433 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005434 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5435 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005436 DD.getUnknownObjCClass(),
5437 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005438}
5439
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005440void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005441 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005442 const ObjCInterfaceDecl *UnknownObjCClass,
5443 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005444 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005445 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005446 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5447 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005448 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005449 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005450 return;
5451 }
5452
5453 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005454 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005455 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005456 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005457}