blob: c3726c226f659d6ba490802ba8ed5b1ca3358f7c [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner2c6fcf52008-06-26 18:38:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements decl-related attribute processing.
10//
11//===----------------------------------------------------------------------===//
12
David Majnemer929025d2016-01-26 19:30:26 +000013#include "clang/AST/ASTConsumer.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000014#include "clang/AST/ASTContext.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000015#include "clang/AST/ASTMutationListener.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000021#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Erik Pilkington5cd57172016-08-16 17:44:11 +000023#include "clang/AST/RecursiveASTVisitor.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000024#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000026#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000027#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000028#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000029#include "clang/Sema/DelayedDiagnostic.h"
Artem Belevichbcec9da2016-06-06 22:54:57 +000030#include "clang/Sema/Initialization.h"
John McCallf1e8b342011-09-29 07:17:38 +000031#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000032#include "clang/Sema/Scope.h"
Reid Kleckner04f9bca2018-03-07 22:48:35 +000033#include "clang/Sema/ScopeInfo.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000034#include "clang/Sema/SemaInternal.h"
George Burgess IV177399e2017-01-09 04:12:14 +000035#include "llvm/ADT/STLExtras.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000036#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000037#include "llvm/Support/MathExtras.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000038
Chris Lattner2c6fcf52008-06-26 18:38:35 +000039using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000040using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000041
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000042namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000043 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000044 C,
45 Cpp,
46 ObjC
47 };
Eugene Zelenko1ced5092016-02-12 22:53:10 +000048} // end namespace AttributeLangSupport
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000049
Chris Lattner58418ff2008-06-29 00:16:31 +000050//===----------------------------------------------------------------------===//
51// Helper functions
52//===----------------------------------------------------------------------===//
53
Ted Kremenek527042b2009-08-14 20:49:40 +000054/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000055/// type (function or function-typed variable) or an Objective-C
56/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000058 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000059}
Eugene Zelenko1ced5092016-02-12 22:53:10 +000060
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000061/// Return true if the given decl has function type (function or
David Majnemer06864812015-04-07 06:01:53 +000062/// function-typed variable) or an Objective-C method or a block.
63static bool isFunctionOrMethodOrBlock(const Decl *D) {
64 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
65}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000066
John McCall3882ace2011-01-05 12:14:39 +000067/// Return true if the given decl has a declarator that should have
68/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000069static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000070 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000071 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
72 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000073}
74
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000075/// hasFunctionProto - Return true if the given decl has a argument
76/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000077/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000079 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000080 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000081 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000082}
83
Alp Toker601b22c2014-01-21 23:35:24 +000084/// getFunctionOrMethodNumParams - Return number of function or method
85/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000086/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000087static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000088 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000089 return cast<FunctionProtoType>(FnTy)->getNumParams();
Aaron Ballmana70c6b52018-02-15 16:20:20 +000090 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000091 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000092 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000093}
94
Erik Pilkington1e368822019-01-04 18:33:06 +000095static const ParmVarDecl *getFunctionOrMethodParam(const Decl *D,
96 unsigned Idx) {
97 if (const auto *FD = dyn_cast<FunctionDecl>(D))
98 return FD->getParamDecl(Idx);
99 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
100 return MD->getParamDecl(Idx);
101 if (const auto *BD = dyn_cast<BlockDecl>(D))
102 return BD->getParamDecl(Idx);
103 return nullptr;
104}
105
Alp Toker601b22c2014-01-21 23:35:24 +0000106static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000107 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +0000108 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000109 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000110 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000111
Alp Toker03376dc2014-07-07 09:02:20 +0000112 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000113}
114
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000115static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
Erik Pilkington1e368822019-01-04 18:33:06 +0000116 if (auto *PVD = getFunctionOrMethodParam(D, Idx))
117 return PVD->getSourceRange();
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000118 return SourceRange();
119}
120
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000121static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000122 if (const FunctionType *FnTy = D->getFunctionType())
George Burgess IV00f70bd2018-03-01 05:43:23 +0000123 return FnTy->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000124 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000125}
126
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000127static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
128 if (const auto *FD = dyn_cast<FunctionDecl>(D))
129 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000130 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000131 return MD->getReturnTypeSourceRange();
132 return SourceRange();
133}
134
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000135static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000136 if (const FunctionType *FnTy = D->getFunctionType())
137 return cast<FunctionProtoType>(FnTy)->isVariadic();
138 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballmane13d0092014-08-01 17:02:34 +0000139 return BD->isVariadic();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000140 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000141}
142
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000143static bool isInstanceMethod(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000144 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000145 return MethodDecl->isInstance();
146 return false;
147}
148
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000149static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000150 const auto *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000151 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000152 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000153
John McCall96fa4842010-05-17 21:00:27 +0000154 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
155 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000156 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000157
John McCall96fa4842010-05-17 21:00:27 +0000158 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000159
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000160 // FIXME: Should we walk the chain of classes?
161 return ClsName == &Ctx.Idents.get("NSString") ||
162 ClsName == &Ctx.Idents.get("NSMutableString");
163}
164
Daniel Dunbar980c6692008-09-26 03:32:58 +0000165static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000166 const auto *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000167 if (!PT)
168 return false;
169
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000170 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000171 if (!RT)
172 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000173
Daniel Dunbar980c6692008-09-26 03:32:58 +0000174 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000175 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000176 return false;
177
178 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
179}
180
Erich Keanee891aa92018-07-13 15:07:47 +0000181static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
Richard Smithb87c4652013-10-31 21:23:20 +0000182 // FIXME: Include the type in the argument list.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000183 return AL.getNumArgs() + AL.hasParsedType();
Richard Smithb87c4652013-10-31 21:23:20 +0000184}
185
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000186template <typename Compare>
Erich Keanee891aa92018-07-13 15:07:47 +0000187static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000188 unsigned Num, unsigned Diag,
189 Compare Comp) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000190 if (Comp(getNumAttributeArgs(AL), Num)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000191 S.Diag(AL.getLoc(), Diag) << AL << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000192 return false;
193 }
194
195 return true;
196}
197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000198/// Check if the attribute has exactly as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000199/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000200static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000201 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000202 diag::err_attribute_wrong_number_arguments,
203 std::not_equal_to<unsigned>());
204}
205
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000206/// Check if the attribute has at least as many args as Num. May
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000207/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000208static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL,
Richard Smithb87c4652013-10-31 21:23:20 +0000209 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000210 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000211 diag::err_attribute_too_few_arguments,
212 std::less<unsigned>());
213}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000214
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000215/// Check if the attribute has at most as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000216/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000217static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL,
218 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000219 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000220 diag::err_attribute_too_many_arguments,
221 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000222}
223
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000224/// A helper function to provide Attribute Location for the Attr types
Erich Keanee891aa92018-07-13 15:07:47 +0000225/// AND the ParsedAttr.
Erich Keane623efd82017-03-30 21:48:55 +0000226template <typename AttrInfo>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000227static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value,
Erich Keane623efd82017-03-30 21:48:55 +0000228 SourceLocation>::type
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000229getAttrLoc(const AttrInfo &AL) {
230 return AL.getLocation();
Erich Keane623efd82017-03-30 21:48:55 +0000231}
Erich Keanee891aa92018-07-13 15:07:47 +0000232static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); }
Erich Keane623efd82017-03-30 21:48:55 +0000233
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000234/// If Expr is a valid integer constant, get the value of the integer
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000235/// expression and return success or failure. May output an error.
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000236///
237/// Negative argument is implicitly converted to unsigned, unless
238/// \p StrictlyUnsigned is true.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000239template <typename AttrInfo>
240static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000241 uint32_t &Val, unsigned Idx = UINT_MAX,
242 bool StrictlyUnsigned = false) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000243 llvm::APSInt I(32);
244 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
245 !Expr->isIntegerConstantExpr(I, S.Context)) {
246 if (Idx != UINT_MAX)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000247 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000248 << AI << Idx << AANT_ArgumentIntegerConstant
249 << Expr->getSourceRange();
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000250 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000251 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000252 << AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange();
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000253 return false;
254 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000255
256 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000257 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
258 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000259 return false;
260 }
261
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000262 if (StrictlyUnsigned && I.isSigned() && I.isNegative()) {
Andrew Savonichev1a5623482018-09-17 10:39:46 +0000263 S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
264 << AI << /*non-negative*/ 1;
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000265 return false;
266 }
267
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000268 Val = (uint32_t)I.getZExtValue();
269 return true;
270}
271
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000272/// Wrapper around checkUInt32Argument, with an extra check to be sure
George Burgess IVe3763372016-12-22 02:50:20 +0000273/// that the result will fit into a regular (signed) int. All args have the same
274/// purpose as they do in checkUInt32Argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000275template <typename AttrInfo>
276static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000277 int &Val, unsigned Idx = UINT_MAX) {
George Burgess IVe3763372016-12-22 02:50:20 +0000278 uint32_t UVal;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000279 if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
George Burgess IVe3763372016-12-22 02:50:20 +0000280 return false;
281
George Burgess IVa8049572016-12-22 19:00:31 +0000282 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
George Burgess IVe3763372016-12-22 02:50:20 +0000283 llvm::APSInt I(32); // for toString
284 I = UVal;
285 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
286 << I.toString(10, false) << 32 << /* Unsigned */ 0;
287 return false;
288 }
289
290 Val = UVal;
291 return true;
292}
293
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000294/// Diagnose mutually exclusive attributes when present on a given
Aaron Ballmanfb763042013-12-02 18:05:46 +0000295/// declaration. Returns true if diagnosed.
296template <typename AttrTy>
Erich Keane44bacdf2018-08-09 13:21:32 +0000297static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000298 if (const auto *A = D->getAttr<AttrTy>()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000299 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
300 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
301 return true;
302 }
303 return false;
304}
305
306template <typename AttrTy>
307static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
308 if (const auto *A = D->getAttr<AttrTy>()) {
309 S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000310 << A;
311 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000312 return true;
313 }
314 return false;
315}
316
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000317/// Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000318/// instance method D. May output an error.
319///
320/// \returns true if IdxExpr is a valid index.
Erich Keane623efd82017-03-30 21:48:55 +0000321template <typename AttrInfo>
322static bool checkFunctionOrMethodParameterIndex(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000323 Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
Joel E. Denny81508102018-03-13 14:51:22 +0000324 const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
David Majnemer06864812015-04-07 06:01:53 +0000325 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000326
327 // In C++ the implicit 'this' function parameter also counts.
328 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000329 bool HP = hasFunctionProto(D);
330 bool HasImplicitThisParam = isInstanceMethod(D);
331 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000332 unsigned NumParams =
333 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000334
335 llvm::APSInt IdxInt;
336 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
337 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000338 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000339 << &AI << AttrArgNum << AANT_ArgumentIntegerConstant
340 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000341 return false;
342 }
343
Joel E. Denny81508102018-03-13 14:51:22 +0000344 unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX);
345 if (IdxSource < 1 || (!IV && IdxSource > NumParams)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000346 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +0000347 << &AI << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000348 return false;
349 }
Joel E. Denny81508102018-03-13 14:51:22 +0000350 if (HasImplicitThisParam && !CanIndexImplicitThis) {
351 if (IdxSource == 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000352 S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
353 << &AI << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000354 return false;
355 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000356 }
357
Joel E. Denny81508102018-03-13 14:51:22 +0000358 Idx = ParamIdx(IdxSource, D);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000359 return true;
360}
361
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000362/// Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000363/// If not emit an error and return false. If the argument is an identifier it
364/// will emit an error with a fixit hint and treat it as if it was a string
365/// literal.
Erich Keanee891aa92018-07-13 15:07:47 +0000366bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
367 StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000368 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000369 // Look for identifiers. If we have one emit a hint to fix it to a literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000370 if (AL.isArgIdent(ArgNum)) {
371 IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000372 Diag(Loc->Loc, diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000373 << AL << AANT_ArgumentString
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000374 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000375 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000376 Str = Loc->Ident->getName();
377 if (ArgLocation)
378 *ArgLocation = Loc->Loc;
379 return true;
380 }
381
382 // Now check for an actual string literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000383 Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
384 const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000385 if (ArgLocation)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000386 *ArgLocation = ArgExpr->getBeginLoc();
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000387
388 if (!Literal || !Literal->isAscii()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000389 Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000390 << AL << AANT_ArgumentString;
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000391 return false;
392 }
393
394 Str = Literal->getString();
395 return true;
396}
397
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000398/// Applies the given attribute to the Decl without performing any
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000399/// additional semantic checking.
400template <typename AttrType>
George Karpenkov1657f362018-11-30 02:18:37 +0000401static void handleSimpleAttribute(Sema &S, Decl *D, SourceRange SR,
402 unsigned SpellingIndex) {
403 D->addAttr(::new (S.Context) AttrType(SR, S.Context, SpellingIndex));
404}
405
406template <typename AttrType>
Erich Keanee891aa92018-07-13 15:07:47 +0000407static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) {
George Karpenkov1657f362018-11-30 02:18:37 +0000408 handleSimpleAttribute<AttrType>(S, D, AL.getRange(),
409 AL.getAttributeSpellingListIndex());
410}
411
412
413template <typename... DiagnosticArgs>
414static const Sema::SemaDiagnosticBuilder&
415appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr) {
416 return Bldr;
417}
418
419template <typename T, typename... DiagnosticArgs>
420static const Sema::SemaDiagnosticBuilder&
421appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr, T &&ExtraArg,
422 DiagnosticArgs &&... ExtraArgs) {
423 return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
424 std::forward<DiagnosticArgs>(ExtraArgs)...);
425}
426
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000427/// Add an attribute {@code AttrType} to declaration {@code D}, provided that
428/// {@code PassesCheck} is true.
429/// Otherwise, emit diagnostic {@code DiagID}, passing in all parameters
430/// specified in {@code ExtraArgs}.
George Karpenkov1657f362018-11-30 02:18:37 +0000431template <typename AttrType, typename... DiagnosticArgs>
432static void
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000433handleSimpleAttributeOrDiagnose(Sema &S, Decl *D, SourceRange SR,
George Karpenkov1657f362018-11-30 02:18:37 +0000434 unsigned SpellingIndex,
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000435 bool PassesCheck,
436 unsigned DiagID, DiagnosticArgs&&... ExtraArgs) {
437 if (!PassesCheck) {
George Karpenkov1657f362018-11-30 02:18:37 +0000438 Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
439 appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
440 return;
441 }
442 handleSimpleAttribute<AttrType>(S, D, SR, SpellingIndex);
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000443}
444
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000445template <typename AttrType, typename... DiagnosticArgs>
446static void
447handleSimpleAttributeOrDiagnose(Sema &S, Decl *D, const ParsedAttr &AL,
448 bool PassesCheck,
449 unsigned DiagID,
450 DiagnosticArgs&&... ExtraArgs) {
451 return handleSimpleAttributeOrDiagnose<AttrType>(
452 S, D, AL.getRange(), AL.getAttributeSpellingListIndex(), PassesCheck,
453 DiagID, std::forward<DiagnosticArgs>(ExtraArgs)...);
454}
455
Justin Lebar3eaaf862016-01-13 01:07:35 +0000456template <typename AttrType>
457static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000458 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000459 handleSimpleAttribute<AttrType>(S, D, AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000460}
461
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000462/// Applies the given attribute to the Decl so long as the Decl doesn't
Justin Lebar3eaaf862016-01-13 01:07:35 +0000463/// already have one of the given incompatible attributes.
464template <typename AttrType, typename IncompatibleAttrType,
465 typename... IncompatibleAttrTypes>
466static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000467 const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000468 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
Justin Lebar3eaaf862016-01-13 01:07:35 +0000469 return;
470 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000471 AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000472}
473
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000474/// Check if the passed-in expression is of type int or bool.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475static bool isIntOrBool(Expr *Exp) {
476 QualType QT = Exp->getType();
477 return QT->isBooleanType() || QT->isIntegerType();
478}
479
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000480
481// Check to see if the type is a smart pointer of some kind. We assume
482// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000483static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Trieu8d3fa392018-09-22 01:50:52 +0000484 auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record,
485 OverloadedOperatorKind Op) {
486 DeclContextLookupResult Result =
487 Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op));
488 return !Result.empty();
489 };
490
491 const RecordDecl *Record = RT->getDecl();
492 bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
493 bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
494 if (foundStarOperator && foundArrowOperator)
495 return true;
496
497 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
498 if (!CXXRecord)
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000499 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000500
Richard Trieu8d3fa392018-09-22 01:50:52 +0000501 for (auto BaseSpecifier : CXXRecord->bases()) {
502 if (!foundStarOperator)
503 foundStarOperator = IsOverloadedOperatorPresent(
504 BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
505 if (!foundArrowOperator)
506 foundArrowOperator = IsOverloadedOperatorPresent(
507 BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
508 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000509
Richard Trieu8d3fa392018-09-22 01:50:52 +0000510 if (foundStarOperator && foundArrowOperator)
511 return true;
512
513 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000514}
515
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000516/// Check if passed in Decl is a pointer type.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000517/// Note that this function may produce an error message.
518/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000519static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000520 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000521 const auto *VD = cast<ValueDecl>(D);
522 QualType QT = VD->getType();
Aaron Ballman553e6812013-12-26 14:54:11 +0000523 if (QT->isAnyPointerType())
524 return true;
525
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000526 if (const auto *RT = QT->getAs<RecordType>()) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000527 // If it's an incomplete type, it could be a smart pointer; skip it.
528 // (We don't want to force template instantiation if we can avoid it,
529 // since that would alter the order in which templates are instantiated.)
530 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000531 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000532
Aaron Ballman553e6812013-12-26 14:54:11 +0000533 if (threadSafetyCheckIsSmartPointer(S, RT))
534 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000535 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000536
Erich Keane44bacdf2018-08-09 13:21:32 +0000537 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000538 return false;
539}
540
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000541/// Checks that the passed in QualType either is of RecordType or points
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000542/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000543static const RecordType *getRecordType(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000544 if (const auto *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000545 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000546
547 // Now check if we point to record type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000548 if (const auto *PT = QT->getAs<PointerType>())
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000549 return PT->getPointeeType()->getAs<RecordType>();
550
Craig Topperc3ec1492014-05-26 06:22:03 +0000551 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000552}
553
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000554template <typename AttrType>
555static bool checkRecordDeclForAttr(const RecordDecl *RD) {
556 // Check if the record itself has the attribute.
557 if (RD->hasAttr<AttrType>())
558 return true;
559
560 // Else check if any base classes have the attribute.
561 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
562 CXXBasePaths BPaths(false, false);
563 if (CRD->lookupInBases(
564 [](const CXXBaseSpecifier *BS, CXXBasePath &) {
565 const auto &Ty = *BS->getType();
566 // If it's type-dependent, we assume it could have the attribute.
567 if (Ty.isDependentType())
568 return true;
569 return Ty.getAs<RecordType>()->getDecl()->hasAttr<AttrType>();
570 },
571 BPaths, true))
572 return true;
573 }
574 return false;
575}
576
Josh Gao55afa752017-08-11 07:54:35 +0000577static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000578 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000579
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000580 if (!RT)
581 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000582
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000583 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000584 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000585 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000586
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000587 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000588 // FIXME -- Check the type that the smart pointer points to.
589 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000590 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000591
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000592 return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl());
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000593}
594
Josh Gao55afa752017-08-11 07:54:35 +0000595static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000596 const auto *TD = Ty->getAs<TypedefType>();
597 if (!TD)
598 return false;
599
600 TypedefNameDecl *TN = TD->getDecl();
601 if (!TN)
602 return false;
603
Josh Gao55afa752017-08-11 07:54:35 +0000604 return TN->hasAttr<CapabilityAttr>();
Aaron Ballman76050722014-04-04 15:13:57 +0000605}
606
Josh Gaob40c1772017-08-08 19:44:35 +0000607static bool typeHasCapability(Sema &S, QualType Ty) {
Josh Gao55afa752017-08-11 07:54:35 +0000608 if (checkTypedefTypeForCapability(Ty))
609 return true;
Josh Gaob40c1772017-08-08 19:44:35 +0000610
Josh Gao55afa752017-08-11 07:54:35 +0000611 if (checkRecordTypeForCapability(S, Ty))
612 return true;
613
614 return false;
Josh Gaob40c1772017-08-08 19:44:35 +0000615}
616
Aaron Ballman76050722014-04-04 15:13:57 +0000617static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
618 // Capability expressions are simple expressions involving the boolean logic
619 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
620 // a DeclRefExpr is found, its type should be checked to determine whether it
621 // is a capability or not.
622
Yi Kong2d58d192017-12-14 22:24:45 +0000623 if (const auto *E = dyn_cast<CastExpr>(Ex))
Aaron Ballman76050722014-04-04 15:13:57 +0000624 return isCapabilityExpr(S, E->getSubExpr());
625 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
626 return isCapabilityExpr(S, E->getSubExpr());
627 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
Yi Kong2d58d192017-12-14 22:24:45 +0000628 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
629 E->getOpcode() == UO_Deref)
Aaron Ballman76050722014-04-04 15:13:57 +0000630 return isCapabilityExpr(S, E->getSubExpr());
631 return false;
632 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
633 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
634 return isCapabilityExpr(S, E->getLHS()) &&
635 isCapabilityExpr(S, E->getRHS());
636 return false;
637 }
638
Yi Kong2d58d192017-12-14 22:24:45 +0000639 return typeHasCapability(S, Ex->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000640}
641
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000642/// Checks that all attribute arguments, starting from Sidx, resolve to
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000643/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000644/// \param Sidx The attribute argument index to start checking with.
645/// \param ParamIdxOk Whether an argument can be indexing into a function
646/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000647static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000648 const ParsedAttr &AL,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000649 SmallVectorImpl<Expr *> &Args,
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000650 unsigned Sidx = 0,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000651 bool ParamIdxOk = false) {
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000652 if (Sidx == AL.getNumArgs()) {
653 // If we don't have any capability arguments, the attribute implicitly
654 // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
655 // a non-static method, and that the class is a (scoped) capability.
656 const auto *MD = dyn_cast<const CXXMethodDecl>(D);
657 if (MD && !MD->isStatic()) {
658 const CXXRecordDecl *RD = MD->getParent();
659 // FIXME -- need to check this again on template instantiation
660 if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
661 !checkRecordDeclForAttr<ScopedLockableAttr>(RD))
662 S.Diag(AL.getLoc(),
663 diag::warn_thread_attribute_not_on_capability_member)
664 << AL << MD->getParent();
665 } else {
666 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
667 << AL;
668 }
669 }
670
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000671 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
672 Expr *ArgExp = AL.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000673
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000674 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000675 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000676 Args.push_back(ArgExp);
677 continue;
678 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000679
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000680 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000681 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000682 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000683 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000684 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000685 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000686 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000687 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000688
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000689 // We allow constant strings to be used as a placeholder for expressions
690 // that are not valid C++ syntax, but warn that they are ignored.
Erich Keane44bacdf2018-08-09 13:21:32 +0000691 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000692 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000693 continue;
694 }
695
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000696 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000697
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000698 // A pointer to member expression of the form &MyClass::mu is treated
699 // specially -- we need to look at the type of the member.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000700 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000701 if (UOp->getOpcode() == UO_AddrOf)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000702 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000703 if (DRE->getDecl()->isCXXInstanceMember())
704 ArgTy = DRE->getDecl()->getType();
705
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000706 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000707 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000708
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000709 // Now check if we index into a record type function param.
Josh Gao55afa752017-08-11 07:54:35 +0000710 if(!RT && ParamIdxOk) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000711 const auto *FD = dyn_cast<FunctionDecl>(D);
712 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
Josh Gao55afa752017-08-11 07:54:35 +0000713 if(FD && IL) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000714 unsigned int NumParams = FD->getNumParams();
715 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000716 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
717 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000718 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
719 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +0000720 << AL << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000721 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000722 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000723 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000724 }
725 }
726
Aaron Ballman76050722014-04-04 15:13:57 +0000727 // If the type does not have a capability, see if the components of the
728 // expression have capabilities. This allows for writing C code where the
729 // capability may be on the type, and the expression is a capability
730 // boolean logic expression. Eg) requires_capability(A || B && !C)
731 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000732 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Erich Keane44bacdf2018-08-09 13:21:32 +0000733 << AL << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000734
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000735 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000736 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000737}
738
Chris Lattner58418ff2008-06-29 00:16:31 +0000739//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000740// Attribute Implementations
741//===----------------------------------------------------------------------===//
742
Erich Keanee891aa92018-07-13 15:07:47 +0000743static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000744 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000745 return;
746
Michael Han99315932013-01-24 16:46:58 +0000747 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000748 PtGuardedVarAttr(AL.getRange(), S.Context,
749 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000750}
751
Erich Keanee891aa92018-07-13 15:07:47 +0000752static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000753 Expr *&Arg) {
754 SmallVector<Expr *, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000755 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000756 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000757 unsigned Size = Args.size();
758 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000759 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000760
Michael Han3be3b442012-07-23 18:48:41 +0000761 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000762
Michael Han3be3b442012-07-23 18:48:41 +0000763 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000764}
765
Erich Keanee891aa92018-07-13 15:07:47 +0000766static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000767 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000768 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000769 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000770
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000771 D->addAttr(::new (S.Context) GuardedByAttr(
772 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000773}
774
Erich Keanee891aa92018-07-13 15:07:47 +0000775static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000776 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000777 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000778 return;
779
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000780 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000781 return;
782
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000783 D->addAttr(::new (S.Context) PtGuardedByAttr(
784 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000785}
786
Erich Keanee891aa92018-07-13 15:07:47 +0000787static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000788 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000789 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000790 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000791
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000792 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000793 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000794 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000795 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000796 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000797 }
798
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000799 // Check that all arguments are lockable objects.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000800 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000801 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000802 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000803
Michael Han3be3b442012-07-23 18:48:41 +0000804 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000805}
806
Erich Keanee891aa92018-07-13 15:07:47 +0000807static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000808 SmallVector<Expr *, 1> Args;
809 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000810 return;
811
812 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000813 D->addAttr(::new (S.Context) AcquiredAfterAttr(
814 AL.getRange(), S.Context, StartArg, Args.size(),
815 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000816}
817
Erich Keanee891aa92018-07-13 15:07:47 +0000818static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000819 SmallVector<Expr *, 1> Args;
820 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000821 return;
822
823 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000824 D->addAttr(::new (S.Context) AcquiredBeforeAttr(
825 AL.getRange(), S.Context, StartArg, Args.size(),
826 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000827}
828
Erich Keanee891aa92018-07-13 15:07:47 +0000829static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000830 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000831 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000832 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000833 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000834
Michael Han3be3b442012-07-23 18:48:41 +0000835 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000836}
837
Erich Keanee891aa92018-07-13 15:07:47 +0000838static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000839 SmallVector<Expr *, 1> Args;
840 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000841 return;
842
843 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000844 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000845 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000846 AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size,
847 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000848}
849
850static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000851 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000852 SmallVector<Expr *, 1> Args;
853 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000854 return;
855
856 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000857 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000858 D->addAttr(::new (S.Context) AssertExclusiveLockAttr(
859 AL.getRange(), S.Context, StartArg, Size,
860 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000861}
862
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000863/// Checks to be sure that the given parameter number is in bounds, and
Aaron Ballman836684a2018-02-25 20:40:06 +0000864/// is an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000865/// false.
866///
Aaron Ballman836684a2018-02-25 20:40:06 +0000867/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000868template <typename AttrInfo>
869static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Joel E. Denny81508102018-03-13 14:51:22 +0000870 const AttrInfo &AI, unsigned AttrArgNo) {
Aaron Ballman836684a2018-02-25 20:40:06 +0000871 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
872 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
Joel E. Denny81508102018-03-13 14:51:22 +0000873 ParamIdx Idx;
Aaron Ballman836684a2018-02-25 20:40:06 +0000874 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000875 Idx))
876 return false;
877
Joel E. Denny81508102018-03-13 14:51:22 +0000878 const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +0000879 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000880 SourceLocation SrcLoc = AttrArg->getBeginLoc();
Erich Keane623efd82017-03-30 21:48:55 +0000881 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +0000882 << AI << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000883 return false;
884 }
885 return true;
886}
887
Erich Keanee891aa92018-07-13 15:07:47 +0000888static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000889 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
890 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000891 return;
892
893 const auto *FD = cast<FunctionDecl>(D);
894 if (!FD->getReturnType()->isPointerType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000895 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
George Burgess IVe3763372016-12-22 02:50:20 +0000896 return;
897 }
898
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000899 const Expr *SizeExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +0000900 int SizeArgNoVal;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000901 // Parameter indices are 1-indexed, hence Index=1
Joel E. Denny81508102018-03-13 14:51:22 +0000902 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Index=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000903 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000904 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0))
George Burgess IVe3763372016-12-22 02:50:20 +0000905 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000906 ParamIdx SizeArgNo(SizeArgNoVal, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000907
Joel E. Denny81508102018-03-13 14:51:22 +0000908 ParamIdx NumberArgNo;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000909 if (AL.getNumArgs() == 2) {
910 const Expr *NumberExpr = AL.getArgAsExpr(1);
Joel E. Denny81508102018-03-13 14:51:22 +0000911 int Val;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000912 // Parameter indices are 1-based, hence Index=2
Joel E. Denny81508102018-03-13 14:51:22 +0000913 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Index=*/2))
George Burgess IVe3763372016-12-22 02:50:20 +0000914 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000915 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000916 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000917 NumberArgNo = ParamIdx(Val, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000918 }
919
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000920 D->addAttr(::new (S.Context)
921 AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo,
922 AL.getAttributeSpellingListIndex()));
George Burgess IVe3763372016-12-22 02:50:20 +0000923}
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000924
Erich Keanee891aa92018-07-13 15:07:47 +0000925static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000926 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000927 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000928 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000929
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000930 if (!isIntOrBool(AL.getArgAsExpr(0))) {
931 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000932 << AL << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000933 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000934 }
935
936 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000937 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000938
Michael Han3be3b442012-07-23 18:48:41 +0000939 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000940}
941
Michael Hana9171bc2012-08-03 17:40:43 +0000942static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000943 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000944 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000945 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000946 return;
947
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000948 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
949 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(),
950 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000951}
952
Michael Hana9171bc2012-08-03 17:40:43 +0000953static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000954 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000955 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000956 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000957 return;
958
Nico Weber462fd1e2015-01-07 23:50:05 +0000959 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000960 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(),
961 Args.size(), AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000962}
963
Erich Keanee891aa92018-07-13 15:07:47 +0000964static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000965 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000966 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000967 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000968 unsigned Size = Args.size();
969 if (Size == 0)
970 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000971
Michael Han99315932013-01-24 16:46:58 +0000972 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000973 LockReturnedAttr(AL.getRange(), S.Context, Args[0],
974 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000975}
976
Erich Keanee891aa92018-07-13 15:07:47 +0000977static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000978 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000979 return;
980
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000981 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000982 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000983 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000984 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000985 if (Size == 0)
986 return;
987 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000988
Michael Han99315932013-01-24 16:46:58 +0000989 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000990 LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size,
991 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000992}
993
Erich Keanee891aa92018-07-13 15:07:47 +0000994static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
George Burgess IV177399e2017-01-09 04:12:14 +0000995 Expr *&Cond, StringRef &Msg) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000996 Cond = AL.getArgAsExpr(0);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000997 if (!Cond->isTypeDependent()) {
998 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
999 if (Converted.isInvalid())
George Burgess IV177399e2017-01-09 04:12:14 +00001000 return false;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001001 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001002 }
1003
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001004 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001005 return false;
1006
1007 if (Msg.empty())
1008 Msg = "<no message provided>";
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001009
1010 SmallVector<PartialDiagnosticAt, 8> Diags;
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001011 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001012 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
1013 Diags)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001014 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
George Burgess IV0d546532016-11-10 21:47:12 +00001015 for (const PartialDiagnosticAt &PDiag : Diags)
1016 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +00001017 return false;
1018 }
1019 return true;
1020}
1021
Erich Keanee891aa92018-07-13 15:07:47 +00001022static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001023 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001024
1025 Expr *Cond;
1026 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001027 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001028 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001029 EnableIfAttr(AL.getRange(), S.Context, Cond, Msg,
1030 AL.getAttributeSpellingListIndex()));
George Burgess IV177399e2017-01-09 04:12:14 +00001031}
1032
1033namespace {
1034/// Determines if a given Expr references any of the given function's
1035/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
1036class ArgumentDependenceChecker
1037 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
1038#ifndef NDEBUG
1039 const CXXRecordDecl *ClassType;
1040#endif
1041 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
1042 bool Result;
1043
1044public:
1045 ArgumentDependenceChecker(const FunctionDecl *FD) {
1046#ifndef NDEBUG
1047 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1048 ClassType = MD->getParent();
1049 else
1050 ClassType = nullptr;
1051#endif
1052 Parms.insert(FD->param_begin(), FD->param_end());
1053 }
1054
1055 bool referencesArgs(Expr *E) {
1056 Result = false;
1057 TraverseStmt(E);
1058 return Result;
1059 }
1060
1061 bool VisitCXXThisExpr(CXXThisExpr *E) {
1062 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
1063 "`this` doesn't refer to the enclosing class?");
1064 Result = true;
1065 return false;
1066 }
1067
1068 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1069 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1070 if (Parms.count(PVD)) {
1071 Result = true;
1072 return false;
1073 }
1074 return true;
1075 }
1076};
1077}
1078
Erich Keanee891aa92018-07-13 15:07:47 +00001079static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001080 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001081
1082 Expr *Cond;
1083 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001084 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001085 return;
1086
1087 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001088 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +00001089 return;
1090
1091 DiagnoseIfAttr::DiagnosticType DiagType;
1092 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001093 S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
George Burgess IV177399e2017-01-09 04:12:14 +00001094 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001095 return;
1096 }
1097
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001098 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +00001099 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001100 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +00001101 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001102 AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent,
1103 cast<NamedDecl>(D), AL.getAttributeSpellingListIndex()));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001104}
1105
Erich Keanee891aa92018-07-13 15:07:47 +00001106static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001107 if (D->hasAttr<PassObjectSizeAttr>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001108 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001109 return;
1110 }
1111
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001112 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001113 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001114 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001115 return;
1116
1117 // pass_object_size's argument is passed in as the second argument of
1118 // __builtin_object_size. So, it has the same constraints as that second
1119 // argument; namely, it must be in the range [0, 3].
1120 if (Type > 3) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001121 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00001122 << AL << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001123 return;
1124 }
1125
1126 // pass_object_size is only supported on constant pointer parameters; as a
1127 // kindness to users, we allow the parameter to be non-const for declarations.
1128 // At this point, we have no clue if `D` belongs to a function declaration or
1129 // definition, so we defer the constness check until later.
1130 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001131 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001132 return;
1133 }
1134
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001135 D->addAttr(::new (S.Context) PassObjectSizeAttr(
1136 AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex()));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001137}
1138
Erich Keanee891aa92018-07-13 15:07:47 +00001139static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001140 ConsumableAttr::ConsumedState DefaultState;
1141
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001142 if (AL.isArgIdent(0)) {
1143 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001144 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1145 DefaultState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001146 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1147 << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001148 return;
1149 }
David Blaikie16f76d22013-09-06 01:28:43 +00001150 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001151 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001152 << AL << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001153 return;
1154 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001155
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001156 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001157 ConsumableAttr(AL.getRange(), S.Context, DefaultState,
1158 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001159}
1160
1161static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Erich Keanee891aa92018-07-13 15:07:47 +00001162 const ParsedAttr &AL) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001163 QualType ThisType = MD->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001164
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001165 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1166 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001167 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001168 RD->getNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001169
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001170 return false;
1171 }
1172 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001173
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001174 return true;
1175}
1176
Erich Keanee891aa92018-07-13 15:07:47 +00001177static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001178 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001179 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001180
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001181 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001182 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001183
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001184 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001185 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001186 CallableWhenAttr::ConsumedState CallableState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001187
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001188 StringRef StateString;
1189 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001190 if (AL.isArgIdent(ArgIndex)) {
1191 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001192 StateString = Ident->Ident->getName();
1193 Loc = Ident->Loc;
1194 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001195 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001196 return;
1197 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001198
1199 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001200 CallableState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001201 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001202 return;
1203 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001204
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001205 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001206 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001207
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001208 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001209 CallableWhenAttr(AL.getRange(), S.Context, States.data(),
1210 States.size(), AL.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001211}
1212
Erich Keanee891aa92018-07-13 15:07:47 +00001213static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001214 ParamTypestateAttr::ConsumedState ParamState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001215
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001216 if (AL.isArgIdent(0)) {
1217 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001218 StringRef StateString = Ident->Ident->getName();
1219
1220 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1221 ParamState)) {
1222 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00001223 << AL << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001224 return;
1225 }
1226 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001227 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1228 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001229 return;
1230 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001231
DeLesley Hutchins69391772013-10-17 23:23:53 +00001232 // FIXME: This check is currently being done in the analysis. It can be
1233 // enabled here only after the parser propagates attributes at
1234 // template specialization definition, not declaration.
1235 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1236 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1237 //
1238 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001239 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001240 // ReturnType.getAsString();
1241 // return;
1242 //}
Fangrui Song6907ce22018-07-30 19:24:48 +00001243
DeLesley Hutchins69391772013-10-17 23:23:53 +00001244 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001245 ParamTypestateAttr(AL.getRange(), S.Context, ParamState,
1246 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001247}
1248
Erich Keanee891aa92018-07-13 15:07:47 +00001249static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001250 ReturnTypestateAttr::ConsumedState ReturnState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001251
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001252 if (AL.isArgIdent(0)) {
1253 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001254 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1255 ReturnState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001256 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1257 << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001258 return;
1259 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001260 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001261 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1262 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001263 return;
1264 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001265
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001266 // FIXME: This check is currently being done in the analysis. It can be
1267 // enabled here only after the parser propagates attributes at
1268 // template specialization definition, not declaration.
1269 //QualType ReturnType;
1270 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001271 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1272 // ReturnType = Param->getType();
1273 //
1274 //} else if (const CXXConstructorDecl *Constructor =
1275 // dyn_cast<CXXConstructorDecl>(D)) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001276 // ReturnType = Constructor->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001277 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001278 //} else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001279 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001280 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1281 //}
1282 //
1283 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1284 //
1285 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1286 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1287 // ReturnType.getAsString();
1288 // return;
1289 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001290
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001291 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001292 ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState,
1293 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001294}
1295
Erich Keanee891aa92018-07-13 15:07:47 +00001296static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001297 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001298 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001299
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001300 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001301 if (AL.isArgIdent(0)) {
1302 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001303 StringRef Param = Ident->Ident->getName();
1304 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001305 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1306 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001307 return;
1308 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001309 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001310 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1311 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001312 return;
1313 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001314
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001315 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001316 SetTypestateAttr(AL.getRange(), S.Context, NewState,
1317 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001318}
1319
Erich Keanee891aa92018-07-13 15:07:47 +00001320static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001321 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001322 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001323
1324 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001325 if (AL.isArgIdent(0)) {
1326 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001327 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001328 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001329 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1330 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001331 return;
1332 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001333 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001334 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1335 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001336 return;
1337 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001338
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001339 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001340 TestTypestateAttr(AL.getRange(), S.Context, TestState,
1341 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001342}
1343
Erich Keanee891aa92018-07-13 15:07:47 +00001344static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001345 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001346 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001347}
1348
Erich Keanee891aa92018-07-13 15:07:47 +00001349static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001350 if (auto *TD = dyn_cast<TagDecl>(D))
1351 TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context,
1352 AL.getAttributeSpellingListIndex()));
1353 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001354 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1355 !FD->getType()->isIncompleteType() &&
1356 FD->isBitField() &&
1357 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001358
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001359 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1360 if (BitfieldByteAligned)
1361 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001362 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001363 << AL << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001364 else
1365 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001366 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001367 } else {
1368 // Report warning about changed offset in the newer compiler versions.
1369 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001370 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001371
1372 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001373 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001374 }
1375
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001376 } else
Erich Keane44bacdf2018-08-09 13:21:32 +00001377 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001378}
1379
Erich Keanee891aa92018-07-13 15:07:47 +00001380static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001381 // The IBOutlet/IBOutletCollection attributes only apply to instance
1382 // variables or properties of Objective-C classes. The outlet must also
1383 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001384 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001385 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001386 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001387 << AL << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001388 return false;
1389 }
1390 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001391 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001392 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001393 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001394 << AL << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001395 return false;
1396 }
1397 }
1398 else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001399 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001400 return false;
1401 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001402
Ted Kremenek7fd17232011-09-29 07:02:25 +00001403 return true;
1404}
1405
Erich Keanee891aa92018-07-13 15:07:47 +00001406static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001407 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001408 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001409
Michael Han99315932013-01-24 16:46:58 +00001410 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001411 IBOutletAttr(AL.getRange(), S.Context,
1412 AL.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001413}
1414
Erich Keanee891aa92018-07-13 15:07:47 +00001415static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001416
1417 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001418 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001419 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001420 return;
1421 }
1422
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001423 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001424 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001425
Richard Smithb1f9a282013-10-31 01:56:18 +00001426 ParsedType PT;
1427
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001428 if (AL.hasParsedType())
1429 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001430 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001431 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001432 S.getScopeForContext(D->getDeclContext()->getParent()));
1433 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001434 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001435 return;
1436 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001437 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001438
Craig Topperc3ec1492014-05-26 06:22:03 +00001439 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001440 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1441 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001442 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001443
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001444 // Diagnose use of non-object type in iboutletcollection attribute.
1445 // FIXME. Gnu attribute extension ignores use of builtin types in
1446 // attributes. So, __attribute__((iboutletcollection(char))) will be
1447 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001448 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001449 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001450 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1451 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001452 return;
1453 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001454
Michael Han99315932013-01-24 16:46:58 +00001455 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001456 IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc,
1457 AL.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001458}
1459
Hal Finkelee90a222014-09-26 05:04:30 +00001460bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1461 if (RefOkay) {
1462 if (T->isReferenceType())
1463 return true;
1464 } else {
1465 T = T.getNonReferenceType();
1466 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001467
Hal Finkelee90a222014-09-26 05:04:30 +00001468 // The nonnull attribute, and other similar attributes, can be applied to a
1469 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001470 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001471 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1472 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001473 for (const auto *I : UD->fields()) {
1474 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001475 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1476 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001477 }
1478 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001479 }
1480
1481 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001482}
1483
Erich Keanee891aa92018-07-13 15:07:47 +00001484static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001485 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001486 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001487 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001488 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001489 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001490 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001491 << AL << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001492 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001493 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001494 << AL << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001495 return false;
1496 }
1497 return true;
1498}
1499
Erich Keanee891aa92018-07-13 15:07:47 +00001500static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001501 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001502 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1503 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001504 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001505 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001506 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001507
1508 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001509 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1510 !attrNonNullArgCheck(
1511 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1512 Ex->getSourceRange(),
1513 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001514 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001515
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001516 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001517 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001518
1519 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001520 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1521 // check if the attribute came from a macro expansion or a template
1522 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001523 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001524 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001525 bool AnyPointers = isFunctionOrMethodVariadic(D);
1526 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1527 I != E && !AnyPointers; ++I) {
1528 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001529 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001530 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001531 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001532
Richard Smith588bd9b2014-08-27 04:59:42 +00001533 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001534 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001535 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001536
Joel E. Denny81508102018-03-13 14:51:22 +00001537 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001538 unsigned Size = NonNullArgs.size();
1539 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001540 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001541 NonNullAttr(AL.getRange(), S.Context, Start, Size,
1542 AL.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001543}
1544
Jordan Rosec9399072014-02-11 17:27:59 +00001545static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00001546 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001547 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001548 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001549 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001550 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001551 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001552 << D->getSourceRange();
1553 }
1554 return;
1555 }
1556
1557 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001558 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001559 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001560 return;
1561
1562 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001563 NonNullAttr(AL.getRange(), S.Context, nullptr, 0,
1564 AL.getAttributeSpellingListIndex()));
Jordan Rosec9399072014-02-11 17:27:59 +00001565}
1566
Erich Keanee891aa92018-07-13 15:07:47 +00001567static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001568 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001569 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001570 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001571 /* isReturnValue */ true))
1572 return;
1573
1574 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001575 ReturnsNonNullAttr(AL.getRange(), S.Context,
1576 AL.getAttributeSpellingListIndex()));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001577}
1578
Erich Keanee891aa92018-07-13 15:07:47 +00001579static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001580 if (D->isInvalidDecl())
1581 return;
1582
1583 // noescape only applies to pointer types.
1584 QualType T = cast<ParmVarDecl>(D)->getType();
1585 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001586 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001587 << AL << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001588 return;
1589 }
1590
1591 D->addAttr(::new (S.Context) NoEscapeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001592 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001593}
1594
Erich Keanee891aa92018-07-13 15:07:47 +00001595static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001596 Expr *E = AL.getArgAsExpr(0),
1597 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1598 S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE,
1599 AL.getAttributeSpellingListIndex());
Hal Finkelee90a222014-09-26 05:04:30 +00001600}
1601
Erich Keanee891aa92018-07-13 15:07:47 +00001602static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001603 S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0),
1604 AL.getAttributeSpellingListIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001605}
1606
Hal Finkelee90a222014-09-26 05:04:30 +00001607void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1608 Expr *OE, unsigned SpellingListIndex) {
1609 QualType ResultType = getFunctionOrMethodResultType(D);
1610 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1611
1612 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1613 SourceLocation AttrLoc = AttrRange.getBegin();
1614
1615 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1616 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1617 << &TmpAttr << AttrRange << SR;
1618 return;
1619 }
1620
1621 if (!E->isValueDependent()) {
1622 llvm::APSInt I(64);
1623 if (!E->isIntegerConstantExpr(I, Context)) {
1624 if (OE)
1625 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1626 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1627 << E->getSourceRange();
1628 else
1629 Diag(AttrLoc, diag::err_attribute_argument_type)
1630 << &TmpAttr << AANT_ArgumentIntegerConstant
1631 << E->getSourceRange();
1632 return;
1633 }
1634
1635 if (!I.isPowerOf2()) {
1636 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1637 << E->getSourceRange();
1638 return;
1639 }
1640 }
1641
1642 if (OE) {
1643 if (!OE->isValueDependent()) {
1644 llvm::APSInt I(64);
1645 if (!OE->isIntegerConstantExpr(I, Context)) {
1646 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1647 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1648 << OE->getSourceRange();
1649 return;
1650 }
1651 }
1652 }
1653
1654 D->addAttr(::new (Context)
1655 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1656}
1657
Erich Keane623efd82017-03-30 21:48:55 +00001658void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr,
1659 unsigned SpellingListIndex) {
1660 QualType ResultType = getFunctionOrMethodResultType(D);
1661
Joel E. Denny81508102018-03-13 14:51:22 +00001662 AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex);
Erich Keane623efd82017-03-30 21:48:55 +00001663 SourceLocation AttrLoc = AttrRange.getBegin();
1664
1665 if (!ResultType->isDependentType() &&
1666 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1667 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1668 << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D);
1669 return;
1670 }
1671
Joel E. Denny81508102018-03-13 14:51:22 +00001672 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001673 const auto *FuncDecl = cast<FunctionDecl>(D);
1674 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Joel E. Denny81508102018-03-13 14:51:22 +00001675 /*AttrArgNo=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001676 return;
1677
Joel E. Denny81508102018-03-13 14:51:22 +00001678 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001679 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001680 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001681 << &TmpAttr
1682 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001683 return;
1684 }
1685
Joel E. Denny81508102018-03-13 14:51:22 +00001686 D->addAttr(::new (Context)
1687 AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex));
Erich Keane623efd82017-03-30 21:48:55 +00001688}
1689
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001690/// Normalize the attribute, __foo__ becomes foo.
1691/// Returns true if normalization was applied.
1692static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001693 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1694 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001695 AttrName = AttrName.drop_front(2).drop_back(2);
1696 return true;
1697 }
1698 return false;
1699}
1700
Erich Keanee891aa92018-07-13 15:07:47 +00001701static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001702 // This attribute must be applied to a function declaration. The first
1703 // argument to the attribute must be an identifier, the name of the resource,
1704 // for example: malloc. The following arguments must be argument indexes, the
1705 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001706 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001707 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001708 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001709
Aaron Ballman00e99962013-08-31 01:11:41 +00001710 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001711 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001712 << AL << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001713 return;
1714 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001715
Richard Smith852e9ce2013-11-27 01:46:48 +00001716 // Figure out our Kind.
1717 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001718 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001719 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001720
Richard Smith852e9ce2013-11-27 01:46:48 +00001721 // Check arguments.
1722 switch (K) {
1723 case OwnershipAttr::Takes:
1724 case OwnershipAttr::Holds:
1725 if (AL.getNumArgs() < 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001726 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001727 return;
1728 }
1729 break;
1730 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001731 if (AL.getNumArgs() > 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001732 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001733 return;
1734 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001735 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001736 }
1737
Richard Smith852e9ce2013-11-27 01:46:48 +00001738 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001739
Richard Smith852e9ce2013-11-27 01:46:48 +00001740 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001741 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001742 Module = &S.PP.getIdentifierTable().get(ModuleName);
1743 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001744
Joel E. Denny81508102018-03-13 14:51:22 +00001745 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001746 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1747 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001748 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001749 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001750 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001751
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001752 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001753 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001754 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001755 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001756 case OwnershipAttr::Takes:
1757 case OwnershipAttr::Holds:
1758 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1759 Err = 0;
1760 break;
1761 case OwnershipAttr::Returns:
1762 if (!T->isIntegerType())
1763 Err = 1;
1764 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001765 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001766 if (-1 != Err) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001767 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1768 << Ex->getSourceRange();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001769 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001770 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001771
1772 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001773 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001774 // Cannot have two ownership attributes of different kinds for the same
1775 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001776 if (I->getOwnKind() != K && I->args_end() !=
1777 std::find(I->args_begin(), I->args_end(), Idx)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001778 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001779 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001780 } else if (K == OwnershipAttr::Returns &&
1781 I->getOwnKind() == OwnershipAttr::Returns) {
1782 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001783 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001784 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1785 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001786 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001787 if (I->args_size())
1788 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001789 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001790 return;
1791 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001792 }
1793 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001794 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001795 }
1796
Joel E. Denny81508102018-03-13 14:51:22 +00001797 ParamIdx *Start = OwnershipArgs.data();
1798 unsigned Size = OwnershipArgs.size();
1799 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001800 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001801 OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size,
1802 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001803}
1804
Erich Keanee891aa92018-07-13 15:07:47 +00001805static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001806 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001807 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001808 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001809 return;
1810 }
1811
1812 // gcc rejects
1813 // class c {
1814 // static int a __attribute__((weakref ("v2")));
1815 // static int b() __attribute__((weakref ("f3")));
1816 // };
1817 // and ignores the attributes of
1818 // void f(void) {
1819 // static int a __attribute__((weakref ("v2")));
1820 // }
1821 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001822 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001823 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001824 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1825 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001826 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001827 }
1828
1829 // The GCC manual says
1830 //
1831 // At present, a declaration to which `weakref' is attached can only
1832 // be `static'.
1833 //
1834 // It also says
1835 //
1836 // Without a TARGET,
1837 // given as an argument to `weakref' or to `alias', `weakref' is
1838 // equivalent to `weak'.
1839 //
1840 // gcc 4.4.1 will accept
1841 // int a7 __attribute__((weakref));
1842 // as
1843 // int a7 __attribute__((weak));
1844 // This looks like a bug in gcc. We reject that for now. We should revisit
1845 // it if this behaviour is actually used.
1846
Rafael Espindolac18086a2010-02-23 22:00:30 +00001847 // GCC rejects
1848 // static ((alias ("y"), weakref)).
1849 // Should we? How to check that weakref is before or after alias?
1850
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001851 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1852 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1853 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001854 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001855 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001856 // GCC will accept anything as the argument of weakref. Should we
1857 // check for an existing decl?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001858 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1859 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001860
Michael Han99315932013-01-24 16:46:58 +00001861 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001862 WeakRefAttr(AL.getRange(), S.Context,
1863 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001864}
1865
Erich Keanee891aa92018-07-13 15:07:47 +00001866static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001867 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001868 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001869 return;
1870
1871 // Aliases should be on declarations, not definitions.
1872 const auto *FD = cast<FunctionDecl>(D);
1873 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001874 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001875 return;
1876 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001877
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001878 D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str,
1879 AL.getAttributeSpellingListIndex()));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001880}
1881
Erich Keanee891aa92018-07-13 15:07:47 +00001882static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001883 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001884 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001885 return;
1886
Douglas Gregore8bbc122011-09-02 00:18:52 +00001887 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001888 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001889 return;
1890 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001891 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001892 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001893 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001894
David Majnemer2dc81462015-01-19 09:00:28 +00001895 // Aliases should be on declarations, not definitions.
1896 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1897 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001898 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001899 return;
1900 }
1901 } else {
1902 const auto *VD = cast<VarDecl>(D);
1903 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001904 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001905 return;
1906 }
1907 }
1908
Nick Desaulniers9b9fe412019-01-09 23:54:55 +00001909 // Mark target used to prevent unneeded-internal-declaration warnings.
1910 if (!S.LangOpts.CPlusPlus) {
1911 // FIXME: demangle Str for C++, as the attribute refers to the mangled
1912 // linkage name, not the pre-mangled identifier.
1913 const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc());
1914 LookupResult LR(S, target, Sema::LookupOrdinaryName);
1915 if (S.LookupQualifiedName(LR, S.getCurLexicalContext()))
1916 for (NamedDecl *ND : LR)
1917 ND->markUsed(S.Context);
1918 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001919
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001920 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1921 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001922}
1923
Erich Keanee891aa92018-07-13 15:07:47 +00001924static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001925 StringRef Model;
1926 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001927 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001928 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001929 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001930
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001931 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001932 if (Model != "global-dynamic" && Model != "local-dynamic"
1933 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001934 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001935 return;
1936 }
1937
Michael Han99315932013-01-24 16:46:58 +00001938 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001939 TLSModelAttr(AL.getRange(), S.Context, Model,
1940 AL.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001941}
1942
Erich Keanee891aa92018-07-13 15:07:47 +00001943static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001944 QualType ResultType = getFunctionOrMethodResultType(D);
1945 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1946 D->addAttr(::new (S.Context) RestrictAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001947 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
David Majnemer631a90b2015-02-04 07:23:21 +00001948 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001949 }
1950
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001951 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001952 << AL << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001953}
1954
Erich Keane3efe0022018-07-20 14:13:28 +00001955static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1956 FunctionDecl *FD = cast<FunctionDecl>(D);
Erich Keane659c8712018-09-10 14:31:56 +00001957
1958 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1959 if (MD->getParent()->isLambda()) {
1960 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
1961 return;
1962 }
1963 }
1964
Erich Keane3efe0022018-07-20 14:13:28 +00001965 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1966 return;
1967
1968 SmallVector<IdentifierInfo *, 8> CPUs;
1969 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
1970 if (!AL.isArgIdent(ArgNo)) {
1971 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001972 << AL << AANT_ArgumentIdentifier;
Erich Keane3efe0022018-07-20 14:13:28 +00001973 return;
1974 }
1975
1976 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1977 StringRef CPUName = CPUArg->Ident->getName().trim();
1978
1979 if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1980 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1981 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1982 return;
1983 }
1984
1985 const TargetInfo &Target = S.Context.getTargetInfo();
1986 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1987 return Target.CPUSpecificManglingCharacter(CPUName) ==
1988 Target.CPUSpecificManglingCharacter(Cur->getName());
1989 })) {
1990 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1991 return;
1992 }
1993 CPUs.push_back(CPUArg->Ident);
1994 }
1995
1996 FD->setIsMultiVersion(true);
1997 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
1998 D->addAttr(::new (S.Context) CPUSpecificAttr(
1999 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
2000 AL.getAttributeSpellingListIndex()));
2001 else
2002 D->addAttr(::new (S.Context) CPUDispatchAttr(
2003 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
2004 AL.getAttributeSpellingListIndex()));
2005}
2006
Erich Keanee891aa92018-07-13 15:07:47 +00002007static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00002008 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002009 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00002010 << AL << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00002011 return;
2012 }
2013
Erich Keane44bacdf2018-08-09 13:21:32 +00002014 if (CommonAttr *CA = S.mergeCommonAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00002015 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00002016}
2017
Erich Keanee891aa92018-07-13 15:07:47 +00002018static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002019 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
Charles Davis0e379112016-08-08 21:19:08 +00002020 return;
2021
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002022 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002023 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
2024 const auto &Arch = Triple.getArch();
2025 if (Arch != llvm::Triple::x86 &&
2026 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002027 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
Erich Keane44bacdf2018-08-09 13:21:32 +00002028 << AL << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002029 return;
2030 }
2031 }
2032
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002033 D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context,
2034 AL.getAttributeSpellingListIndex()));
Charles Davis0e379112016-08-08 21:19:08 +00002035}
2036
Erich Keanee891aa92018-07-13 15:07:47 +00002037static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002038 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00002039
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002040 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002041 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002042 << Attrs << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002043 return;
2044 }
2045
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002046 D->addAttr(::new (S.Context) NoReturnAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00002047 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002048}
2049
Erich Keanee891aa92018-07-13 15:07:47 +00002050static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00002051 if (!S.getLangOpts().CFProtectionBranch)
2052 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
2053 else
2054 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00002055}
2056
Erich Keanee891aa92018-07-13 15:07:47 +00002057bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002058 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
2059 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002060 return true;
2061 }
2062
2063 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002064}
2065
Erich Keanee891aa92018-07-13 15:07:47 +00002066bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002067 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002068 if (!AL.existsInTarget(Context.getTargetInfo())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002069 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002070 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002071 return true;
2072 }
2073
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002074 return false;
2075}
2076
Erich Keanee891aa92018-07-13 15:07:47 +00002077static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2078
Ted Kremenek5295ce82010-08-19 00:51:58 +00002079 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2080 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00002081 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002082 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00002083 if (!VD || (!VD->getType()->isBlockPointerType() &&
2084 !VD->getType()->isFunctionPointerType())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002085 S.Diag(AL.getLoc(), AL.isCXX11Attribute()
2086 ? diag::err_attribute_wrong_decl_type
2087 : diag::warn_attribute_wrong_decl_type)
2088 << AL << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00002089 return;
2090 }
2091 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002092
Michael Han99315932013-01-24 16:46:58 +00002093 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002094 AnalyzerNoReturnAttr(AL.getRange(), S.Context,
2095 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002096}
2097
John Thompsoncdb847ba2010-08-09 21:53:52 +00002098// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00002099static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2100 /*
2101 Returning a Vector Class in Registers
2102
2103 According to the PPU ABI specifications, a class with a single member of
2104 vector type is returned in memory when used as the return value of a
2105 function.
2106 This results in inefficient code when implementing vector classes. To return
2107 the value in a single vector register, add the vecreturn attribute to the
2108 class definition. This attribute is also applicable to struct types.
2109
2110 Example:
2111
2112 struct Vector
2113 {
2114 __vector float xyzw;
2115 } __attribute__((vecreturn));
2116
2117 Vector Add(Vector lhs, Vector rhs)
2118 {
2119 Vector result;
2120 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2121 return result; // This will be returned in a register
2122 }
2123 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002124 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002125 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002126 return;
2127 }
2128
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002129 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002130 int count = 0;
2131
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002132 if (!isa<CXXRecordDecl>(R)) {
2133 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002134 return;
2135 }
2136
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002137 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2138 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002139 return;
2140 }
2141
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002142 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002143 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002144 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002145 return;
2146 }
2147 count++;
2148 }
2149
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002150 D->addAttr(::new (S.Context) VecReturnAttr(
2151 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002152}
2153
Richard Smithe233fbf2013-01-28 22:42:45 +00002154static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002155 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002156 if (isa<ParmVarDecl>(D)) {
2157 // [[carries_dependency]] can only be applied to a parameter if it is a
2158 // parameter of a function declaration or lambda.
2159 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002160 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002161 diag::err_carries_dependency_param_not_function_decl);
2162 return;
2163 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002164 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002165
2166 D->addAttr(::new (S.Context) CarriesDependencyAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002167 AL.getRange(), S.Context,
2168 AL.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002169}
2170
Erich Keanee891aa92018-07-13 15:07:47 +00002171static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002172 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002173
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002174 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002175 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002176 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Erich Keane44bacdf2018-08-09 13:21:32 +00002177 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002178
2179 D->addAttr(::new (S.Context) UnusedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002180 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002181}
2182
Erich Keanee891aa92018-07-13 15:07:47 +00002183static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002184 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002185 if (AL.getNumArgs() &&
2186 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002187 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002188
Michael Han99315932013-01-24 16:46:58 +00002189 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002190 ConstructorAttr(AL.getRange(), S.Context, priority,
2191 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002192}
2193
Erich Keanee891aa92018-07-13 15:07:47 +00002194static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002195 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002196 if (AL.getNumArgs() &&
2197 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002198 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002199
Michael Han99315932013-01-24 16:46:58 +00002200 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002201 DestructorAttr(AL.getRange(), S.Context, priority,
2202 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002203}
2204
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002205template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002206static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002207 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002208 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002209 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002210 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002211
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002212 D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str,
2213 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002214}
2215
Ted Kremenek438f8db2014-02-22 01:06:05 +00002216static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002217 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002218 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002219 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
Erich Keane44bacdf2018-08-09 13:21:32 +00002220 << AL << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002221 return;
2222 }
2223
Ted Kremenek28eace62013-11-23 01:01:34 +00002224 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002225 ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context,
2226 AL.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00002227}
2228
Jordy Rose740b0c22012-05-08 03:27:22 +00002229static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2230 IdentifierInfo *Platform,
2231 VersionTuple Introduced,
2232 VersionTuple Deprecated,
2233 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002234 StringRef PlatformName
2235 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2236 if (PlatformName.empty())
2237 PlatformName = Platform->getName();
2238
2239 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2240 // of these steps are needed).
2241 if (!Introduced.empty() && !Deprecated.empty() &&
2242 !(Introduced <= Deprecated)) {
2243 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2244 << 1 << PlatformName << Deprecated.getAsString()
2245 << 0 << Introduced.getAsString();
2246 return true;
2247 }
2248
2249 if (!Introduced.empty() && !Obsoleted.empty() &&
2250 !(Introduced <= Obsoleted)) {
2251 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2252 << 2 << PlatformName << Obsoleted.getAsString()
2253 << 0 << Introduced.getAsString();
2254 return true;
2255 }
2256
2257 if (!Deprecated.empty() && !Obsoleted.empty() &&
2258 !(Deprecated <= Obsoleted)) {
2259 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2260 << 2 << PlatformName << Obsoleted.getAsString()
2261 << 1 << Deprecated.getAsString();
2262 return true;
2263 }
2264
2265 return false;
2266}
2267
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002268/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002269///
2270/// If either version tuple is empty, then they are assumed to match. If
2271/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2272static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2273 bool BeforeIsOkay) {
2274 if (X.empty() || Y.empty())
2275 return true;
2276
2277 if (X == Y)
2278 return true;
2279
2280 if (BeforeIsOkay && X < Y)
2281 return true;
2282
2283 return false;
2284}
2285
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002286AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002287 IdentifierInfo *Platform,
Manman Ren719a8642016-05-06 21:04:01 +00002288 bool Implicit,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002289 VersionTuple Introduced,
2290 VersionTuple Deprecated,
2291 VersionTuple Obsoleted,
2292 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002293 StringRef Message,
Manman Rend8039df2016-02-22 04:47:24 +00002294 bool IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002295 StringRef Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002296 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00002297 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002298 VersionTuple MergedIntroduced = Introduced;
2299 VersionTuple MergedDeprecated = Deprecated;
2300 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002301 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002302 bool OverrideOrImpl = false;
2303 switch (AMK) {
2304 case AMK_None:
2305 case AMK_Redeclaration:
2306 OverrideOrImpl = false;
2307 break;
2308
2309 case AMK_Override:
2310 case AMK_ProtocolImplementation:
2311 OverrideOrImpl = true;
2312 break;
2313 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002314
Rafael Espindolac67f2232012-05-10 02:50:16 +00002315 if (D->hasAttrs()) {
2316 AttrVec &Attrs = D->getAttrs();
2317 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002318 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002319 if (!OldAA) {
2320 ++i;
2321 continue;
2322 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002323
Rafael Espindolac67f2232012-05-10 02:50:16 +00002324 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2325 if (OldPlatform != Platform) {
2326 ++i;
2327 continue;
2328 }
2329
Tim Northover7a73cc72015-10-30 16:30:49 +00002330 // If there is an existing availability attribute for this platform that
2331 // is explicit and the new one is implicit use the explicit one and
2332 // discard the new implicit attribute.
Manman Ren719a8642016-05-06 21:04:01 +00002333 if (!OldAA->isImplicit() && Implicit) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002334 return nullptr;
2335 }
2336
2337 // If there is an existing attribute for this platform that is implicit
2338 // and the new attribute is explicit then erase the old one and
2339 // continue processing the attributes.
Manman Ren719a8642016-05-06 21:04:01 +00002340 if (!Implicit && OldAA->isImplicit()) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002341 Attrs.erase(Attrs.begin() + i);
2342 --e;
2343 continue;
2344 }
2345
Rafael Espindolac67f2232012-05-10 02:50:16 +00002346 FoundAny = true;
2347 VersionTuple OldIntroduced = OldAA->getIntroduced();
2348 VersionTuple OldDeprecated = OldAA->getDeprecated();
2349 VersionTuple OldObsoleted = OldAA->getObsoleted();
2350 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002351
Douglas Gregord2a713e2015-09-30 21:27:42 +00002352 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2353 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2354 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002355 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002356 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2357 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002358 int Which = -1;
2359 VersionTuple FirstVersion;
2360 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002361 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002362 Which = 0;
2363 FirstVersion = OldIntroduced;
2364 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002365 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002366 Which = 1;
2367 FirstVersion = Deprecated;
2368 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002369 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002370 Which = 2;
2371 FirstVersion = Obsoleted;
2372 SecondVersion = OldObsoleted;
2373 }
2374
2375 if (Which == -1) {
2376 Diag(OldAA->getLocation(),
2377 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002378 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2379 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002380 } else {
2381 Diag(OldAA->getLocation(),
2382 diag::warn_mismatched_availability_override)
2383 << Which
2384 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002385 << FirstVersion.getAsString() << SecondVersion.getAsString()
2386 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002387 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002388 if (AMK == AMK_Override)
2389 Diag(Range.getBegin(), diag::note_overridden_method);
2390 else
2391 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002392 } else {
2393 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2394 Diag(Range.getBegin(), diag::note_previous_attribute);
2395 }
2396
Rafael Espindolac67f2232012-05-10 02:50:16 +00002397 Attrs.erase(Attrs.begin() + i);
2398 --e;
2399 continue;
2400 }
2401
2402 VersionTuple MergedIntroduced2 = MergedIntroduced;
2403 VersionTuple MergedDeprecated2 = MergedDeprecated;
2404 VersionTuple MergedObsoleted2 = MergedObsoleted;
2405
2406 if (MergedIntroduced2.empty())
2407 MergedIntroduced2 = OldIntroduced;
2408 if (MergedDeprecated2.empty())
2409 MergedDeprecated2 = OldDeprecated;
2410 if (MergedObsoleted2.empty())
2411 MergedObsoleted2 = OldObsoleted;
2412
2413 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2414 MergedIntroduced2, MergedDeprecated2,
2415 MergedObsoleted2)) {
2416 Attrs.erase(Attrs.begin() + i);
2417 --e;
2418 continue;
2419 }
2420
2421 MergedIntroduced = MergedIntroduced2;
2422 MergedDeprecated = MergedDeprecated2;
2423 MergedObsoleted = MergedObsoleted2;
2424 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002425 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002426 }
2427
2428 if (FoundAny &&
2429 MergedIntroduced == Introduced &&
2430 MergedDeprecated == Deprecated &&
2431 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002432 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002433
Douglas Gregord2a713e2015-09-30 21:27:42 +00002434 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002435 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002436 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002437 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002438 !OverrideOrImpl) {
Manman Ren719a8642016-05-06 21:04:01 +00002439 auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002440 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002441 Obsoleted, IsUnavailable, Message,
Manman Ren75bc6762016-03-21 17:30:55 +00002442 IsStrict, Replacement,
2443 AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002444 Avail->setImplicit(Implicit);
2445 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002446 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002447 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002448}
2449
Erich Keanee891aa92018-07-13 15:07:47 +00002450static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002451 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002452 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002453 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2454 unsigned Index = AL.getAttributeSpellingListIndex();
Fangrui Song6907ce22018-07-30 19:24:48 +00002455
Aaron Ballman00e99962013-08-31 01:11:41 +00002456 IdentifierInfo *II = Platform->Ident;
2457 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2458 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2459 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002460
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002461 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002462 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002463 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002464
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002465 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2466 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2467 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2468 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2469 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002470 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002471 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002472 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002473 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002474 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002475 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002476
Michael Wu260e9622018-11-12 02:44:33 +00002477 if (II->isStr("swift")) {
2478 if (Introduced.isValid() || Obsoleted.isValid() ||
2479 (!IsUnavailable && !Deprecated.isValid())) {
2480 S.Diag(AL.getLoc(),
2481 diag::warn_availability_swift_unavailable_deprecated_only);
2482 return;
2483 }
2484 }
2485
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002486 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II,
Manman Ren719a8642016-05-06 21:04:01 +00002487 false/*Implicit*/,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002488 Introduced.Version,
2489 Deprecated.Version,
2490 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002491 IsUnavailable, Str,
Manman Ren75bc6762016-03-21 17:30:55 +00002492 IsStrict, Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002493 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002494 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002495 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002496 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002497
2498 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2499 // matches before the start of the watchOS platform.
2500 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2501 IdentifierInfo *NewII = nullptr;
2502 if (II->getName() == "ios")
2503 NewII = &S.Context.Idents.get("watchos");
2504 else if (II->getName() == "ios_app_extension")
2505 NewII = &S.Context.Idents.get("watchos_app_extension");
2506
2507 if (NewII) {
2508 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2509 if (Version.empty())
2510 return Version;
2511 auto Major = Version.getMajor();
2512 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2513 if (NewMajor >= 2) {
2514 if (Version.getMinor().hasValue()) {
2515 if (Version.getSubminor().hasValue())
2516 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2517 Version.getSubminor().getValue());
2518 else
2519 return VersionTuple(NewMajor, Version.getMinor().getValue());
2520 }
2521 }
2522
2523 return VersionTuple(2, 0);
2524 };
2525
2526 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2527 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2528 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2529
2530 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002531 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002532 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002533 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002534 NewIntroduced,
2535 NewDeprecated,
2536 NewObsoleted,
2537 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002538 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002539 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002540 Sema::AMK_None,
2541 Index);
2542 if (NewAttr)
2543 D->addAttr(NewAttr);
2544 }
2545 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2546 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2547 // matches before the start of the tvOS platform.
2548 IdentifierInfo *NewII = nullptr;
2549 if (II->getName() == "ios")
2550 NewII = &S.Context.Idents.get("tvos");
2551 else if (II->getName() == "ios_app_extension")
2552 NewII = &S.Context.Idents.get("tvos_app_extension");
2553
2554 if (NewII) {
2555 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002556 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002557 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002558 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002559 Introduced.Version,
2560 Deprecated.Version,
2561 Obsoleted.Version,
2562 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002563 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002564 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002565 Sema::AMK_None,
2566 Index);
2567 if (NewAttr)
2568 D->addAttr(NewAttr);
2569 }
2570 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002571}
2572
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002573static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002574 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002575 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002576 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002577 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002578 "Invalid number of arguments in an external_source_symbol attribute");
2579
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002580 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002581 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002582 Language = SE->getString();
2583 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002584 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002585 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002586 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002587
2588 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002589 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2590 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002591}
2592
John McCalld041a9b2013-02-20 01:54:26 +00002593template <class T>
2594static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2595 typename T::VisibilityType value,
2596 unsigned attrSpellingListIndex) {
2597 T *existingAttr = D->getAttr<T>();
2598 if (existingAttr) {
2599 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2600 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002601 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002602 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2603 S.Diag(range.getBegin(), diag::note_previous_attribute);
2604 D->dropAttr<T>();
2605 }
2606 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2607}
2608
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002609VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002610 VisibilityAttr::VisibilityType Vis,
2611 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002612 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2613 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002614}
2615
John McCalld041a9b2013-02-20 01:54:26 +00002616TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2617 TypeVisibilityAttr::VisibilityType Vis,
2618 unsigned AttrSpellingListIndex) {
2619 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2620 AttrSpellingListIndex);
2621}
2622
Erich Keanee891aa92018-07-13 15:07:47 +00002623static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002624 bool isTypeVisibility) {
2625 // Visibility attributes don't mean anything on a typedef.
2626 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002627 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002628 return;
2629 }
2630
2631 // 'type_visibility' can only go on a type or namespace.
2632 if (isTypeVisibility &&
2633 !(isa<TagDecl>(D) ||
2634 isa<ObjCInterfaceDecl>(D) ||
2635 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002636 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002637 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002638 return;
2639 }
2640
Benjamin Kramer70370212013-09-09 15:08:57 +00002641 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002642 StringRef TypeStr;
2643 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002644 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002645 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002646
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002647 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002648 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002649 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2650 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002651 return;
2652 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002653
Aaron Ballman682ee422013-09-11 19:47:58 +00002654 // Complain about attempts to use protected visibility on targets
2655 // (like Darwin) that don't support it.
2656 if (type == VisibilityAttr::Protected &&
2657 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002658 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002659 type = VisibilityAttr::Default;
2660 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002661
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002662 unsigned Index = AL.getAttributeSpellingListIndex();
2663 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002664 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002665 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002666 (TypeVisibilityAttr::VisibilityType) type,
2667 Index);
2668 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002669 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002670 }
2671 if (newAttr)
2672 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002673}
2674
Erich Keanee891aa92018-07-13 15:07:47 +00002675static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002676 const auto *M = cast<ObjCMethodDecl>(D);
2677 if (!AL.isArgIdent(0)) {
2678 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002679 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002680 return;
2681 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002682
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002683 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002684 ObjCMethodFamilyAttr::FamilyKind F;
2685 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002686 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002687 return;
2688 }
2689
Alp Toker314cc812014-01-25 16:55:45 +00002690 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002691 !M->getReturnType()->isObjCObjectPointerType()) {
2692 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2693 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002694 // Ignore the attribute.
2695 return;
2696 }
2697
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002698 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2699 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002700}
2701
Erich Keanee891aa92018-07-13 15:07:47 +00002702static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002703 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002704 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002705 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002706 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2707 return;
2708 }
2709 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002710 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002711 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002712 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002713 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2714 return;
2715 }
2716 }
2717 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002718 // It is okay to include this attribute on properties, e.g.:
2719 //
2720 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2721 //
2722 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002723 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002724 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002725 }
Michael Han99315932013-01-24 16:46:58 +00002726 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002727 ObjCNSObjectAttr(AL.getRange(), S.Context,
2728 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002729}
2730
Erich Keanee891aa92018-07-13 15:07:47 +00002731static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002732 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002733 QualType T = TD->getUnderlyingType();
2734 if (!T->isObjCObjectPointerType()) {
2735 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2736 return;
2737 }
2738 } else {
2739 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2740 return;
2741 }
2742 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002743 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2744 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002745}
2746
Erich Keanee891aa92018-07-13 15:07:47 +00002747static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002748 if (!AL.isArgIdent(0)) {
2749 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002750 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002751 return;
2752 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002753
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002754 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002755 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002756 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002757 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002758 return;
2759 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002760
Michael Han99315932013-01-24 16:46:58 +00002761 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002762 BlocksAttr(AL.getRange(), S.Context, type,
2763 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002764}
2765
Erich Keanee891aa92018-07-13 15:07:47 +00002766static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002767 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002768 if (AL.getNumArgs() > 0) {
2769 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002770 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002771 if (E->isTypeDependent() || E->isValueDependent() ||
2772 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002773 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002774 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002775 return;
2776 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002777
John McCallb46f2872011-09-09 07:56:05 +00002778 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002779 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002780 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002781 return;
2782 }
John McCallb46f2872011-09-09 07:56:05 +00002783
2784 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002785 }
2786
Aaron Ballman18a78382013-11-21 00:28:23 +00002787 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002788 if (AL.getNumArgs() > 1) {
2789 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002790 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002791 if (E->isTypeDependent() || E->isValueDependent() ||
2792 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002793 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002794 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002795 return;
2796 }
2797 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002798
John McCallb46f2872011-09-09 07:56:05 +00002799 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002800 // FIXME: This error message could be improved, it would be nice
2801 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002802 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002803 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002804 return;
2805 }
2806 }
2807
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002808 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002809 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002810 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002811 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002812 return;
2813 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002814
Chris Lattner9363e312009-03-17 23:03:47 +00002815 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002816 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002817 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002818 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002819 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002820 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002821 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002822 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002823 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002824 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002825 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002826 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002827 return;
2828 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002829 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002830 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002831 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002832 const FunctionType *FT = Ty->isFunctionPointerType()
2833 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002834 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002835 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002836 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002837 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002838 return;
2839 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002840 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002841 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002842 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002843 return;
2844 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002845 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002846 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002847 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002848 return;
2849 }
Michael Han99315932013-01-24 16:46:58 +00002850 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002851 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2852 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002853}
2854
Erich Keanee891aa92018-07-13 15:07:47 +00002855static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002856 if (D->getFunctionType() &&
2857 D->getFunctionType()->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002858 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002859 return;
2860 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002861 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002862 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002863 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002864 return;
2865 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002866
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002867 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002868 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002869 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2870 !AL.getScopeName())
Erich Keane44bacdf2018-08-09 13:21:32 +00002871 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballmane7964782016-03-07 22:44:55 +00002872
Fangrui Song6907ce22018-07-30 19:24:48 +00002873 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002874 WarnUnusedResultAttr(AL.getRange(), S.Context,
2875 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002876}
2877
Erich Keanee891aa92018-07-13 15:07:47 +00002878static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002879 // weak_import only applies to variable & function declarations.
2880 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002881 if (!D->canBeWeakImported(isDef)) {
2882 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002883 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002884 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002885 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002886 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002887 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002888 // Nothing to warn about here.
2889 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002890 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002891 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002892
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002893 return;
2894 }
2895
Michael Han99315932013-01-24 16:46:58 +00002896 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002897 WeakImportAttr(AL.getRange(), S.Context,
2898 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002899}
2900
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002901// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002902template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002903static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002904 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002905 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002906 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002907 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2908 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002909 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002910 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002911 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002912 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002913 return;
2914 }
2915 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002916
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002917 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2918 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2919 Existing->getYDim() == WGSize[1] &&
2920 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002921 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002922
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002923 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002924 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002925 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002926}
2927
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002928// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002929static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002930 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002931 const Expr *E = AL.getArgAsExpr(0);
2932 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002933 return;
2934 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002935 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002936 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002937 return;
2938 }
2939
2940 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2941 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2942 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002943 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002944
2945 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002946 AL.getRange(), S.Context, SGSize,
2947 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002948}
2949
Erich Keanee891aa92018-07-13 15:07:47 +00002950static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002951 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002952 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002953 return;
2954 }
2955
Craig Topperc3ec1492014-05-26 06:22:03 +00002956 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002957 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002958 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002959
2960 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2961 (ParmType->isBooleanType() ||
2962 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002963 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002964 << ParmType;
2965 return;
2966 }
2967
Aaron Ballmana9e05402013-12-02 22:16:55 +00002968 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002969 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002970 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002971 return;
2972 }
2973 }
2974
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002975 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002976 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002977 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002978}
2979
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002980SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002981 StringRef Name,
2982 unsigned AttrSpellingListIndex) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002983 // Explicit or partial specializations do not inherit
2984 // the section attribute from the primary template.
2985 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2986 if (AttrSpellingListIndex == SectionAttr::Declspec_allocate &&
2987 FD->isFunctionTemplateSpecialization())
2988 return nullptr;
2989 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002990 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2991 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002992 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002993 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2994 << 1 /*section*/;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002995 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002996 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002997 }
Michael Han99315932013-01-24 16:46:58 +00002998 return ::new (Context) SectionAttr(Range, Context, Name,
2999 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00003000}
3001
Reid Kleckner2a133222015-03-04 23:39:17 +00003002bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
3003 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
3004 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00003005 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00003006 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00003007 return false;
3008 }
3009 return true;
3010}
3011
Erich Keanee891aa92018-07-13 15:07:47 +00003012static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00003013 // Make sure that there is a string literal as the sections's single
3014 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003015 StringRef Str;
3016 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003017 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00003018 return;
Mike Stump11289f42009-09-09 15:08:12 +00003019
Reid Kleckner2a133222015-03-04 23:39:17 +00003020 if (!S.checkSectionName(LiteralLoc, Str))
3021 return;
3022
Chris Lattner30ba6742009-08-10 19:03:04 +00003023 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003024 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00003025 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003026 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00003027 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00003028 return;
3029 }
Mike Stump11289f42009-09-09 15:08:12 +00003030
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003031 unsigned Index = AL.getAttributeSpellingListIndex();
3032 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003033 if (NewAttr)
3034 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00003035}
3036
Erich Keane7963e8b2018-07-18 20:04:48 +00003037static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) {
3038 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
3039 if (!Error.empty()) {
3040 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
3041 << 0 /*'code-seg'*/;
3042 return false;
3043 }
3044 return true;
3045}
3046
3047CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range,
3048 StringRef Name,
3049 unsigned AttrSpellingListIndex) {
3050 // Explicit or partial specializations do not inherit
3051 // the code_seg attribute from the primary template.
3052 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3053 if (FD->isFunctionTemplateSpecialization())
3054 return nullptr;
3055 }
3056 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3057 if (ExistingAttr->getName() == Name)
3058 return nullptr;
3059 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3060 << 0 /*codeseg*/;
3061 Diag(Range.getBegin(), diag::note_previous_attribute);
3062 return nullptr;
3063 }
3064 return ::new (Context) CodeSegAttr(Range, Context, Name,
3065 AttrSpellingListIndex);
3066}
3067
3068static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3069 StringRef Str;
3070 SourceLocation LiteralLoc;
3071 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3072 return;
3073 if (!checkCodeSegName(S, LiteralLoc, Str))
3074 return;
3075 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3076 if (!ExistingAttr->isImplicit()) {
3077 S.Diag(AL.getLoc(),
3078 ExistingAttr->getName() == Str
3079 ? diag::warn_duplicate_codeseg_attribute
3080 : diag::err_conflicting_codeseg_attribute);
3081 return;
3082 }
3083 D->dropAttr<CodeSegAttr>();
3084 }
3085 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str,
3086 AL.getAttributeSpellingListIndex()))
3087 D->addAttr(CSA);
3088}
3089
Erich Keane57e15cd2017-07-19 22:06:33 +00003090// Check for things we'd like to warn about. Multiversioning issues are
3091// handled later in the process, once we know how many exist.
3092bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3093 enum FirstParam { Unsupported, Duplicate };
3094 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00003095 for (auto Str : {"tune=", "fpmath="})
3096 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00003097 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3098 << Unsupported << None << Str;
3099
3100 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
3101
3102 if (!ParsedAttrs.Architecture.empty() &&
3103 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3104 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3105 << Unsupported << Architecture << ParsedAttrs.Architecture;
3106
3107 if (ParsedAttrs.DuplicateArchitecture)
3108 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3109 << Duplicate << None << "arch=";
3110
3111 for (const auto &Feature : ParsedAttrs.Features) {
3112 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3113 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3114 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3115 << Unsupported << None << CurFeature;
3116 }
3117
Erich Keane29636aa2018-02-16 17:31:59 +00003118 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003119}
3120
Erich Keanee891aa92018-07-13 15:07:47 +00003121static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003122 StringRef Str;
3123 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003124 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003125 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003126 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003127
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003128 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00003129 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003130 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00003131 D->addAttr(NewAttr);
3132}
3133
Erich Keanee891aa92018-07-13 15:07:47 +00003134static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003135 Expr *E = AL.getArgAsExpr(0);
3136 uint32_t VecWidth;
3137 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3138 AL.setInvalid();
3139 return;
3140 }
3141
3142 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3143 if (Existing && Existing->getVectorWidth() != VecWidth) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003144 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Craig Topper74c10e32018-07-09 19:00:16 +00003145 return;
3146 }
3147
3148 D->addAttr(::new (S.Context)
3149 MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth,
3150 AL.getAttributeSpellingListIndex()));
3151}
3152
Erich Keanee891aa92018-07-13 15:07:47 +00003153static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003154 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003155 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003156 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003157 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003158
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003159 // gcc only allows for simple identifiers. Since we support more than gcc, we
3160 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003161 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003162 if (DRE->hasQualifier())
3163 S.Diag(Loc, diag::warn_cleanup_ext);
3164 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3165 NI = DRE->getNameInfo();
3166 if (!FD) {
3167 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3168 << NI.getName();
3169 return;
3170 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003171 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003172 if (ULE->hasExplicitTemplateArgs())
3173 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003174 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3175 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003176 if (!FD) {
3177 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3178 << NI.getName();
3179 if (ULE->getType() == S.Context.OverloadTy)
3180 S.NoteAllOverloadCandidates(ULE);
3181 return;
3182 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003183 } else {
3184 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003185 return;
3186 }
3187
Anders Carlssond277d792009-01-31 01:16:18 +00003188 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003189 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3190 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003191 return;
3192 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003193
Anders Carlsson723f55d2009-02-07 23:16:50 +00003194 // We're currently more strict than GCC about what function types we accept.
3195 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003196 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003197 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003198 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3199 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003200 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3201 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003202 return;
3203 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003204
Michael Han99315932013-01-24 16:46:58 +00003205 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003206 CleanupAttr(AL.getRange(), S.Context, FD,
3207 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003208}
3209
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003210static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003211 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003212 if (!AL.isArgIdent(0)) {
3213 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003214 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003215 return;
3216 }
3217
3218 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003219 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003220 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3221 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003222 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003223 return;
3224 }
3225
3226 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003227 AL.getRange(), S.Context, ExtensibilityKind,
3228 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003229}
3230
Mike Stumpd3bb5572009-07-24 19:02:52 +00003231/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003232/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003233static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003234 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003235 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003236 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003237 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003238
Eric Christopherb64963e2015-08-13 21:34:35 +00003239 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003240 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003241
Eric Christopherb64963e2015-08-13 21:34:35 +00003242 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3243 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003244 !isCFStringType(Ty, S.Context) &&
3245 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003246 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003247 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003248 << "a string type" << IdxExpr->getSourceRange()
3249 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003250 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003251 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003252 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003253 if (!isNSStringType(Ty, S.Context) &&
3254 !isCFStringType(Ty, S.Context) &&
3255 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003256 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003257 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003258 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003259 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003260 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003261 }
3262
Joel E. Denny81508102018-03-13 14:51:22 +00003263 D->addAttr(::new (S.Context) FormatArgAttr(
3264 AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003265}
3266
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003267enum FormatAttrKind {
3268 CFStringFormat,
3269 NSStringFormat,
3270 StrftimeFormat,
3271 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003272 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003273 InvalidFormat
3274};
3275
3276/// getFormatAttrKind - Map from format attribute names to supported format
3277/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003278static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003279 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003280 // Check for formats that get handled specially.
3281 .Case("NSString", NSStringFormat)
3282 .Case("CFString", CFStringFormat)
3283 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003284
Mehdi Amini06d367c2016-10-24 20:39:34 +00003285 // Otherwise, check for supported formats.
3286 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3287 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3288 .Case("kprintf", SupportedFormat) // OpenBSD.
3289 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3290 .Case("os_trace", SupportedFormat)
3291 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003292
Mehdi Amini06d367c2016-10-24 20:39:34 +00003293 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3294 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003295}
3296
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003297/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003298/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003299static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003300 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003301 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003302 return;
3303 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003304
Aaron Ballman4a611152013-11-27 16:34:09 +00003305 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003306 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3307 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003308 return;
3309 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003310 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003311 if (S.Context.getAsArrayType(T))
3312 T = S.Context.getBaseElementType(T);
3313 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003314 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3315 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003316 return;
3317 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003318
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003319 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003320 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003321 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3322 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003323 return;
3324 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003325
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003326 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003327 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003328 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003329 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003330 return;
3331 }
Michael Han99315932013-01-24 16:46:58 +00003332 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003333 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3334 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003335}
3336
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003337FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3338 IdentifierInfo *Format, int FormatIdx,
3339 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003340 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003341 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003342 for (auto *F : D->specific_attrs<FormatAttr>()) {
3343 if (F->getType() == Format &&
3344 F->getFormatIdx() == FormatIdx &&
3345 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003346 // If we don't have a valid location for this attribute, adopt the
3347 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003348 if (F->getLocation().isInvalid())
3349 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003350 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003351 }
3352 }
3353
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003354 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3355 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003356}
3357
Mike Stumpd3bb5572009-07-24 19:02:52 +00003358/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003359/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003360static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003361 if (!AL.isArgIdent(0)) {
3362 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003363 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003364 return;
3365 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003366
Chandler Carruth743682b2010-11-16 08:35:43 +00003367 // In C++ the implicit 'this' function parameter also counts, and they are
3368 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003369 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003370 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003371
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003372 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003373 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003374
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003375 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003376 // If we've modified the string name, we need a new identifier for it.
3377 II = &S.Context.Idents.get(Format);
3378 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003379
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003380 // Check for supported formats.
3381 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003382
Chris Lattner12161d32010-03-22 21:08:50 +00003383 if (Kind == IgnoredFormat)
3384 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003385
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003386 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003387 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003388 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003389 return;
3390 }
3391
3392 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003393 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003394 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003395 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003396 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003397
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003398 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003399 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003400 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003401 return;
3402 }
3403
3404 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003405 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003406
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003407 if (HasImplicitThisParam) {
3408 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003409 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003410 diag::err_format_attribute_implicit_this_format_string)
3411 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003412 return;
3413 }
3414 ArgIdx--;
3415 }
Mike Stump11289f42009-09-09 15:08:12 +00003416
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003417 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003418 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003419
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003420 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003421 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003422 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003423 << "a CFString" << IdxExpr->getSourceRange()
3424 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003425 return;
3426 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003427 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003428 // FIXME: do we need to check if the type is NSString*? What are the
3429 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003430 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003431 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003432 << "an NSString" << IdxExpr->getSourceRange()
3433 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003434 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003435 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003436 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003437 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003438 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003439 << "a string type" << IdxExpr->getSourceRange()
3440 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003441 return;
3442 }
3443
3444 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003445 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003446 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003447 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003448 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003449
3450 // check if the function is variadic if the 3rd argument non-zero
3451 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003452 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003453 ++NumArgs; // +1 for ...
3454 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003455 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003456 return;
3457 }
3458 }
3459
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003460 // strftime requires FirstArg to be 0 because it doesn't read from any
3461 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003462 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003463 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003464 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003465 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003466 return;
3467 }
3468 // if 0 it disables parameter checking (to use with e.g. va_list)
3469 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003470 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003471 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003472 return;
3473 }
3474
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003475 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003476 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003477 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003478 if (NewAttr)
3479 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003480}
3481
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003482/// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
3483static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3484 // The index that identifies the callback callee is mandatory.
3485 if (AL.getNumArgs() == 0) {
3486 S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee)
3487 << AL.getRange();
3488 return;
3489 }
3490
3491 bool HasImplicitThisParam = isInstanceMethod(D);
3492 int32_t NumArgs = getFunctionOrMethodNumParams(D);
3493
3494 FunctionDecl *FD = D->getAsFunction();
3495 assert(FD && "Expected a function declaration!");
3496
3497 llvm::StringMap<int> NameIdxMapping;
3498 NameIdxMapping["__"] = -1;
3499
3500 NameIdxMapping["this"] = 0;
3501
3502 int Idx = 1;
3503 for (const ParmVarDecl *PVD : FD->parameters())
3504 NameIdxMapping[PVD->getName()] = Idx++;
3505
3506 auto UnknownName = NameIdxMapping.end();
3507
3508 SmallVector<int, 8> EncodingIndices;
3509 for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) {
3510 SourceRange SR;
3511 int32_t ArgIdx;
3512
3513 if (AL.isArgIdent(I)) {
3514 IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
3515 auto It = NameIdxMapping.find(IdLoc->Ident->getName());
3516 if (It == UnknownName) {
3517 S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown)
3518 << IdLoc->Ident << IdLoc->Loc;
3519 return;
3520 }
3521
3522 SR = SourceRange(IdLoc->Loc);
3523 ArgIdx = It->second;
3524 } else if (AL.isArgExpr(I)) {
3525 Expr *IdxExpr = AL.getArgAsExpr(I);
3526
3527 // If the expression is not parseable as an int32_t we have a problem.
3528 if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1,
3529 false)) {
3530 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3531 << AL << (I + 1) << IdxExpr->getSourceRange();
3532 return;
3533 }
3534
3535 // Check oob, excluding the special values, 0 and -1.
3536 if (ArgIdx < -1 || ArgIdx > NumArgs) {
3537 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3538 << AL << (I + 1) << IdxExpr->getSourceRange();
3539 return;
3540 }
3541
3542 SR = IdxExpr->getSourceRange();
3543 } else {
3544 llvm_unreachable("Unexpected ParsedAttr argument type!");
3545 }
3546
3547 if (ArgIdx == 0 && !HasImplicitThisParam) {
3548 S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
3549 << (I + 1) << SR;
3550 return;
3551 }
3552
3553 // Adjust for the case we do not have an implicit "this" parameter. In this
3554 // case we decrease all positive values by 1 to get LLVM argument indices.
3555 if (!HasImplicitThisParam && ArgIdx > 0)
3556 ArgIdx -= 1;
3557
3558 EncodingIndices.push_back(ArgIdx);
3559 }
3560
3561 int CalleeIdx = EncodingIndices.front();
3562 // Check if the callee index is proper, thus not "this" and not "unknown".
3563 if (CalleeIdx < HasImplicitThisParam) {
3564 S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee)
3565 << AL.getRange();
3566 return;
3567 }
3568
3569 // Get the callee type, note the index adjustment as the AST doesn't contain
3570 // the this type (which the callee cannot reference anyway!).
3571 const Type *CalleeType =
3572 getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam)
3573 .getTypePtr();
3574 if (!CalleeType || !CalleeType->isFunctionPointerType()) {
3575 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3576 << AL.getRange();
3577 return;
3578 }
3579
3580 const Type *CalleeFnType =
3581 CalleeType->getPointeeType()->getUnqualifiedDesugaredType();
3582
3583 // TODO: Check the type of the callee arguments.
3584
3585 const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
3586 if (!CalleeFnProtoType) {
3587 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3588 << AL.getRange();
3589 return;
3590 }
3591
3592 if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
3593 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3594 << AL << (unsigned)(EncodingIndices.size() - 1);
3595 return;
3596 }
3597
3598 if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
3599 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3600 << AL << (unsigned)(EncodingIndices.size() - 1);
3601 return;
3602 }
3603
3604 if (CalleeFnProtoType->isVariadic()) {
3605 S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
3606 return;
3607 }
3608
3609 // Do not allow multiple callback attributes.
3610 if (D->hasAttr<CallbackAttr>()) {
3611 S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
3612 return;
3613 }
3614
3615 D->addAttr(::new (S.Context) CallbackAttr(
3616 AL.getRange(), S.Context, EncodingIndices.data(), EncodingIndices.size(),
3617 AL.getAttributeSpellingListIndex()));
3618}
3619
Erich Keanee891aa92018-07-13 15:07:47 +00003620static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003621 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003622 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003623 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003624 if (TD && TD->getUnderlyingType()->isUnionType())
3625 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3626 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003627 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003628
3629 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003630 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3631 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003632 return;
3633 }
3634
John McCallf937c022011-10-07 06:10:15 +00003635 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003636 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003637 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003638 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003639 return;
3640 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003641
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003642 RecordDecl::field_iterator Field = RD->field_begin(),
3643 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003644 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003645 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003646 return;
3647 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003648
David Blaikie40ed2972012-06-06 20:45:41 +00003649 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003650 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003651 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003652 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003653 diag::warn_transparent_union_attribute_floating)
3654 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003655 return;
3656 }
3657
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003658 if (FirstType->isIncompleteType())
3659 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003660 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3661 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3662 for (; Field != FieldEnd; ++Field) {
3663 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003664 if (FieldType->isIncompleteType())
3665 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003666 // FIXME: this isn't fully correct; we also need to test whether the
3667 // members of the union would all have the same calling convention as the
3668 // first member of the union. Checking just the size and alignment isn't
3669 // sufficient (consider structs passed on the stack instead of in registers
3670 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003671 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003672 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003673 // Warn if we drop the attribute.
3674 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003675 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003676 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003677 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003678 diag::warn_transparent_union_attribute_field_size_align)
3679 << isSize << Field->getDeclName() << FieldBits;
3680 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003681 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003682 diag::note_transparent_union_first_field_size_align)
3683 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003684 return;
3685 }
3686 }
3687
Michael Han99315932013-01-24 16:46:58 +00003688 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003689 TransparentUnionAttr(AL.getRange(), S.Context,
3690 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003691}
3692
Erich Keanee891aa92018-07-13 15:07:47 +00003693static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003694 // Make sure that there is a string literal as the annotation's single
3695 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003696 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003697 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003698 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003699
3700 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003701 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3702 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003703 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003704 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003705
Michael Han99315932013-01-24 16:46:58 +00003706 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003707 AnnotateAttr(AL.getRange(), S.Context, Str,
3708 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003709}
3710
Erich Keanee891aa92018-07-13 15:07:47 +00003711static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003712 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3713 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003714}
3715
3716void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3717 unsigned SpellingListIndex) {
3718 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3719 SourceLocation AttrLoc = AttrRange.getBegin();
3720
3721 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003722 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003723 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003724 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003725 T = VD->getType();
3726 else
3727 llvm_unreachable("Unknown decl type for align_value");
3728
3729 if (!T->isDependentType() && !T->isAnyPointerType() &&
3730 !T->isReferenceType() && !T->isMemberPointerType()) {
3731 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3732 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3733 return;
3734 }
3735
3736 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003737 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003738 ExprResult ICE
3739 = VerifyIntegerConstantExpression(E, &Alignment,
3740 diag::err_align_value_attribute_argument_not_int,
3741 /*AllowFold*/ false);
3742 if (ICE.isInvalid())
3743 return;
3744
3745 if (!Alignment.isPowerOf2()) {
3746 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3747 << E->getSourceRange();
3748 return;
3749 }
3750
3751 D->addAttr(::new (Context)
3752 AlignValueAttr(AttrRange, Context, ICE.get(),
3753 SpellingListIndex));
3754 return;
3755 }
3756
3757 // Save dependent expressions in the AST to be instantiated.
3758 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003759}
3760
Erich Keanee891aa92018-07-13 15:07:47 +00003761static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003762 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003763 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003764 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003765 return;
3766 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003767
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003768 if (AL.getNumArgs() == 0) {
3769 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3770 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003771 return;
3772 }
3773
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003774 Expr *E = AL.getArgAsExpr(0);
3775 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3776 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003777 diag::err_pack_expansion_without_parameter_packs);
3778 return;
3779 }
3780
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003781 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003782 return;
3783
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003784 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3785 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003786}
3787
3788void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003789 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003790 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3791 SourceLocation AttrLoc = AttrRange.getBegin();
3792
Richard Smith1dba27c2013-01-29 09:02:09 +00003793 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003794 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003795 // C++11 [dcl.align]p1:
3796 // An alignment-specifier may be applied to a variable or to a class
3797 // data member, but it shall not be applied to a bit-field, a function
3798 // parameter, the formal parameter of a catch clause, or a variable
3799 // declared with the register storage class specifier. An
3800 // alignment-specifier may also be applied to the declaration of a class
3801 // or enumeration type.
3802 // C11 6.7.5/2:
3803 // An alignment attribute shall not be specified in a declaration of
3804 // a typedef, or a bit-field, or a function, or a parameter, or an
3805 // object declared with the register storage-class specifier.
3806 int DiagKind = -1;
3807 if (isa<ParmVarDecl>(D)) {
3808 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003809 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003810 if (VD->getStorageClass() == SC_Register)
3811 DiagKind = 1;
3812 if (VD->isExceptionVariable())
3813 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003814 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003815 if (FD->isBitField())
3816 DiagKind = 3;
3817 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003818 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003819 << (TmpAttr.isC11() ? ExpectedVariableOrField
3820 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003821 return;
3822 }
3823 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003824 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003825 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003826 return;
3827 }
3828 }
3829
Richard Smith90ae9672018-06-20 23:36:55 +00003830 if (E->isValueDependent()) {
3831 // We can't support a dependent alignment on a non-dependent type,
3832 // because we have no way to model that a type is "alignment-dependent"
3833 // but not dependent in any other way.
3834 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3835 if (!TND->getUnderlyingType()->isDependentType()) {
3836 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3837 << E->getSourceRange();
3838 return;
3839 }
3840 }
3841
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003842 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003843 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3844 AA->setPackExpansion(IsPackExpansion);
3845 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003846 return;
3847 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003848
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003849 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003850 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003851 ExprResult ICE
3852 = VerifyIntegerConstantExpression(E, &Alignment,
3853 diag::err_aligned_attribute_argument_not_int,
3854 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003855 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003856 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003857
David Majnemer0be6bd02015-07-26 09:02:21 +00003858 uint64_t AlignVal = Alignment.getZExtValue();
3859
Richard Smith848e1f12013-02-01 08:12:08 +00003860 // C++11 [dcl.align]p2:
3861 // -- if the constant expression evaluates to zero, the alignment
3862 // specifier shall have no effect
3863 // C11 6.7.5p6:
3864 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003865 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003866 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003867 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3868 << E->getSourceRange();
3869 return;
3870 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003871 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003872
David Majnemerabecae72014-02-12 20:36:10 +00003873 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003874 unsigned MaxValidAlignment =
3875 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3876 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003877 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003878 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3879 << E->getSourceRange();
3880 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003881 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003882
David Majnemer0be6bd02015-07-26 09:02:21 +00003883 if (Context.getTargetInfo().isTLSSupported()) {
3884 unsigned MaxTLSAlign =
3885 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3886 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003887 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003888 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3889 VD->getTLSKind() != VarDecl::TLS_None) {
3890 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3891 << (unsigned)AlignVal << VD << MaxTLSAlign;
3892 return;
3893 }
3894 }
3895
Richard Smith44c247f2013-02-22 08:32:16 +00003896 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003897 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003898 AA->setPackExpansion(IsPackExpansion);
3899 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003900}
3901
Michael Hanaf02bbe2013-02-01 01:19:17 +00003902void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003903 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003904 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003905 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003906 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3907 SpellingListIndex);
3908 AA->setPackExpansion(IsPackExpansion);
3909 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003910}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003911
Richard Smith848e1f12013-02-01 08:12:08 +00003912void Sema::CheckAlignasUnderalignment(Decl *D) {
3913 assert(D->hasAttrs() && "no attributes on decl");
3914
David Majnemer475b25e2015-01-21 10:54:38 +00003915 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003916 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003917 UnderlyingTy = DiagTy = VD->getType();
3918 } else {
3919 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003920 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003921 UnderlyingTy = ED->getIntegerType();
3922 }
3923 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003924 return;
3925
3926 // C++11 [dcl.align]p5, C11 6.7.5/4:
3927 // The combined effect of all alignment attributes in a declaration shall
3928 // not specify an alignment that is less strict than the alignment that
3929 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003930 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003931 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003932 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003933 if (I->isAlignmentDependent())
3934 return;
3935 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003936 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003937 Align = std::max(Align, I->getAlignment(Context));
3938 }
3939
3940 if (AlignasAttr && Align) {
3941 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003942 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003943 if (NaturalAlign > RequestedAlign)
3944 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003945 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003946 }
3947}
3948
David Majnemer2c4e00a2014-01-29 22:07:36 +00003949bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003950 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003951 MSInheritanceAttr::Spelling SemanticSpelling) {
3952 assert(RD->hasDefinition() && "RD has no definition!");
3953
David Majnemer98c9ee22014-02-07 00:43:07 +00003954 // We may not have seen base specifiers or any virtual methods yet. We will
3955 // have to wait until the record is defined to catch any mismatches.
3956 if (!RD->getDefinition()->isCompleteDefinition())
3957 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003958
David Majnemer98c9ee22014-02-07 00:43:07 +00003959 // The unspecified model never matches what a definition could need.
3960 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3961 return false;
3962
David Majnemer4bb09802014-02-10 19:50:15 +00003963 if (BestCase) {
3964 if (RD->calculateInheritanceModel() == SemanticSpelling)
3965 return false;
3966 } else {
3967 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3968 return false;
3969 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003970
3971 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3972 << 0 /*definition*/;
3973 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3974 << RD->getNameAsString();
3975 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003976}
3977
Alexey Bataevf278eb12015-11-19 10:13:11 +00003978/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3979/// attribute.
3980static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3981 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003982 IntegerMode = true;
3983 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003984 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003985 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003986 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003987 case 'Q':
3988 DestWidth = 8;
3989 break;
3990 case 'H':
3991 DestWidth = 16;
3992 break;
3993 case 'S':
3994 DestWidth = 32;
3995 break;
3996 case 'D':
3997 DestWidth = 64;
3998 break;
3999 case 'X':
4000 DestWidth = 96;
4001 break;
4002 case 'T':
4003 DestWidth = 128;
4004 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00004005 }
4006 if (Str[1] == 'F') {
4007 IntegerMode = false;
4008 } else if (Str[1] == 'C') {
4009 IntegerMode = false;
4010 ComplexMode = true;
4011 } else if (Str[1] != 'I') {
4012 DestWidth = 0;
4013 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00004014 break;
4015 case 4:
4016 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
4017 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00004018 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00004019 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00004020 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00004021 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00004022 break;
4023 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00004024 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00004025 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00004026 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00004027 case 11:
4028 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00004029 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00004030 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004031 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00004032}
4033
4034/// handleModeAttr - This attribute modifies the width of a decl with primitive
4035/// type.
4036///
4037/// Despite what would be logical, the mode attribute is a decl attribute, not a
4038/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
4039/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00004040static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00004041 // This attribute isn't documented, but glibc uses it. It changes
4042 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004043 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004044 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
4045 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004046 return;
4047 }
4048
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004049 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004050
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004051 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004052}
4053
4054void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
4055 unsigned SpellingListIndex, bool InInstantiation) {
4056 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00004057 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004058 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00004059
4060 unsigned DestWidth = 0;
4061 bool IntegerMode = true;
4062 bool ComplexMode = false;
4063 llvm::APInt VectorSize(64, 0);
4064 if (Str.size() >= 4 && Str[0] == 'V') {
4065 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
4066 size_t StrSize = Str.size();
4067 size_t VectorStringLength = 0;
4068 while ((VectorStringLength + 1) < StrSize &&
4069 isdigit(Str[VectorStringLength + 1]))
4070 ++VectorStringLength;
4071 if (VectorStringLength &&
4072 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
4073 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004074 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00004075 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004076 // Avoid duplicate warning from template instantiation.
4077 if (!InInstantiation)
4078 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00004079 } else {
4080 VectorSize = 0;
4081 }
4082 }
4083
4084 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004085 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
4086
4087 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
4088 // and friends, at least with glibc.
4089 // FIXME: Make sure floating-point mappings are accurate
4090 // FIXME: Support XF and TF types
4091 if (!DestWidth) {
4092 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
4093 return;
4094 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00004095
4096 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004097 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00004098 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004099 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004100 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
4101 // Try to get type from enum declaration, default to int.
4102 OldTy = ED->getIntegerType();
4103 if (OldTy.isNull())
4104 OldTy = Context.IntTy;
4105 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004106 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00004107
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004108 if (OldTy->isDependentType()) {
4109 D->addAttr(::new (Context)
4110 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
4111 return;
4112 }
4113
Alexey Bataev326057d2015-06-19 07:46:21 +00004114 // Base type can also be a vector type (see PR17453).
4115 // Distinguish between base type and base element type.
4116 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004117 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00004118 OldElemTy = VT->getElementType();
4119
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004120 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
4121 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
4122 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
4123 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
4124 VectorSize.getBoolValue()) {
4125 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
4126 return;
4127 }
4128 bool IntegralOrAnyEnumType =
4129 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
4130
4131 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
4132 !IntegralOrAnyEnumType)
4133 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00004134 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004135 if (!IntegralOrAnyEnumType)
4136 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004137 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004138 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004139 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004140 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00004141 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004142 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004143 }
4144
Alexey Bataev326057d2015-06-19 07:46:21 +00004145 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004146
4147 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004148 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
4149 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004150 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004151 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004152
Alexey Bataev326057d2015-06-19 07:46:21 +00004153 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004154 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004155 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004156 }
4157
Eli Friedman4735374e2009-03-03 06:41:03 +00004158 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004159 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004160 }
4161
4162 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004163 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004164 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
4165 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004166 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004167 // Complex machine mode does not support base vector types.
4168 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004169 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004170 return;
4171 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004172 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00004173 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004174 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004175 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004176 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00004177 }
4178
4179 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004180 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004181 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004182 }
4183
4184 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004185 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004186 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004187 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004188 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004189 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004190 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004191
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004192 D->addAttr(::new (Context)
4193 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00004194}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004195
Erich Keanee891aa92018-07-13 15:07:47 +00004196static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Han99315932013-01-24 16:46:58 +00004197 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004198 NoDebugAttr(AL.getRange(), S.Context,
4199 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00004200}
4201
Paul Robinson30e41fb2014-12-15 18:57:28 +00004202AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00004203 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00004204 unsigned AttrSpellingListIndex) {
4205 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004206 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00004207 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4208 return nullptr;
4209 }
4210
4211 if (D->hasAttr<AlwaysInlineAttr>())
4212 return nullptr;
4213
4214 return ::new (Context) AlwaysInlineAttr(Range, Context,
4215 AttrSpellingListIndex);
4216}
4217
Erich Keane44bacdf2018-08-09 13:21:32 +00004218CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
4219 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004220 return nullptr;
4221
Erich Keane44bacdf2018-08-09 13:21:32 +00004222 return ::new (Context)
4223 CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004224}
4225
Erich Keane44bacdf2018-08-09 13:21:32 +00004226CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
4227 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
4228 return nullptr;
4229
4230 return ::new (Context)
4231 CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex());
4232}
4233
4234InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4235 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004236 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004237 // Attribute applies to Var but not any subclass of it (like ParmVar,
4238 // ImplicitParm or VarTemplateSpecialization).
4239 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004240 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4241 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4242 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004243 return nullptr;
4244 }
4245 // Attribute does not apply to non-static local variables.
4246 if (VD->hasLocalStorage()) {
4247 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4248 return nullptr;
4249 }
4250 }
4251
Erich Keane44bacdf2018-08-09 13:21:32 +00004252 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
4253 return nullptr;
4254
4255 return ::new (Context) InternalLinkageAttr(
4256 AL.getRange(), Context, AL.getAttributeSpellingListIndex());
4257}
4258InternalLinkageAttr *
4259Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4260 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4261 // Attribute applies to Var but not any subclass of it (like ParmVar,
4262 // ImplicitParm or VarTemplateSpecialization).
4263 if (VD->getKind() != Decl::Var) {
4264 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4265 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4266 : ExpectedVariableOrFunction);
4267 return nullptr;
4268 }
4269 // Attribute does not apply to non-static local variables.
4270 if (VD->hasLocalStorage()) {
4271 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4272 return nullptr;
4273 }
4274 }
4275
4276 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004277 return nullptr;
4278
4279 return ::new (Context)
Erich Keane44bacdf2018-08-09 13:21:32 +00004280 InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004281}
4282
Paul Robinson30e41fb2014-12-15 18:57:28 +00004283MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
4284 unsigned AttrSpellingListIndex) {
4285 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4286 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
4287 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4288 return nullptr;
4289 }
4290
4291 if (D->hasAttr<MinSizeAttr>())
4292 return nullptr;
4293
4294 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
4295}
4296
Zola Bridges826ef592019-01-18 17:20:46 +00004297NoSpeculativeLoadHardeningAttr *Sema::mergeNoSpeculativeLoadHardeningAttr(
4298 Decl *D, const NoSpeculativeLoadHardeningAttr &AL) {
4299 if (checkAttrMutualExclusion<SpeculativeLoadHardeningAttr>(*this, D, AL))
4300 return nullptr;
4301
4302 return ::new (Context) NoSpeculativeLoadHardeningAttr(
4303 AL.getRange(), Context, AL.getSpellingListIndex());
4304}
4305
Paul Robinson30e41fb2014-12-15 18:57:28 +00004306OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
4307 unsigned AttrSpellingListIndex) {
4308 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4309 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4310 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4311 D->dropAttr<AlwaysInlineAttr>();
4312 }
4313 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4314 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4315 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4316 D->dropAttr<MinSizeAttr>();
4317 }
4318
4319 if (D->hasAttr<OptimizeNoneAttr>())
4320 return nullptr;
4321
4322 return ::new (Context) OptimizeNoneAttr(Range, Context,
4323 AttrSpellingListIndex);
4324}
4325
Zola Bridges826ef592019-01-18 17:20:46 +00004326SpeculativeLoadHardeningAttr *Sema::mergeSpeculativeLoadHardeningAttr(
4327 Decl *D, const SpeculativeLoadHardeningAttr &AL) {
4328 if (checkAttrMutualExclusion<NoSpeculativeLoadHardeningAttr>(*this, D, AL))
4329 return nullptr;
4330
4331 return ::new (Context) SpeculativeLoadHardeningAttr(
4332 AL.getRange(), Context, AL.getSpellingListIndex());
4333}
4334
Erich Keanee891aa92018-07-13 15:07:47 +00004335static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004336 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004337 return;
4338
Paul Robinson080b1f32015-01-13 18:34:56 +00004339 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004340 D, AL.getRange(), AL.getName(),
4341 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004342 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004343}
4344
Erich Keanee891aa92018-07-13 15:07:47 +00004345static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004346 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004347 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004348 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004349}
4350
Erich Keanee891aa92018-07-13 15:07:47 +00004351static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004352 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004353 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004354 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004355}
4356
Erich Keanee891aa92018-07-13 15:07:47 +00004357static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004358 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004359 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004360 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004361 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004362 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004363 return;
4364 }
4365 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004366 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004367}
4368
Erich Keanee891aa92018-07-13 15:07:47 +00004369static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004370 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004371 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004372 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004373 // extern __shared__ is only allowed on arrays with no length (e.g.
4374 // "int x[]").
Yaxun Liu97670892018-10-02 17:48:54 +00004375 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004376 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004377 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004378 return;
4379 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004380 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004381 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004382 << S.CurrentCUDATarget())
4383 return;
Justin Lebar10411012016-09-30 23:57:30 +00004384 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004385 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004386}
4387
Erich Keanee891aa92018-07-13 15:07:47 +00004388static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004389 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4390 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004391 return;
4392 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004393 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004394 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004395 SourceRange RTRange = FD->getReturnTypeSourceRange();
4396 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004397 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004398 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4399 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004400 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004401 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004402 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4403 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004404 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004405 << Method;
4406 return;
4407 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004408 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004409 }
4410 // Only warn for "inline" when compiling for host, to cut down on noise.
4411 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004412 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004413
Aaron Ballman3aff6332013-12-02 19:30:36 +00004414 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004415 CUDAGlobalAttr(AL.getRange(), S.Context,
4416 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004417}
4418
Erich Keanee891aa92018-07-13 15:07:47 +00004419static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004420 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004421 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004422 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004423 return;
4424 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004425
Michael Han99315932013-01-24 16:46:58 +00004426 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004427 GNUInlineAttr(AL.getRange(), S.Context,
4428 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004429}
4430
Erich Keanee891aa92018-07-13 15:07:47 +00004431static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004432 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004433
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004434 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004435 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4436 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004437 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004438 return;
4439
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004440 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004441 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004442 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004443 return;
4444 }
4445
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004446 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004447 case ParsedAttr::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004448 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004449 FastCallAttr(AL.getRange(), S.Context,
4450 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004451 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004452 case ParsedAttr::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004453 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004454 StdCallAttr(AL.getRange(), S.Context,
4455 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004456 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004457 case ParsedAttr::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004458 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004459 ThisCallAttr(AL.getRange(), S.Context,
4460 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004461 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004462 case ParsedAttr::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004463 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004464 CDeclAttr(AL.getRange(), S.Context,
4465 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004466 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004467 case ParsedAttr::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004468 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004469 PascalAttr(AL.getRange(), S.Context,
4470 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004471 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004472 case ParsedAttr::AT_SwiftCall:
John McCall477f2bb2016-03-03 06:39:32 +00004473 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004474 SwiftCallAttr(AL.getRange(), S.Context,
4475 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004476 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004477 case ParsedAttr::AT_VectorCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004478 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004479 VectorCallAttr(AL.getRange(), S.Context,
4480 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004481 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004482 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004483 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004484 MSABIAttr(AL.getRange(), S.Context,
4485 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004486 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004487 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004488 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004489 SysVABIAttr(AL.getRange(), S.Context,
4490 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004491 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004492 case ParsedAttr::AT_RegCall:
Erich Keane757d3172016-11-02 18:29:35 +00004493 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004494 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004495 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004496 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004497 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004498 switch (CC) {
4499 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004500 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004501 break;
4502 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004503 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004504 break;
4505 default:
4506 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004507 }
4508
Michael Han99315932013-01-24 16:46:58 +00004509 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004510 PcsAttr(AL.getRange(), S.Context, PCS,
4511 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004512 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004513 }
Sander de Smalen44a22532018-11-26 16:38:37 +00004514 case ParsedAttr::AT_AArch64VectorPcs:
4515 D->addAttr(::new(S.Context)
4516 AArch64VectorPcsAttr(AL.getRange(), S.Context,
4517 AL.getAttributeSpellingListIndex()));
4518 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004519 case ParsedAttr::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004520 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004521 IntelOclBiccAttr(AL.getRange(), S.Context,
4522 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004523 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004524 case ParsedAttr::AT_PreserveMost:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004525 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004526 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004527 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004528 case ParsedAttr::AT_PreserveAll:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004529 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004530 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004531 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004532 default:
4533 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004534 }
4535}
4536
Erich Keanee891aa92018-07-13 15:07:47 +00004537static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004538 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004539 return;
4540
4541 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004542 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004543 StringRef RuleName;
4544
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004545 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004546 return;
4547
4548 // FIXME: Warn if the rule name is unknown. This is tricky because only
4549 // clang-tidy knows about available rules.
4550 DiagnosticIdentifiers.push_back(RuleName);
4551 }
4552 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004553 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4554 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004555}
4556
Erich Keanee891aa92018-07-13 15:07:47 +00004557bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004558 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004559 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004560 return true;
4561
Erich Keaneb11ebc52017-09-27 03:20:13 +00004562 if (Attrs.hasProcessingCache()) {
4563 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004564 return false;
4565 }
4566
Erich Keanee891aa92018-07-13 15:07:47 +00004567 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004568 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4569 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004570 return true;
4571 }
4572
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004573 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004574 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004575 case ParsedAttr::AT_CDecl:
4576 CC = CC_C;
4577 break;
4578 case ParsedAttr::AT_FastCall:
4579 CC = CC_X86FastCall;
4580 break;
4581 case ParsedAttr::AT_StdCall:
4582 CC = CC_X86StdCall;
4583 break;
4584 case ParsedAttr::AT_ThisCall:
4585 CC = CC_X86ThisCall;
4586 break;
4587 case ParsedAttr::AT_Pascal:
4588 CC = CC_X86Pascal;
4589 break;
4590 case ParsedAttr::AT_SwiftCall:
4591 CC = CC_Swift;
4592 break;
4593 case ParsedAttr::AT_VectorCall:
4594 CC = CC_X86VectorCall;
4595 break;
Sander de Smalen44a22532018-11-26 16:38:37 +00004596 case ParsedAttr::AT_AArch64VectorPcs:
4597 CC = CC_AArch64VectorCall;
4598 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004599 case ParsedAttr::AT_RegCall:
4600 CC = CC_X86RegCall;
4601 break;
4602 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004603 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004604 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004605 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004606 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004607 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4608 CC_C;
4609 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004610 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004611 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004612 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4613 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004614 return true;
4615 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004616 if (StrRef == "aapcs") {
4617 CC = CC_AAPCS;
4618 break;
4619 } else if (StrRef == "aapcs-vfp") {
4620 CC = CC_AAPCS_VFP;
4621 break;
4622 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004623
Erich Keaneb11ebc52017-09-27 03:20:13 +00004624 Attrs.setInvalid();
4625 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004626 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004627 }
Erich Keanee891aa92018-07-13 15:07:47 +00004628 case ParsedAttr::AT_IntelOclBicc:
4629 CC = CC_IntelOclBicc;
4630 break;
4631 case ParsedAttr::AT_PreserveMost:
4632 CC = CC_PreserveMost;
4633 break;
4634 case ParsedAttr::AT_PreserveAll:
4635 CC = CC_PreserveAll;
4636 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004637 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004638 }
4639
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004640 const TargetInfo &TI = Context.getTargetInfo();
4641 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004642 if (A != TargetInfo::CCCR_OK) {
4643 if (A == TargetInfo::CCCR_Warning)
Erich Keane44bacdf2018-08-09 13:21:32 +00004644 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004645
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004646 // This convention is not valid for the target. Use the default function or
4647 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004648 bool IsCXXMethod = false, IsVariadic = false;
4649 if (FD) {
4650 IsCXXMethod = FD->isCXXInstanceMember();
4651 IsVariadic = FD->isVariadic();
4652 }
4653 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004654 }
4655
Erich Keaneb11ebc52017-09-27 03:20:13 +00004656 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004657 return false;
4658}
4659
John McCall477f2bb2016-03-03 06:39:32 +00004660/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004661static bool isValidSwiftContextType(QualType Ty) {
4662 if (!Ty->hasPointerRepresentation())
4663 return Ty->isDependentType();
4664 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004665}
4666
4667/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004668static bool isValidSwiftIndirectResultType(QualType Ty) {
4669 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4670 Ty = PtrType->getPointeeType();
4671 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4672 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004673 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004674 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004675 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004676 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004677}
4678
4679/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004680static bool isValidSwiftErrorResultType(QualType Ty) {
4681 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4682 Ty = PtrType->getPointeeType();
4683 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4684 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004685 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004686 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004687 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004688 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004689 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004690 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004691}
4692
Erich Keanee891aa92018-07-13 15:07:47 +00004693static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004694 ParameterABI Abi) {
4695 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4696 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004697}
4698
4699void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4700 unsigned spellingIndex) {
4701
4702 QualType type = cast<ParmVarDecl>(D)->getType();
4703
4704 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4705 if (existingAttr->getABI() != abi) {
4706 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4707 << getParameterABISpelling(abi) << existingAttr;
4708 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4709 return;
4710 }
4711 }
4712
4713 switch (abi) {
4714 case ParameterABI::Ordinary:
4715 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4716
4717 case ParameterABI::SwiftContext:
4718 if (!isValidSwiftContextType(type)) {
4719 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4720 << getParameterABISpelling(abi)
4721 << /*pointer to pointer */ 0 << type;
4722 }
4723 D->addAttr(::new (Context)
4724 SwiftContextAttr(range, Context, spellingIndex));
4725 return;
4726
4727 case ParameterABI::SwiftErrorResult:
4728 if (!isValidSwiftErrorResultType(type)) {
4729 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4730 << getParameterABISpelling(abi)
4731 << /*pointer to pointer */ 1 << type;
4732 }
4733 D->addAttr(::new (Context)
4734 SwiftErrorResultAttr(range, Context, spellingIndex));
4735 return;
4736
4737 case ParameterABI::SwiftIndirectResult:
4738 if (!isValidSwiftIndirectResultType(type)) {
4739 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4740 << getParameterABISpelling(abi)
4741 << /*pointer*/ 0 << type;
4742 }
4743 D->addAttr(::new (Context)
4744 SwiftIndirectResultAttr(range, Context, spellingIndex));
4745 return;
4746 }
4747 llvm_unreachable("bad parameter ABI attribute");
4748}
4749
John McCall3882ace2011-01-05 12:14:39 +00004750/// Checks a regparm attribute, returning true if it is ill-formed and
4751/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004752bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004753 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004754 return true;
4755
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004756 if (!checkAttributeNumArgs(*this, AL, 1)) {
4757 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004758 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004759 }
Eli Friedman7044b762009-03-27 21:06:47 +00004760
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004761 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004762 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4763 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4764 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004765 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004766 }
4767
Douglas Gregore8bbc122011-09-02 00:18:52 +00004768 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004769 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004770 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004771 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004772 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004773 }
4774
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004775 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004776 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004777 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004778 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004779 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004780 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004781 }
4782
John McCall3882ace2011-01-05 12:14:39 +00004783 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004784}
4785
Artem Belevichbcec9da2016-06-06 22:54:57 +00004786// Checks whether an argument of launch_bounds attribute is
4787// acceptable, performs implicit conversion to Rvalue, and returns
4788// non-nullptr Expr result on success. Otherwise, it returns nullptr
4789// and may output an error.
4790static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004791 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004792 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004793 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004794 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004795
4796 // Accept template arguments for now as they depend on something else.
4797 // We'll get to check them when they eventually get instantiated.
4798 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004799 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004800
4801 llvm::APSInt I(64);
4802 if (!E->isIntegerConstantExpr(I, S.Context)) {
4803 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004804 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004805 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004806 }
4807 // Make sure we can fit it in 32 bits.
4808 if (!I.isIntN(32)) {
4809 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4810 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004811 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004812 }
4813 if (I < 0)
4814 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004815 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004816
Artem Belevichbcec9da2016-06-06 22:54:57 +00004817 // We may need to perform implicit conversion of the argument.
4818 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4819 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4820 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4821 assert(!ValArg.isInvalid() &&
4822 "Unexpected PerformCopyInitialization() failure.");
4823
4824 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004825}
4826
4827void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4828 Expr *MinBlocks, unsigned SpellingListIndex) {
4829 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4830 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004831 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4832 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004833 return;
4834
Artem Belevichbcec9da2016-06-06 22:54:57 +00004835 if (MinBlocks) {
4836 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4837 if (MinBlocks == nullptr)
4838 return;
4839 }
Artem Belevich7093e402015-04-21 22:55:54 +00004840
4841 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4842 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4843}
4844
Erich Keanee891aa92018-07-13 15:07:47 +00004845static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004846 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4847 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004848 return;
4849
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004850 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4851 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4852 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004853}
4854
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004855static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004856 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004857 if (!AL.isArgIdent(0)) {
4858 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004859 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004860 return;
4861 }
Joel E. Denny81508102018-03-13 14:51:22 +00004862
4863 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004864 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004865 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004866 return;
4867
Joel E. Denny81508102018-03-13 14:51:22 +00004868 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004869 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004870 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004871 return;
4872
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004873 bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004874 if (IsPointer) {
4875 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004876 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4877 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4878 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004879 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004880 }
4881
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004882 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
4883 AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx,
4884 TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004885}
4886
4887static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004888 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004889 if (!AL.isArgIdent(0)) {
4890 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004891 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004892 return;
4893 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004894
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004895 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004896 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004897
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004898 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004899 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004900 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004901 return;
4902 }
4903
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004904 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004905 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004906 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004907 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004908
Michael Han99315932013-01-24 16:46:58 +00004909 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004910 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004911 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004912 AL.getLayoutCompatible(),
4913 AL.getMustBeNull(),
4914 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004915}
4916
Erich Keanee891aa92018-07-13 15:07:47 +00004917static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004918 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004919
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004920 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004921 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004922 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004923 return;
4924
Joel E. Denny81508102018-03-13 14:51:22 +00004925 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
4926 D->addAttr(::new (S.Context) XRayLogArgsAttr(
4927 AL.getRange(), S.Context, ArgCount.getSourceIndex(),
4928 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004929}
4930
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004931//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004932// Checker-specific attribute handlers.
4933//===----------------------------------------------------------------------===//
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004934static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4935 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004936}
4937
George Karpenkov1657f362018-11-30 02:18:37 +00004938static bool isValidSubjectOfNSAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004939 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004940 QT->isObjCNSObjectType();
John McCalled433932011-01-25 03:31:58 +00004941}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004942
George Karpenkov1657f362018-11-30 02:18:37 +00004943static bool isValidSubjectOfCFAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004944 return QT->isDependentType() || QT->isPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004945 isValidSubjectOfNSAttribute(QT);
John McCalled433932011-01-25 03:31:58 +00004946}
4947
George Karpenkov1657f362018-11-30 02:18:37 +00004948static bool isValidSubjectOfOSAttribute(QualType QT) {
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004949 if (QT->isDependentType())
4950 return true;
4951 QualType PT = QT->getPointeeType();
4952 return !PT.isNull() && PT->getAsCXXRecordDecl() != nullptr;
John McCall3b5a8f52016-03-03 00:10:03 +00004953}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004954
George Karpenkov1657f362018-11-30 02:18:37 +00004955void Sema::AddXConsumedAttr(Decl *D, SourceRange SR, unsigned SpellingIndex,
4956 RetainOwnershipKind K,
4957 bool IsTemplateInstantiation) {
4958 ValueDecl *VD = cast<ValueDecl>(D);
4959 switch (K) {
4960 case RetainOwnershipKind::OS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004961 handleSimpleAttributeOrDiagnose<OSConsumedAttr>(
4962 *this, VD, SR, SpellingIndex, isValidSubjectOfOSAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00004963 diag::warn_ns_attribute_wrong_parameter_type,
4964 /*ExtraArgs=*/SR, "os_consumed", /*pointers*/ 1);
4965 return;
4966 case RetainOwnershipKind::NS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004967 handleSimpleAttributeOrDiagnose<NSConsumedAttr>(
4968 *this, VD, SR, SpellingIndex, isValidSubjectOfNSAttribute(VD->getType()),
John McCall3b5a8f52016-03-03 00:10:03 +00004969
George Karpenkov1657f362018-11-30 02:18:37 +00004970 // These attributes are normally just advisory, but in ARC, ns_consumed
4971 // is significant. Allow non-dependent code to contain inappropriate
4972 // attributes even in ARC, but require template instantiations to be
4973 // set up correctly.
4974 ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount)
4975 ? diag::err_ns_attribute_wrong_parameter_type
4976 : diag::warn_ns_attribute_wrong_parameter_type),
4977 /*ExtraArgs=*/SR, "ns_consumed", /*objc pointers*/ 0);
4978 return;
4979 case RetainOwnershipKind::CF:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004980 handleSimpleAttributeOrDiagnose<CFConsumedAttr>(
George Karpenkov1657f362018-11-30 02:18:37 +00004981 *this, VD, SR, SpellingIndex,
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004982 isValidSubjectOfCFAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00004983 diag::warn_ns_attribute_wrong_parameter_type,
4984 /*ExtraArgs=*/SR, "cf_consumed", /*pointers*/1);
John McCalled433932011-01-25 03:31:58 +00004985 return;
4986 }
George Karpenkov1657f362018-11-30 02:18:37 +00004987}
John McCalled433932011-01-25 03:31:58 +00004988
George Karpenkov1657f362018-11-30 02:18:37 +00004989static Sema::RetainOwnershipKind
4990parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) {
4991 switch (AL.getKind()) {
4992 case ParsedAttr::AT_CFConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004993 case ParsedAttr::AT_CFReturnsRetained:
4994 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004995 return Sema::RetainOwnershipKind::CF;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004996 case ParsedAttr::AT_OSConsumesThis:
George Karpenkov1657f362018-11-30 02:18:37 +00004997 case ParsedAttr::AT_OSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004998 case ParsedAttr::AT_OSReturnsRetained:
4999 case ParsedAttr::AT_OSReturnsNotRetained:
5000 case ParsedAttr::AT_OSReturnsRetainedOnZero:
5001 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
George Karpenkov1657f362018-11-30 02:18:37 +00005002 return Sema::RetainOwnershipKind::OS;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005003 case ParsedAttr::AT_NSConsumesSelf:
George Karpenkov1657f362018-11-30 02:18:37 +00005004 case ParsedAttr::AT_NSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005005 case ParsedAttr::AT_NSReturnsRetained:
5006 case ParsedAttr::AT_NSReturnsNotRetained:
5007 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005008 return Sema::RetainOwnershipKind::NS;
5009 default:
5010 llvm_unreachable("Wrong argument supplied");
5011 }
John McCalled433932011-01-25 03:31:58 +00005012}
5013
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005014bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
5015 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00005016 return false;
5017
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005018 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
5019 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00005020 return true;
5021}
5022
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005023/// \return whether the parameter is a pointer to OSObject pointer.
5024static bool isValidOSObjectOutParameter(const Decl *D) {
5025 const auto *PVD = dyn_cast<ParmVarDecl>(D);
5026 if (!PVD)
5027 return false;
5028 QualType QT = PVD->getType();
5029 QualType PT = QT->getPointeeType();
5030 return !PT.isNull() && isValidSubjectOfOSAttribute(PT);
5031}
5032
George Karpenkov1657f362018-11-30 02:18:37 +00005033static void handleXReturnsXRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005034 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005035 QualType ReturnType;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005036 Sema::RetainOwnershipKind K = parsedAttrToRetainOwnershipKind(AL);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005037
George Karpenkov1657f362018-11-30 02:18:37 +00005038 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005039 ReturnType = MD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005040 } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
5041 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) {
John McCall31168b02011-06-15 23:02:42 +00005042 return; // ignore: was handled as a type attribute
George Karpenkov1657f362018-11-30 02:18:37 +00005043 } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005044 ReturnType = PD->getType();
George Karpenkov1657f362018-11-30 02:18:37 +00005045 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005046 ReturnType = FD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005047 } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
5048 // Attributes on parameters are used for out-parameters,
5049 // passed as pointers-to-pointers.
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005050 unsigned DiagID = K == Sema::RetainOwnershipKind::CF
5051 ? /*pointer-to-CF-pointer*/2
5052 : /*pointer-to-OSObject-pointer*/3;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005053 ReturnType = Param->getType()->getPointeeType();
5054 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005055 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005056 << AL << DiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005057 return;
5058 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005059 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00005060 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005061 } else {
5062 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005063 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005064 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005065 case ParsedAttr::AT_NSReturnsRetained:
5066 case ParsedAttr::AT_NSReturnsAutoreleased:
5067 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005068 ExpectedDeclKind = ExpectedFunctionOrMethod;
5069 break;
5070
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005071 case ParsedAttr::AT_OSReturnsRetained:
5072 case ParsedAttr::AT_OSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00005073 case ParsedAttr::AT_CFReturnsRetained:
5074 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005075 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
5076 break;
5077 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005078 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005079 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005080 return;
5081 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005082
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005083 bool TypeOK;
5084 bool Cf;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005085 unsigned ParmDiagID = 2; // Pointer-to-CF-pointer
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005086 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00005087 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005088 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005089 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
5090 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00005091 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005092
5093 case ParsedAttr::AT_NSReturnsAutoreleased:
5094 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005095 TypeOK = isValidSubjectOfNSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005096 Cf = false;
John McCalled433932011-01-25 03:31:58 +00005097 break;
5098
Erich Keanee891aa92018-07-13 15:07:47 +00005099 case ParsedAttr::AT_CFReturnsRetained:
5100 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005101 TypeOK = isValidSubjectOfCFAttribute(ReturnType);
5102 Cf = true;
5103 break;
5104
5105 case ParsedAttr::AT_OSReturnsRetained:
5106 case ParsedAttr::AT_OSReturnsNotRetained:
5107 TypeOK = isValidSubjectOfOSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005108 Cf = true;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005109 ParmDiagID = 3; // Pointer-to-OSObject-pointer
John McCalled433932011-01-25 03:31:58 +00005110 break;
5111 }
5112
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005113 if (!TypeOK) {
5114 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00005115 return;
5116
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005117 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005118 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005119 << AL << ParmDiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005120 } else {
5121 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
5122 enum : unsigned {
5123 Function,
5124 Method,
5125 Property
5126 } SubjectKind = Function;
5127 if (isa<ObjCMethodDecl>(D))
5128 SubjectKind = Method;
5129 else if (isa<ObjCPropertyDecl>(D))
5130 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005131 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005132 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005133 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005134 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00005135 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005136
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005137 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005138 default:
David Blaikie83d382b2011-09-23 05:06:16 +00005139 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005140 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005141 handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL);
John McCalled433932011-01-25 03:31:58 +00005142 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005143 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005144 handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005145 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005146 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005147 handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005148 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005149 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005150 handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005151 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005152 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005153 handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL);
5154 return;
5155 case ParsedAttr::AT_OSReturnsRetained:
5156 handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL);
5157 return;
5158 case ParsedAttr::AT_OSReturnsNotRetained:
5159 handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005160 return;
5161 };
5162}
5163
John McCallcf166702011-07-22 08:53:00 +00005164static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005165 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00005166 const int EP_ObjCMethod = 1;
5167 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00005168
Erich Keaneb11ebc52017-09-27 03:20:13 +00005169 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005170 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005171 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00005172 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005173 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005174 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00005175
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00005176 if (!resultType->isReferenceType() &&
5177 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005178 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005179 << SourceRange(loc) << Attrs
5180 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
5181 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00005182
5183 // Drop the attribute.
5184 return;
5185 }
5186
Nico Weber462fd1e2015-01-07 23:50:05 +00005187 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00005188 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00005189}
5190
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005191static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005192 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005193 const auto *Method = cast<ObjCMethodDecl>(D);
5194
5195 const DeclContext *DC = Method->getDeclContext();
5196 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005197 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005198 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005199 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
5200 return;
5201 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005202 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005203 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005204 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005205 return;
5206 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005207
5208 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
5209 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005210}
5211
Erich Keanee891aa92018-07-13 15:07:47 +00005212static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005213 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00005214
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005215 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005216 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005217 return;
5218 }
John McCall28592582015-02-01 22:34:06 +00005219
5220 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005221 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00005222 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005223 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00005224 return;
5225 }
5226
5227 // Only allow 'cv void *'.
5228 QualType T = TD->getUnderlyingType();
5229 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005230 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00005231 return;
5232 }
5233 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005234
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005235 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005236 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
5237 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005238}
5239
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005240static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005241 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005242 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00005243
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005244 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005245 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005246 return;
5247 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005248
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005249 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005250 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
5251 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005252}
5253
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005254static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005255 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005256 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005257 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005258 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005259 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005260 return;
5261 }
5262 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005263 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005264 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005265 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005266 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005267 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005268 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005269 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005270}
5271
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005272static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005273 const ParsedAttr &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005274 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005275 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005276 IFace = CatDecl->getClassInterface();
5277 else
5278 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005279
5280 if (!IFace)
5281 return;
5282
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00005283 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00005284 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005285 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
5286 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005287}
5288
Erich Keanee891aa92018-07-13 15:07:47 +00005289static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005290 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005291 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005292 return;
5293 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005294 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005295 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005296 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005297}
5298
Nico Webera6916892016-06-10 18:53:04 +00005299// When a user wants to use objc_boxable with a union or struct
5300// but they don't have access to the declaration (legacy/third-party code)
5301// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00005302// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00005303static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00005304 bool notify = false;
5305
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005306 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00005307 if (RD && RD->getDefinition()) {
5308 RD = RD->getDefinition();
5309 notify = true;
5310 }
5311
5312 if (RD) {
5313 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005314 ObjCBoxableAttr(AL.getRange(), S.Context,
5315 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00005316 RD->addAttr(BoxableAttr);
5317 if (notify) {
5318 // we need to notify ASTReader/ASTWriter about
5319 // modification of existing declaration
5320 if (ASTMutationListener *L = S.getASTMutationListener())
5321 L->AddedAttributeToRecord(BoxableAttr, RD);
5322 }
5323 }
5324}
5325
Erich Keanee891aa92018-07-13 15:07:47 +00005326static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005327 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005328
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005329 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005330 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005331}
5332
Chandler Carruthedc2c642011-07-02 00:01:44 +00005333static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005334 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005335 const auto *VD = cast<ValueDecl>(D);
5336 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005337
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005338 if (!QT->isDependentType() &&
5339 !QT->isObjCLifetimeType()) {
5340 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5341 << QT;
John McCall31168b02011-06-15 23:02:42 +00005342 return;
5343 }
5344
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005345 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005346
5347 // If we have no lifetime yet, check the lifetime we're presumably
5348 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005349 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5350 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005351
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005352 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005353 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005354 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005355 "didn't infer lifetime for non-dependent type?");
5356 break;
5357
5358 case Qualifiers::OCL_Weak: // meaningful
5359 case Qualifiers::OCL_Strong: // meaningful
5360 break;
5361
5362 case Qualifiers::OCL_ExplicitNone:
5363 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005364 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5365 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005366 break;
5367 }
5368
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005369 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005370 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
5371 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00005372}
5373
Francois Picheta83957a2010-12-19 06:50:37 +00005374//===----------------------------------------------------------------------===//
5375// Microsoft specific attribute handlers.
5376//===----------------------------------------------------------------------===//
5377
Nico Weber88f5ed92016-09-13 18:55:26 +00005378UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
5379 unsigned AttrSpellingListIndex, StringRef Uuid) {
5380 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005381 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005382 return nullptr;
5383 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5384 Diag(Range.getBegin(), diag::note_previous_uuid);
5385 D->dropAttr<UuidAttr>();
5386 }
5387
5388 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5389}
5390
Erich Keanee891aa92018-07-13 15:07:47 +00005391static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005392 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005393 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005394 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005395 return;
5396 }
5397
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005398 StringRef StrRef;
5399 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005400 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005401 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005402
David Majnemer89085342013-08-09 08:56:20 +00005403 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5404 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005405 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5406 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005407
Reid Kleckner140c4a72013-05-17 14:04:52 +00005408 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005409 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005410 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005411 return;
5412 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005413
David Majnemer89085342013-08-09 08:56:20 +00005414 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005415 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005416 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005417 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005418 return;
5419 }
David Majnemer89085342013-08-09 08:56:20 +00005420 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005421 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005422 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005423 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005424 }
Francois Picheta83957a2010-12-19 06:50:37 +00005425
Nico Weber469891e2017-05-05 17:05:56 +00005426 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5427 // the only thing in the [] list, the [] too), and add an insertion of
5428 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5429 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005430 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005431 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005432 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5433 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005434
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005435 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5436 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005437 if (UA)
5438 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005439}
5440
Erich Keanee891aa92018-07-13 15:07:47 +00005441static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005442 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005443 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005444 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005445 return;
5446 }
5447 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005448 D, AL.getRange(), /*BestCase=*/true,
5449 AL.getAttributeSpellingListIndex(),
5450 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005451 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005452 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005453 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5454 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005455}
5456
Erich Keanee891aa92018-07-13 15:07:47 +00005457static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005458 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005459 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005460 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005461 return;
5462 }
5463 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005464 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005465 return;
5466 }
5467 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005468 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005469 return;
5470 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005471 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5472 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005473}
5474
Erich Keanee891aa92018-07-13 15:07:47 +00005475static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005476 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005477 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005478 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005479 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005480 return;
5481 Tags.push_back(Tag);
5482 }
5483
5484 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5485 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005486 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005487 return;
5488 }
5489 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005490 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005491 return;
5492 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005493 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005494 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005495 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005496 return;
5497
5498 // Store tags sorted and without duplicates.
Fangrui Song55fab262018-09-26 22:16:28 +00005499 llvm::sort(Tags);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005500 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5501
5502 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005503 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5504 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005505}
5506
Erich Keanee891aa92018-07-13 15:07:47 +00005507static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005508 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005509 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005510 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005511 return;
5512 }
5513
5514 StringRef Str;
5515 SourceLocation ArgLoc;
5516
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005517 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005518 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005519 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005520 return;
5521
5522 ARMInterruptAttr::InterruptType Kind;
5523 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005524 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5525 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005526 return;
5527 }
5528
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005529 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005530 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005531 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005532}
5533
Erich Keanee891aa92018-07-13 15:07:47 +00005534static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005535 // MSP430 'interrupt' attribute is applied to
5536 // a function with no parameters and void return type.
5537 if (!isFunctionOrMethod(D)) {
5538 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5539 << "'interrupt'" << ExpectedFunctionOrMethod;
5540 return;
5541 }
5542
5543 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5544 S.Diag(D->getLocation(), diag::warn_msp430_interrupt_attribute)
5545 << 0;
5546 return;
5547 }
5548
5549 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5550 S.Diag(D->getLocation(), diag::warn_msp430_interrupt_attribute)
5551 << 1;
5552 return;
5553 }
5554
5555 // The attribute takes one integer argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005556 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005557 return;
5558
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005559 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005560 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5561 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005562 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005563 }
5564
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005565 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005566 llvm::APSInt NumParams(32);
5567 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005568 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005569 << AL << AANT_ArgumentIntegerConstant
5570 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005571 return;
5572 }
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005573 // The argument should be in range 0..63.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005574 unsigned Num = NumParams.getLimitedValue(255);
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005575 if (Num > 63) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005576 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005577 << AL << (int)NumParams.getSExtValue()
5578 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005579 return;
5580 }
5581
Aaron Ballman36a53502014-01-16 13:03:14 +00005582 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005583 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5584 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005585 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005586}
5587
Erich Keanee891aa92018-07-13 15:07:47 +00005588static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005589 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005590 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005591 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005592 return;
5593 }
5594
5595 StringRef Str;
5596 SourceLocation ArgLoc;
5597
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005598 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005599 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005600 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005601 return;
5602
5603 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5604 // a) Must be a function.
5605 // b) Must have no parameters.
5606 // c) Must have the 'void' return type.
5607 // d) Cannot have the 'mips16' attribute, as that instruction set
5608 // lacks the 'eret' instruction.
5609 // e) The attribute itself must either have no argument or one of the
5610 // valid interrupt types, see [MipsInterruptDocs].
5611
5612 if (!isFunctionOrMethod(D)) {
5613 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5614 << "'interrupt'" << ExpectedFunctionOrMethod;
5615 return;
5616 }
5617
5618 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5619 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5620 << 0;
5621 return;
5622 }
5623
5624 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5625 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5626 << 1;
5627 return;
5628 }
5629
Erich Keane44bacdf2018-08-09 13:21:32 +00005630 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005631 return;
5632
5633 MipsInterruptAttr::InterruptType Kind;
5634 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005635 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005636 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005637 return;
5638 }
5639
5640 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005641 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005642}
5643
Erich Keanee891aa92018-07-13 15:07:47 +00005644static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005645 // Semantic checks for a function with the 'interrupt' attribute.
5646 // a) Must be a function.
5647 // b) Must have the 'void' return type.
5648 // c) Must take 1 or 2 arguments.
5649 // d) The 1st argument must be a pointer.
5650 // e) The 2nd argument (if any) must be an unsigned integer.
5651 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5652 CXXMethodDecl::isStaticOverloadedOperator(
5653 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005654 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005655 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005656 return;
5657 }
5658 // Interrupt handler must have void return type.
5659 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5660 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5661 diag::err_anyx86_interrupt_attribute)
5662 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5663 ? 0
5664 : 1)
5665 << 0;
5666 return;
5667 }
5668 // Interrupt handler must have 1 or 2 parameters.
5669 unsigned NumParams = getFunctionOrMethodNumParams(D);
5670 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005671 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005672 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5673 ? 0
5674 : 1)
5675 << 1;
5676 return;
5677 }
5678 // The first argument must be a pointer.
5679 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5680 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5681 diag::err_anyx86_interrupt_attribute)
5682 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5683 ? 0
5684 : 1)
5685 << 2;
5686 return;
5687 }
5688 // The second argument, if present, must be an unsigned integer.
5689 unsigned TypeSize =
5690 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5691 ? 64
5692 : 32;
5693 if (NumParams == 2 &&
5694 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5695 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5696 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5697 diag::err_anyx86_interrupt_attribute)
5698 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5699 ? 0
5700 : 1)
5701 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5702 return;
5703 }
5704 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005705 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005706 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5707}
5708
Erich Keanee891aa92018-07-13 15:07:47 +00005709static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005710 if (!isFunctionOrMethod(D)) {
5711 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5712 << "'interrupt'" << ExpectedFunction;
5713 return;
5714 }
5715
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005716 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005717 return;
5718
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005719 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005720}
5721
Erich Keanee891aa92018-07-13 15:07:47 +00005722static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005723 if (!isFunctionOrMethod(D)) {
5724 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5725 << "'signal'" << ExpectedFunction;
5726 return;
5727 }
5728
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005729 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005730 return;
5731
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005732 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005733}
5734
Ana Pazos1eee1b72018-07-26 17:37:45 +00005735
5736static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5737 const ParsedAttr &AL) {
5738 // Warn about repeated attributes.
5739 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5740 S.Diag(AL.getRange().getBegin(),
5741 diag::warn_riscv_repeated_interrupt_attribute);
5742 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5743 return;
5744 }
5745
5746 // Check the attribute argument. Argument is optional.
5747 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5748 return;
5749
5750 StringRef Str;
5751 SourceLocation ArgLoc;
5752
5753 // 'machine'is the default interrupt mode.
5754 if (AL.getNumArgs() == 0)
5755 Str = "machine";
5756 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5757 return;
5758
5759 // Semantic checks for a function with the 'interrupt' attribute:
5760 // - Must be a function.
5761 // - Must have no parameters.
5762 // - Must have the 'void' return type.
5763 // - The attribute itself must either have no argument or one of the
5764 // valid interrupt types, see [RISCVInterruptDocs].
5765
5766 if (D->getFunctionType() == nullptr) {
5767 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5768 << "'interrupt'" << ExpectedFunction;
5769 return;
5770 }
5771
5772 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5773 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 0;
5774 return;
5775 }
5776
5777 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5778 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 1;
5779 return;
5780 }
5781
5782 RISCVInterruptAttr::InterruptType Kind;
5783 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005784 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5785 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005786 return;
5787 }
5788
5789 D->addAttr(::new (S.Context) RISCVInterruptAttr(
5790 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
5791}
5792
Erich Keanee891aa92018-07-13 15:07:47 +00005793static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005794 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005795 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5796 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005797 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005798 break;
5799 case llvm::Triple::mipsel:
5800 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005801 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005802 break;
5803 case llvm::Triple::x86:
5804 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005805 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005806 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005807 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005808 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005809 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005810 case llvm::Triple::riscv32:
5811 case llvm::Triple::riscv64:
5812 handleRISCVInterruptAttr(S, D, AL);
5813 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005814 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005815 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005816 break;
5817 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005818}
5819
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005820static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005821 const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005822 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005823 Expr *MinExpr = AL.getArgAsExpr(0);
5824 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005825 return;
5826
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005827 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005828 Expr *MaxExpr = AL.getArgAsExpr(1);
5829 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005830 return;
5831
5832 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005833 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005834 return;
5835 }
5836 if (Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005837 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005838 return;
5839 }
5840
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005841 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005842 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5843 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005844}
5845
Erich Keanee891aa92018-07-13 15:07:47 +00005846static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005847 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005848 Expr *MinExpr = AL.getArgAsExpr(0);
5849 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005850 return;
5851
5852 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005853 if (AL.getNumArgs() == 2) {
5854 Expr *MaxExpr = AL.getArgAsExpr(1);
5855 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005856 return;
5857 }
5858
5859 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005860 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005861 return;
5862 }
5863 if (Max != 0 && Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005864 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005865 return;
5866 }
5867
5868 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005869 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5870 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005871}
5872
Erich Keanee891aa92018-07-13 15:07:47 +00005873static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005874 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005875 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5876 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005877 return;
5878
5879 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005880 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5881 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005882}
5883
Erich Keanee891aa92018-07-13 15:07:47 +00005884static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005885 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005886 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5887 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005888 return;
5889
5890 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005891 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5892 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005893}
5894
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005895static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005896 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005897 // If we try to apply it to a function pointer, don't warn, but don't
5898 // do anything, either. It doesn't matter anyway, because there's nothing
5899 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005900 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005901 if (VD && VD->getType()->isFunctionPointerType())
5902 return;
5903 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005904 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005905 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5906 TD->getUnderlyingType()->isFunctionType()))
5907 return;
5908 // Attribute can only be applied to function types.
5909 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005910 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005911 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005912 return;
5913 }
5914
Aaron Ballman36a53502014-01-16 13:03:14 +00005915 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005916 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5917 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005918}
5919
Erich Keanee891aa92018-07-13 15:07:47 +00005920static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005921 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005922 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5923 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005924 return;
5925
5926 // TODO: Investigate what happens with the next major version of MSVC.
Reid Kleckner1a94d872018-12-17 23:16:43 +00005927 if (Version != LangOptions::MSVC2015 / 100) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005928 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005929 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005930 return;
5931 }
5932
Reid Kleckner1a94d872018-12-17 23:16:43 +00005933 // The attribute expects a "major" version number like 19, but new versions of
5934 // MSVC have moved to updating the "minor", or less significant numbers, so we
5935 // have to multiply by 100 now.
5936 Version *= 100;
5937
David Majnemercd3ebfe2016-05-23 17:16:12 +00005938 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005939 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5940 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005941}
5942
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005943DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5944 unsigned AttrSpellingListIndex) {
5945 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005946 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005947 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005948 }
5949
5950 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005951 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005952
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005953 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005954}
5955
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005956DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5957 unsigned AttrSpellingListIndex) {
5958 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005959 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005960 D->dropAttr<DLLImportAttr>();
5961 }
5962
5963 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005964 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005965
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005966 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005967}
5968
Erich Keanee891aa92018-07-13 15:07:47 +00005969static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005970 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5971 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005972 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00005973 return;
5974 }
5975
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005976 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00005977 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005978 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5979 // MinGW doesn't allow dllimport on inline functions.
5980 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00005981 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005982 return;
5983 }
5984 }
5985
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005986 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005987 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5988 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005989 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00005990 return;
5991 }
5992 }
5993
Hans Wennborge82f19c2014-06-24 23:57:05 +00005994 unsigned Index = A.getAttributeSpellingListIndex();
Erich Keanee891aa92018-07-13 15:07:47 +00005995 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Hans Wennborge82f19c2014-06-24 23:57:05 +00005996 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5997 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005998 if (NewAttr)
5999 D->addAttr(NewAttr);
6000}
6001
David Majnemer2c4e00a2014-01-29 22:07:36 +00006002MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00006003Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00006004 unsigned AttrSpellingListIndex,
6005 MSInheritanceAttr::Spelling SemanticSpelling) {
6006 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
6007 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00006008 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006009 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
6010 << 1 /*previous declaration*/;
6011 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
6012 D->dropAttr<MSInheritanceAttr>();
6013 }
6014
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006015 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006016 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00006017 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
6018 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006019 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006020 }
6021 } else {
6022 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
6023 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
6024 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006025 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006026 }
6027 if (RD->getDescribedClassTemplate()) {
6028 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
6029 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006030 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006031 }
6032 }
6033
6034 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00006035 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006036}
6037
Erich Keanee891aa92018-07-13 15:07:47 +00006038static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006039 // The capability attributes take a single string parameter for the name of
6040 // the capability they represent. The lockable attribute does not take any
6041 // parameters. However, semantically, both attributes represent the same
6042 // concept, and so they use the same semantic attribute. Eventually, the
6043 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00006044 //
Alp Toker958027b2014-07-14 19:42:55 +00006045 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00006046 // literal will be considered a "mutex."
6047 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006048 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00006049 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006050 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006051 return;
6052
Aaron Ballman6c810072014-03-05 21:47:13 +00006053 // Currently, there are only two names allowed for a capability: role and
6054 // mutex (case insensitive). Diagnose other capability names.
6055 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
6056 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
6057
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006058 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
6059 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006060}
6061
Erich Keanee891aa92018-07-13 15:07:47 +00006062static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00006063 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006064 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00006065 return;
6066
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006067 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00006068 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006069 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006070}
6071
6072static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006073 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006074 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006075 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006076 return;
6077
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006078 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006079 S.Context,
6080 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006081 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006082}
6083
6084static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006085 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006086 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006087 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006088 return;
6089
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006090 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006091 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006092 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006093 Args.data(),
6094 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006095 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006096}
6097
6098static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006099 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006100 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00006101 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006102 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006103
Aaron Ballman18d85ae2014-03-20 16:02:49 +00006104 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006105 AL.getRange(), S.Context, Args.data(), Args.size(),
6106 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006107}
6108
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006109static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006110 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006111 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006112 return;
6113
6114 // check that all arguments are lockable objects
6115 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006116 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006117 if (Args.empty())
6118 return;
6119
6120 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006121 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
6122 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006123
6124 D->addAttr(RCA);
6125}
6126
Erich Keanee891aa92018-07-13 15:07:47 +00006127static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006128 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00006129 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006130 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00006131 // Do not want to attach the attribute to the namespace because that will
6132 // cause confusing diagnostic reports for uses of declarations within the
6133 // namespace.
6134 return;
6135 }
6136 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00006137
Manman Renc7890fe2016-03-16 18:50:49 +00006138 // Handle the cases where the attribute has a text message.
6139 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006140 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
6141 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00006142 return;
6143
6144 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006145 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
6146 checkAttributeAtMostNumArgs(S, AL, 1);
6147 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
Sander de Smalen44a22532018-11-26 16:38:37 +00006148 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
6149 return;
6150
6151 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
6152 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
6153
6154 D->addAttr(::new (S.Context)
6155 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006156 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006157}
6158
6159static bool isGlobalVar(const Decl *D) {
6160 if (const auto *S = dyn_cast<VarDecl>(D))
6161 return S->hasGlobalStorage();
6162 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00006163}
6164
Erich Keanee891aa92018-07-13 15:07:47 +00006165static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006166 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00006167 return;
6168
Benjamin Kramer1b582012016-02-13 18:11:49 +00006169 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00006170
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006171 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00006172 StringRef SanitizerName;
6173 SourceLocation LiteralLoc;
6174
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006175 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00006176 return;
6177
6178 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
6179 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006180 else if (isGlobalVar(D) && SanitizerName != "address")
6181 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006182 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00006183 Sanitizers.push_back(SanitizerName);
6184 }
6185
6186 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006187 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
6188 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00006189}
6190
6191static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006192 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006193 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00006194 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006195 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
6196 .Case("no_address_safety_analysis", "address")
6197 .Case("no_sanitize_address", "address")
6198 .Case("no_sanitize_thread", "thread")
6199 .Case("no_sanitize_memory", "memory");
6200 if (isGlobalVar(D) && SanitizerName != "address")
6201 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006202 << AL << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00006203 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006204 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
6205 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00006206}
6207
Erich Keanee891aa92018-07-13 15:07:47 +00006208static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006209 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006210 D->addAttr(Internal);
6211}
6212
Erich Keanee891aa92018-07-13 15:07:47 +00006213static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006214 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006215 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00006216 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006217 else
Erich Keane44bacdf2018-08-09 13:21:32 +00006218 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
6219 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006220}
6221
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006222/// Handles semantic checking for features that are common to all attributes,
6223/// such as checking whether a parameter was properly specified, or the correct
6224/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006225static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006226 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006227 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00006228 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006229 //
6230 // We also bail on unknown and ignored attributes because those are handled
6231 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00006232 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006233 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006234 // Check whether the attribute requires specific language extensions to be
6235 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006236 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00006237 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00006238 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006239 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00006240 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006241 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00006242 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006243
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006244 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006245 // If there are no optional arguments, then checking for the argument count
6246 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006247 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006248 return true;
6249 } else {
6250 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006251 if (AL.getMinArgs() &&
6252 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006253 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006254 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
6255 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006256 return true;
6257 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00006258
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006259 if (S.CheckAttrTarget(AL))
6260 return true;
6261
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006262 return false;
6263}
6264
Erich Keanee891aa92018-07-13 15:07:47 +00006265static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006266 if (D->isInvalidDecl())
6267 return;
6268
6269 // Check if there is only one access qualifier.
6270 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006271 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
6272 AL.getSemanticSpelling()) {
6273 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
6274 << AL.getName()->getName() << AL.getRange();
6275 } else {
6276 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
6277 << D->getSourceRange();
6278 D->setInvalidDecl(true);
6279 return;
6280 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00006281 }
6282
6283 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
6284 // image object can be read and written.
6285 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
6286 // object. Using the read_write (or __read_write) qualifier with the pipe
6287 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006288 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006289 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006290 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006291 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006292 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00006293 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00006294 D->setInvalidDecl(true);
6295 return;
6296 }
6297 }
6298 }
6299
6300 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006301 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00006302}
6303
Erik Pilkington5a559e62018-08-21 17:24:06 +00006304static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006305 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00006306 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
6307 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
6308 return;
6309 }
6310
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006311 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00006312 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006313 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00006314 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00006315}
6316
JF Bastien14daa202018-12-18 05:12:21 +00006317static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6318 assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic &&
6319 "uninitialized is only valid on automatic duration variables");
6320 unsigned Index = AL.getAttributeSpellingListIndex();
6321 D->addAttr(::new (S.Context)
6322 UninitializedAttr(AL.getLoc(), S.Context, Index));
6323}
6324
Erik Pilkington1e368822019-01-04 18:33:06 +00006325static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD,
6326 bool DiagnoseFailure) {
6327 QualType Ty = VD->getType();
6328 if (!Ty->isObjCRetainableType()) {
6329 if (DiagnoseFailure) {
6330 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6331 << 0;
6332 }
6333 return false;
6334 }
6335
6336 Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime();
6337
6338 // Sema::inferObjCARCLifetime must run after processing decl attributes
6339 // (because __block lowers to an attribute), so if the lifetime hasn't been
6340 // explicitly specified, infer it locally now.
6341 if (LifetimeQual == Qualifiers::OCL_None)
6342 LifetimeQual = Ty->getObjCARCImplicitLifetime();
6343
6344 // The attributes only really makes sense for __strong variables; ignore any
6345 // attempts to annotate a parameter with any other lifetime qualifier.
6346 if (LifetimeQual != Qualifiers::OCL_Strong) {
6347 if (DiagnoseFailure) {
6348 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6349 << 1;
6350 }
6351 return false;
6352 }
6353
6354 // Tampering with the type of a VarDecl here is a bit of a hack, but we need
6355 // to ensure that the variable is 'const' so that we can error on
6356 // modification, which can otherwise over-release.
6357 VD->setType(Ty.withConst());
6358 VD->setARCPseudoStrong(true);
6359 return true;
6360}
6361
6362static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D,
6363 const ParsedAttr &AL) {
6364 if (auto *VD = dyn_cast<VarDecl>(D)) {
6365 assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically");
6366 if (!VD->hasLocalStorage()) {
6367 S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6368 << 0;
6369 return;
6370 }
6371
6372 if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true))
6373 return;
6374
6375 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6376 return;
6377 }
6378
6379 // If D is a function-like declaration (method, block, or function), then we
6380 // make every parameter psuedo-strong.
6381 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); I != E; ++I) {
6382 auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I));
6383 QualType Ty = PVD->getType();
6384
6385 // If a user wrote a parameter with __strong explicitly, then assume they
6386 // want "real" strong semantics for that parameter. This works because if
6387 // the parameter was written with __strong, then the strong qualifier will
6388 // be non-local.
6389 if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() ==
6390 Qualifiers::OCL_Strong)
6391 continue;
6392
6393 tryMakeVariablePseudoStrong(S, PVD, /*DiagnoseFailure=*/false);
6394 }
6395 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6396}
6397
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00006398//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006399// Top Level Sema Entry Points
6400//===----------------------------------------------------------------------===//
6401
Richard Smithf8a75c32013-08-29 00:47:48 +00006402/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
6403/// the attribute applies to decls. If the attribute is a type attribute, just
6404/// silently ignore it if a GNU attribute.
6405static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006406 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00006407 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00006408 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00006409 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00006410
Richard Smithf8a75c32013-08-29 00:47:48 +00006411 // Ignore C++11 attributes on declarator chunks: they appertain to the type
6412 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006413 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00006414 return;
6415
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006416 // Unknown attributes are automatically warned on. Target-specific attributes
6417 // which do not apply to the current target architecture are treated as
6418 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006419 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006420 !AL.existsInTarget(S.Context.getTargetInfo())) {
Simon Pilgrimfc0ff612018-12-17 12:17:37 +00006421 S.Diag(AL.getLoc(),
6422 AL.isDeclspecAttribute()
6423 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
6424 : (unsigned)diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006425 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006426 return;
6427 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006428
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006429 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006430 return;
6431
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006432 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006433 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006434 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00006435 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006436 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00006437 break;
6438 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006439 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00006440 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006441 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006442 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006443 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006444 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006445 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006446 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006447 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006448 case ParsedAttr::AT_DLLExport:
6449 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006450 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006451 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006452 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006453 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006454 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006455 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006456 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006457 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006458 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006459 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006460 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006461 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006462 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006463 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006464 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006465 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006466 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006467 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006468 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006469 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006470 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006471 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006472 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006473 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006474 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006475 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006476 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006477 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006478 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006479 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006480 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006481 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006482 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006483 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006484 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006485 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006486 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006487 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006488 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006489 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006490 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006491 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006492 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006493 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006494 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006495 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006496 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006497 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006498 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006499 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006500 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006501 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006502 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006503 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006504 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006505 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006506 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006507 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006508 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006509 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006510 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006511 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006512 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006513 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006514 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006515 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006516 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006517 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006518 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006519 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006520 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006521 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006522 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006523 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006524 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006525 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006526 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006527 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006528 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006529 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006530 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006531 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006532 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006533 case ParsedAttr::AT_CPUDispatch:
6534 case ParsedAttr::AT_CPUSpecific:
6535 handleCPUSpecificAttr(S, D, AL);
6536 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006537 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006538 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006539 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006540 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006541 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006542 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006543 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006544 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006545 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006546 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006547 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006548 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006549 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006550 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006551 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006552 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006553 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006554 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006555 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006556 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006557 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006558 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006559 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006560 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006561 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006562 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006563 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006564 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006565 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006566 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006567 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006568 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006569 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006570 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006571 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006572 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006573 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006574 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006575 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006576 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006577 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006578 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006579 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006580 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006581 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006582 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006583 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006584 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006585 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006586 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006587 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006588 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006589 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006590 break;
Johannes Doerfertac991bb2019-01-19 05:36:54 +00006591 case ParsedAttr::AT_Callback:
6592 handleCallbackAttr(S, D, AL);
6593 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006594 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006595 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006596 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006597 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006598 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006599 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006600 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006601 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006602 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006603 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006604 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006605 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006606 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006607 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006608 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006609 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006610 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006611 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006612 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006613 case ParsedAttr::AT_LifetimeBound:
6614 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6615 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006616 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006617 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006618 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006619 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006620 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006621 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006622 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006623 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006624 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006625 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006626 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006627 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006628 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006629 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006630 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006631 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006632 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6633 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006634 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006635 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006636 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006637 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006638 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006639 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006640 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006641 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006642 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006643 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006644 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006645 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006646 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006647 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006648 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006649 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006650 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006651 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006652 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006653 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006654 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006655 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006656 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006657 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006658 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006659 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006660 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006661 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006662 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006663 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006664 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006665 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006666 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006667 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006668 handleNoCfCheckAttr(S, D, AL);
6669 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006670 case ParsedAttr::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006671 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006672 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006673 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006674 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006675 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006676 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006677 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006678 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006679 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006680 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006681 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006682 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006683 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006684 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006685 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006686 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006687 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006688 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006689 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006690 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006691 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006692 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006693 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006694 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006695 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006696 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006697 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006698 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006699 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006700 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006701 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006702 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006703 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006704 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006705 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006706 case ParsedAttr::AT_ObjCRuntimeVisible:
6707 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6708 break;
6709 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006710 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006711 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006712 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006713 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6714 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006715 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006716 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006717 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6718 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006719 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006720 case ParsedAttr::AT_CFConsumed:
6721 case ParsedAttr::AT_NSConsumed:
George Karpenkov1657f362018-11-30 02:18:37 +00006722 case ParsedAttr::AT_OSConsumed:
6723 S.AddXConsumedAttr(D, AL.getRange(), AL.getAttributeSpellingListIndex(),
6724 parsedAttrToRetainOwnershipKind(AL),
6725 /*IsTemplateInstantiation=*/false);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006726 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006727 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006728 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006729 break;
George Karpenkovda2c77f2018-12-06 22:06:59 +00006730 case ParsedAttr::AT_OSConsumesThis:
6731 handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL);
6732 break;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00006733 case ParsedAttr::AT_OSReturnsRetainedOnZero:
6734 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnZeroAttr>(
6735 S, D, AL, isValidOSObjectOutParameter(D),
6736 diag::warn_ns_attribute_wrong_parameter_type,
6737 /*Extra Args=*/AL, /*pointer-to-OSObject-pointer*/ 3, AL.getRange());
6738 break;
6739 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
6740 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnNonZeroAttr>(
6741 S, D, AL, isValidOSObjectOutParameter(D),
6742 diag::warn_ns_attribute_wrong_parameter_type,
6743 /*Extra Args=*/AL, /*pointer-to-OSObject-poointer*/ 3, AL.getRange());
6744 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006745 case ParsedAttr::AT_NSReturnsAutoreleased:
6746 case ParsedAttr::AT_NSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006747 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006748 case ParsedAttr::AT_CFReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006749 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006750 case ParsedAttr::AT_OSReturnsNotRetained:
6751 case ParsedAttr::AT_OSReturnsRetained:
6752 handleXReturnsXRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006753 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006754 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006755 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006756 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006757 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006758 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006759 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006760 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006761 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006762 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006763 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006764 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006765 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006766 case ParsedAttr::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006767 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006768 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006769 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006770 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006771 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006772 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006773 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006774 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006775 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006776 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006777 break;
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006778 case ParsedAttr::AT_SpeculativeLoadHardening:
Zola Bridges826ef592019-01-18 17:20:46 +00006779 handleSimpleAttributeWithExclusions<SpeculativeLoadHardeningAttr,
6780 NoSpeculativeLoadHardeningAttr>(S, D,
6781 AL);
6782 break;
6783 case ParsedAttr::AT_NoSpeculativeLoadHardening:
6784 handleSimpleAttributeWithExclusions<NoSpeculativeLoadHardeningAttr,
6785 SpeculativeLoadHardeningAttr>(S, D, AL);
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006786 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006787 case ParsedAttr::AT_CodeSeg:
6788 handleCodeSegAttr(S, D, AL);
6789 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006790 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006791 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006792 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006793 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006794 handleMinVectorWidthAttr(S, D, AL);
6795 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006796 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006797 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006798 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006799 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006800 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006801 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006802 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006803 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006804 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006805 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006806 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006807 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006808 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006809 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006810 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006811 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006812 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006813 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006814 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006815 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006816 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006817 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006818 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006819 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006820 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006821 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6822 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006823 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006824 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00006825 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
6826 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006827 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006828 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006829 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006830 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006831 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006832 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006833 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006834 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006835 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006836 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006837 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006838 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006839 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006840 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006841 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006842 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006843 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006844 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006845 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006846 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006847 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006848 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006849 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006850 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006851 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006852 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006853 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006854 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006855 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006856 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006857 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006858 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006859 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006860 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006861 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006862 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006863 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006864 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006865 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006866 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006867 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006868 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006869 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006870 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006871 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006872 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006873 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006874 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006875 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006876 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006877 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006878 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006879 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006880 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006881 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006882 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006883 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006884 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006885 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006886 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006887 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006888 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006889 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006890 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006891 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006892 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006893 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006894 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006895 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006896 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006897 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00006898 // Interacts with -fstack-protector options.
6899 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
6900 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006901 case ParsedAttr::AT_StdCall:
6902 case ParsedAttr::AT_CDecl:
6903 case ParsedAttr::AT_FastCall:
6904 case ParsedAttr::AT_ThisCall:
6905 case ParsedAttr::AT_Pascal:
6906 case ParsedAttr::AT_RegCall:
6907 case ParsedAttr::AT_SwiftCall:
6908 case ParsedAttr::AT_VectorCall:
6909 case ParsedAttr::AT_MSABI:
6910 case ParsedAttr::AT_SysVABI:
6911 case ParsedAttr::AT_Pcs:
6912 case ParsedAttr::AT_IntelOclBicc:
6913 case ParsedAttr::AT_PreserveMost:
6914 case ParsedAttr::AT_PreserveAll:
Sander de Smalen44a22532018-11-26 16:38:37 +00006915 case ParsedAttr::AT_AArch64VectorPcs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006916 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006917 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006918 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006919 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006920 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006921 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006922 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006923 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006924 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006925 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006926 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006927 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006928 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006929 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006930 case ParsedAttr::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006931 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006932 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006933 case ParsedAttr::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006934 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006935 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006936 case ParsedAttr::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006937 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006938 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006939 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006940 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006941 break;
Louis Dionned2695792018-10-04 15:49:42 +00006942 case ParsedAttr::AT_ExcludeFromExplicitInstantiation:
6943 handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL);
6944 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006945 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006946 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006947 break;
John McCall8d32c052012-05-22 21:28:12 +00006948
6949 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006950 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006951 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006952 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006953 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006954 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006955 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006956 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006957 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006958 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006959 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006960 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00006961 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006962 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006963 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00006964 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006965 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006966 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00006967 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006968 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006969 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006970 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006971 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006972 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006973 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006974 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006975 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006976 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00006977
Erich Keanee891aa92018-07-13 15:07:47 +00006978 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006979 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006980 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006981
6982 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006983 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006984 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006985 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006986 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006987 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006988 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006989 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006990 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006991 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006992 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006993 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006994 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006995 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006996 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006997 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006998 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006999 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00007000 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007001 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007002 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00007003 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007004 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007005 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00007006 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007007 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007008 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007009 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007010 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007011 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007012 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007013 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007014 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007015 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007016 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007017 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007018 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007019 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007020 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007021 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007022 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007023 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007024 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007025 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007026 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007027 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007028 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007029 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007030 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007031
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007032 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007033 case ParsedAttr::AT_Capability:
7034 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007035 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007036 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007037 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007038 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007039 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00007040
Erich Keanee891aa92018-07-13 15:07:47 +00007041 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007042 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007043 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007044 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007045 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007046 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007047 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007048 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007049 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007050 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007051 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007052 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007053
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00007054 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007055 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007056 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00007057 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007058 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007059 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007060 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007061 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007062 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007063 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007064 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007065 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007066 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007067 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007068 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00007069 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007070 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007071 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00007072 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007073 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007074 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007075 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007076 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007077 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007078 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007079
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007080 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007081 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007082 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007083 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007084 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007085 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007086 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007087 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00007088 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00007089 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007090 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007091 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00007092 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007093 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007094 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007095 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007096 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007097 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007098 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00007099 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00007100
7101 // Move semantics attribute.
7102 case ParsedAttr::AT_Reinitializes:
7103 handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
7104 break;
Erik Pilkington5a559e62018-08-21 17:24:06 +00007105
7106 case ParsedAttr::AT_AlwaysDestroy:
7107 case ParsedAttr::AT_NoDestroy:
7108 handleDestroyAttr(S, D, AL);
7109 break;
JF Bastien14daa202018-12-18 05:12:21 +00007110
7111 case ParsedAttr::AT_Uninitialized:
7112 handleUninitializedAttr(S, D, AL);
7113 break;
Erik Pilkington1e368822019-01-04 18:33:06 +00007114
7115 case ParsedAttr::AT_ObjCExternallyRetained:
7116 handleObjCExternallyRetainedAttr(S, D, AL);
7117 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007118 }
7119}
7120
7121/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
7122/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00007123void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00007124 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00007125 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00007126 if (AttrList.empty())
7127 return;
7128
Erich Keanee891aa92018-07-13 15:07:47 +00007129 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00007130 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00007131
Joey Gouly2cd9db12013-12-13 16:15:28 +00007132 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00007133 // GCC accepts
7134 // static int a9 __attribute__((weakref));
7135 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00007136 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00007137 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
7138 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00007139 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00007140 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007141 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007142
Aaron Ballmanbe243a72014-12-04 22:45:31 +00007143 // FIXME: We should be able to handle this in TableGen as well. It would be
7144 // good to have a way to specify "these attributes must appear as a group",
7145 // for these. Additionally, it would be good to have a way to specify "these
7146 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00007147 if (!D->hasAttr<OpenCLKernelAttr>()) {
7148 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007149 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00007150 // FIXME: This emits a different error message than
7151 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00007152 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007153 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007154 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007155 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007156 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007157 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007158 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007159 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007160 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00007161 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
7162 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00007163 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
7164 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
7165 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7166 << A << ExpectedKernelFunction;
7167 D->setInvalidDecl();
7168 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
7169 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7170 << A << ExpectedKernelFunction;
7171 D->setInvalidDecl();
7172 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
7173 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7174 << A << ExpectedKernelFunction;
7175 D->setInvalidDecl();
7176 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
7177 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7178 << A << ExpectedKernelFunction;
7179 D->setInvalidDecl();
7180 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007181 }
7182 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007183}
7184
Hiroshi Inoue939d9322017-06-30 05:40:31 +00007185// Helper for delayed processing TransparentUnion attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007186void Sema::ProcessDeclAttributeDelayed(Decl *D,
7187 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007188 for (const ParsedAttr &AL : AttrList)
7189 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00007190 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00007191 break;
7192 }
7193}
7194
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007195// Annotation attributes are the only attributes allowed after an access
7196// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00007197bool Sema::ProcessAccessDeclAttributeList(
7198 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007199 for (const ParsedAttr &AL : AttrList) {
7200 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00007201 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007202 } else {
Erich Keanec480f302018-07-12 21:09:05 +00007203 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007204 return true;
7205 }
7206 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007207 return false;
7208}
7209
John McCall42856de2011-10-01 05:17:03 +00007210/// checkUnusedDeclAttributes - Check a list of attributes to see if it
7211/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00007212static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00007213 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00007214 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007215 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
7216 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00007217 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00007218 continue;
John McCall42856de2011-10-01 05:17:03 +00007219
Erich Keanee891aa92018-07-13 15:07:47 +00007220 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00007221 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00007222 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007223 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00007224 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
7225 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007226 }
7227 }
7228}
7229
7230/// checkUnusedDeclAttributes - Given a declarator which is not being
7231/// used to build a declaration, complain about any decl attributes
7232/// which might be lying around on it.
7233void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00007234 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00007235 ::checkUnusedDeclAttributes(*this, D.getAttributes());
7236 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
7237 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
7238}
7239
Ryan Flynn7d470f32009-07-30 03:15:39 +00007240/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00007241/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00007242NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
7243 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00007244 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00007245 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007246 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00007247 FunctionDecl *NewFD;
7248 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00007249 // FIXME: Mangling?
7250 // FIXME: Is the qualifier info correct?
7251 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00007252 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
7253 Loc, Loc, DeclarationName(II),
7254 FD->getType(), FD->getTypeSourceInfo(),
7255 SC_None, false/*isInlineSpecified*/,
7256 FD->hasPrototype(),
7257 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007258 NewD = NewFD;
7259
7260 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00007261 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00007262
7263 // Fake up parameter variables; they are declared as if this were
7264 // a typedef.
7265 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007266 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00007267 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00007268 for (const auto &AI : FT->param_types()) {
7269 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007270 Param->setScopeInfo(0, Params.size());
7271 Params.push_back(Param);
7272 }
David Blaikie9c70e042011-09-21 18:16:56 +00007273 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00007274 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007275 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00007276 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00007277 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00007278 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00007279 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007280 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00007281 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00007282 }
7283 return NewD;
7284}
7285
James Dennett634962f2012-06-14 21:40:34 +00007286/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00007287/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00007288void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00007289 if (W.getUsed()) return; // only do this once
7290 W.setUsed(true);
7291 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
7292 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00007293 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00007294 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
7295 W.getLocation()));
7296 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00007297 WeakTopLevelDecl.push_back(NewD);
7298 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
7299 // to insert Decl at TU scope, sorry.
7300 DeclContext *SavedContext = CurContext;
7301 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00007302 NewD->setDeclContext(CurContext);
7303 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00007304 PushOnScopeChains(NewD, S);
7305 CurContext = SavedContext;
7306 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00007307 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00007308 }
7309}
7310
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007311void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
7312 // It's valid to "forward-declare" #pragma weak, in which case we
7313 // have to do this.
7314 LoadExternalWeakUndeclaredIdentifiers();
7315 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00007316 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007317 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007318 if (VD->isExternC())
7319 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007320 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007321 if (FD->isExternC())
7322 ND = FD;
7323 if (ND) {
7324 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00007325 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007326 if (I != WeakUndeclaredIdentifiers.end()) {
7327 WeakInfo W = I->second;
7328 DeclApplyPragmaWeak(S, ND, W);
7329 WeakUndeclaredIdentifiers[Id] = W;
7330 }
7331 }
7332 }
7333 }
7334}
7335
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007336/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
7337/// it, apply them to D. This is a bit tricky because PD can have attributes
7338/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00007339void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007340 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00007341 if (!PD.getDeclSpec().getAttributes().empty())
7342 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00007343
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007344 // Walk the declarator structure, applying decl attributes that were in a type
7345 // position to the decl itself. This handles cases like:
7346 // int *__attr__(x)** D;
7347 // when X is a decl attribute.
7348 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00007349 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
7350 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00007351
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007352 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00007353 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00007354
7355 // Apply additional attributes specified by '#pragma clang attribute'.
7356 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007357}
John McCall28a6aea2009-11-04 02:18:39 +00007358
John McCall31168b02011-06-15 23:02:42 +00007359/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00007360/// If so, it'll still be annotated with an attribute that makes it
7361/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007362static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00007363 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00007364 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00007365 // Private ivars are always okay. Unfortunately, people don't
7366 // always properly make their ivars private, even in system headers.
7367 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007368 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
7369 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00007370 return false;
7371
John McCallc6af8c62015-10-28 05:03:19 +00007372 // Silently accept unsupported uses of __weak in both user and system
7373 // declarations when it's been disabled, for ease of integration with
7374 // -fno-objc-arc files. We do have to take some care against attempts
7375 // to define such things; for now, we've only done that for ivars
7376 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007377 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00007378 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
7379 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
7380 reason = UnavailableAttr::IR_ForbiddenWeak;
7381 return true;
7382 }
John McCallb61e14e2015-10-27 04:54:50 +00007383 }
7384
John McCallc6af8c62015-10-28 05:03:19 +00007385 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007386 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00007387 // Currently, all the failures dealt with this way are due to ARC
7388 // restrictions.
7389 reason = UnavailableAttr::IR_ARCForbiddenType;
7390 return true;
John McCallb61e14e2015-10-27 04:54:50 +00007391 }
7392
7393 return false;
John McCall31168b02011-06-15 23:02:42 +00007394}
7395
7396/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007397static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
7398 Decl *D) {
7399 auto Reason = UnavailableAttr::IR_None;
7400 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
7401 assert(Reason && "didn't set reason?");
7402 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00007403 return;
7404 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00007405 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007406 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00007407 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007408 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007409 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007410 DD.getForbiddenTypeDiagnostic() ==
7411 diag::err_arc_array_param_no_ownership) {
7412 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007413 return;
7414 }
7415 }
John McCall31168b02011-06-15 23:02:42 +00007416
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007417 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
7418 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
7419 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00007420}
7421
Manman Ren45b1ab12016-05-06 19:57:16 +00007422static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
7423 const Decl *D) {
7424 // Check each AvailabilityAttr to find the one for this platform.
7425 for (const auto *A : D->attrs()) {
7426 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
7427 // FIXME: this is copied from CheckAvailability. We should try to
7428 // de-duplicate.
7429
7430 // Check if this is an App Extension "platform", and if so chop off
7431 // the suffix for matching with the actual platform.
7432 StringRef ActualPlatform = Avail->getPlatform()->getName();
7433 StringRef RealizedPlatform = ActualPlatform;
7434 if (Context.getLangOpts().AppExt) {
7435 size_t suffix = RealizedPlatform.rfind("_app_extension");
7436 if (suffix != StringRef::npos)
7437 RealizedPlatform = RealizedPlatform.slice(0, suffix);
7438 }
7439
7440 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
7441
7442 // Match the platform name.
7443 if (RealizedPlatform == TargetPlatform)
7444 return Avail;
7445 }
7446 }
7447 return nullptr;
7448}
7449
Erik Pilkington9f866a72017-07-18 20:32:07 +00007450/// The diagnostic we should emit for \c D, and the declaration that
7451/// originated it, or \c AR_Available.
7452///
7453/// \param D The declaration to check.
7454/// \param Message If non-null, this will be populated with the message from
7455/// the availability attribute that is selected.
Erik Pilkington42578572018-09-10 22:20:09 +00007456/// \param ClassReceiver If we're checking the the method of a class message
7457/// send, the class. Otherwise nullptr.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007458static std::pair<AvailabilityResult, const NamedDecl *>
Erik Pilkington42578572018-09-10 22:20:09 +00007459ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D,
7460 std::string *Message,
7461 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007462 AvailabilityResult Result = D->getAvailability(Message);
7463
7464 // For typedefs, if the typedef declaration appears available look
7465 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007466 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007467 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007468 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007469 D = TT->getDecl();
7470 Result = D->getAvailability(Message);
7471 continue;
7472 }
7473 }
7474 break;
7475 }
7476
7477 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007478 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007479 if (IDecl->getDefinition()) {
7480 D = IDecl->getDefinition();
7481 Result = D->getAvailability(Message);
7482 }
7483 }
7484
7485 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7486 if (Result == AR_Available) {
7487 const DeclContext *DC = ECD->getDeclContext();
7488 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
7489 Result = TheEnumDecl->getAvailability(Message);
7490 D = TheEnumDecl;
7491 }
7492 }
7493
Erik Pilkington42578572018-09-10 22:20:09 +00007494 // For +new, infer availability from -init.
7495 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7496 if (S.NSAPIObj && ClassReceiver) {
7497 ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod(
7498 S.NSAPIObj->getInitSelector());
7499 if (Init && Result == AR_Available && MD->isClassMethod() &&
7500 MD->getSelector() == S.NSAPIObj->getNewSelector() &&
7501 MD->definedInNSObject(S.getASTContext())) {
7502 Result = Init->getAvailability(Message);
7503 D = Init;
7504 }
7505 }
7506 }
7507
Erik Pilkington9f866a72017-07-18 20:32:07 +00007508 return {Result, D};
7509}
7510
7511
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007512/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007513/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
7514/// in a deprecated context, but not the other way around.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007515static bool
7516ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
7517 VersionTuple DeclVersion, Decl *Ctx,
7518 const NamedDecl *OffendingDecl) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007519 assert(K != AR_Available && "Expected an unavailable declaration here!");
7520
7521 // Checks if we should emit the availability diagnostic in the context of C.
7522 auto CheckContext = [&](const Decl *C) {
7523 if (K == AR_NotYetIntroduced) {
7524 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
7525 if (AA->getIntroduced() >= DeclVersion)
7526 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007527 } else if (K == AR_Deprecated) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007528 if (C->isDeprecated())
7529 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007530 } else if (K == AR_Unavailable) {
7531 // It is perfectly fine to refer to an 'unavailable' Objective-C method
Alex Lorenz194d00e2019-01-17 18:12:45 +00007532 // when it is referenced from within the @implementation itself. In this
7533 // context, we interpret unavailable as a form of access control.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007534 if (const auto *MD = dyn_cast<ObjCMethodDecl>(OffendingDecl)) {
7535 if (const auto *Impl = dyn_cast<ObjCImplDecl>(C)) {
Alex Lorenz194d00e2019-01-17 18:12:45 +00007536 if (MD->getClassInterface() == Impl->getClassInterface())
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007537 return true;
7538 }
7539 }
7540 }
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007541
7542 if (C->isUnavailable())
7543 return true;
7544 return false;
7545 };
7546
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007547 do {
7548 if (CheckContext(Ctx))
7549 return false;
7550
7551 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00007552 // Unless it is "+load" method.
7553 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
7554 if (MethodD->isClassMethod() &&
7555 MethodD->getSelector().getAsString() == "load")
7556 return true;
7557
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007558 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007559 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
7560 if (CheckContext(Interface))
7561 return false;
7562 }
7563 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007564 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007565 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
7566 if (CheckContext(Interface))
7567 return false;
7568 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
7569
7570 return true;
7571}
7572
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007573static bool
7574shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
7575 const VersionTuple &DeploymentVersion,
7576 const VersionTuple &DeclVersion) {
7577 const auto &Triple = Context.getTargetInfo().getTriple();
7578 VersionTuple ForceAvailabilityFromVersion;
7579 switch (Triple.getOS()) {
7580 case llvm::Triple::IOS:
7581 case llvm::Triple::TvOS:
7582 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7583 break;
7584 case llvm::Triple::WatchOS:
7585 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7586 break;
7587 case llvm::Triple::Darwin:
7588 case llvm::Triple::MacOSX:
7589 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7590 break;
7591 default:
7592 // New targets should always warn about availability.
7593 return Triple.getVendor() == llvm::Triple::Apple;
7594 }
7595 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7596 DeclVersion >= ForceAvailabilityFromVersion;
7597}
7598
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007599static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7600 for (Decl *Ctx = OrigCtx; Ctx;
7601 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7602 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7603 return cast<NamedDecl>(Ctx);
7604 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7605 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7606 return Imp->getClassInterface();
7607 return CD;
7608 }
7609 }
7610
7611 return dyn_cast<NamedDecl>(OrigCtx);
7612}
7613
Alex Lorenz727c21e2017-07-26 13:58:02 +00007614namespace {
7615
7616struct AttributeInsertion {
7617 StringRef Prefix;
7618 SourceLocation Loc;
7619 StringRef Suffix;
7620
7621 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007622 return {" ", D->getEndLoc(), ""};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007623 }
7624 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7625 return {" ", Loc, ""};
7626 }
7627 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007628 return {"", D->getBeginLoc(), "\n"};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007629 }
7630};
7631
7632} // end anonymous namespace
7633
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007634/// Tries to parse a string as ObjC method name.
7635///
7636/// \param Name The string to parse. Expected to originate from availability
7637/// attribute argument.
7638/// \param SlotNames The vector that will be populated with slot names. In case
7639/// of unsuccessful parsing can contain invalid data.
7640/// \returns A number of method parameters if parsing was successful, None
7641/// otherwise.
7642static Optional<unsigned>
7643tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
7644 const LangOptions &LangOpts) {
7645 // Accept replacements starting with - or + as valid ObjC method names.
7646 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
7647 Name = Name.drop_front(1);
7648 if (Name.empty())
7649 return None;
7650 Name.split(SlotNames, ':');
7651 unsigned NumParams;
7652 if (Name.back() == ':') {
7653 // Remove an empty string at the end that doesn't represent any slot.
7654 SlotNames.pop_back();
7655 NumParams = SlotNames.size();
7656 } else {
7657 if (SlotNames.size() != 1)
7658 // Not a valid method name, just a colon-separated string.
7659 return None;
7660 NumParams = 0;
7661 }
7662 // Verify all slot names are valid.
7663 bool AllowDollar = LangOpts.DollarIdents;
7664 for (StringRef S : SlotNames) {
7665 if (S.empty())
7666 continue;
7667 if (!isValidIdentifier(S, AllowDollar))
7668 return None;
7669 }
7670 return NumParams;
7671}
7672
Alex Lorenz727c21e2017-07-26 13:58:02 +00007673/// Returns a source location in which it's appropriate to insert a new
7674/// attribute for the given declaration \D.
7675static Optional<AttributeInsertion>
7676createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7677 const LangOptions &LangOpts) {
7678 if (isa<ObjCPropertyDecl>(D))
7679 return AttributeInsertion::createInsertionAfter(D);
7680 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7681 if (MD->hasBody())
7682 return None;
7683 return AttributeInsertion::createInsertionAfter(D);
7684 }
7685 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7686 SourceLocation Loc =
7687 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7688 if (Loc.isInvalid())
7689 return None;
7690 // Insert after the 'struct'/whatever keyword.
7691 return AttributeInsertion::createInsertionAfter(Loc);
7692 }
7693 return AttributeInsertion::createInsertionBefore(D);
7694}
7695
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007696/// Actually emit an availability diagnostic for a reference to an unavailable
7697/// decl.
7698///
7699/// \param Ctx The context that the reference occurred in
7700/// \param ReferringDecl The exact declaration that was referenced.
7701/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007702/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007703/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7704/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7705/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007706static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007707 Decl *Ctx, const NamedDecl *ReferringDecl,
7708 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007709 StringRef Message,
7710 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007711 const ObjCInterfaceDecl *UnknownObjCClass,
7712 const ObjCPropertyDecl *ObjCProperty,
7713 bool ObjCPropertyAccess) {
7714 // Diagnostics for deprecated or unavailable.
7715 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007716 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007717 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007718
7719 // Matches 'diag::note_property_attribute' options.
7720 unsigned property_note_select;
7721
7722 // Matches diag::note_availability_specified_here.
7723 unsigned available_here_select_kind;
7724
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007725 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007726 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007727 DeclVersion = AA->getIntroduced();
7728
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007729 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx,
7730 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007731 return;
7732
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007733 SourceLocation Loc = Locs.front();
7734
Erik Pilkington8b352c42017-08-14 19:49:12 +00007735 // The declaration can have multiple availability attributes, we are looking
7736 // at one of them.
7737 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7738 if (A && A->isInherited()) {
7739 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7740 Redecl = Redecl->getPreviousDecl()) {
7741 const AvailabilityAttr *AForRedecl =
7742 getAttrForPlatform(S.Context, Redecl);
7743 if (AForRedecl && !AForRedecl->isInherited()) {
7744 // If D is a declaration with inherited attributes, the note should
7745 // point to the declaration with actual attributes.
7746 NoteLocation = Redecl->getLocation();
7747 break;
7748 }
7749 }
7750 }
7751
Aaron Ballmanfb237522014-10-15 15:37:51 +00007752 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007753 case AR_NotYetIntroduced: {
7754 // We would like to emit the diagnostic even if -Wunguarded-availability is
7755 // not specified for deployment targets >= to iOS 11 or equivalent or
7756 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7757 // later.
7758 const AvailabilityAttr *AA =
7759 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7760 VersionTuple Introduced = AA->getIntroduced();
7761
7762 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7763 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7764 Introduced);
7765 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7766 : diag::warn_unguarded_availability;
7767
Erik Pilkington0535b0f2019-01-14 19:17:31 +00007768 std::string PlatformName = AvailabilityAttr::getPrettyPlatformName(
7769 S.getASTContext().getTargetInfo().getPlatformName());
Erik Pilkington8b352c42017-08-14 19:49:12 +00007770
Erik Pilkington0535b0f2019-01-14 19:17:31 +00007771 S.Diag(Loc, Warning) << OffendingDecl << PlatformName
7772 << Introduced.getAsString();
7773
7774 S.Diag(OffendingDecl->getLocation(),
7775 diag::note_partial_availability_specified_here)
7776 << OffendingDecl << PlatformName << Introduced.getAsString()
7777 << S.Context.getTargetInfo().getPlatformMinVersion().getAsString();
Erik Pilkington8b352c42017-08-14 19:49:12 +00007778
7779 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007780 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007781 if (TD->getDeclName().isEmpty()) {
7782 S.Diag(TD->getLocation(),
7783 diag::note_decl_unguarded_availability_silence)
7784 << /*Anonymous*/ 1 << TD->getKindName();
7785 return;
7786 }
7787 auto FixitNoteDiag =
7788 S.Diag(Enclosing->getLocation(),
7789 diag::note_decl_unguarded_availability_silence)
7790 << /*Named*/ 0 << Enclosing;
7791 // Don't offer a fixit for declarations with availability attributes.
7792 if (Enclosing->hasAttr<AvailabilityAttr>())
7793 return;
7794 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7795 return;
7796 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7797 Enclosing, S.getSourceManager(), S.getLangOpts());
7798 if (!Insertion)
7799 return;
7800 std::string PlatformName =
7801 AvailabilityAttr::getPlatformNameSourceSpelling(
7802 S.getASTContext().getTargetInfo().getPlatformName())
7803 .lower();
7804 std::string Introduced =
7805 OffendingDecl->getVersionIntroduced().getAsString();
7806 FixitNoteDiag << FixItHint::CreateInsertion(
7807 Insertion->Loc,
7808 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7809 "(" + Introduced + "))" + Insertion->Suffix)
7810 .str());
7811 }
7812 return;
7813 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007814 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007815 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7816 : diag::warn_property_method_deprecated;
7817 diag_message = diag::warn_deprecated_message;
7818 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7819 property_note_select = /* deprecated */ 0;
7820 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007821 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7822 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007823 break;
7824
Erik Pilkington796a3e22016-08-05 22:59:03 +00007825 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007826 diag = !ObjCPropertyAccess ? diag::err_unavailable
7827 : diag::err_property_method_unavailable;
7828 diag_message = diag::err_unavailable_message;
7829 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7830 property_note_select = /* unavailable */ 1;
7831 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007832
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007833 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7834 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007835 // Most of these failures are due to extra restrictions in ARC;
7836 // reflect that in the primary diagnostic when applicable.
7837 auto flagARCError = [&] {
7838 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007839 S.getSourceManager().isInSystemHeader(
7840 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007841 diag = diag::err_unavailable_in_arc;
7842 };
7843
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007844 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007845 case UnavailableAttr::IR_None: break;
7846
7847 case UnavailableAttr::IR_ARCForbiddenType:
7848 flagARCError();
7849 diag_available_here = diag::note_arc_forbidden_type;
7850 break;
7851
7852 case UnavailableAttr::IR_ForbiddenWeak:
7853 if (S.getLangOpts().ObjCWeakRuntime)
7854 diag_available_here = diag::note_arc_weak_disabled;
7855 else
7856 diag_available_here = diag::note_arc_weak_no_runtime;
7857 break;
7858
7859 case UnavailableAttr::IR_ARCForbiddenConversion:
7860 flagARCError();
7861 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7862 break;
7863
7864 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7865 flagARCError();
7866 diag_available_here = diag::note_arc_init_returns_unrelated;
7867 break;
7868
7869 case UnavailableAttr::IR_ARCFieldWithOwnership:
7870 flagARCError();
7871 diag_available_here = diag::note_arc_field_with_ownership;
7872 break;
7873 }
7874 }
John McCallb61e14e2015-10-27 04:54:50 +00007875 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007876 break;
7877
Erik Pilkington796a3e22016-08-05 22:59:03 +00007878 case AR_Available:
7879 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007880 }
7881
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007882 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007883 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007884 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007885 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7886 Replacement = AL->getReplacement();
7887 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7888 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007889
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007890 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00007891 if (!Replacement.empty())
7892 UseRange =
7893 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007894 if (UseRange.isValid()) {
7895 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
7896 Selector Sel = MethodDecl->getSelector();
7897 SmallVector<StringRef, 12> SelectorSlotNames;
7898 Optional<unsigned> NumParams = tryParseObjCMethodName(
7899 Replacement, SelectorSlotNames, S.getLangOpts());
7900 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
7901 assert(SelectorSlotNames.size() == Locs.size());
7902 for (unsigned I = 0; I < Locs.size(); ++I) {
7903 if (!Sel.getNameForSlot(I).empty()) {
7904 CharSourceRange NameRange = CharSourceRange::getCharRange(
7905 Locs[I], S.getLocForEndOfToken(Locs[I]));
7906 FixIts.push_back(FixItHint::CreateReplacement(
7907 NameRange, SelectorSlotNames[I]));
7908 } else
7909 FixIts.push_back(
7910 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
7911 }
7912 } else
7913 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7914 } else
7915 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7916 }
Manman Renc7890fe2016-03-16 18:50:49 +00007917 }
7918
Aaron Ballmanfb237522014-10-15 15:37:51 +00007919 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007920 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007921 if (ObjCProperty)
7922 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7923 << ObjCProperty->getDeclName() << property_note_select;
7924 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007925 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007926 if (ObjCProperty)
7927 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7928 << ObjCProperty->getDeclName() << property_note_select;
7929 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007930 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007931 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7932 }
7933
Erik Pilkington8b352c42017-08-14 19:49:12 +00007934 S.Diag(NoteLocation, diag_available_here)
7935 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007936}
7937
7938static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
7939 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007940 assert(DD.Kind == DelayedDiagnostic::Availability &&
7941 "Expected an availability diagnostic here");
7942
Aaron Ballmanfb237522014-10-15 15:37:51 +00007943 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00007944 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007945 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007946 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
7947 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
7948 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00007949}
7950
John McCall2ec85372012-05-07 06:16:41 +00007951void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7952 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007953 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007954 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007955
John McCall2ec85372012-05-07 06:16:41 +00007956 // When delaying diagnostics to run in the context of a parsed
7957 // declaration, we only want to actually emit anything if parsing
7958 // succeeds.
7959 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007960
John McCall2ec85372012-05-07 06:16:41 +00007961 // We emit all the active diagnostics in this pool or any of its
7962 // parents. In general, we'll get one pool for the decl spec
7963 // and a child pool for each declarator; in a decl group like:
7964 // deprecated_typedef foo, *bar, baz();
7965 // only the declarator pops will be passed decls. This is correct;
7966 // we really do need to consider delayed diagnostics from the decl spec
7967 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007968 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007969 do {
Richard Smith5c9b3b72018-09-25 22:12:44 +00007970 bool AnyAccessFailures = false;
John McCall6347b682012-05-07 06:16:58 +00007971 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007972 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7973 // This const_cast is a bit lame. Really, Triggered should be mutable.
7974 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007975 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007976 continue;
7977
John McCallc1465822011-02-14 07:13:47 +00007978 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007979 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007980 // Don't bother giving deprecation/unavailable diagnostics if
7981 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007982 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00007983 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00007984 break;
7985
7986 case DelayedDiagnostic::Access:
Richard Smith5c9b3b72018-09-25 22:12:44 +00007987 // Only produce one access control diagnostic for a structured binding
7988 // declaration: we don't need to tell the user that all the fields are
7989 // inaccessible one at a time.
7990 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
7991 continue;
John McCall2ec85372012-05-07 06:16:41 +00007992 HandleDelayedAccessCheck(diag, decl);
Richard Smith5c9b3b72018-09-25 22:12:44 +00007993 if (diag.Triggered)
7994 AnyAccessFailures = true;
John McCall86121512010-01-27 03:50:35 +00007995 break;
John McCall31168b02011-06-15 23:02:42 +00007996
7997 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007998 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007999 break;
John McCall86121512010-01-27 03:50:35 +00008000 }
8001 }
John McCall2ec85372012-05-07 06:16:41 +00008002 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00008003}
8004
John McCall6347b682012-05-07 06:16:58 +00008005/// Given a set of delayed diagnostics, re-emit them as if they had
8006/// been delayed in the current context instead of in the given pool.
8007/// Essentially, this just moves them to the current pool.
8008void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
8009 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
8010 assert(curPool && "re-emitting in undelayed context not supported");
8011 curPool->steal(pool);
8012}
8013
Erik Pilkington9f866a72017-07-18 20:32:07 +00008014static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
8015 const NamedDecl *ReferringDecl,
8016 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008017 StringRef Message,
8018 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008019 const ObjCInterfaceDecl *UnknownObjCClass,
8020 const ObjCPropertyDecl *ObjCProperty,
8021 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00008022 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00008023 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
8024 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008025 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008026 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008027 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00008028 return;
8029 }
8030
Erik Pilkington9f866a72017-07-18 20:32:07 +00008031 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
8032 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008033 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008034 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00008035}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00008036
Erik Pilkington5cd57172016-08-16 17:44:11 +00008037namespace {
8038
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008039/// Returns true if the given statement can be a body-like child of \p Parent.
8040bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
8041 switch (Parent->getStmtClass()) {
8042 case Stmt::IfStmtClass:
8043 return cast<IfStmt>(Parent)->getThen() == S ||
8044 cast<IfStmt>(Parent)->getElse() == S;
8045 case Stmt::WhileStmtClass:
8046 return cast<WhileStmt>(Parent)->getBody() == S;
8047 case Stmt::DoStmtClass:
8048 return cast<DoStmt>(Parent)->getBody() == S;
8049 case Stmt::ForStmtClass:
8050 return cast<ForStmt>(Parent)->getBody() == S;
8051 case Stmt::CXXForRangeStmtClass:
8052 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
8053 case Stmt::ObjCForCollectionStmtClass:
8054 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
8055 case Stmt::CaseStmtClass:
8056 case Stmt::DefaultStmtClass:
8057 return cast<SwitchCase>(Parent)->getSubStmt() == S;
8058 default:
8059 return false;
8060 }
8061}
8062
8063class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
8064 const Stmt *Target;
8065
8066public:
8067 bool VisitStmt(Stmt *S) { return S != Target; }
8068
8069 /// Returns true if the given statement is present in the given declaration.
8070 static bool isContained(const Stmt *Target, const Decl *D) {
8071 StmtUSEFinder Visitor;
8072 Visitor.Target = Target;
8073 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
8074 }
8075};
8076
8077/// Traverses the AST and finds the last statement that used a given
8078/// declaration.
8079class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
8080 const Decl *D;
8081
8082public:
8083 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
8084 if (DRE->getDecl() == D)
8085 return false;
8086 return true;
8087 }
8088
8089 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
8090 const CompoundStmt *Scope) {
8091 LastDeclUSEFinder Visitor;
8092 Visitor.D = D;
8093 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
8094 const Stmt *S = *I;
8095 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
8096 return S;
8097 }
8098 return nullptr;
8099 }
8100};
8101
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008102/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00008103///
8104/// This is done with a traversal of the AST of a function that makes reference
8105/// to a partially available declaration. Whenever we encounter an \c if of the
8106/// form: \c if(@available(...)), we use the version from the condition to visit
8107/// the then statement.
8108class DiagnoseUnguardedAvailability
8109 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
8110 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
8111
8112 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008113 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00008114
8115 /// Stack of potentially nested 'if (@available(...))'s.
8116 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008117 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00008118
Erik Pilkington42578572018-09-10 22:20:09 +00008119 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range,
8120 ObjCInterfaceDecl *ClassReceiver = nullptr);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008121
8122public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008123 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
8124 : SemaRef(SemaRef), Ctx(Ctx) {
8125 AvailabilityStack.push_back(
8126 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008127 }
8128
Alex Lorenz6f911122017-05-16 13:58:53 +00008129 bool TraverseDecl(Decl *D) {
8130 // Avoid visiting nested functions to prevent duplicate warnings.
8131 if (!D || isa<FunctionDecl>(D))
8132 return true;
8133 return Base::TraverseDecl(D);
8134 }
8135
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008136 bool TraverseStmt(Stmt *S) {
8137 if (!S)
8138 return true;
8139 StmtStack.push_back(S);
8140 bool Result = Base::TraverseStmt(S);
8141 StmtStack.pop_back();
8142 return Result;
8143 }
8144
Erik Pilkington5cd57172016-08-16 17:44:11 +00008145 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
8146
8147 bool TraverseIfStmt(IfStmt *If);
8148
Alex Lorenz6f911122017-05-16 13:58:53 +00008149 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
8150
Erik Pilkingtonba87c622017-08-18 20:20:56 +00008151 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
8152 // to any useful diagnostics.
8153 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
8154
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008155 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
8156 if (PRE->isClassReceiver())
8157 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
8158 return true;
8159 }
8160
Erik Pilkington5cd57172016-08-16 17:44:11 +00008161 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
Erik Pilkington42578572018-09-10 22:20:09 +00008162 if (ObjCMethodDecl *D = Msg->getMethodDecl()) {
8163 ObjCInterfaceDecl *ID = nullptr;
8164 QualType ReceiverTy = Msg->getClassReceiver();
8165 if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType())
8166 ID = ReceiverTy->getAsObjCInterfaceType()->getInterface();
8167
Erik Pilkington5cd57172016-08-16 17:44:11 +00008168 DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00008169 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID);
8170 }
Erik Pilkington5cd57172016-08-16 17:44:11 +00008171 return true;
8172 }
8173
8174 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
8175 DiagnoseDeclAvailability(DRE->getDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008176 SourceRange(DRE->getBeginLoc(), DRE->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00008177 return true;
8178 }
8179
8180 bool VisitMemberExpr(MemberExpr *ME) {
8181 DiagnoseDeclAvailability(ME->getMemberDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008182 SourceRange(ME->getBeginLoc(), ME->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00008183 return true;
8184 }
8185
Alex Lorenz0a484ba2017-05-24 15:15:29 +00008186 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008187 SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use)
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008188 << (!SemaRef.getLangOpts().ObjC);
Alex Lorenz0a484ba2017-05-24 15:15:29 +00008189 return true;
8190 }
8191
Erik Pilkington5cd57172016-08-16 17:44:11 +00008192 bool VisitTypeLoc(TypeLoc Ty);
8193};
8194
8195void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00008196 NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008197 AvailabilityResult Result;
8198 const NamedDecl *OffendingDecl;
8199 std::tie(Result, OffendingDecl) =
Erik Pilkington42578572018-09-10 22:20:09 +00008200 ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008201 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008202 // All other diagnostic kinds have already been handled in
8203 // DiagnoseAvailabilityOfDecl.
8204 if (Result != AR_NotYetIntroduced)
8205 return;
8206
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008207 const AvailabilityAttr *AA =
8208 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008209 VersionTuple Introduced = AA->getIntroduced();
8210
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008211 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008212 return;
8213
8214 // If the context of this function is less available than D, we should not
8215 // emit a diagnostic.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00008216 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx,
8217 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008218 return;
8219
Alex Lorenzc9a369f2017-06-22 17:02:24 +00008220 // We would like to emit the diagnostic even if -Wunguarded-availability is
8221 // not specified for deployment targets >= to iOS 11 or equivalent or
8222 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
8223 // later.
8224 unsigned DiagKind =
8225 shouldDiagnoseAvailabilityByDefault(
8226 SemaRef.Context,
8227 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
8228 ? diag::warn_unguarded_availability_new
8229 : diag::warn_unguarded_availability;
8230
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008231 std::string PlatformName = AvailabilityAttr::getPrettyPlatformName(
8232 SemaRef.getASTContext().getTargetInfo().getPlatformName());
8233
Alex Lorenzc9a369f2017-06-22 17:02:24 +00008234 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008235 << Range << D << PlatformName << Introduced.getAsString();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008236
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008237 SemaRef.Diag(OffendingDecl->getLocation(),
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008238 diag::note_partial_availability_specified_here)
8239 << OffendingDecl << PlatformName << Introduced.getAsString()
8240 << SemaRef.Context.getTargetInfo()
8241 .getPlatformMinVersion()
8242 .getAsString();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008243
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008244 auto FixitDiag =
8245 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
8246 << Range << D
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008247 << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0
8248 : /*__builtin_available*/ 1);
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008249
8250 // Find the statement which should be enclosed in the if @available check.
8251 if (StmtStack.empty())
8252 return;
8253 const Stmt *StmtOfUse = StmtStack.back();
8254 const CompoundStmt *Scope = nullptr;
8255 for (const Stmt *S : llvm::reverse(StmtStack)) {
8256 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
8257 Scope = CS;
8258 break;
8259 }
8260 if (isBodyLikeChildStmt(StmtOfUse, S)) {
8261 // The declaration won't be seen outside of the statement, so we don't
8262 // have to wrap the uses of any declared variables in if (@available).
8263 // Therefore we can avoid setting Scope here.
8264 break;
8265 }
8266 StmtOfUse = S;
8267 }
8268 const Stmt *LastStmtOfUse = nullptr;
8269 if (isa<DeclStmt>(StmtOfUse) && Scope) {
8270 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
8271 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
8272 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
8273 break;
8274 }
8275 }
8276 }
8277
8278 const SourceManager &SM = SemaRef.getSourceManager();
8279 SourceLocation IfInsertionLoc =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008280 SM.getExpansionLoc(StmtOfUse->getBeginLoc());
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008281 SourceLocation StmtEndLoc =
8282 SM.getExpansionRange(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008283 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc())
Richard Smithb5f81712018-04-30 05:25:48 +00008284 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008285 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
8286 return;
8287
8288 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
8289 const char *ExtraIndentation = " ";
8290 std::string FixItString;
8291 llvm::raw_string_ostream FixItOS(FixItString);
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008292 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available"
8293 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00008294 << "("
8295 << AvailabilityAttr::getPlatformNameSourceSpelling(
8296 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008297 << " " << Introduced.getAsString() << ", *)) {\n"
8298 << Indentation << ExtraIndentation;
8299 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
8300 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
8301 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
8302 /*SkipTrailingWhitespaceAndNewLine=*/false);
8303 if (ElseInsertionLoc.isInvalid())
8304 ElseInsertionLoc =
8305 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
8306 FixItOS.str().clear();
8307 FixItOS << "\n"
8308 << Indentation << "} else {\n"
8309 << Indentation << ExtraIndentation
8310 << "// Fallback on earlier versions\n"
8311 << Indentation << "}";
8312 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008313 }
8314}
8315
8316bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
8317 const Type *TyPtr = Ty.getTypePtr();
8318 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
8319
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008320 if (Range.isInvalid())
8321 return true;
8322
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008323 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008324 TagDecl *TD = TT->getDecl();
8325 DiagnoseDeclAvailability(TD, Range);
8326
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008327 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008328 TypedefNameDecl *D = TD->getDecl();
8329 DiagnoseDeclAvailability(D, Range);
8330
8331 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
8332 if (NamedDecl *D = ObjCO->getInterface())
8333 DiagnoseDeclAvailability(D, Range);
8334 }
8335
8336 return true;
8337}
8338
8339bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
8340 VersionTuple CondVersion;
8341 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
8342 CondVersion = E->getVersion();
8343
8344 // If we're using the '*' case here or if this check is redundant, then we
8345 // use the enclosing version to check both branches.
8346 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00008347 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008348 } else {
8349 // This isn't an availability checking 'if', we can just continue.
8350 return Base::TraverseIfStmt(If);
8351 }
8352
8353 AvailabilityStack.push_back(CondVersion);
8354 bool ShouldContinue = TraverseStmt(If->getThen());
8355 AvailabilityStack.pop_back();
8356
8357 return ShouldContinue && TraverseStmt(If->getElse());
8358}
8359
8360} // end anonymous namespace
8361
8362void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
8363 Stmt *Body = nullptr;
8364
8365 if (auto *FD = D->getAsFunction()) {
8366 // FIXME: We only examine the pattern decl for availability violations now,
8367 // but we should also examine instantiated templates.
8368 if (FD->isTemplateInstantiation())
8369 return;
8370
8371 Body = FD->getBody();
8372 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
8373 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00008374 else if (auto *BD = dyn_cast<BlockDecl>(D))
8375 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008376
8377 assert(Body && "Need a body here!");
8378
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008379 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008380}
Erik Pilkington9f866a72017-07-18 20:32:07 +00008381
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008382void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
8383 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008384 const ObjCInterfaceDecl *UnknownObjCClass,
8385 bool ObjCPropertyAccess,
Erik Pilkington42578572018-09-10 22:20:09 +00008386 bool AvoidPartialAvailabilityChecks,
8387 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008388 std::string Message;
8389 AvailabilityResult Result;
8390 const NamedDecl* OffendingDecl;
8391 // See if this declaration is unavailable, deprecated, or partial.
Erik Pilkington42578572018-09-10 22:20:09 +00008392 std::tie(Result, OffendingDecl) =
8393 ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver);
Erik Pilkington9f866a72017-07-18 20:32:07 +00008394 if (Result == AR_Available)
8395 return;
8396
8397 if (Result == AR_NotYetIntroduced) {
8398 if (AvoidPartialAvailabilityChecks)
8399 return;
8400
8401 // We need to know the @available context in the current function to
8402 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
8403 // when we're done parsing the current function.
8404 if (getCurFunctionOrMethodDecl()) {
8405 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
8406 return;
8407 } else if (getCurBlock() || getCurLambda()) {
8408 getCurFunction()->HasPotentialAvailabilityViolations = true;
8409 return;
8410 }
8411 }
8412
8413 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008414 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008415 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
8416 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
8417 if (PDeclResult == Result)
8418 ObjCPDecl = PD;
8419 }
8420 }
8421
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008422 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008423 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
8424}