blob: 7f0ccdc6e750de8ac1967f463026a7cc805e79c6 [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
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002286AvailabilityAttr *Sema::mergeAvailabilityAttr(
2287 NamedDecl *D, SourceRange Range, IdentifierInfo *Platform, bool Implicit,
2288 VersionTuple Introduced, VersionTuple Deprecated, VersionTuple Obsoleted,
2289 bool IsUnavailable, StringRef Message, bool IsStrict, StringRef Replacement,
2290 AvailabilityMergeKind AMK, int Priority, unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002291 VersionTuple MergedIntroduced = Introduced;
2292 VersionTuple MergedDeprecated = Deprecated;
2293 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002294 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002295 bool OverrideOrImpl = false;
2296 switch (AMK) {
2297 case AMK_None:
2298 case AMK_Redeclaration:
2299 OverrideOrImpl = false;
2300 break;
2301
2302 case AMK_Override:
2303 case AMK_ProtocolImplementation:
2304 OverrideOrImpl = true;
2305 break;
2306 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002307
Rafael Espindolac67f2232012-05-10 02:50:16 +00002308 if (D->hasAttrs()) {
2309 AttrVec &Attrs = D->getAttrs();
2310 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002311 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002312 if (!OldAA) {
2313 ++i;
2314 continue;
2315 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002316
Rafael Espindolac67f2232012-05-10 02:50:16 +00002317 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2318 if (OldPlatform != Platform) {
2319 ++i;
2320 continue;
2321 }
2322
Tim Northover7a73cc72015-10-30 16:30:49 +00002323 // If there is an existing availability attribute for this platform that
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002324 // has a lower priority use the existing one and discard the new
2325 // attribute.
2326 if (OldAA->getPriority() < Priority)
Tim Northover7a73cc72015-10-30 16:30:49 +00002327 return nullptr;
Tim Northover7a73cc72015-10-30 16:30:49 +00002328
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002329 // If there is an existing attribute for this platform that has a higher
2330 // priority than the new attribute then erase the old one and continue
2331 // processing the attributes.
2332 if (OldAA->getPriority() > Priority) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002333 Attrs.erase(Attrs.begin() + i);
2334 --e;
2335 continue;
2336 }
2337
Rafael Espindolac67f2232012-05-10 02:50:16 +00002338 FoundAny = true;
2339 VersionTuple OldIntroduced = OldAA->getIntroduced();
2340 VersionTuple OldDeprecated = OldAA->getDeprecated();
2341 VersionTuple OldObsoleted = OldAA->getObsoleted();
2342 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002343
Douglas Gregord2a713e2015-09-30 21:27:42 +00002344 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2345 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2346 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002347 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002348 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2349 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002350 int Which = -1;
2351 VersionTuple FirstVersion;
2352 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002353 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002354 Which = 0;
2355 FirstVersion = OldIntroduced;
2356 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002357 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002358 Which = 1;
2359 FirstVersion = Deprecated;
2360 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002361 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002362 Which = 2;
2363 FirstVersion = Obsoleted;
2364 SecondVersion = OldObsoleted;
2365 }
2366
2367 if (Which == -1) {
2368 Diag(OldAA->getLocation(),
2369 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002370 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2371 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002372 } else {
2373 Diag(OldAA->getLocation(),
2374 diag::warn_mismatched_availability_override)
2375 << Which
2376 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002377 << FirstVersion.getAsString() << SecondVersion.getAsString()
2378 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002379 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002380 if (AMK == AMK_Override)
2381 Diag(Range.getBegin(), diag::note_overridden_method);
2382 else
2383 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002384 } else {
2385 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2386 Diag(Range.getBegin(), diag::note_previous_attribute);
2387 }
2388
Rafael Espindolac67f2232012-05-10 02:50:16 +00002389 Attrs.erase(Attrs.begin() + i);
2390 --e;
2391 continue;
2392 }
2393
2394 VersionTuple MergedIntroduced2 = MergedIntroduced;
2395 VersionTuple MergedDeprecated2 = MergedDeprecated;
2396 VersionTuple MergedObsoleted2 = MergedObsoleted;
2397
2398 if (MergedIntroduced2.empty())
2399 MergedIntroduced2 = OldIntroduced;
2400 if (MergedDeprecated2.empty())
2401 MergedDeprecated2 = OldDeprecated;
2402 if (MergedObsoleted2.empty())
2403 MergedObsoleted2 = OldObsoleted;
2404
2405 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2406 MergedIntroduced2, MergedDeprecated2,
2407 MergedObsoleted2)) {
2408 Attrs.erase(Attrs.begin() + i);
2409 --e;
2410 continue;
2411 }
2412
2413 MergedIntroduced = MergedIntroduced2;
2414 MergedDeprecated = MergedDeprecated2;
2415 MergedObsoleted = MergedObsoleted2;
2416 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002417 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002418 }
2419
2420 if (FoundAny &&
2421 MergedIntroduced == Introduced &&
2422 MergedDeprecated == Deprecated &&
2423 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002424 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002425
Douglas Gregord2a713e2015-09-30 21:27:42 +00002426 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002427 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002428 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002429 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002430 !OverrideOrImpl) {
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002431 auto *Avail = ::new (Context)
2432 AvailabilityAttr(Range, Context, Platform, Introduced, Deprecated,
2433 Obsoleted, IsUnavailable, Message, IsStrict,
2434 Replacement, Priority, AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002435 Avail->setImplicit(Implicit);
2436 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002437 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002438 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002439}
2440
Erich Keanee891aa92018-07-13 15:07:47 +00002441static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002442 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002443 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002444 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2445 unsigned Index = AL.getAttributeSpellingListIndex();
Fangrui Song6907ce22018-07-30 19:24:48 +00002446
Aaron Ballman00e99962013-08-31 01:11:41 +00002447 IdentifierInfo *II = Platform->Ident;
2448 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2449 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2450 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002451
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002452 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002453 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002454 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002455
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002456 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2457 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2458 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2459 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2460 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002461 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002462 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002463 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002464 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002465 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002466 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002467
Michael Wu260e9622018-11-12 02:44:33 +00002468 if (II->isStr("swift")) {
2469 if (Introduced.isValid() || Obsoleted.isValid() ||
2470 (!IsUnavailable && !Deprecated.isValid())) {
2471 S.Diag(AL.getLoc(),
2472 diag::warn_availability_swift_unavailable_deprecated_only);
2473 return;
2474 }
2475 }
2476
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002477 int PriorityModifier = AL.isPragmaClangAttribute()
2478 ? Sema::AP_PragmaClangAttribute
2479 : Sema::AP_Explicit;
2480 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2481 ND, AL.getRange(), II, false /*Implicit*/, Introduced.Version,
2482 Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict,
2483 Replacement, Sema::AMK_None, PriorityModifier, Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002484 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002485 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002486
2487 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2488 // matches before the start of the watchOS platform.
2489 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2490 IdentifierInfo *NewII = nullptr;
2491 if (II->getName() == "ios")
2492 NewII = &S.Context.Idents.get("watchos");
2493 else if (II->getName() == "ios_app_extension")
2494 NewII = &S.Context.Idents.get("watchos_app_extension");
2495
2496 if (NewII) {
2497 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2498 if (Version.empty())
2499 return Version;
2500 auto Major = Version.getMajor();
2501 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2502 if (NewMajor >= 2) {
2503 if (Version.getMinor().hasValue()) {
2504 if (Version.getSubminor().hasValue())
2505 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2506 Version.getSubminor().getValue());
2507 else
2508 return VersionTuple(NewMajor, Version.getMinor().getValue());
2509 }
2510 }
2511
2512 return VersionTuple(2, 0);
2513 };
2514
2515 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2516 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2517 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2518
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002519 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2520 ND, AL.getRange(), NewII, true /*Implicit*/, NewIntroduced,
2521 NewDeprecated, NewObsoleted, IsUnavailable, Str, IsStrict,
2522 Replacement, Sema::AMK_None,
2523 PriorityModifier + Sema::AP_InferredFromOtherPlatform, Index);
Tim Northover7a73cc72015-10-30 16:30:49 +00002524 if (NewAttr)
2525 D->addAttr(NewAttr);
2526 }
2527 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2528 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2529 // matches before the start of the tvOS platform.
2530 IdentifierInfo *NewII = nullptr;
2531 if (II->getName() == "ios")
2532 NewII = &S.Context.Idents.get("tvos");
2533 else if (II->getName() == "ios_app_extension")
2534 NewII = &S.Context.Idents.get("tvos_app_extension");
2535
2536 if (NewII) {
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002537 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2538 ND, AL.getRange(), NewII, true /*Implicit*/, Introduced.Version,
2539 Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict,
2540 Replacement, Sema::AMK_None,
2541 PriorityModifier + Sema::AP_InferredFromOtherPlatform, Index);
2542 if (NewAttr)
2543 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002544 }
2545 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002546}
2547
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002548static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002549 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002550 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002551 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002552 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002553 "Invalid number of arguments in an external_source_symbol attribute");
2554
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002555 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002556 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002557 Language = SE->getString();
2558 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002559 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002560 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002561 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002562
2563 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002564 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2565 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002566}
2567
John McCalld041a9b2013-02-20 01:54:26 +00002568template <class T>
2569static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2570 typename T::VisibilityType value,
2571 unsigned attrSpellingListIndex) {
2572 T *existingAttr = D->getAttr<T>();
2573 if (existingAttr) {
2574 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2575 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002576 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002577 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2578 S.Diag(range.getBegin(), diag::note_previous_attribute);
2579 D->dropAttr<T>();
2580 }
2581 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2582}
2583
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002584VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002585 VisibilityAttr::VisibilityType Vis,
2586 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002587 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2588 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002589}
2590
John McCalld041a9b2013-02-20 01:54:26 +00002591TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2592 TypeVisibilityAttr::VisibilityType Vis,
2593 unsigned AttrSpellingListIndex) {
2594 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2595 AttrSpellingListIndex);
2596}
2597
Erich Keanee891aa92018-07-13 15:07:47 +00002598static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002599 bool isTypeVisibility) {
2600 // Visibility attributes don't mean anything on a typedef.
2601 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002602 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002603 return;
2604 }
2605
2606 // 'type_visibility' can only go on a type or namespace.
2607 if (isTypeVisibility &&
2608 !(isa<TagDecl>(D) ||
2609 isa<ObjCInterfaceDecl>(D) ||
2610 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002611 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002612 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002613 return;
2614 }
2615
Benjamin Kramer70370212013-09-09 15:08:57 +00002616 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002617 StringRef TypeStr;
2618 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002619 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002620 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002621
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002622 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002623 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002624 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2625 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002626 return;
2627 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002628
Aaron Ballman682ee422013-09-11 19:47:58 +00002629 // Complain about attempts to use protected visibility on targets
2630 // (like Darwin) that don't support it.
2631 if (type == VisibilityAttr::Protected &&
2632 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002633 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002634 type = VisibilityAttr::Default;
2635 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002636
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002637 unsigned Index = AL.getAttributeSpellingListIndex();
2638 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002639 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002640 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002641 (TypeVisibilityAttr::VisibilityType) type,
2642 Index);
2643 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002644 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002645 }
2646 if (newAttr)
2647 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002648}
2649
Erich Keanee891aa92018-07-13 15:07:47 +00002650static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002651 const auto *M = cast<ObjCMethodDecl>(D);
2652 if (!AL.isArgIdent(0)) {
2653 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002654 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002655 return;
2656 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002657
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002658 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002659 ObjCMethodFamilyAttr::FamilyKind F;
2660 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002661 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002662 return;
2663 }
2664
Alp Toker314cc812014-01-25 16:55:45 +00002665 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002666 !M->getReturnType()->isObjCObjectPointerType()) {
2667 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2668 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002669 // Ignore the attribute.
2670 return;
2671 }
2672
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002673 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2674 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002675}
2676
Erich Keanee891aa92018-07-13 15:07:47 +00002677static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002678 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002679 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002680 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002681 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2682 return;
2683 }
2684 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002685 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002686 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002687 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002688 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2689 return;
2690 }
2691 }
2692 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002693 // It is okay to include this attribute on properties, e.g.:
2694 //
2695 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2696 //
2697 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002698 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002699 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002700 }
Michael Han99315932013-01-24 16:46:58 +00002701 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002702 ObjCNSObjectAttr(AL.getRange(), S.Context,
2703 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002704}
2705
Erich Keanee891aa92018-07-13 15:07:47 +00002706static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002707 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002708 QualType T = TD->getUnderlyingType();
2709 if (!T->isObjCObjectPointerType()) {
2710 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2711 return;
2712 }
2713 } else {
2714 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2715 return;
2716 }
2717 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002718 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2719 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002720}
2721
Erich Keanee891aa92018-07-13 15:07:47 +00002722static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002723 if (!AL.isArgIdent(0)) {
2724 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002725 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002726 return;
2727 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002728
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002729 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002730 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002731 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002732 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002733 return;
2734 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002735
Michael Han99315932013-01-24 16:46:58 +00002736 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002737 BlocksAttr(AL.getRange(), S.Context, type,
2738 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002739}
2740
Erich Keanee891aa92018-07-13 15:07:47 +00002741static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002742 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002743 if (AL.getNumArgs() > 0) {
2744 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002745 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002746 if (E->isTypeDependent() || E->isValueDependent() ||
2747 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002748 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002749 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002750 return;
2751 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002752
John McCallb46f2872011-09-09 07:56:05 +00002753 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002754 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002755 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002756 return;
2757 }
John McCallb46f2872011-09-09 07:56:05 +00002758
2759 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002760 }
2761
Aaron Ballman18a78382013-11-21 00:28:23 +00002762 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002763 if (AL.getNumArgs() > 1) {
2764 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002765 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002766 if (E->isTypeDependent() || E->isValueDependent() ||
2767 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002768 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002769 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002770 return;
2771 }
2772 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002773
John McCallb46f2872011-09-09 07:56:05 +00002774 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002775 // FIXME: This error message could be improved, it would be nice
2776 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002777 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002778 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002779 return;
2780 }
2781 }
2782
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002783 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002784 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002785 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002786 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002787 return;
2788 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002789
Chris Lattner9363e312009-03-17 23:03:47 +00002790 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002791 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002792 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002793 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002794 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002795 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002796 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002797 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002798 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002799 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002800 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002801 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002802 return;
2803 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002804 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002805 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002806 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002807 const FunctionType *FT = Ty->isFunctionPointerType()
2808 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002809 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002810 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002811 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002812 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002813 return;
2814 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002815 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002816 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002817 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002818 return;
2819 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002820 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002821 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002822 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002823 return;
2824 }
Michael Han99315932013-01-24 16:46:58 +00002825 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002826 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2827 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002828}
2829
Erich Keanee891aa92018-07-13 15:07:47 +00002830static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002831 if (D->getFunctionType() &&
2832 D->getFunctionType()->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002833 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002834 return;
2835 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002836 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002837 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002838 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002839 return;
2840 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002841
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002842 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002843 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002844 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2845 !AL.getScopeName())
Erich Keane44bacdf2018-08-09 13:21:32 +00002846 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballmane7964782016-03-07 22:44:55 +00002847
Fangrui Song6907ce22018-07-30 19:24:48 +00002848 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002849 WarnUnusedResultAttr(AL.getRange(), S.Context,
2850 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002851}
2852
Erich Keanee891aa92018-07-13 15:07:47 +00002853static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002854 // weak_import only applies to variable & function declarations.
2855 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002856 if (!D->canBeWeakImported(isDef)) {
2857 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002858 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002859 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002860 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002861 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002862 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002863 // Nothing to warn about here.
2864 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002865 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002866 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002867
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002868 return;
2869 }
2870
Michael Han99315932013-01-24 16:46:58 +00002871 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002872 WeakImportAttr(AL.getRange(), S.Context,
2873 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002874}
2875
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002876// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002877template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002878static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002879 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002880 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002881 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002882 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2883 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002884 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002885 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002886 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002887 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002888 return;
2889 }
2890 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002891
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002892 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2893 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2894 Existing->getYDim() == WGSize[1] &&
2895 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002896 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002897
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002898 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002899 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002900 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002901}
2902
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002903// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002904static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002905 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002906 const Expr *E = AL.getArgAsExpr(0);
2907 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002908 return;
2909 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002910 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002911 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002912 return;
2913 }
2914
2915 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2916 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2917 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002918 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002919
2920 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002921 AL.getRange(), S.Context, SGSize,
2922 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002923}
2924
Erich Keanee891aa92018-07-13 15:07:47 +00002925static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002926 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002927 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002928 return;
2929 }
2930
Craig Topperc3ec1492014-05-26 06:22:03 +00002931 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002932 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002933 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002934
2935 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2936 (ParmType->isBooleanType() ||
2937 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002938 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002939 << ParmType;
2940 return;
2941 }
2942
Aaron Ballmana9e05402013-12-02 22:16:55 +00002943 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002944 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002945 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002946 return;
2947 }
2948 }
2949
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002950 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002951 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002952 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002953}
2954
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002955SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002956 StringRef Name,
2957 unsigned AttrSpellingListIndex) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002958 // Explicit or partial specializations do not inherit
2959 // the section attribute from the primary template.
2960 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2961 if (AttrSpellingListIndex == SectionAttr::Declspec_allocate &&
2962 FD->isFunctionTemplateSpecialization())
2963 return nullptr;
2964 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002965 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2966 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002967 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002968 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2969 << 1 /*section*/;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002970 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002971 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002972 }
Michael Han99315932013-01-24 16:46:58 +00002973 return ::new (Context) SectionAttr(Range, Context, Name,
2974 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002975}
2976
Reid Kleckner2a133222015-03-04 23:39:17 +00002977bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2978 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2979 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002980 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002981 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002982 return false;
2983 }
2984 return true;
2985}
2986
Erich Keanee891aa92018-07-13 15:07:47 +00002987static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002988 // Make sure that there is a string literal as the sections's single
2989 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002990 StringRef Str;
2991 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002992 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002993 return;
Mike Stump11289f42009-09-09 15:08:12 +00002994
Reid Kleckner2a133222015-03-04 23:39:17 +00002995 if (!S.checkSectionName(LiteralLoc, Str))
2996 return;
2997
Chris Lattner30ba6742009-08-10 19:03:04 +00002998 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002999 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00003000 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003001 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00003002 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00003003 return;
3004 }
Mike Stump11289f42009-09-09 15:08:12 +00003005
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003006 unsigned Index = AL.getAttributeSpellingListIndex();
3007 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003008 if (NewAttr)
3009 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00003010}
3011
Erich Keane7963e8b2018-07-18 20:04:48 +00003012static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) {
3013 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
3014 if (!Error.empty()) {
3015 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
3016 << 0 /*'code-seg'*/;
3017 return false;
3018 }
3019 return true;
3020}
3021
3022CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range,
3023 StringRef Name,
3024 unsigned AttrSpellingListIndex) {
3025 // Explicit or partial specializations do not inherit
3026 // the code_seg attribute from the primary template.
3027 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3028 if (FD->isFunctionTemplateSpecialization())
3029 return nullptr;
3030 }
3031 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3032 if (ExistingAttr->getName() == Name)
3033 return nullptr;
3034 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3035 << 0 /*codeseg*/;
3036 Diag(Range.getBegin(), diag::note_previous_attribute);
3037 return nullptr;
3038 }
3039 return ::new (Context) CodeSegAttr(Range, Context, Name,
3040 AttrSpellingListIndex);
3041}
3042
3043static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3044 StringRef Str;
3045 SourceLocation LiteralLoc;
3046 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3047 return;
3048 if (!checkCodeSegName(S, LiteralLoc, Str))
3049 return;
3050 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3051 if (!ExistingAttr->isImplicit()) {
3052 S.Diag(AL.getLoc(),
3053 ExistingAttr->getName() == Str
3054 ? diag::warn_duplicate_codeseg_attribute
3055 : diag::err_conflicting_codeseg_attribute);
3056 return;
3057 }
3058 D->dropAttr<CodeSegAttr>();
3059 }
3060 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str,
3061 AL.getAttributeSpellingListIndex()))
3062 D->addAttr(CSA);
3063}
3064
Erich Keane57e15cd2017-07-19 22:06:33 +00003065// Check for things we'd like to warn about. Multiversioning issues are
3066// handled later in the process, once we know how many exist.
3067bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3068 enum FirstParam { Unsupported, Duplicate };
3069 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00003070 for (auto Str : {"tune=", "fpmath="})
3071 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00003072 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3073 << Unsupported << None << Str;
3074
3075 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
3076
3077 if (!ParsedAttrs.Architecture.empty() &&
3078 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3079 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3080 << Unsupported << Architecture << ParsedAttrs.Architecture;
3081
3082 if (ParsedAttrs.DuplicateArchitecture)
3083 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3084 << Duplicate << None << "arch=";
3085
3086 for (const auto &Feature : ParsedAttrs.Features) {
3087 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3088 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3089 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3090 << Unsupported << None << CurFeature;
3091 }
3092
Erich Keane29636aa2018-02-16 17:31:59 +00003093 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003094}
3095
Erich Keanee891aa92018-07-13 15:07:47 +00003096static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003097 StringRef Str;
3098 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003099 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003100 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003101 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003102
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003103 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00003104 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003105 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00003106 D->addAttr(NewAttr);
3107}
3108
Erich Keanee891aa92018-07-13 15:07:47 +00003109static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003110 Expr *E = AL.getArgAsExpr(0);
3111 uint32_t VecWidth;
3112 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3113 AL.setInvalid();
3114 return;
3115 }
3116
3117 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3118 if (Existing && Existing->getVectorWidth() != VecWidth) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003119 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Craig Topper74c10e32018-07-09 19:00:16 +00003120 return;
3121 }
3122
3123 D->addAttr(::new (S.Context)
3124 MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth,
3125 AL.getAttributeSpellingListIndex()));
3126}
3127
Erich Keanee891aa92018-07-13 15:07:47 +00003128static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003129 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003130 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003131 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003132 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003133
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003134 // gcc only allows for simple identifiers. Since we support more than gcc, we
3135 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003136 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003137 if (DRE->hasQualifier())
3138 S.Diag(Loc, diag::warn_cleanup_ext);
3139 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3140 NI = DRE->getNameInfo();
3141 if (!FD) {
3142 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3143 << NI.getName();
3144 return;
3145 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003146 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003147 if (ULE->hasExplicitTemplateArgs())
3148 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003149 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3150 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003151 if (!FD) {
3152 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3153 << NI.getName();
3154 if (ULE->getType() == S.Context.OverloadTy)
3155 S.NoteAllOverloadCandidates(ULE);
3156 return;
3157 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003158 } else {
3159 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003160 return;
3161 }
3162
Anders Carlssond277d792009-01-31 01:16:18 +00003163 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003164 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3165 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003166 return;
3167 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003168
Anders Carlsson723f55d2009-02-07 23:16:50 +00003169 // We're currently more strict than GCC about what function types we accept.
3170 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003171 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003172 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003173 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3174 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003175 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3176 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003177 return;
3178 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003179
Michael Han99315932013-01-24 16:46:58 +00003180 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003181 CleanupAttr(AL.getRange(), S.Context, FD,
3182 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003183}
3184
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003185static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003186 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003187 if (!AL.isArgIdent(0)) {
3188 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003189 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003190 return;
3191 }
3192
3193 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003194 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003195 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3196 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003197 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003198 return;
3199 }
3200
3201 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003202 AL.getRange(), S.Context, ExtensibilityKind,
3203 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003204}
3205
Mike Stumpd3bb5572009-07-24 19:02:52 +00003206/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003207/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003208static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003209 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003210 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003211 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003212 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003213
Eric Christopherb64963e2015-08-13 21:34:35 +00003214 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003215 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003216
Eric Christopherb64963e2015-08-13 21:34:35 +00003217 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3218 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003219 !isCFStringType(Ty, S.Context) &&
3220 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003221 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003222 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003223 << "a string type" << IdxExpr->getSourceRange()
3224 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003225 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003226 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003227 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003228 if (!isNSStringType(Ty, S.Context) &&
3229 !isCFStringType(Ty, S.Context) &&
3230 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003231 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003232 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003233 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003234 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003235 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003236 }
3237
Joel E. Denny81508102018-03-13 14:51:22 +00003238 D->addAttr(::new (S.Context) FormatArgAttr(
3239 AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003240}
3241
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003242enum FormatAttrKind {
3243 CFStringFormat,
3244 NSStringFormat,
3245 StrftimeFormat,
3246 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003247 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003248 InvalidFormat
3249};
3250
3251/// getFormatAttrKind - Map from format attribute names to supported format
3252/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003253static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003254 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003255 // Check for formats that get handled specially.
3256 .Case("NSString", NSStringFormat)
3257 .Case("CFString", CFStringFormat)
3258 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003259
Mehdi Amini06d367c2016-10-24 20:39:34 +00003260 // Otherwise, check for supported formats.
3261 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3262 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3263 .Case("kprintf", SupportedFormat) // OpenBSD.
3264 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3265 .Case("os_trace", SupportedFormat)
3266 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003267
Mehdi Amini06d367c2016-10-24 20:39:34 +00003268 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3269 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003270}
3271
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003272/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003273/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003274static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003275 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003276 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003277 return;
3278 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003279
Aaron Ballman4a611152013-11-27 16:34:09 +00003280 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003281 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3282 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003283 return;
3284 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003285 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003286 if (S.Context.getAsArrayType(T))
3287 T = S.Context.getBaseElementType(T);
3288 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003289 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3290 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003291 return;
3292 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003293
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003294 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003295 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003296 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3297 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003298 return;
3299 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003300
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003301 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003302 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003303 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003304 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003305 return;
3306 }
Michael Han99315932013-01-24 16:46:58 +00003307 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003308 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3309 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003310}
3311
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003312FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3313 IdentifierInfo *Format, int FormatIdx,
3314 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003315 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003316 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003317 for (auto *F : D->specific_attrs<FormatAttr>()) {
3318 if (F->getType() == Format &&
3319 F->getFormatIdx() == FormatIdx &&
3320 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003321 // If we don't have a valid location for this attribute, adopt the
3322 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003323 if (F->getLocation().isInvalid())
3324 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003325 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003326 }
3327 }
3328
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003329 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3330 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003331}
3332
Mike Stumpd3bb5572009-07-24 19:02:52 +00003333/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003334/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003335static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003336 if (!AL.isArgIdent(0)) {
3337 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003338 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003339 return;
3340 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003341
Chandler Carruth743682b2010-11-16 08:35:43 +00003342 // In C++ the implicit 'this' function parameter also counts, and they are
3343 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003344 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003345 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003346
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003347 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003348 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003349
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003350 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003351 // If we've modified the string name, we need a new identifier for it.
3352 II = &S.Context.Idents.get(Format);
3353 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003354
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003355 // Check for supported formats.
3356 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003357
Chris Lattner12161d32010-03-22 21:08:50 +00003358 if (Kind == IgnoredFormat)
3359 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003360
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003361 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003362 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003363 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003364 return;
3365 }
3366
3367 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003368 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003369 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003370 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003371 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003372
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003373 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003374 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003375 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003376 return;
3377 }
3378
3379 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003380 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003381
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003382 if (HasImplicitThisParam) {
3383 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003384 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003385 diag::err_format_attribute_implicit_this_format_string)
3386 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003387 return;
3388 }
3389 ArgIdx--;
3390 }
Mike Stump11289f42009-09-09 15:08:12 +00003391
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003392 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003393 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003394
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003395 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003396 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003397 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003398 << "a CFString" << IdxExpr->getSourceRange()
3399 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003400 return;
3401 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003402 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003403 // FIXME: do we need to check if the type is NSString*? What are the
3404 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003405 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003406 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003407 << "an NSString" << IdxExpr->getSourceRange()
3408 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003409 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003410 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003411 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003412 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003413 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003414 << "a string type" << IdxExpr->getSourceRange()
3415 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003416 return;
3417 }
3418
3419 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003420 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003421 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003422 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003423 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003424
3425 // check if the function is variadic if the 3rd argument non-zero
3426 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003427 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003428 ++NumArgs; // +1 for ...
3429 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003430 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003431 return;
3432 }
3433 }
3434
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003435 // strftime requires FirstArg to be 0 because it doesn't read from any
3436 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003437 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003438 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003439 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003440 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003441 return;
3442 }
3443 // if 0 it disables parameter checking (to use with e.g. va_list)
3444 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003445 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003446 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003447 return;
3448 }
3449
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003450 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003451 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003452 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003453 if (NewAttr)
3454 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003455}
3456
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003457/// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
3458static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3459 // The index that identifies the callback callee is mandatory.
3460 if (AL.getNumArgs() == 0) {
3461 S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee)
3462 << AL.getRange();
3463 return;
3464 }
3465
3466 bool HasImplicitThisParam = isInstanceMethod(D);
3467 int32_t NumArgs = getFunctionOrMethodNumParams(D);
3468
3469 FunctionDecl *FD = D->getAsFunction();
3470 assert(FD && "Expected a function declaration!");
3471
3472 llvm::StringMap<int> NameIdxMapping;
3473 NameIdxMapping["__"] = -1;
3474
3475 NameIdxMapping["this"] = 0;
3476
3477 int Idx = 1;
3478 for (const ParmVarDecl *PVD : FD->parameters())
3479 NameIdxMapping[PVD->getName()] = Idx++;
3480
3481 auto UnknownName = NameIdxMapping.end();
3482
3483 SmallVector<int, 8> EncodingIndices;
3484 for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) {
3485 SourceRange SR;
3486 int32_t ArgIdx;
3487
3488 if (AL.isArgIdent(I)) {
3489 IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
3490 auto It = NameIdxMapping.find(IdLoc->Ident->getName());
3491 if (It == UnknownName) {
3492 S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown)
3493 << IdLoc->Ident << IdLoc->Loc;
3494 return;
3495 }
3496
3497 SR = SourceRange(IdLoc->Loc);
3498 ArgIdx = It->second;
3499 } else if (AL.isArgExpr(I)) {
3500 Expr *IdxExpr = AL.getArgAsExpr(I);
3501
3502 // If the expression is not parseable as an int32_t we have a problem.
3503 if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1,
3504 false)) {
3505 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3506 << AL << (I + 1) << IdxExpr->getSourceRange();
3507 return;
3508 }
3509
3510 // Check oob, excluding the special values, 0 and -1.
3511 if (ArgIdx < -1 || ArgIdx > NumArgs) {
3512 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3513 << AL << (I + 1) << IdxExpr->getSourceRange();
3514 return;
3515 }
3516
3517 SR = IdxExpr->getSourceRange();
3518 } else {
3519 llvm_unreachable("Unexpected ParsedAttr argument type!");
3520 }
3521
3522 if (ArgIdx == 0 && !HasImplicitThisParam) {
3523 S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
3524 << (I + 1) << SR;
3525 return;
3526 }
3527
3528 // Adjust for the case we do not have an implicit "this" parameter. In this
3529 // case we decrease all positive values by 1 to get LLVM argument indices.
3530 if (!HasImplicitThisParam && ArgIdx > 0)
3531 ArgIdx -= 1;
3532
3533 EncodingIndices.push_back(ArgIdx);
3534 }
3535
3536 int CalleeIdx = EncodingIndices.front();
3537 // Check if the callee index is proper, thus not "this" and not "unknown".
Johannes Doerferte068d052019-01-21 14:23:46 +00003538 // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam"
3539 // is false and positive if "HasImplicitThisParam" is true.
3540 if (CalleeIdx < (int)HasImplicitThisParam) {
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003541 S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee)
3542 << AL.getRange();
3543 return;
3544 }
3545
3546 // Get the callee type, note the index adjustment as the AST doesn't contain
3547 // the this type (which the callee cannot reference anyway!).
3548 const Type *CalleeType =
3549 getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam)
3550 .getTypePtr();
3551 if (!CalleeType || !CalleeType->isFunctionPointerType()) {
3552 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3553 << AL.getRange();
3554 return;
3555 }
3556
3557 const Type *CalleeFnType =
3558 CalleeType->getPointeeType()->getUnqualifiedDesugaredType();
3559
3560 // TODO: Check the type of the callee arguments.
3561
3562 const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
3563 if (!CalleeFnProtoType) {
3564 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3565 << AL.getRange();
3566 return;
3567 }
3568
3569 if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
3570 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3571 << AL << (unsigned)(EncodingIndices.size() - 1);
3572 return;
3573 }
3574
3575 if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
3576 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3577 << AL << (unsigned)(EncodingIndices.size() - 1);
3578 return;
3579 }
3580
3581 if (CalleeFnProtoType->isVariadic()) {
3582 S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
3583 return;
3584 }
3585
3586 // Do not allow multiple callback attributes.
3587 if (D->hasAttr<CallbackAttr>()) {
3588 S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
3589 return;
3590 }
3591
3592 D->addAttr(::new (S.Context) CallbackAttr(
3593 AL.getRange(), S.Context, EncodingIndices.data(), EncodingIndices.size(),
3594 AL.getAttributeSpellingListIndex()));
3595}
3596
Erich Keanee891aa92018-07-13 15:07:47 +00003597static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003598 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003599 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003600 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003601 if (TD && TD->getUnderlyingType()->isUnionType())
3602 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3603 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003604 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003605
3606 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003607 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3608 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003609 return;
3610 }
3611
John McCallf937c022011-10-07 06:10:15 +00003612 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003613 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003614 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003615 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003616 return;
3617 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003618
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003619 RecordDecl::field_iterator Field = RD->field_begin(),
3620 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003621 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003622 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003623 return;
3624 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003625
David Blaikie40ed2972012-06-06 20:45:41 +00003626 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003627 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003628 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003629 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003630 diag::warn_transparent_union_attribute_floating)
3631 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003632 return;
3633 }
3634
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003635 if (FirstType->isIncompleteType())
3636 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003637 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3638 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3639 for (; Field != FieldEnd; ++Field) {
3640 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003641 if (FieldType->isIncompleteType())
3642 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003643 // FIXME: this isn't fully correct; we also need to test whether the
3644 // members of the union would all have the same calling convention as the
3645 // first member of the union. Checking just the size and alignment isn't
3646 // sufficient (consider structs passed on the stack instead of in registers
3647 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003648 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003649 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003650 // Warn if we drop the attribute.
3651 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003652 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003653 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003654 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003655 diag::warn_transparent_union_attribute_field_size_align)
3656 << isSize << Field->getDeclName() << FieldBits;
3657 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003658 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003659 diag::note_transparent_union_first_field_size_align)
3660 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003661 return;
3662 }
3663 }
3664
Michael Han99315932013-01-24 16:46:58 +00003665 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003666 TransparentUnionAttr(AL.getRange(), S.Context,
3667 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003668}
3669
Erich Keanee891aa92018-07-13 15:07:47 +00003670static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003671 // Make sure that there is a string literal as the annotation's single
3672 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003673 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003674 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003675 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003676
3677 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003678 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3679 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003680 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003681 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003682
Michael Han99315932013-01-24 16:46:58 +00003683 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003684 AnnotateAttr(AL.getRange(), S.Context, Str,
3685 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003686}
3687
Erich Keanee891aa92018-07-13 15:07:47 +00003688static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003689 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3690 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003691}
3692
3693void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3694 unsigned SpellingListIndex) {
3695 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3696 SourceLocation AttrLoc = AttrRange.getBegin();
3697
3698 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003699 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003700 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003701 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003702 T = VD->getType();
3703 else
3704 llvm_unreachable("Unknown decl type for align_value");
3705
3706 if (!T->isDependentType() && !T->isAnyPointerType() &&
3707 !T->isReferenceType() && !T->isMemberPointerType()) {
3708 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3709 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3710 return;
3711 }
3712
3713 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003714 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003715 ExprResult ICE
3716 = VerifyIntegerConstantExpression(E, &Alignment,
3717 diag::err_align_value_attribute_argument_not_int,
3718 /*AllowFold*/ false);
3719 if (ICE.isInvalid())
3720 return;
3721
3722 if (!Alignment.isPowerOf2()) {
3723 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3724 << E->getSourceRange();
3725 return;
3726 }
3727
3728 D->addAttr(::new (Context)
3729 AlignValueAttr(AttrRange, Context, ICE.get(),
3730 SpellingListIndex));
3731 return;
3732 }
3733
3734 // Save dependent expressions in the AST to be instantiated.
3735 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003736}
3737
Erich Keanee891aa92018-07-13 15:07:47 +00003738static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003739 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003740 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003741 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003742 return;
3743 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003744
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003745 if (AL.getNumArgs() == 0) {
3746 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3747 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003748 return;
3749 }
3750
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003751 Expr *E = AL.getArgAsExpr(0);
3752 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3753 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003754 diag::err_pack_expansion_without_parameter_packs);
3755 return;
3756 }
3757
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003758 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003759 return;
3760
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003761 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3762 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003763}
3764
3765void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003766 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003767 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3768 SourceLocation AttrLoc = AttrRange.getBegin();
3769
Richard Smith1dba27c2013-01-29 09:02:09 +00003770 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003771 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003772 // C++11 [dcl.align]p1:
3773 // An alignment-specifier may be applied to a variable or to a class
3774 // data member, but it shall not be applied to a bit-field, a function
3775 // parameter, the formal parameter of a catch clause, or a variable
3776 // declared with the register storage class specifier. An
3777 // alignment-specifier may also be applied to the declaration of a class
3778 // or enumeration type.
3779 // C11 6.7.5/2:
3780 // An alignment attribute shall not be specified in a declaration of
3781 // a typedef, or a bit-field, or a function, or a parameter, or an
3782 // object declared with the register storage-class specifier.
3783 int DiagKind = -1;
3784 if (isa<ParmVarDecl>(D)) {
3785 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003786 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003787 if (VD->getStorageClass() == SC_Register)
3788 DiagKind = 1;
3789 if (VD->isExceptionVariable())
3790 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003791 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003792 if (FD->isBitField())
3793 DiagKind = 3;
3794 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003795 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003796 << (TmpAttr.isC11() ? ExpectedVariableOrField
3797 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003798 return;
3799 }
3800 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003801 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003802 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003803 return;
3804 }
3805 }
3806
Richard Smith90ae9672018-06-20 23:36:55 +00003807 if (E->isValueDependent()) {
3808 // We can't support a dependent alignment on a non-dependent type,
3809 // because we have no way to model that a type is "alignment-dependent"
3810 // but not dependent in any other way.
3811 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3812 if (!TND->getUnderlyingType()->isDependentType()) {
3813 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3814 << E->getSourceRange();
3815 return;
3816 }
3817 }
3818
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003819 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003820 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3821 AA->setPackExpansion(IsPackExpansion);
3822 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003823 return;
3824 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003825
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003826 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003827 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003828 ExprResult ICE
3829 = VerifyIntegerConstantExpression(E, &Alignment,
3830 diag::err_aligned_attribute_argument_not_int,
3831 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003832 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003833 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003834
David Majnemer0be6bd02015-07-26 09:02:21 +00003835 uint64_t AlignVal = Alignment.getZExtValue();
3836
Richard Smith848e1f12013-02-01 08:12:08 +00003837 // C++11 [dcl.align]p2:
3838 // -- if the constant expression evaluates to zero, the alignment
3839 // specifier shall have no effect
3840 // C11 6.7.5p6:
3841 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003842 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003843 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003844 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3845 << E->getSourceRange();
3846 return;
3847 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003848 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003849
David Majnemerabecae72014-02-12 20:36:10 +00003850 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003851 unsigned MaxValidAlignment =
3852 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3853 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003854 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003855 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3856 << E->getSourceRange();
3857 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003858 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003859
David Majnemer0be6bd02015-07-26 09:02:21 +00003860 if (Context.getTargetInfo().isTLSSupported()) {
3861 unsigned MaxTLSAlign =
3862 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3863 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003864 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003865 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3866 VD->getTLSKind() != VarDecl::TLS_None) {
3867 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3868 << (unsigned)AlignVal << VD << MaxTLSAlign;
3869 return;
3870 }
3871 }
3872
Richard Smith44c247f2013-02-22 08:32:16 +00003873 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003874 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003875 AA->setPackExpansion(IsPackExpansion);
3876 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003877}
3878
Michael Hanaf02bbe2013-02-01 01:19:17 +00003879void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003880 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003881 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003882 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003883 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3884 SpellingListIndex);
3885 AA->setPackExpansion(IsPackExpansion);
3886 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003887}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003888
Richard Smith848e1f12013-02-01 08:12:08 +00003889void Sema::CheckAlignasUnderalignment(Decl *D) {
3890 assert(D->hasAttrs() && "no attributes on decl");
3891
David Majnemer475b25e2015-01-21 10:54:38 +00003892 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003893 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003894 UnderlyingTy = DiagTy = VD->getType();
3895 } else {
3896 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003897 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003898 UnderlyingTy = ED->getIntegerType();
3899 }
3900 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003901 return;
3902
3903 // C++11 [dcl.align]p5, C11 6.7.5/4:
3904 // The combined effect of all alignment attributes in a declaration shall
3905 // not specify an alignment that is less strict than the alignment that
3906 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003907 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003908 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003909 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003910 if (I->isAlignmentDependent())
3911 return;
3912 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003913 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003914 Align = std::max(Align, I->getAlignment(Context));
3915 }
3916
3917 if (AlignasAttr && Align) {
3918 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003919 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003920 if (NaturalAlign > RequestedAlign)
3921 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003922 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003923 }
3924}
3925
David Majnemer2c4e00a2014-01-29 22:07:36 +00003926bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003927 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003928 MSInheritanceAttr::Spelling SemanticSpelling) {
3929 assert(RD->hasDefinition() && "RD has no definition!");
3930
David Majnemer98c9ee22014-02-07 00:43:07 +00003931 // We may not have seen base specifiers or any virtual methods yet. We will
3932 // have to wait until the record is defined to catch any mismatches.
3933 if (!RD->getDefinition()->isCompleteDefinition())
3934 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003935
David Majnemer98c9ee22014-02-07 00:43:07 +00003936 // The unspecified model never matches what a definition could need.
3937 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3938 return false;
3939
David Majnemer4bb09802014-02-10 19:50:15 +00003940 if (BestCase) {
3941 if (RD->calculateInheritanceModel() == SemanticSpelling)
3942 return false;
3943 } else {
3944 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3945 return false;
3946 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003947
3948 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3949 << 0 /*definition*/;
3950 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3951 << RD->getNameAsString();
3952 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003953}
3954
Alexey Bataevf278eb12015-11-19 10:13:11 +00003955/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3956/// attribute.
3957static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3958 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003959 IntegerMode = true;
3960 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003961 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003962 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003963 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003964 case 'Q':
3965 DestWidth = 8;
3966 break;
3967 case 'H':
3968 DestWidth = 16;
3969 break;
3970 case 'S':
3971 DestWidth = 32;
3972 break;
3973 case 'D':
3974 DestWidth = 64;
3975 break;
3976 case 'X':
3977 DestWidth = 96;
3978 break;
3979 case 'T':
3980 DestWidth = 128;
3981 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003982 }
3983 if (Str[1] == 'F') {
3984 IntegerMode = false;
3985 } else if (Str[1] == 'C') {
3986 IntegerMode = false;
3987 ComplexMode = true;
3988 } else if (Str[1] != 'I') {
3989 DestWidth = 0;
3990 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003991 break;
3992 case 4:
3993 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3994 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003995 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003996 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003997 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003998 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003999 break;
4000 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00004001 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00004002 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00004003 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00004004 case 11:
4005 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00004006 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00004007 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004008 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00004009}
4010
4011/// handleModeAttr - This attribute modifies the width of a decl with primitive
4012/// type.
4013///
4014/// Despite what would be logical, the mode attribute is a decl attribute, not a
4015/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
4016/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00004017static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00004018 // This attribute isn't documented, but glibc uses it. It changes
4019 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004020 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004021 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
4022 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004023 return;
4024 }
4025
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004026 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004027
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004028 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004029}
4030
4031void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
4032 unsigned SpellingListIndex, bool InInstantiation) {
4033 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00004034 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004035 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00004036
4037 unsigned DestWidth = 0;
4038 bool IntegerMode = true;
4039 bool ComplexMode = false;
4040 llvm::APInt VectorSize(64, 0);
4041 if (Str.size() >= 4 && Str[0] == 'V') {
4042 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
4043 size_t StrSize = Str.size();
4044 size_t VectorStringLength = 0;
4045 while ((VectorStringLength + 1) < StrSize &&
4046 isdigit(Str[VectorStringLength + 1]))
4047 ++VectorStringLength;
4048 if (VectorStringLength &&
4049 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
4050 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004051 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00004052 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004053 // Avoid duplicate warning from template instantiation.
4054 if (!InInstantiation)
4055 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00004056 } else {
4057 VectorSize = 0;
4058 }
4059 }
4060
4061 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004062 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
4063
4064 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
4065 // and friends, at least with glibc.
4066 // FIXME: Make sure floating-point mappings are accurate
4067 // FIXME: Support XF and TF types
4068 if (!DestWidth) {
4069 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
4070 return;
4071 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00004072
4073 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004074 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00004075 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004076 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004077 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
4078 // Try to get type from enum declaration, default to int.
4079 OldTy = ED->getIntegerType();
4080 if (OldTy.isNull())
4081 OldTy = Context.IntTy;
4082 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004083 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00004084
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004085 if (OldTy->isDependentType()) {
4086 D->addAttr(::new (Context)
4087 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
4088 return;
4089 }
4090
Alexey Bataev326057d2015-06-19 07:46:21 +00004091 // Base type can also be a vector type (see PR17453).
4092 // Distinguish between base type and base element type.
4093 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004094 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00004095 OldElemTy = VT->getElementType();
4096
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004097 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
4098 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
4099 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
4100 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
4101 VectorSize.getBoolValue()) {
4102 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
4103 return;
4104 }
4105 bool IntegralOrAnyEnumType =
4106 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
4107
4108 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
4109 !IntegralOrAnyEnumType)
4110 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00004111 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004112 if (!IntegralOrAnyEnumType)
4113 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004114 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004115 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004116 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004117 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00004118 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004119 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004120 }
4121
Alexey Bataev326057d2015-06-19 07:46:21 +00004122 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004123
4124 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004125 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
4126 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004127 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004128 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004129
Alexey Bataev326057d2015-06-19 07:46:21 +00004130 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004131 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004132 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004133 }
4134
Eli Friedman4735374e2009-03-03 06:41:03 +00004135 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004136 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004137 }
4138
4139 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004140 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004141 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
4142 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004143 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004144 // Complex machine mode does not support base vector types.
4145 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004146 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004147 return;
4148 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004149 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00004150 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004151 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004152 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004153 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00004154 }
4155
4156 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004157 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004158 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004159 }
4160
4161 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004162 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004163 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004164 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004165 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004166 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004167 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004168
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004169 D->addAttr(::new (Context)
4170 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00004171}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004172
Erich Keanee891aa92018-07-13 15:07:47 +00004173static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Han99315932013-01-24 16:46:58 +00004174 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004175 NoDebugAttr(AL.getRange(), S.Context,
4176 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00004177}
4178
Paul Robinson30e41fb2014-12-15 18:57:28 +00004179AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00004180 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00004181 unsigned AttrSpellingListIndex) {
4182 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004183 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00004184 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4185 return nullptr;
4186 }
4187
4188 if (D->hasAttr<AlwaysInlineAttr>())
4189 return nullptr;
4190
4191 return ::new (Context) AlwaysInlineAttr(Range, Context,
4192 AttrSpellingListIndex);
4193}
4194
Erich Keane44bacdf2018-08-09 13:21:32 +00004195CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
4196 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004197 return nullptr;
4198
Erich Keane44bacdf2018-08-09 13:21:32 +00004199 return ::new (Context)
4200 CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004201}
4202
Erich Keane44bacdf2018-08-09 13:21:32 +00004203CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
4204 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
4205 return nullptr;
4206
4207 return ::new (Context)
4208 CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex());
4209}
4210
4211InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4212 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004213 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004214 // Attribute applies to Var but not any subclass of it (like ParmVar,
4215 // ImplicitParm or VarTemplateSpecialization).
4216 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004217 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4218 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4219 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004220 return nullptr;
4221 }
4222 // Attribute does not apply to non-static local variables.
4223 if (VD->hasLocalStorage()) {
4224 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4225 return nullptr;
4226 }
4227 }
4228
Erich Keane44bacdf2018-08-09 13:21:32 +00004229 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
4230 return nullptr;
4231
4232 return ::new (Context) InternalLinkageAttr(
4233 AL.getRange(), Context, AL.getAttributeSpellingListIndex());
4234}
4235InternalLinkageAttr *
4236Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4237 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4238 // Attribute applies to Var but not any subclass of it (like ParmVar,
4239 // ImplicitParm or VarTemplateSpecialization).
4240 if (VD->getKind() != Decl::Var) {
4241 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4242 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4243 : ExpectedVariableOrFunction);
4244 return nullptr;
4245 }
4246 // Attribute does not apply to non-static local variables.
4247 if (VD->hasLocalStorage()) {
4248 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4249 return nullptr;
4250 }
4251 }
4252
4253 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004254 return nullptr;
4255
4256 return ::new (Context)
Erich Keane44bacdf2018-08-09 13:21:32 +00004257 InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004258}
4259
Paul Robinson30e41fb2014-12-15 18:57:28 +00004260MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
4261 unsigned AttrSpellingListIndex) {
4262 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4263 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
4264 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4265 return nullptr;
4266 }
4267
4268 if (D->hasAttr<MinSizeAttr>())
4269 return nullptr;
4270
4271 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
4272}
4273
Zola Bridges826ef592019-01-18 17:20:46 +00004274NoSpeculativeLoadHardeningAttr *Sema::mergeNoSpeculativeLoadHardeningAttr(
4275 Decl *D, const NoSpeculativeLoadHardeningAttr &AL) {
4276 if (checkAttrMutualExclusion<SpeculativeLoadHardeningAttr>(*this, D, AL))
4277 return nullptr;
4278
4279 return ::new (Context) NoSpeculativeLoadHardeningAttr(
4280 AL.getRange(), Context, AL.getSpellingListIndex());
4281}
4282
Paul Robinson30e41fb2014-12-15 18:57:28 +00004283OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
4284 unsigned AttrSpellingListIndex) {
4285 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4286 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4287 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4288 D->dropAttr<AlwaysInlineAttr>();
4289 }
4290 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4291 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4292 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4293 D->dropAttr<MinSizeAttr>();
4294 }
4295
4296 if (D->hasAttr<OptimizeNoneAttr>())
4297 return nullptr;
4298
4299 return ::new (Context) OptimizeNoneAttr(Range, Context,
4300 AttrSpellingListIndex);
4301}
4302
Zola Bridges826ef592019-01-18 17:20:46 +00004303SpeculativeLoadHardeningAttr *Sema::mergeSpeculativeLoadHardeningAttr(
4304 Decl *D, const SpeculativeLoadHardeningAttr &AL) {
4305 if (checkAttrMutualExclusion<NoSpeculativeLoadHardeningAttr>(*this, D, AL))
4306 return nullptr;
4307
4308 return ::new (Context) SpeculativeLoadHardeningAttr(
4309 AL.getRange(), Context, AL.getSpellingListIndex());
4310}
4311
Erich Keanee891aa92018-07-13 15:07:47 +00004312static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004313 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004314 return;
4315
Paul Robinson080b1f32015-01-13 18:34:56 +00004316 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004317 D, AL.getRange(), AL.getName(),
4318 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004319 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004320}
4321
Erich Keanee891aa92018-07-13 15:07:47 +00004322static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004323 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004324 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004325 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004326}
4327
Erich Keanee891aa92018-07-13 15:07:47 +00004328static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004329 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004330 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004331 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004332}
4333
Erich Keanee891aa92018-07-13 15:07:47 +00004334static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004335 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004336 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004337 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004338 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004339 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004340 return;
4341 }
4342 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004343 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004344}
4345
Erich Keanee891aa92018-07-13 15:07:47 +00004346static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004347 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004348 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004349 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004350 // extern __shared__ is only allowed on arrays with no length (e.g.
4351 // "int x[]").
Yaxun Liu97670892018-10-02 17:48:54 +00004352 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004353 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004354 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004355 return;
4356 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004357 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004358 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004359 << S.CurrentCUDATarget())
4360 return;
Justin Lebar10411012016-09-30 23:57:30 +00004361 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004362 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004363}
4364
Erich Keanee891aa92018-07-13 15:07:47 +00004365static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004366 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4367 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004368 return;
4369 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004370 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004371 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004372 SourceRange RTRange = FD->getReturnTypeSourceRange();
4373 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004374 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004375 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4376 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004377 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004378 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004379 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4380 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004381 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004382 << Method;
4383 return;
4384 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004385 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004386 }
4387 // Only warn for "inline" when compiling for host, to cut down on noise.
4388 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004389 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004390
Aaron Ballman3aff6332013-12-02 19:30:36 +00004391 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004392 CUDAGlobalAttr(AL.getRange(), S.Context,
4393 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004394}
4395
Erich Keanee891aa92018-07-13 15:07:47 +00004396static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004397 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004398 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004399 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004400 return;
4401 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004402
Michael Han99315932013-01-24 16:46:58 +00004403 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004404 GNUInlineAttr(AL.getRange(), S.Context,
4405 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004406}
4407
Erich Keanee891aa92018-07-13 15:07:47 +00004408static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004409 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004410
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004411 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004412 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4413 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004414 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004415 return;
4416
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004417 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004418 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004419 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004420 return;
4421 }
4422
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004423 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004424 case ParsedAttr::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004425 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004426 FastCallAttr(AL.getRange(), S.Context,
4427 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004428 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004429 case ParsedAttr::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004430 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004431 StdCallAttr(AL.getRange(), S.Context,
4432 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004433 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004434 case ParsedAttr::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004435 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004436 ThisCallAttr(AL.getRange(), S.Context,
4437 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004438 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004439 case ParsedAttr::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004440 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004441 CDeclAttr(AL.getRange(), S.Context,
4442 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004443 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004444 case ParsedAttr::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004445 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004446 PascalAttr(AL.getRange(), S.Context,
4447 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004448 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004449 case ParsedAttr::AT_SwiftCall:
John McCall477f2bb2016-03-03 06:39:32 +00004450 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004451 SwiftCallAttr(AL.getRange(), S.Context,
4452 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004453 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004454 case ParsedAttr::AT_VectorCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004455 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004456 VectorCallAttr(AL.getRange(), S.Context,
4457 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004458 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004459 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004460 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004461 MSABIAttr(AL.getRange(), S.Context,
4462 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004463 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004464 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004465 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004466 SysVABIAttr(AL.getRange(), S.Context,
4467 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004468 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004469 case ParsedAttr::AT_RegCall:
Erich Keane757d3172016-11-02 18:29:35 +00004470 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004471 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004472 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004473 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004474 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004475 switch (CC) {
4476 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004477 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004478 break;
4479 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004480 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004481 break;
4482 default:
4483 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004484 }
4485
Michael Han99315932013-01-24 16:46:58 +00004486 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004487 PcsAttr(AL.getRange(), S.Context, PCS,
4488 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004489 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004490 }
Sander de Smalen44a22532018-11-26 16:38:37 +00004491 case ParsedAttr::AT_AArch64VectorPcs:
4492 D->addAttr(::new(S.Context)
4493 AArch64VectorPcsAttr(AL.getRange(), S.Context,
4494 AL.getAttributeSpellingListIndex()));
4495 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004496 case ParsedAttr::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004497 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004498 IntelOclBiccAttr(AL.getRange(), S.Context,
4499 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004500 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004501 case ParsedAttr::AT_PreserveMost:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004502 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004503 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004504 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004505 case ParsedAttr::AT_PreserveAll:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004506 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004507 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004508 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004509 default:
4510 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004511 }
4512}
4513
Erich Keanee891aa92018-07-13 15:07:47 +00004514static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004515 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004516 return;
4517
4518 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004519 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004520 StringRef RuleName;
4521
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004522 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004523 return;
4524
4525 // FIXME: Warn if the rule name is unknown. This is tricky because only
4526 // clang-tidy knows about available rules.
4527 DiagnosticIdentifiers.push_back(RuleName);
4528 }
4529 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004530 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4531 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004532}
4533
Erich Keanee891aa92018-07-13 15:07:47 +00004534bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004535 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004536 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004537 return true;
4538
Erich Keaneb11ebc52017-09-27 03:20:13 +00004539 if (Attrs.hasProcessingCache()) {
4540 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004541 return false;
4542 }
4543
Erich Keanee891aa92018-07-13 15:07:47 +00004544 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004545 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4546 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004547 return true;
4548 }
4549
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004550 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004551 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004552 case ParsedAttr::AT_CDecl:
4553 CC = CC_C;
4554 break;
4555 case ParsedAttr::AT_FastCall:
4556 CC = CC_X86FastCall;
4557 break;
4558 case ParsedAttr::AT_StdCall:
4559 CC = CC_X86StdCall;
4560 break;
4561 case ParsedAttr::AT_ThisCall:
4562 CC = CC_X86ThisCall;
4563 break;
4564 case ParsedAttr::AT_Pascal:
4565 CC = CC_X86Pascal;
4566 break;
4567 case ParsedAttr::AT_SwiftCall:
4568 CC = CC_Swift;
4569 break;
4570 case ParsedAttr::AT_VectorCall:
4571 CC = CC_X86VectorCall;
4572 break;
Sander de Smalen44a22532018-11-26 16:38:37 +00004573 case ParsedAttr::AT_AArch64VectorPcs:
4574 CC = CC_AArch64VectorCall;
4575 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004576 case ParsedAttr::AT_RegCall:
4577 CC = CC_X86RegCall;
4578 break;
4579 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004580 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004581 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004582 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004583 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004584 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4585 CC_C;
4586 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004587 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004588 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004589 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4590 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004591 return true;
4592 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004593 if (StrRef == "aapcs") {
4594 CC = CC_AAPCS;
4595 break;
4596 } else if (StrRef == "aapcs-vfp") {
4597 CC = CC_AAPCS_VFP;
4598 break;
4599 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004600
Erich Keaneb11ebc52017-09-27 03:20:13 +00004601 Attrs.setInvalid();
4602 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004603 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004604 }
Erich Keanee891aa92018-07-13 15:07:47 +00004605 case ParsedAttr::AT_IntelOclBicc:
4606 CC = CC_IntelOclBicc;
4607 break;
4608 case ParsedAttr::AT_PreserveMost:
4609 CC = CC_PreserveMost;
4610 break;
4611 case ParsedAttr::AT_PreserveAll:
4612 CC = CC_PreserveAll;
4613 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004614 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004615 }
4616
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004617 const TargetInfo &TI = Context.getTargetInfo();
4618 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004619 if (A != TargetInfo::CCCR_OK) {
4620 if (A == TargetInfo::CCCR_Warning)
Erich Keane44bacdf2018-08-09 13:21:32 +00004621 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004622
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004623 // This convention is not valid for the target. Use the default function or
4624 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004625 bool IsCXXMethod = false, IsVariadic = false;
4626 if (FD) {
4627 IsCXXMethod = FD->isCXXInstanceMember();
4628 IsVariadic = FD->isVariadic();
4629 }
4630 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004631 }
4632
Erich Keaneb11ebc52017-09-27 03:20:13 +00004633 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004634 return false;
4635}
4636
John McCall477f2bb2016-03-03 06:39:32 +00004637/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004638static bool isValidSwiftContextType(QualType Ty) {
4639 if (!Ty->hasPointerRepresentation())
4640 return Ty->isDependentType();
4641 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004642}
4643
4644/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004645static bool isValidSwiftIndirectResultType(QualType Ty) {
4646 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4647 Ty = PtrType->getPointeeType();
4648 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4649 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004650 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004651 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004652 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004653 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004654}
4655
4656/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004657static bool isValidSwiftErrorResultType(QualType Ty) {
4658 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4659 Ty = PtrType->getPointeeType();
4660 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4661 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004662 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004663 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004664 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004665 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004666 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004667 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004668}
4669
Erich Keanee891aa92018-07-13 15:07:47 +00004670static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004671 ParameterABI Abi) {
4672 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4673 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004674}
4675
4676void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4677 unsigned spellingIndex) {
4678
4679 QualType type = cast<ParmVarDecl>(D)->getType();
4680
4681 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4682 if (existingAttr->getABI() != abi) {
4683 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4684 << getParameterABISpelling(abi) << existingAttr;
4685 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4686 return;
4687 }
4688 }
4689
4690 switch (abi) {
4691 case ParameterABI::Ordinary:
4692 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4693
4694 case ParameterABI::SwiftContext:
4695 if (!isValidSwiftContextType(type)) {
4696 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4697 << getParameterABISpelling(abi)
4698 << /*pointer to pointer */ 0 << type;
4699 }
4700 D->addAttr(::new (Context)
4701 SwiftContextAttr(range, Context, spellingIndex));
4702 return;
4703
4704 case ParameterABI::SwiftErrorResult:
4705 if (!isValidSwiftErrorResultType(type)) {
4706 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4707 << getParameterABISpelling(abi)
4708 << /*pointer to pointer */ 1 << type;
4709 }
4710 D->addAttr(::new (Context)
4711 SwiftErrorResultAttr(range, Context, spellingIndex));
4712 return;
4713
4714 case ParameterABI::SwiftIndirectResult:
4715 if (!isValidSwiftIndirectResultType(type)) {
4716 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4717 << getParameterABISpelling(abi)
4718 << /*pointer*/ 0 << type;
4719 }
4720 D->addAttr(::new (Context)
4721 SwiftIndirectResultAttr(range, Context, spellingIndex));
4722 return;
4723 }
4724 llvm_unreachable("bad parameter ABI attribute");
4725}
4726
John McCall3882ace2011-01-05 12:14:39 +00004727/// Checks a regparm attribute, returning true if it is ill-formed and
4728/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004729bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004730 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004731 return true;
4732
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004733 if (!checkAttributeNumArgs(*this, AL, 1)) {
4734 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004735 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004736 }
Eli Friedman7044b762009-03-27 21:06:47 +00004737
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004738 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004739 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4740 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4741 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004742 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004743 }
4744
Douglas Gregore8bbc122011-09-02 00:18:52 +00004745 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004746 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004747 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004748 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004749 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004750 }
4751
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004752 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004753 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004754 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004755 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004756 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004757 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004758 }
4759
John McCall3882ace2011-01-05 12:14:39 +00004760 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004761}
4762
Artem Belevichbcec9da2016-06-06 22:54:57 +00004763// Checks whether an argument of launch_bounds attribute is
4764// acceptable, performs implicit conversion to Rvalue, and returns
4765// non-nullptr Expr result on success. Otherwise, it returns nullptr
4766// and may output an error.
4767static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004768 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004769 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004770 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004771 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004772
4773 // Accept template arguments for now as they depend on something else.
4774 // We'll get to check them when they eventually get instantiated.
4775 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004776 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004777
4778 llvm::APSInt I(64);
4779 if (!E->isIntegerConstantExpr(I, S.Context)) {
4780 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004781 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004782 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004783 }
4784 // Make sure we can fit it in 32 bits.
4785 if (!I.isIntN(32)) {
4786 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4787 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004788 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004789 }
4790 if (I < 0)
4791 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004792 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004793
Artem Belevichbcec9da2016-06-06 22:54:57 +00004794 // We may need to perform implicit conversion of the argument.
4795 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4796 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4797 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4798 assert(!ValArg.isInvalid() &&
4799 "Unexpected PerformCopyInitialization() failure.");
4800
4801 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004802}
4803
4804void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4805 Expr *MinBlocks, unsigned SpellingListIndex) {
4806 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4807 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004808 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4809 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004810 return;
4811
Artem Belevichbcec9da2016-06-06 22:54:57 +00004812 if (MinBlocks) {
4813 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4814 if (MinBlocks == nullptr)
4815 return;
4816 }
Artem Belevich7093e402015-04-21 22:55:54 +00004817
4818 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4819 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4820}
4821
Erich Keanee891aa92018-07-13 15:07:47 +00004822static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004823 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4824 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004825 return;
4826
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004827 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4828 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4829 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004830}
4831
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004832static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004833 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004834 if (!AL.isArgIdent(0)) {
4835 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004836 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004837 return;
4838 }
Joel E. Denny81508102018-03-13 14:51:22 +00004839
4840 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004841 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004842 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004843 return;
4844
Joel E. Denny81508102018-03-13 14:51:22 +00004845 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004846 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004847 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004848 return;
4849
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004850 bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004851 if (IsPointer) {
4852 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004853 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4854 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4855 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004856 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004857 }
4858
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004859 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
4860 AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx,
4861 TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004862}
4863
4864static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004865 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004866 if (!AL.isArgIdent(0)) {
4867 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004868 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004869 return;
4870 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004871
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004872 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004873 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004874
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004875 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004876 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004877 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004878 return;
4879 }
4880
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004881 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004882 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004883 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004884 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004885
Michael Han99315932013-01-24 16:46:58 +00004886 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004887 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004888 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004889 AL.getLayoutCompatible(),
4890 AL.getMustBeNull(),
4891 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004892}
4893
Erich Keanee891aa92018-07-13 15:07:47 +00004894static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004895 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004896
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004897 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004898 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004899 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004900 return;
4901
Joel E. Denny81508102018-03-13 14:51:22 +00004902 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
4903 D->addAttr(::new (S.Context) XRayLogArgsAttr(
4904 AL.getRange(), S.Context, ArgCount.getSourceIndex(),
4905 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004906}
4907
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004908//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004909// Checker-specific attribute handlers.
4910//===----------------------------------------------------------------------===//
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004911static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4912 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004913}
4914
George Karpenkov1657f362018-11-30 02:18:37 +00004915static bool isValidSubjectOfNSAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004916 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004917 QT->isObjCNSObjectType();
John McCalled433932011-01-25 03:31:58 +00004918}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004919
George Karpenkov1657f362018-11-30 02:18:37 +00004920static bool isValidSubjectOfCFAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004921 return QT->isDependentType() || QT->isPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004922 isValidSubjectOfNSAttribute(QT);
John McCalled433932011-01-25 03:31:58 +00004923}
4924
George Karpenkov1657f362018-11-30 02:18:37 +00004925static bool isValidSubjectOfOSAttribute(QualType QT) {
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004926 if (QT->isDependentType())
4927 return true;
4928 QualType PT = QT->getPointeeType();
4929 return !PT.isNull() && PT->getAsCXXRecordDecl() != nullptr;
John McCall3b5a8f52016-03-03 00:10:03 +00004930}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004931
George Karpenkov1657f362018-11-30 02:18:37 +00004932void Sema::AddXConsumedAttr(Decl *D, SourceRange SR, unsigned SpellingIndex,
4933 RetainOwnershipKind K,
4934 bool IsTemplateInstantiation) {
4935 ValueDecl *VD = cast<ValueDecl>(D);
4936 switch (K) {
4937 case RetainOwnershipKind::OS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004938 handleSimpleAttributeOrDiagnose<OSConsumedAttr>(
4939 *this, VD, SR, SpellingIndex, isValidSubjectOfOSAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00004940 diag::warn_ns_attribute_wrong_parameter_type,
4941 /*ExtraArgs=*/SR, "os_consumed", /*pointers*/ 1);
4942 return;
4943 case RetainOwnershipKind::NS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004944 handleSimpleAttributeOrDiagnose<NSConsumedAttr>(
4945 *this, VD, SR, SpellingIndex, isValidSubjectOfNSAttribute(VD->getType()),
John McCall3b5a8f52016-03-03 00:10:03 +00004946
George Karpenkov1657f362018-11-30 02:18:37 +00004947 // These attributes are normally just advisory, but in ARC, ns_consumed
4948 // is significant. Allow non-dependent code to contain inappropriate
4949 // attributes even in ARC, but require template instantiations to be
4950 // set up correctly.
4951 ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount)
4952 ? diag::err_ns_attribute_wrong_parameter_type
4953 : diag::warn_ns_attribute_wrong_parameter_type),
4954 /*ExtraArgs=*/SR, "ns_consumed", /*objc pointers*/ 0);
4955 return;
4956 case RetainOwnershipKind::CF:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004957 handleSimpleAttributeOrDiagnose<CFConsumedAttr>(
George Karpenkov1657f362018-11-30 02:18:37 +00004958 *this, VD, SR, SpellingIndex,
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004959 isValidSubjectOfCFAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00004960 diag::warn_ns_attribute_wrong_parameter_type,
4961 /*ExtraArgs=*/SR, "cf_consumed", /*pointers*/1);
John McCalled433932011-01-25 03:31:58 +00004962 return;
4963 }
George Karpenkov1657f362018-11-30 02:18:37 +00004964}
John McCalled433932011-01-25 03:31:58 +00004965
George Karpenkov1657f362018-11-30 02:18:37 +00004966static Sema::RetainOwnershipKind
4967parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) {
4968 switch (AL.getKind()) {
4969 case ParsedAttr::AT_CFConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004970 case ParsedAttr::AT_CFReturnsRetained:
4971 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004972 return Sema::RetainOwnershipKind::CF;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004973 case ParsedAttr::AT_OSConsumesThis:
George Karpenkov1657f362018-11-30 02:18:37 +00004974 case ParsedAttr::AT_OSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004975 case ParsedAttr::AT_OSReturnsRetained:
4976 case ParsedAttr::AT_OSReturnsNotRetained:
4977 case ParsedAttr::AT_OSReturnsRetainedOnZero:
4978 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
George Karpenkov1657f362018-11-30 02:18:37 +00004979 return Sema::RetainOwnershipKind::OS;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004980 case ParsedAttr::AT_NSConsumesSelf:
George Karpenkov1657f362018-11-30 02:18:37 +00004981 case ParsedAttr::AT_NSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004982 case ParsedAttr::AT_NSReturnsRetained:
4983 case ParsedAttr::AT_NSReturnsNotRetained:
4984 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00004985 return Sema::RetainOwnershipKind::NS;
4986 default:
4987 llvm_unreachable("Wrong argument supplied");
4988 }
John McCalled433932011-01-25 03:31:58 +00004989}
4990
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004991bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4992 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004993 return false;
4994
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004995 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4996 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004997 return true;
4998}
4999
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005000/// \return whether the parameter is a pointer to OSObject pointer.
5001static bool isValidOSObjectOutParameter(const Decl *D) {
5002 const auto *PVD = dyn_cast<ParmVarDecl>(D);
5003 if (!PVD)
5004 return false;
5005 QualType QT = PVD->getType();
5006 QualType PT = QT->getPointeeType();
5007 return !PT.isNull() && isValidSubjectOfOSAttribute(PT);
5008}
5009
George Karpenkov1657f362018-11-30 02:18:37 +00005010static void handleXReturnsXRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005011 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005012 QualType ReturnType;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005013 Sema::RetainOwnershipKind K = parsedAttrToRetainOwnershipKind(AL);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005014
George Karpenkov1657f362018-11-30 02:18:37 +00005015 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005016 ReturnType = MD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005017 } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
5018 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) {
John McCall31168b02011-06-15 23:02:42 +00005019 return; // ignore: was handled as a type attribute
George Karpenkov1657f362018-11-30 02:18:37 +00005020 } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005021 ReturnType = PD->getType();
George Karpenkov1657f362018-11-30 02:18:37 +00005022 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005023 ReturnType = FD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005024 } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
5025 // Attributes on parameters are used for out-parameters,
5026 // passed as pointers-to-pointers.
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005027 unsigned DiagID = K == Sema::RetainOwnershipKind::CF
5028 ? /*pointer-to-CF-pointer*/2
5029 : /*pointer-to-OSObject-pointer*/3;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005030 ReturnType = Param->getType()->getPointeeType();
5031 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005032 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005033 << AL << DiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005034 return;
5035 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005036 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00005037 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005038 } else {
5039 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005040 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005041 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005042 case ParsedAttr::AT_NSReturnsRetained:
5043 case ParsedAttr::AT_NSReturnsAutoreleased:
5044 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005045 ExpectedDeclKind = ExpectedFunctionOrMethod;
5046 break;
5047
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005048 case ParsedAttr::AT_OSReturnsRetained:
5049 case ParsedAttr::AT_OSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00005050 case ParsedAttr::AT_CFReturnsRetained:
5051 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005052 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
5053 break;
5054 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005055 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005056 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005057 return;
5058 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005059
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005060 bool TypeOK;
5061 bool Cf;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005062 unsigned ParmDiagID = 2; // Pointer-to-CF-pointer
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005063 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00005064 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005065 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005066 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
5067 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00005068 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005069
5070 case ParsedAttr::AT_NSReturnsAutoreleased:
5071 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005072 TypeOK = isValidSubjectOfNSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005073 Cf = false;
John McCalled433932011-01-25 03:31:58 +00005074 break;
5075
Erich Keanee891aa92018-07-13 15:07:47 +00005076 case ParsedAttr::AT_CFReturnsRetained:
5077 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005078 TypeOK = isValidSubjectOfCFAttribute(ReturnType);
5079 Cf = true;
5080 break;
5081
5082 case ParsedAttr::AT_OSReturnsRetained:
5083 case ParsedAttr::AT_OSReturnsNotRetained:
5084 TypeOK = isValidSubjectOfOSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005085 Cf = true;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005086 ParmDiagID = 3; // Pointer-to-OSObject-pointer
John McCalled433932011-01-25 03:31:58 +00005087 break;
5088 }
5089
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005090 if (!TypeOK) {
5091 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00005092 return;
5093
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005094 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005095 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005096 << AL << ParmDiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005097 } else {
5098 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
5099 enum : unsigned {
5100 Function,
5101 Method,
5102 Property
5103 } SubjectKind = Function;
5104 if (isa<ObjCMethodDecl>(D))
5105 SubjectKind = Method;
5106 else if (isa<ObjCPropertyDecl>(D))
5107 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005108 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005109 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005110 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005111 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00005112 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005113
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005114 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005115 default:
David Blaikie83d382b2011-09-23 05:06:16 +00005116 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005117 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005118 handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL);
John McCalled433932011-01-25 03:31:58 +00005119 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005120 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005121 handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005122 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005123 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005124 handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005125 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005126 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005127 handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005128 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005129 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005130 handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL);
5131 return;
5132 case ParsedAttr::AT_OSReturnsRetained:
5133 handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL);
5134 return;
5135 case ParsedAttr::AT_OSReturnsNotRetained:
5136 handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005137 return;
5138 };
5139}
5140
John McCallcf166702011-07-22 08:53:00 +00005141static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005142 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00005143 const int EP_ObjCMethod = 1;
5144 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00005145
Erich Keaneb11ebc52017-09-27 03:20:13 +00005146 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005147 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005148 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00005149 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005150 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005151 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00005152
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00005153 if (!resultType->isReferenceType() &&
5154 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005155 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005156 << SourceRange(loc) << Attrs
5157 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
5158 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00005159
5160 // Drop the attribute.
5161 return;
5162 }
5163
Nico Weber462fd1e2015-01-07 23:50:05 +00005164 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00005165 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00005166}
5167
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005168static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005169 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005170 const auto *Method = cast<ObjCMethodDecl>(D);
5171
5172 const DeclContext *DC = Method->getDeclContext();
5173 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005174 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005175 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005176 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
5177 return;
5178 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005179 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005180 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005181 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005182 return;
5183 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005184
5185 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
5186 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005187}
5188
Erich Keanee891aa92018-07-13 15:07:47 +00005189static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005190 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00005191
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005192 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005193 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005194 return;
5195 }
John McCall28592582015-02-01 22:34:06 +00005196
5197 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005198 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00005199 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005200 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00005201 return;
5202 }
5203
5204 // Only allow 'cv void *'.
5205 QualType T = TD->getUnderlyingType();
5206 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005207 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00005208 return;
5209 }
5210 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005211
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005212 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005213 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
5214 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005215}
5216
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005217static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005218 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005219 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00005220
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005221 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005222 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005223 return;
5224 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005225
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005226 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005227 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
5228 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005229}
5230
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005231static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005232 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005233 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005234 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005235 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005236 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005237 return;
5238 }
5239 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005240 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005241 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005242 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005243 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005244 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005245 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005246 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005247}
5248
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005249static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005250 const ParsedAttr &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005251 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005252 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005253 IFace = CatDecl->getClassInterface();
5254 else
5255 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005256
5257 if (!IFace)
5258 return;
5259
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00005260 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00005261 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005262 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
5263 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005264}
5265
Erich Keanee891aa92018-07-13 15:07:47 +00005266static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005267 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005268 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005269 return;
5270 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005271 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005272 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005273 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005274}
5275
Nico Webera6916892016-06-10 18:53:04 +00005276// When a user wants to use objc_boxable with a union or struct
5277// but they don't have access to the declaration (legacy/third-party code)
5278// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00005279// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00005280static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00005281 bool notify = false;
5282
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005283 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00005284 if (RD && RD->getDefinition()) {
5285 RD = RD->getDefinition();
5286 notify = true;
5287 }
5288
5289 if (RD) {
5290 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005291 ObjCBoxableAttr(AL.getRange(), S.Context,
5292 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00005293 RD->addAttr(BoxableAttr);
5294 if (notify) {
5295 // we need to notify ASTReader/ASTWriter about
5296 // modification of existing declaration
5297 if (ASTMutationListener *L = S.getASTMutationListener())
5298 L->AddedAttributeToRecord(BoxableAttr, RD);
5299 }
5300 }
5301}
5302
Erich Keanee891aa92018-07-13 15:07:47 +00005303static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005304 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005305
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005306 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005307 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005308}
5309
Chandler Carruthedc2c642011-07-02 00:01:44 +00005310static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005311 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005312 const auto *VD = cast<ValueDecl>(D);
5313 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005314
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005315 if (!QT->isDependentType() &&
5316 !QT->isObjCLifetimeType()) {
5317 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5318 << QT;
John McCall31168b02011-06-15 23:02:42 +00005319 return;
5320 }
5321
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005322 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005323
5324 // If we have no lifetime yet, check the lifetime we're presumably
5325 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005326 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5327 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005328
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005329 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005330 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005331 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005332 "didn't infer lifetime for non-dependent type?");
5333 break;
5334
5335 case Qualifiers::OCL_Weak: // meaningful
5336 case Qualifiers::OCL_Strong: // meaningful
5337 break;
5338
5339 case Qualifiers::OCL_ExplicitNone:
5340 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005341 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5342 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005343 break;
5344 }
5345
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005346 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005347 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
5348 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00005349}
5350
Francois Picheta83957a2010-12-19 06:50:37 +00005351//===----------------------------------------------------------------------===//
5352// Microsoft specific attribute handlers.
5353//===----------------------------------------------------------------------===//
5354
Nico Weber88f5ed92016-09-13 18:55:26 +00005355UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
5356 unsigned AttrSpellingListIndex, StringRef Uuid) {
5357 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005358 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005359 return nullptr;
5360 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5361 Diag(Range.getBegin(), diag::note_previous_uuid);
5362 D->dropAttr<UuidAttr>();
5363 }
5364
5365 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5366}
5367
Erich Keanee891aa92018-07-13 15:07:47 +00005368static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005369 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005370 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005371 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005372 return;
5373 }
5374
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005375 StringRef StrRef;
5376 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005377 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005378 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005379
David Majnemer89085342013-08-09 08:56:20 +00005380 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5381 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005382 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5383 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005384
Reid Kleckner140c4a72013-05-17 14:04:52 +00005385 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005386 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005387 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005388 return;
5389 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005390
David Majnemer89085342013-08-09 08:56:20 +00005391 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005392 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005393 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005394 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005395 return;
5396 }
David Majnemer89085342013-08-09 08:56:20 +00005397 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005398 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005399 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005400 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005401 }
Francois Picheta83957a2010-12-19 06:50:37 +00005402
Nico Weber469891e2017-05-05 17:05:56 +00005403 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5404 // the only thing in the [] list, the [] too), and add an insertion of
5405 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5406 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005407 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005408 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005409 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5410 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005411
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005412 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5413 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005414 if (UA)
5415 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005416}
5417
Erich Keanee891aa92018-07-13 15:07:47 +00005418static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005419 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005420 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005421 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005422 return;
5423 }
5424 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005425 D, AL.getRange(), /*BestCase=*/true,
5426 AL.getAttributeSpellingListIndex(),
5427 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005428 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005429 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005430 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5431 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005432}
5433
Erich Keanee891aa92018-07-13 15:07:47 +00005434static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005435 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005436 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005437 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005438 return;
5439 }
5440 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005441 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005442 return;
5443 }
5444 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005445 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005446 return;
5447 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005448 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5449 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005450}
5451
Erich Keanee891aa92018-07-13 15:07:47 +00005452static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005453 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005454 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005455 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005456 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005457 return;
5458 Tags.push_back(Tag);
5459 }
5460
5461 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5462 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005463 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005464 return;
5465 }
5466 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005467 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005468 return;
5469 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005470 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005471 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005472 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005473 return;
5474
5475 // Store tags sorted and without duplicates.
Fangrui Song55fab262018-09-26 22:16:28 +00005476 llvm::sort(Tags);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005477 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5478
5479 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005480 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5481 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005482}
5483
Erich Keanee891aa92018-07-13 15:07:47 +00005484static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005485 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005486 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005487 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005488 return;
5489 }
5490
5491 StringRef Str;
5492 SourceLocation ArgLoc;
5493
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005494 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005495 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005496 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005497 return;
5498
5499 ARMInterruptAttr::InterruptType Kind;
5500 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005501 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5502 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005503 return;
5504 }
5505
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005506 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005507 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005508 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005509}
5510
Erich Keanee891aa92018-07-13 15:07:47 +00005511static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005512 // MSP430 'interrupt' attribute is applied to
5513 // a function with no parameters and void return type.
5514 if (!isFunctionOrMethod(D)) {
5515 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5516 << "'interrupt'" << ExpectedFunctionOrMethod;
5517 return;
5518 }
5519
5520 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005521 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5522 << /*MSP430*/ 1 << 0;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005523 return;
5524 }
5525
5526 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005527 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5528 << /*MSP430*/ 1 << 1;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005529 return;
5530 }
5531
5532 // The attribute takes one integer argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005533 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005534 return;
5535
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005536 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005537 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5538 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005539 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005540 }
5541
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005542 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005543 llvm::APSInt NumParams(32);
5544 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005545 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005546 << AL << AANT_ArgumentIntegerConstant
5547 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005548 return;
5549 }
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005550 // The argument should be in range 0..63.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005551 unsigned Num = NumParams.getLimitedValue(255);
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005552 if (Num > 63) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005553 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005554 << AL << (int)NumParams.getSExtValue()
5555 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005556 return;
5557 }
5558
Aaron Ballman36a53502014-01-16 13:03:14 +00005559 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005560 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5561 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005562 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005563}
5564
Erich Keanee891aa92018-07-13 15:07:47 +00005565static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005566 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005567 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005568 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005569 return;
5570 }
5571
5572 StringRef Str;
5573 SourceLocation ArgLoc;
5574
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005575 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005576 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005577 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005578 return;
5579
5580 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5581 // a) Must be a function.
5582 // b) Must have no parameters.
5583 // c) Must have the 'void' return type.
5584 // d) Cannot have the 'mips16' attribute, as that instruction set
5585 // lacks the 'eret' instruction.
5586 // e) The attribute itself must either have no argument or one of the
5587 // valid interrupt types, see [MipsInterruptDocs].
5588
5589 if (!isFunctionOrMethod(D)) {
5590 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5591 << "'interrupt'" << ExpectedFunctionOrMethod;
5592 return;
5593 }
5594
5595 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005596 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5597 << /*MIPS*/ 0 << 0;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005598 return;
5599 }
5600
5601 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005602 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5603 << /*MIPS*/ 0 << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005604 return;
5605 }
5606
Erich Keane44bacdf2018-08-09 13:21:32 +00005607 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005608 return;
5609
5610 MipsInterruptAttr::InterruptType Kind;
5611 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005612 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005613 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005614 return;
5615 }
5616
5617 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005618 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005619}
5620
Erich Keanee891aa92018-07-13 15:07:47 +00005621static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005622 // Semantic checks for a function with the 'interrupt' attribute.
5623 // a) Must be a function.
5624 // b) Must have the 'void' return type.
5625 // c) Must take 1 or 2 arguments.
5626 // d) The 1st argument must be a pointer.
5627 // e) The 2nd argument (if any) must be an unsigned integer.
5628 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5629 CXXMethodDecl::isStaticOverloadedOperator(
5630 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005631 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005632 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005633 return;
5634 }
5635 // Interrupt handler must have void return type.
5636 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5637 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5638 diag::err_anyx86_interrupt_attribute)
5639 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5640 ? 0
5641 : 1)
5642 << 0;
5643 return;
5644 }
5645 // Interrupt handler must have 1 or 2 parameters.
5646 unsigned NumParams = getFunctionOrMethodNumParams(D);
5647 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005648 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005649 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5650 ? 0
5651 : 1)
5652 << 1;
5653 return;
5654 }
5655 // The first argument must be a pointer.
5656 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5657 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5658 diag::err_anyx86_interrupt_attribute)
5659 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5660 ? 0
5661 : 1)
5662 << 2;
5663 return;
5664 }
5665 // The second argument, if present, must be an unsigned integer.
5666 unsigned TypeSize =
5667 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5668 ? 64
5669 : 32;
5670 if (NumParams == 2 &&
5671 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5672 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5673 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5674 diag::err_anyx86_interrupt_attribute)
5675 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5676 ? 0
5677 : 1)
5678 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5679 return;
5680 }
5681 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005682 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005683 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5684}
5685
Erich Keanee891aa92018-07-13 15:07:47 +00005686static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005687 if (!isFunctionOrMethod(D)) {
5688 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5689 << "'interrupt'" << ExpectedFunction;
5690 return;
5691 }
5692
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005693 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005694 return;
5695
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005696 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005697}
5698
Erich Keanee891aa92018-07-13 15:07:47 +00005699static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005700 if (!isFunctionOrMethod(D)) {
5701 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5702 << "'signal'" << ExpectedFunction;
5703 return;
5704 }
5705
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005706 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005707 return;
5708
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005709 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005710}
5711
Dan Gohmanb4323692019-01-24 21:08:30 +00005712static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5713 if (!isFunctionOrMethod(D)) {
5714 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5715 << "'import_module'" << ExpectedFunction;
5716 return;
5717 }
5718
5719 auto *FD = cast<FunctionDecl>(D);
5720 if (FD->isThisDeclarationADefinition()) {
5721 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5722 return;
5723 }
5724
5725 StringRef Str;
5726 SourceLocation ArgLoc;
5727 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5728 return;
5729
5730 FD->addAttr(::new (S.Context) WebAssemblyImportModuleAttr(
5731 AL.getRange(), S.Context, Str,
5732 AL.getAttributeSpellingListIndex()));
5733}
Ana Pazos1eee1b72018-07-26 17:37:45 +00005734
Dan Gohmancae84592019-02-01 22:25:23 +00005735static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5736 if (!isFunctionOrMethod(D)) {
5737 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5738 << "'import_name'" << ExpectedFunction;
5739 return;
5740 }
5741
5742 auto *FD = cast<FunctionDecl>(D);
5743 if (FD->isThisDeclarationADefinition()) {
5744 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5745 return;
5746 }
5747
5748 StringRef Str;
5749 SourceLocation ArgLoc;
5750 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5751 return;
5752
5753 FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr(
5754 AL.getRange(), S.Context, Str,
5755 AL.getAttributeSpellingListIndex()));
5756}
5757
Ana Pazos1eee1b72018-07-26 17:37:45 +00005758static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5759 const ParsedAttr &AL) {
5760 // Warn about repeated attributes.
5761 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5762 S.Diag(AL.getRange().getBegin(),
5763 diag::warn_riscv_repeated_interrupt_attribute);
5764 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5765 return;
5766 }
5767
5768 // Check the attribute argument. Argument is optional.
5769 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5770 return;
5771
5772 StringRef Str;
5773 SourceLocation ArgLoc;
5774
5775 // 'machine'is the default interrupt mode.
5776 if (AL.getNumArgs() == 0)
5777 Str = "machine";
5778 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5779 return;
5780
5781 // Semantic checks for a function with the 'interrupt' attribute:
5782 // - Must be a function.
5783 // - Must have no parameters.
5784 // - Must have the 'void' return type.
5785 // - The attribute itself must either have no argument or one of the
5786 // valid interrupt types, see [RISCVInterruptDocs].
5787
5788 if (D->getFunctionType() == nullptr) {
5789 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5790 << "'interrupt'" << ExpectedFunction;
5791 return;
5792 }
5793
5794 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005795 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5796 << /*RISC-V*/ 2 << 0;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005797 return;
5798 }
5799
5800 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005801 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5802 << /*RISC-V*/ 2 << 1;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005803 return;
5804 }
5805
5806 RISCVInterruptAttr::InterruptType Kind;
5807 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005808 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5809 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005810 return;
5811 }
5812
5813 D->addAttr(::new (S.Context) RISCVInterruptAttr(
5814 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
5815}
5816
Erich Keanee891aa92018-07-13 15:07:47 +00005817static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005818 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005819 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5820 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005821 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005822 break;
5823 case llvm::Triple::mipsel:
5824 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005825 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005826 break;
5827 case llvm::Triple::x86:
5828 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005829 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005830 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005831 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005832 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005833 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005834 case llvm::Triple::riscv32:
5835 case llvm::Triple::riscv64:
5836 handleRISCVInterruptAttr(S, D, AL);
5837 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005838 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005839 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005840 break;
5841 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005842}
5843
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005844static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005845 const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005846 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005847 Expr *MinExpr = AL.getArgAsExpr(0);
5848 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005849 return;
5850
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005851 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005852 Expr *MaxExpr = AL.getArgAsExpr(1);
5853 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005854 return;
5855
5856 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005857 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005858 return;
5859 }
5860 if (Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005861 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005862 return;
5863 }
5864
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005865 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005866 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5867 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005868}
5869
Erich Keanee891aa92018-07-13 15:07:47 +00005870static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005871 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005872 Expr *MinExpr = AL.getArgAsExpr(0);
5873 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005874 return;
5875
5876 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005877 if (AL.getNumArgs() == 2) {
5878 Expr *MaxExpr = AL.getArgAsExpr(1);
5879 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005880 return;
5881 }
5882
5883 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005884 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005885 return;
5886 }
5887 if (Max != 0 && Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005888 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005889 return;
5890 }
5891
5892 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005893 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5894 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005895}
5896
Erich Keanee891aa92018-07-13 15:07:47 +00005897static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005898 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005899 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5900 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005901 return;
5902
5903 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005904 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5905 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005906}
5907
Erich Keanee891aa92018-07-13 15:07:47 +00005908static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005909 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005910 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5911 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005912 return;
5913
5914 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005915 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5916 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005917}
5918
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005919static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005920 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005921 // If we try to apply it to a function pointer, don't warn, but don't
5922 // do anything, either. It doesn't matter anyway, because there's nothing
5923 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005924 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005925 if (VD && VD->getType()->isFunctionPointerType())
5926 return;
5927 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005928 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005929 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5930 TD->getUnderlyingType()->isFunctionType()))
5931 return;
5932 // Attribute can only be applied to function types.
5933 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005934 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005935 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005936 return;
5937 }
5938
Aaron Ballman36a53502014-01-16 13:03:14 +00005939 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005940 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5941 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005942}
5943
Erich Keanee891aa92018-07-13 15:07:47 +00005944static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005945 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005946 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5947 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005948 return;
5949
5950 // TODO: Investigate what happens with the next major version of MSVC.
Reid Kleckner1a94d872018-12-17 23:16:43 +00005951 if (Version != LangOptions::MSVC2015 / 100) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005952 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005953 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005954 return;
5955 }
5956
Reid Kleckner1a94d872018-12-17 23:16:43 +00005957 // The attribute expects a "major" version number like 19, but new versions of
5958 // MSVC have moved to updating the "minor", or less significant numbers, so we
5959 // have to multiply by 100 now.
5960 Version *= 100;
5961
David Majnemercd3ebfe2016-05-23 17:16:12 +00005962 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005963 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5964 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005965}
5966
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005967DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5968 unsigned AttrSpellingListIndex) {
5969 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005970 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005971 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005972 }
5973
5974 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005975 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005976
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005977 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005978}
5979
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005980DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5981 unsigned AttrSpellingListIndex) {
5982 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005983 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005984 D->dropAttr<DLLImportAttr>();
5985 }
5986
5987 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005988 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005989
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005990 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005991}
5992
Erich Keanee891aa92018-07-13 15:07:47 +00005993static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005994 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5995 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005996 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00005997 return;
5998 }
5999
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006000 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00006001 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00006002 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
6003 // MinGW doesn't allow dllimport on inline functions.
6004 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00006005 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00006006 return;
6007 }
6008 }
6009
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006010 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00006011 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
6012 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006013 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00006014 return;
6015 }
6016 }
6017
Hans Wennborge82f19c2014-06-24 23:57:05 +00006018 unsigned Index = A.getAttributeSpellingListIndex();
Erich Keanee891aa92018-07-13 15:07:47 +00006019 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Hans Wennborge82f19c2014-06-24 23:57:05 +00006020 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
6021 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006022 if (NewAttr)
6023 D->addAttr(NewAttr);
6024}
6025
David Majnemer2c4e00a2014-01-29 22:07:36 +00006026MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00006027Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00006028 unsigned AttrSpellingListIndex,
6029 MSInheritanceAttr::Spelling SemanticSpelling) {
6030 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
6031 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00006032 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006033 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
6034 << 1 /*previous declaration*/;
6035 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
6036 D->dropAttr<MSInheritanceAttr>();
6037 }
6038
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006039 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006040 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00006041 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
6042 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006043 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006044 }
6045 } else {
6046 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
6047 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
6048 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006049 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006050 }
6051 if (RD->getDescribedClassTemplate()) {
6052 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
6053 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006054 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006055 }
6056 }
6057
6058 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00006059 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006060}
6061
Erich Keanee891aa92018-07-13 15:07:47 +00006062static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006063 // The capability attributes take a single string parameter for the name of
6064 // the capability they represent. The lockable attribute does not take any
6065 // parameters. However, semantically, both attributes represent the same
6066 // concept, and so they use the same semantic attribute. Eventually, the
6067 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00006068 //
Alp Toker958027b2014-07-14 19:42:55 +00006069 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00006070 // literal will be considered a "mutex."
6071 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006072 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00006073 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006074 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006075 return;
6076
Aaron Ballman6c810072014-03-05 21:47:13 +00006077 // Currently, there are only two names allowed for a capability: role and
6078 // mutex (case insensitive). Diagnose other capability names.
6079 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
6080 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
6081
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006082 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
6083 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006084}
6085
Erich Keanee891aa92018-07-13 15:07:47 +00006086static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00006087 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006088 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00006089 return;
6090
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006091 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00006092 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006093 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006094}
6095
6096static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006097 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006098 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006099 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006100 return;
6101
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006102 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006103 S.Context,
6104 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006105 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006106}
6107
6108static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006109 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006110 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006111 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006112 return;
6113
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006114 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006115 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006116 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006117 Args.data(),
6118 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006119 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006120}
6121
6122static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006123 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006124 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00006125 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006126 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006127
Aaron Ballman18d85ae2014-03-20 16:02:49 +00006128 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006129 AL.getRange(), S.Context, Args.data(), Args.size(),
6130 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006131}
6132
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006133static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006134 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006135 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006136 return;
6137
6138 // check that all arguments are lockable objects
6139 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006140 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006141 if (Args.empty())
6142 return;
6143
6144 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006145 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
6146 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006147
6148 D->addAttr(RCA);
6149}
6150
Erich Keanee891aa92018-07-13 15:07:47 +00006151static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006152 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00006153 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006154 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00006155 // Do not want to attach the attribute to the namespace because that will
6156 // cause confusing diagnostic reports for uses of declarations within the
6157 // namespace.
6158 return;
6159 }
6160 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00006161
Manman Renc7890fe2016-03-16 18:50:49 +00006162 // Handle the cases where the attribute has a text message.
6163 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006164 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
6165 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00006166 return;
6167
6168 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006169 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
6170 checkAttributeAtMostNumArgs(S, AL, 1);
6171 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
Sander de Smalen44a22532018-11-26 16:38:37 +00006172 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
6173 return;
6174
6175 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
6176 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
6177
6178 D->addAttr(::new (S.Context)
6179 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006180 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006181}
6182
6183static bool isGlobalVar(const Decl *D) {
6184 if (const auto *S = dyn_cast<VarDecl>(D))
6185 return S->hasGlobalStorage();
6186 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00006187}
6188
Erich Keanee891aa92018-07-13 15:07:47 +00006189static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006190 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00006191 return;
6192
Benjamin Kramer1b582012016-02-13 18:11:49 +00006193 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00006194
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006195 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00006196 StringRef SanitizerName;
6197 SourceLocation LiteralLoc;
6198
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006199 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00006200 return;
6201
6202 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
6203 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006204 else if (isGlobalVar(D) && SanitizerName != "address")
6205 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006206 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00006207 Sanitizers.push_back(SanitizerName);
6208 }
6209
6210 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006211 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
6212 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00006213}
6214
6215static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006216 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006217 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00006218 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006219 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
6220 .Case("no_address_safety_analysis", "address")
6221 .Case("no_sanitize_address", "address")
6222 .Case("no_sanitize_thread", "thread")
6223 .Case("no_sanitize_memory", "memory");
6224 if (isGlobalVar(D) && SanitizerName != "address")
6225 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006226 << AL << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00006227 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006228 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
6229 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00006230}
6231
Erich Keanee891aa92018-07-13 15:07:47 +00006232static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006233 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006234 D->addAttr(Internal);
6235}
6236
Erich Keanee891aa92018-07-13 15:07:47 +00006237static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006238 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006239 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00006240 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006241 else
Erich Keane44bacdf2018-08-09 13:21:32 +00006242 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
6243 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006244}
6245
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006246/// Handles semantic checking for features that are common to all attributes,
6247/// such as checking whether a parameter was properly specified, or the correct
6248/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006249static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006250 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006251 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00006252 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006253 //
6254 // We also bail on unknown and ignored attributes because those are handled
6255 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00006256 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006257 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006258 // Check whether the attribute requires specific language extensions to be
6259 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006260 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00006261 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00006262 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006263 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00006264 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006265 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00006266 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006267
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006268 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006269 // If there are no optional arguments, then checking for the argument count
6270 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006271 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006272 return true;
6273 } else {
6274 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006275 if (AL.getMinArgs() &&
6276 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006277 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006278 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
6279 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006280 return true;
6281 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00006282
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006283 if (S.CheckAttrTarget(AL))
6284 return true;
6285
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006286 return false;
6287}
6288
Erich Keanee891aa92018-07-13 15:07:47 +00006289static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006290 if (D->isInvalidDecl())
6291 return;
6292
6293 // Check if there is only one access qualifier.
6294 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006295 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
6296 AL.getSemanticSpelling()) {
6297 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
6298 << AL.getName()->getName() << AL.getRange();
6299 } else {
6300 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
6301 << D->getSourceRange();
6302 D->setInvalidDecl(true);
6303 return;
6304 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00006305 }
6306
6307 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
6308 // image object can be read and written.
6309 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
6310 // object. Using the read_write (or __read_write) qualifier with the pipe
6311 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006312 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006313 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006314 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006315 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006316 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00006317 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00006318 D->setInvalidDecl(true);
6319 return;
6320 }
6321 }
6322 }
6323
6324 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006325 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00006326}
6327
Erik Pilkington5a559e62018-08-21 17:24:06 +00006328static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006329 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00006330 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
6331 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
6332 return;
6333 }
6334
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006335 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00006336 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006337 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00006338 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00006339}
6340
JF Bastien14daa202018-12-18 05:12:21 +00006341static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6342 assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic &&
6343 "uninitialized is only valid on automatic duration variables");
6344 unsigned Index = AL.getAttributeSpellingListIndex();
6345 D->addAttr(::new (S.Context)
6346 UninitializedAttr(AL.getLoc(), S.Context, Index));
6347}
6348
Erik Pilkington1e368822019-01-04 18:33:06 +00006349static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD,
6350 bool DiagnoseFailure) {
6351 QualType Ty = VD->getType();
6352 if (!Ty->isObjCRetainableType()) {
6353 if (DiagnoseFailure) {
6354 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6355 << 0;
6356 }
6357 return false;
6358 }
6359
6360 Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime();
6361
6362 // Sema::inferObjCARCLifetime must run after processing decl attributes
6363 // (because __block lowers to an attribute), so if the lifetime hasn't been
6364 // explicitly specified, infer it locally now.
6365 if (LifetimeQual == Qualifiers::OCL_None)
6366 LifetimeQual = Ty->getObjCARCImplicitLifetime();
6367
6368 // The attributes only really makes sense for __strong variables; ignore any
6369 // attempts to annotate a parameter with any other lifetime qualifier.
6370 if (LifetimeQual != Qualifiers::OCL_Strong) {
6371 if (DiagnoseFailure) {
6372 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6373 << 1;
6374 }
6375 return false;
6376 }
6377
6378 // Tampering with the type of a VarDecl here is a bit of a hack, but we need
6379 // to ensure that the variable is 'const' so that we can error on
6380 // modification, which can otherwise over-release.
6381 VD->setType(Ty.withConst());
6382 VD->setARCPseudoStrong(true);
6383 return true;
6384}
6385
6386static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D,
6387 const ParsedAttr &AL) {
6388 if (auto *VD = dyn_cast<VarDecl>(D)) {
6389 assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically");
6390 if (!VD->hasLocalStorage()) {
6391 S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6392 << 0;
6393 return;
6394 }
6395
6396 if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true))
6397 return;
6398
6399 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6400 return;
6401 }
6402
6403 // If D is a function-like declaration (method, block, or function), then we
6404 // make every parameter psuedo-strong.
6405 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); I != E; ++I) {
6406 auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I));
6407 QualType Ty = PVD->getType();
6408
6409 // If a user wrote a parameter with __strong explicitly, then assume they
6410 // want "real" strong semantics for that parameter. This works because if
6411 // the parameter was written with __strong, then the strong qualifier will
6412 // be non-local.
6413 if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() ==
6414 Qualifiers::OCL_Strong)
6415 continue;
6416
6417 tryMakeVariablePseudoStrong(S, PVD, /*DiagnoseFailure=*/false);
6418 }
6419 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6420}
6421
Erik Pilkingtone3cd7352019-02-11 23:21:39 +00006422static void handleFortifyStdLib(Sema &S, Decl *D, const ParsedAttr &AL) {
6423 auto *FD = cast<FunctionDecl>(D);
6424 unsigned VariantID = Builtin::getFortifiedVariantFunction(FD->getBuiltinID());
6425 if (VariantID == 0) {
6426 S.Diag(D->getLocation(), diag::err_fortify_std_lib_bad_decl);
6427 return;
6428 }
6429
6430 uint32_t BOSType, Flag;
6431 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), BOSType, 0, true) ||
6432 !checkUInt32Argument(S, AL, AL.getArgAsExpr(1), Flag, 1, true))
6433 return;
6434
6435 if (BOSType > 3) {
6436 S.Diag(AL.getArgAsExpr(0)->getBeginLoc(),
6437 diag::err_attribute_argument_outof_range)
6438 << AL << 0 << 3;
6439 return;
6440 }
6441
6442 D->addAttr(::new (S.getASTContext()) FortifyStdLibAttr(
6443 AL.getLoc(), S.getASTContext(), BOSType, Flag,
6444 AL.getAttributeSpellingListIndex()));
6445}
6446
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00006447//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006448// Top Level Sema Entry Points
6449//===----------------------------------------------------------------------===//
6450
Richard Smithf8a75c32013-08-29 00:47:48 +00006451/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
6452/// the attribute applies to decls. If the attribute is a type attribute, just
6453/// silently ignore it if a GNU attribute.
6454static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006455 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00006456 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00006457 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00006458 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00006459
Richard Smithf8a75c32013-08-29 00:47:48 +00006460 // Ignore C++11 attributes on declarator chunks: they appertain to the type
6461 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006462 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00006463 return;
6464
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006465 // Unknown attributes are automatically warned on. Target-specific attributes
6466 // which do not apply to the current target architecture are treated as
6467 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006468 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006469 !AL.existsInTarget(S.Context.getTargetInfo())) {
Simon Pilgrimfc0ff612018-12-17 12:17:37 +00006470 S.Diag(AL.getLoc(),
6471 AL.isDeclspecAttribute()
6472 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
6473 : (unsigned)diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006474 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006475 return;
6476 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006477
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006478 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006479 return;
6480
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006481 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006482 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006483 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00006484 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006485 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00006486 break;
6487 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006488 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00006489 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006490 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006491 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006492 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006493 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006494 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006495 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006496 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006497 case ParsedAttr::AT_DLLExport:
6498 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006499 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006500 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006501 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006502 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006503 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006504 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006505 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006506 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006507 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006508 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006509 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006510 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006511 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006512 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006513 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006514 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006515 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006516 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006517 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006518 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006519 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006520 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006521 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006522 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006523 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006524 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006525 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006526 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006527 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006528 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006529 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006530 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006531 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006532 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006533 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006534 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006535 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006536 break;
Dan Gohmanb4323692019-01-24 21:08:30 +00006537 case ParsedAttr::AT_WebAssemblyImportModule:
6538 handleWebAssemblyImportModuleAttr(S, D, AL);
6539 break;
Dan Gohmancae84592019-02-01 22:25:23 +00006540 case ParsedAttr::AT_WebAssemblyImportName:
6541 handleWebAssemblyImportNameAttr(S, D, AL);
6542 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006543 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006544 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006545 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006546 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006547 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006548 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006549 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006550 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006551 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006552 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006553 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006554 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006555 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006556 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006557 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006558 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006559 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006560 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006561 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006562 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006563 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006564 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006565 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006566 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006567 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006568 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006569 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006570 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006571 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006572 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006573 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006574 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006575 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006576 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006577 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006578 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006579 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006580 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006581 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006582 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006583 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006584 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006585 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006586 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006587 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006588 case ParsedAttr::AT_CPUDispatch:
6589 case ParsedAttr::AT_CPUSpecific:
6590 handleCPUSpecificAttr(S, D, AL);
6591 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006592 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006593 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006594 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006595 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006596 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006597 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006598 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006599 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006600 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006601 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006602 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006603 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006604 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006605 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006606 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006607 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006608 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006609 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006610 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006611 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006612 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006613 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006614 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006615 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006616 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006617 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006618 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006619 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006620 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006621 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006622 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006623 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006624 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006625 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006626 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006627 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006628 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006629 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006630 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006631 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006632 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006633 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006634 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006635 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006636 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006637 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006638 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006639 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006640 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006641 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006642 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006643 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006644 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006645 break;
Johannes Doerfertac991bb2019-01-19 05:36:54 +00006646 case ParsedAttr::AT_Callback:
6647 handleCallbackAttr(S, D, AL);
6648 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006649 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006650 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006651 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006652 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006653 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006654 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006655 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006656 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006657 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006658 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006659 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006660 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006661 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006662 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006663 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006664 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006665 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006666 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006667 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006668 case ParsedAttr::AT_LifetimeBound:
6669 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6670 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006671 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006672 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006673 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006674 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006675 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006676 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006677 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006678 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006679 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006680 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006681 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006682 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006683 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006684 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006685 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006686 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006687 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6688 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006689 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006690 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006691 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006692 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006693 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006694 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006695 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006696 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006697 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006698 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006699 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006700 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006701 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006702 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006703 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006704 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006705 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006706 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006707 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006708 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006709 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006710 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006711 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006712 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006713 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006714 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006715 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006716 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006717 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006718 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006719 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006720 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006721 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006722 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006723 handleNoCfCheckAttr(S, D, AL);
6724 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006725 case ParsedAttr::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006726 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006727 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006728 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006729 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006730 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006731 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006732 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006733 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006734 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006735 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006736 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006737 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006738 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006739 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006740 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006741 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006742 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006743 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006744 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006745 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006746 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006747 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006748 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006749 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006750 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006751 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006752 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006753 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006754 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006755 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006756 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006757 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006758 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006759 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006760 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006761 case ParsedAttr::AT_ObjCRuntimeVisible:
6762 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6763 break;
6764 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006765 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006766 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006767 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006768 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6769 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006770 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006771 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006772 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6773 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006774 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006775 case ParsedAttr::AT_CFConsumed:
6776 case ParsedAttr::AT_NSConsumed:
George Karpenkov1657f362018-11-30 02:18:37 +00006777 case ParsedAttr::AT_OSConsumed:
6778 S.AddXConsumedAttr(D, AL.getRange(), AL.getAttributeSpellingListIndex(),
6779 parsedAttrToRetainOwnershipKind(AL),
6780 /*IsTemplateInstantiation=*/false);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006781 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006782 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006783 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006784 break;
George Karpenkovda2c77f2018-12-06 22:06:59 +00006785 case ParsedAttr::AT_OSConsumesThis:
6786 handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL);
6787 break;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00006788 case ParsedAttr::AT_OSReturnsRetainedOnZero:
6789 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnZeroAttr>(
6790 S, D, AL, isValidOSObjectOutParameter(D),
6791 diag::warn_ns_attribute_wrong_parameter_type,
6792 /*Extra Args=*/AL, /*pointer-to-OSObject-pointer*/ 3, AL.getRange());
6793 break;
6794 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
6795 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnNonZeroAttr>(
6796 S, D, AL, isValidOSObjectOutParameter(D),
6797 diag::warn_ns_attribute_wrong_parameter_type,
6798 /*Extra Args=*/AL, /*pointer-to-OSObject-poointer*/ 3, AL.getRange());
6799 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006800 case ParsedAttr::AT_NSReturnsAutoreleased:
6801 case ParsedAttr::AT_NSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006802 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006803 case ParsedAttr::AT_CFReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006804 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006805 case ParsedAttr::AT_OSReturnsNotRetained:
6806 case ParsedAttr::AT_OSReturnsRetained:
6807 handleXReturnsXRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006808 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006809 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006810 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006811 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006812 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006813 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006814 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006815 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006816 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006817 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006818 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006819 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006820 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006821 case ParsedAttr::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006822 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006823 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006824 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006825 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006826 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006827 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006828 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006829 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006830 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006831 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006832 break;
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006833 case ParsedAttr::AT_SpeculativeLoadHardening:
Zola Bridges826ef592019-01-18 17:20:46 +00006834 handleSimpleAttributeWithExclusions<SpeculativeLoadHardeningAttr,
6835 NoSpeculativeLoadHardeningAttr>(S, D,
6836 AL);
6837 break;
6838 case ParsedAttr::AT_NoSpeculativeLoadHardening:
6839 handleSimpleAttributeWithExclusions<NoSpeculativeLoadHardeningAttr,
6840 SpeculativeLoadHardeningAttr>(S, D, AL);
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006841 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006842 case ParsedAttr::AT_CodeSeg:
6843 handleCodeSegAttr(S, D, AL);
6844 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006845 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006846 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006847 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006848 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006849 handleMinVectorWidthAttr(S, D, AL);
6850 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006851 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006852 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006853 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006854 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006855 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006856 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006857 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006858 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006859 break;
Joe Danielsf7393d22019-02-04 23:32:55 +00006860 case ParsedAttr::AT_ObjCNonLazyClass:
6861 handleSimpleAttribute<ObjCNonLazyClassAttr>(S, D, AL);
6862 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006863 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006864 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006865 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006866 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006867 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006868 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006869 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006870 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006871 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006872 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006873 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006874 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006875 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006876 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006877 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006878 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006879 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6880 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006881 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006882 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00006883 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
6884 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006885 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006886 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006887 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006888 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006889 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006890 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006891 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006892 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006893 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006894 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006895 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006896 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006897 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006898 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006899 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006900 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006901 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006902 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006903 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006904 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006905 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006906 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006907 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006908 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006909 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006910 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006911 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006912 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006913 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006914 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006915 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006916 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006917 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006918 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006919 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006920 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006921 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006922 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006923 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006924 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006925 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006926 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006927 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006928 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006929 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006930 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006931 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006932 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006933 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006934 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006935 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006936 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006937 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006938 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006939 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006940 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006941 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006942 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006943 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006944 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006945 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006946 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006947 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006948 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006949 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006950 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006951 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006952 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006953 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006954 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006955 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00006956 // Interacts with -fstack-protector options.
6957 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
6958 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006959 case ParsedAttr::AT_StdCall:
6960 case ParsedAttr::AT_CDecl:
6961 case ParsedAttr::AT_FastCall:
6962 case ParsedAttr::AT_ThisCall:
6963 case ParsedAttr::AT_Pascal:
6964 case ParsedAttr::AT_RegCall:
6965 case ParsedAttr::AT_SwiftCall:
6966 case ParsedAttr::AT_VectorCall:
6967 case ParsedAttr::AT_MSABI:
6968 case ParsedAttr::AT_SysVABI:
6969 case ParsedAttr::AT_Pcs:
6970 case ParsedAttr::AT_IntelOclBicc:
6971 case ParsedAttr::AT_PreserveMost:
6972 case ParsedAttr::AT_PreserveAll:
Sander de Smalen44a22532018-11-26 16:38:37 +00006973 case ParsedAttr::AT_AArch64VectorPcs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006974 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006975 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006976 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006977 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006978 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006979 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006980 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006981 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006982 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006983 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006984 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006985 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006986 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006987 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006988 case ParsedAttr::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006989 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006990 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006991 case ParsedAttr::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006992 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006993 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006994 case ParsedAttr::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006995 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006996 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006997 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006998 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006999 break;
Louis Dionned2695792018-10-04 15:49:42 +00007000 case ParsedAttr::AT_ExcludeFromExplicitInstantiation:
7001 handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL);
7002 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007003 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007004 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00007005 break;
John McCall8d32c052012-05-22 21:28:12 +00007006
7007 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007008 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007009 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007010 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007011 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007012 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007013 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007014 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007015 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00007016 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007017 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007018 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00007019 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007020 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007021 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00007022 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007023 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007024 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00007025 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007026 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007027 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007028 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007029 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007030 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007031 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007032 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007033 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00007034 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00007035
Erich Keanee891aa92018-07-13 15:07:47 +00007036 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007037 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00007038 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007039
7040 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007041 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007042 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007043 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007044 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007045 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007046 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007047 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007048 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007049 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007050 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007051 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007052 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007053 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007054 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007055 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007056 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007057 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00007058 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007059 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007060 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00007061 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007062 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007063 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00007064 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007065 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007066 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007067 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007068 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007069 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007070 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007071 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007072 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007073 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007074 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007075 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007076 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007077 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007078 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007079 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007080 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007081 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007082 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007083 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007084 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007085 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007086 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007087 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007088 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007089
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007090 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007091 case ParsedAttr::AT_Capability:
7092 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007093 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007094 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007095 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007096 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007097 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00007098
Erich Keanee891aa92018-07-13 15:07:47 +00007099 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007100 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007101 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007102 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007103 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007104 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007105 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007106 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007107 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007108 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007109 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007110 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007111
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00007112 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007113 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007114 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00007115 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007116 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007117 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007118 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007119 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007120 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007121 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007122 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007123 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007124 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007125 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007126 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00007127 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007128 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007129 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00007130 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007131 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007132 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007133 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007134 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007135 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007136 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007137
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007138 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007139 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007140 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007141 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007142 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007143 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007144 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007145 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00007146 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00007147 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007148 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007149 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00007150 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007151 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007152 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007153 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007154 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007155 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007156 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00007157 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00007158
7159 // Move semantics attribute.
7160 case ParsedAttr::AT_Reinitializes:
7161 handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
7162 break;
Erik Pilkington5a559e62018-08-21 17:24:06 +00007163
7164 case ParsedAttr::AT_AlwaysDestroy:
7165 case ParsedAttr::AT_NoDestroy:
7166 handleDestroyAttr(S, D, AL);
7167 break;
JF Bastien14daa202018-12-18 05:12:21 +00007168
7169 case ParsedAttr::AT_Uninitialized:
7170 handleUninitializedAttr(S, D, AL);
7171 break;
Erik Pilkington1e368822019-01-04 18:33:06 +00007172
7173 case ParsedAttr::AT_ObjCExternallyRetained:
7174 handleObjCExternallyRetainedAttr(S, D, AL);
7175 break;
Erik Pilkingtone3cd7352019-02-11 23:21:39 +00007176
7177 case ParsedAttr::AT_FortifyStdLib:
7178 handleFortifyStdLib(S, D, AL);
7179 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007180 }
7181}
7182
7183/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
7184/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00007185void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00007186 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00007187 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00007188 if (AttrList.empty())
7189 return;
7190
Erich Keanee891aa92018-07-13 15:07:47 +00007191 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00007192 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00007193
Joey Gouly2cd9db12013-12-13 16:15:28 +00007194 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00007195 // GCC accepts
7196 // static int a9 __attribute__((weakref));
7197 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00007198 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00007199 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
7200 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00007201 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00007202 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007203 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007204
Aaron Ballmanbe243a72014-12-04 22:45:31 +00007205 // FIXME: We should be able to handle this in TableGen as well. It would be
7206 // good to have a way to specify "these attributes must appear as a group",
7207 // for these. Additionally, it would be good to have a way to specify "these
7208 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00007209 if (!D->hasAttr<OpenCLKernelAttr>()) {
7210 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007211 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00007212 // FIXME: This emits a different error message than
7213 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00007214 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007215 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007216 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007217 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007218 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007219 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007220 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007221 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007222 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00007223 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
7224 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00007225 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
7226 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
7227 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7228 << A << ExpectedKernelFunction;
7229 D->setInvalidDecl();
7230 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
7231 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7232 << A << ExpectedKernelFunction;
7233 D->setInvalidDecl();
7234 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
7235 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7236 << A << ExpectedKernelFunction;
7237 D->setInvalidDecl();
7238 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
7239 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7240 << A << ExpectedKernelFunction;
7241 D->setInvalidDecl();
7242 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007243 }
7244 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007245}
7246
Hiroshi Inoue939d9322017-06-30 05:40:31 +00007247// Helper for delayed processing TransparentUnion attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007248void Sema::ProcessDeclAttributeDelayed(Decl *D,
7249 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007250 for (const ParsedAttr &AL : AttrList)
7251 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00007252 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00007253 break;
7254 }
7255}
7256
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007257// Annotation attributes are the only attributes allowed after an access
7258// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00007259bool Sema::ProcessAccessDeclAttributeList(
7260 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007261 for (const ParsedAttr &AL : AttrList) {
7262 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00007263 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007264 } else {
Erich Keanec480f302018-07-12 21:09:05 +00007265 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007266 return true;
7267 }
7268 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007269 return false;
7270}
7271
John McCall42856de2011-10-01 05:17:03 +00007272/// checkUnusedDeclAttributes - Check a list of attributes to see if it
7273/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00007274static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00007275 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00007276 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007277 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
7278 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00007279 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00007280 continue;
John McCall42856de2011-10-01 05:17:03 +00007281
Erich Keanee891aa92018-07-13 15:07:47 +00007282 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00007283 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00007284 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007285 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00007286 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
7287 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007288 }
7289 }
7290}
7291
7292/// checkUnusedDeclAttributes - Given a declarator which is not being
7293/// used to build a declaration, complain about any decl attributes
7294/// which might be lying around on it.
7295void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00007296 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00007297 ::checkUnusedDeclAttributes(*this, D.getAttributes());
7298 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
7299 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
7300}
7301
Ryan Flynn7d470f32009-07-30 03:15:39 +00007302/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00007303/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00007304NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
7305 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00007306 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00007307 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007308 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00007309 FunctionDecl *NewFD;
7310 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00007311 // FIXME: Mangling?
7312 // FIXME: Is the qualifier info correct?
7313 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00007314 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
7315 Loc, Loc, DeclarationName(II),
7316 FD->getType(), FD->getTypeSourceInfo(),
7317 SC_None, false/*isInlineSpecified*/,
7318 FD->hasPrototype(),
7319 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007320 NewD = NewFD;
7321
7322 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00007323 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00007324
7325 // Fake up parameter variables; they are declared as if this were
7326 // a typedef.
7327 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007328 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00007329 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00007330 for (const auto &AI : FT->param_types()) {
7331 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007332 Param->setScopeInfo(0, Params.size());
7333 Params.push_back(Param);
7334 }
David Blaikie9c70e042011-09-21 18:16:56 +00007335 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00007336 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007337 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00007338 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00007339 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00007340 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00007341 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007342 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00007343 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00007344 }
7345 return NewD;
7346}
7347
James Dennett634962f2012-06-14 21:40:34 +00007348/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00007349/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00007350void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00007351 if (W.getUsed()) return; // only do this once
7352 W.setUsed(true);
7353 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
7354 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00007355 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00007356 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
7357 W.getLocation()));
7358 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00007359 WeakTopLevelDecl.push_back(NewD);
7360 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
7361 // to insert Decl at TU scope, sorry.
7362 DeclContext *SavedContext = CurContext;
7363 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00007364 NewD->setDeclContext(CurContext);
7365 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00007366 PushOnScopeChains(NewD, S);
7367 CurContext = SavedContext;
7368 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00007369 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00007370 }
7371}
7372
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007373void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
7374 // It's valid to "forward-declare" #pragma weak, in which case we
7375 // have to do this.
7376 LoadExternalWeakUndeclaredIdentifiers();
7377 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00007378 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007379 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007380 if (VD->isExternC())
7381 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007382 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007383 if (FD->isExternC())
7384 ND = FD;
7385 if (ND) {
7386 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00007387 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007388 if (I != WeakUndeclaredIdentifiers.end()) {
7389 WeakInfo W = I->second;
7390 DeclApplyPragmaWeak(S, ND, W);
7391 WeakUndeclaredIdentifiers[Id] = W;
7392 }
7393 }
7394 }
7395 }
7396}
7397
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007398/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
7399/// it, apply them to D. This is a bit tricky because PD can have attributes
7400/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00007401void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007402 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00007403 if (!PD.getDeclSpec().getAttributes().empty())
7404 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00007405
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007406 // Walk the declarator structure, applying decl attributes that were in a type
7407 // position to the decl itself. This handles cases like:
7408 // int *__attr__(x)** D;
7409 // when X is a decl attribute.
7410 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00007411 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
7412 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00007413
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007414 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00007415 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00007416
7417 // Apply additional attributes specified by '#pragma clang attribute'.
7418 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007419}
John McCall28a6aea2009-11-04 02:18:39 +00007420
John McCall31168b02011-06-15 23:02:42 +00007421/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00007422/// If so, it'll still be annotated with an attribute that makes it
7423/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007424static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00007425 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00007426 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00007427 // Private ivars are always okay. Unfortunately, people don't
7428 // always properly make their ivars private, even in system headers.
7429 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007430 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
7431 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00007432 return false;
7433
John McCallc6af8c62015-10-28 05:03:19 +00007434 // Silently accept unsupported uses of __weak in both user and system
7435 // declarations when it's been disabled, for ease of integration with
7436 // -fno-objc-arc files. We do have to take some care against attempts
7437 // to define such things; for now, we've only done that for ivars
7438 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007439 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00007440 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
7441 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
7442 reason = UnavailableAttr::IR_ForbiddenWeak;
7443 return true;
7444 }
John McCallb61e14e2015-10-27 04:54:50 +00007445 }
7446
John McCallc6af8c62015-10-28 05:03:19 +00007447 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007448 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00007449 // Currently, all the failures dealt with this way are due to ARC
7450 // restrictions.
7451 reason = UnavailableAttr::IR_ARCForbiddenType;
7452 return true;
John McCallb61e14e2015-10-27 04:54:50 +00007453 }
7454
7455 return false;
John McCall31168b02011-06-15 23:02:42 +00007456}
7457
7458/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007459static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
7460 Decl *D) {
7461 auto Reason = UnavailableAttr::IR_None;
7462 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
7463 assert(Reason && "didn't set reason?");
7464 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00007465 return;
7466 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00007467 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007468 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00007469 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007470 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007471 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007472 DD.getForbiddenTypeDiagnostic() ==
7473 diag::err_arc_array_param_no_ownership) {
7474 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007475 return;
7476 }
7477 }
John McCall31168b02011-06-15 23:02:42 +00007478
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007479 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
7480 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
7481 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00007482}
7483
Manman Ren45b1ab12016-05-06 19:57:16 +00007484static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
7485 const Decl *D) {
7486 // Check each AvailabilityAttr to find the one for this platform.
7487 for (const auto *A : D->attrs()) {
7488 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
7489 // FIXME: this is copied from CheckAvailability. We should try to
7490 // de-duplicate.
7491
7492 // Check if this is an App Extension "platform", and if so chop off
7493 // the suffix for matching with the actual platform.
7494 StringRef ActualPlatform = Avail->getPlatform()->getName();
7495 StringRef RealizedPlatform = ActualPlatform;
7496 if (Context.getLangOpts().AppExt) {
7497 size_t suffix = RealizedPlatform.rfind("_app_extension");
7498 if (suffix != StringRef::npos)
7499 RealizedPlatform = RealizedPlatform.slice(0, suffix);
7500 }
7501
7502 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
7503
7504 // Match the platform name.
7505 if (RealizedPlatform == TargetPlatform)
7506 return Avail;
7507 }
7508 }
7509 return nullptr;
7510}
7511
Erik Pilkington9f866a72017-07-18 20:32:07 +00007512/// The diagnostic we should emit for \c D, and the declaration that
7513/// originated it, or \c AR_Available.
7514///
7515/// \param D The declaration to check.
7516/// \param Message If non-null, this will be populated with the message from
7517/// the availability attribute that is selected.
Erik Pilkington42578572018-09-10 22:20:09 +00007518/// \param ClassReceiver If we're checking the the method of a class message
7519/// send, the class. Otherwise nullptr.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007520static std::pair<AvailabilityResult, const NamedDecl *>
Erik Pilkington42578572018-09-10 22:20:09 +00007521ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D,
7522 std::string *Message,
7523 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007524 AvailabilityResult Result = D->getAvailability(Message);
7525
7526 // For typedefs, if the typedef declaration appears available look
7527 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007528 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007529 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007530 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007531 D = TT->getDecl();
7532 Result = D->getAvailability(Message);
7533 continue;
7534 }
7535 }
7536 break;
7537 }
7538
7539 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007540 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007541 if (IDecl->getDefinition()) {
7542 D = IDecl->getDefinition();
7543 Result = D->getAvailability(Message);
7544 }
7545 }
7546
7547 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7548 if (Result == AR_Available) {
7549 const DeclContext *DC = ECD->getDeclContext();
7550 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
7551 Result = TheEnumDecl->getAvailability(Message);
7552 D = TheEnumDecl;
7553 }
7554 }
7555
Erik Pilkington42578572018-09-10 22:20:09 +00007556 // For +new, infer availability from -init.
7557 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7558 if (S.NSAPIObj && ClassReceiver) {
7559 ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod(
7560 S.NSAPIObj->getInitSelector());
7561 if (Init && Result == AR_Available && MD->isClassMethod() &&
7562 MD->getSelector() == S.NSAPIObj->getNewSelector() &&
7563 MD->definedInNSObject(S.getASTContext())) {
7564 Result = Init->getAvailability(Message);
7565 D = Init;
7566 }
7567 }
7568 }
7569
Erik Pilkington9f866a72017-07-18 20:32:07 +00007570 return {Result, D};
7571}
7572
7573
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007574/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007575/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
7576/// in a deprecated context, but not the other way around.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007577static bool
7578ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
7579 VersionTuple DeclVersion, Decl *Ctx,
7580 const NamedDecl *OffendingDecl) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007581 assert(K != AR_Available && "Expected an unavailable declaration here!");
7582
7583 // Checks if we should emit the availability diagnostic in the context of C.
7584 auto CheckContext = [&](const Decl *C) {
7585 if (K == AR_NotYetIntroduced) {
7586 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
7587 if (AA->getIntroduced() >= DeclVersion)
7588 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007589 } else if (K == AR_Deprecated) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007590 if (C->isDeprecated())
7591 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007592 } else if (K == AR_Unavailable) {
7593 // It is perfectly fine to refer to an 'unavailable' Objective-C method
Alex Lorenz194d00e2019-01-17 18:12:45 +00007594 // when it is referenced from within the @implementation itself. In this
7595 // context, we interpret unavailable as a form of access control.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007596 if (const auto *MD = dyn_cast<ObjCMethodDecl>(OffendingDecl)) {
7597 if (const auto *Impl = dyn_cast<ObjCImplDecl>(C)) {
Alex Lorenz194d00e2019-01-17 18:12:45 +00007598 if (MD->getClassInterface() == Impl->getClassInterface())
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007599 return true;
7600 }
7601 }
7602 }
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007603
7604 if (C->isUnavailable())
7605 return true;
7606 return false;
7607 };
7608
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007609 do {
7610 if (CheckContext(Ctx))
7611 return false;
7612
7613 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00007614 // Unless it is "+load" method.
7615 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
7616 if (MethodD->isClassMethod() &&
7617 MethodD->getSelector().getAsString() == "load")
7618 return true;
7619
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007620 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007621 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
7622 if (CheckContext(Interface))
7623 return false;
7624 }
7625 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007626 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007627 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
7628 if (CheckContext(Interface))
7629 return false;
7630 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
7631
7632 return true;
7633}
7634
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007635static bool
7636shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
7637 const VersionTuple &DeploymentVersion,
7638 const VersionTuple &DeclVersion) {
7639 const auto &Triple = Context.getTargetInfo().getTriple();
7640 VersionTuple ForceAvailabilityFromVersion;
7641 switch (Triple.getOS()) {
7642 case llvm::Triple::IOS:
7643 case llvm::Triple::TvOS:
7644 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7645 break;
7646 case llvm::Triple::WatchOS:
7647 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7648 break;
7649 case llvm::Triple::Darwin:
7650 case llvm::Triple::MacOSX:
7651 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7652 break;
7653 default:
7654 // New targets should always warn about availability.
7655 return Triple.getVendor() == llvm::Triple::Apple;
7656 }
7657 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7658 DeclVersion >= ForceAvailabilityFromVersion;
7659}
7660
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007661static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7662 for (Decl *Ctx = OrigCtx; Ctx;
7663 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7664 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7665 return cast<NamedDecl>(Ctx);
7666 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7667 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7668 return Imp->getClassInterface();
7669 return CD;
7670 }
7671 }
7672
7673 return dyn_cast<NamedDecl>(OrigCtx);
7674}
7675
Alex Lorenz727c21e2017-07-26 13:58:02 +00007676namespace {
7677
7678struct AttributeInsertion {
7679 StringRef Prefix;
7680 SourceLocation Loc;
7681 StringRef Suffix;
7682
7683 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007684 return {" ", D->getEndLoc(), ""};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007685 }
7686 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7687 return {" ", Loc, ""};
7688 }
7689 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007690 return {"", D->getBeginLoc(), "\n"};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007691 }
7692};
7693
7694} // end anonymous namespace
7695
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007696/// Tries to parse a string as ObjC method name.
7697///
7698/// \param Name The string to parse. Expected to originate from availability
7699/// attribute argument.
7700/// \param SlotNames The vector that will be populated with slot names. In case
7701/// of unsuccessful parsing can contain invalid data.
7702/// \returns A number of method parameters if parsing was successful, None
7703/// otherwise.
7704static Optional<unsigned>
7705tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
7706 const LangOptions &LangOpts) {
7707 // Accept replacements starting with - or + as valid ObjC method names.
7708 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
7709 Name = Name.drop_front(1);
7710 if (Name.empty())
7711 return None;
7712 Name.split(SlotNames, ':');
7713 unsigned NumParams;
7714 if (Name.back() == ':') {
7715 // Remove an empty string at the end that doesn't represent any slot.
7716 SlotNames.pop_back();
7717 NumParams = SlotNames.size();
7718 } else {
7719 if (SlotNames.size() != 1)
7720 // Not a valid method name, just a colon-separated string.
7721 return None;
7722 NumParams = 0;
7723 }
7724 // Verify all slot names are valid.
7725 bool AllowDollar = LangOpts.DollarIdents;
7726 for (StringRef S : SlotNames) {
7727 if (S.empty())
7728 continue;
7729 if (!isValidIdentifier(S, AllowDollar))
7730 return None;
7731 }
7732 return NumParams;
7733}
7734
Alex Lorenz727c21e2017-07-26 13:58:02 +00007735/// Returns a source location in which it's appropriate to insert a new
7736/// attribute for the given declaration \D.
7737static Optional<AttributeInsertion>
7738createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7739 const LangOptions &LangOpts) {
7740 if (isa<ObjCPropertyDecl>(D))
7741 return AttributeInsertion::createInsertionAfter(D);
7742 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7743 if (MD->hasBody())
7744 return None;
7745 return AttributeInsertion::createInsertionAfter(D);
7746 }
7747 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7748 SourceLocation Loc =
7749 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7750 if (Loc.isInvalid())
7751 return None;
7752 // Insert after the 'struct'/whatever keyword.
7753 return AttributeInsertion::createInsertionAfter(Loc);
7754 }
7755 return AttributeInsertion::createInsertionBefore(D);
7756}
7757
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007758/// Actually emit an availability diagnostic for a reference to an unavailable
7759/// decl.
7760///
7761/// \param Ctx The context that the reference occurred in
7762/// \param ReferringDecl The exact declaration that was referenced.
7763/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007764/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007765/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7766/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7767/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007768static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007769 Decl *Ctx, const NamedDecl *ReferringDecl,
7770 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007771 StringRef Message,
7772 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007773 const ObjCInterfaceDecl *UnknownObjCClass,
7774 const ObjCPropertyDecl *ObjCProperty,
7775 bool ObjCPropertyAccess) {
7776 // Diagnostics for deprecated or unavailable.
7777 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007778 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007779 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007780
7781 // Matches 'diag::note_property_attribute' options.
7782 unsigned property_note_select;
7783
7784 // Matches diag::note_availability_specified_here.
7785 unsigned available_here_select_kind;
7786
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007787 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007788 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007789 DeclVersion = AA->getIntroduced();
7790
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007791 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx,
7792 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007793 return;
7794
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007795 SourceLocation Loc = Locs.front();
7796
Erik Pilkington8b352c42017-08-14 19:49:12 +00007797 // The declaration can have multiple availability attributes, we are looking
7798 // at one of them.
7799 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7800 if (A && A->isInherited()) {
7801 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7802 Redecl = Redecl->getPreviousDecl()) {
7803 const AvailabilityAttr *AForRedecl =
7804 getAttrForPlatform(S.Context, Redecl);
7805 if (AForRedecl && !AForRedecl->isInherited()) {
7806 // If D is a declaration with inherited attributes, the note should
7807 // point to the declaration with actual attributes.
7808 NoteLocation = Redecl->getLocation();
7809 break;
7810 }
7811 }
7812 }
7813
Aaron Ballmanfb237522014-10-15 15:37:51 +00007814 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007815 case AR_NotYetIntroduced: {
7816 // We would like to emit the diagnostic even if -Wunguarded-availability is
7817 // not specified for deployment targets >= to iOS 11 or equivalent or
7818 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7819 // later.
7820 const AvailabilityAttr *AA =
7821 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7822 VersionTuple Introduced = AA->getIntroduced();
7823
7824 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7825 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7826 Introduced);
7827 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7828 : diag::warn_unguarded_availability;
7829
Erik Pilkington0535b0f2019-01-14 19:17:31 +00007830 std::string PlatformName = AvailabilityAttr::getPrettyPlatformName(
7831 S.getASTContext().getTargetInfo().getPlatformName());
Erik Pilkington8b352c42017-08-14 19:49:12 +00007832
Erik Pilkington0535b0f2019-01-14 19:17:31 +00007833 S.Diag(Loc, Warning) << OffendingDecl << PlatformName
7834 << Introduced.getAsString();
7835
7836 S.Diag(OffendingDecl->getLocation(),
7837 diag::note_partial_availability_specified_here)
7838 << OffendingDecl << PlatformName << Introduced.getAsString()
7839 << S.Context.getTargetInfo().getPlatformMinVersion().getAsString();
Erik Pilkington8b352c42017-08-14 19:49:12 +00007840
7841 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007842 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007843 if (TD->getDeclName().isEmpty()) {
7844 S.Diag(TD->getLocation(),
7845 diag::note_decl_unguarded_availability_silence)
7846 << /*Anonymous*/ 1 << TD->getKindName();
7847 return;
7848 }
7849 auto FixitNoteDiag =
7850 S.Diag(Enclosing->getLocation(),
7851 diag::note_decl_unguarded_availability_silence)
7852 << /*Named*/ 0 << Enclosing;
7853 // Don't offer a fixit for declarations with availability attributes.
7854 if (Enclosing->hasAttr<AvailabilityAttr>())
7855 return;
7856 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7857 return;
7858 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7859 Enclosing, S.getSourceManager(), S.getLangOpts());
7860 if (!Insertion)
7861 return;
7862 std::string PlatformName =
7863 AvailabilityAttr::getPlatformNameSourceSpelling(
7864 S.getASTContext().getTargetInfo().getPlatformName())
7865 .lower();
7866 std::string Introduced =
7867 OffendingDecl->getVersionIntroduced().getAsString();
7868 FixitNoteDiag << FixItHint::CreateInsertion(
7869 Insertion->Loc,
7870 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7871 "(" + Introduced + "))" + Insertion->Suffix)
7872 .str());
7873 }
7874 return;
7875 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007876 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007877 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7878 : diag::warn_property_method_deprecated;
7879 diag_message = diag::warn_deprecated_message;
7880 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7881 property_note_select = /* deprecated */ 0;
7882 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007883 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7884 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007885 break;
7886
Erik Pilkington796a3e22016-08-05 22:59:03 +00007887 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007888 diag = !ObjCPropertyAccess ? diag::err_unavailable
7889 : diag::err_property_method_unavailable;
7890 diag_message = diag::err_unavailable_message;
7891 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7892 property_note_select = /* unavailable */ 1;
7893 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007894
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007895 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7896 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007897 // Most of these failures are due to extra restrictions in ARC;
7898 // reflect that in the primary diagnostic when applicable.
7899 auto flagARCError = [&] {
7900 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007901 S.getSourceManager().isInSystemHeader(
7902 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007903 diag = diag::err_unavailable_in_arc;
7904 };
7905
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007906 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007907 case UnavailableAttr::IR_None: break;
7908
7909 case UnavailableAttr::IR_ARCForbiddenType:
7910 flagARCError();
7911 diag_available_here = diag::note_arc_forbidden_type;
7912 break;
7913
7914 case UnavailableAttr::IR_ForbiddenWeak:
7915 if (S.getLangOpts().ObjCWeakRuntime)
7916 diag_available_here = diag::note_arc_weak_disabled;
7917 else
7918 diag_available_here = diag::note_arc_weak_no_runtime;
7919 break;
7920
7921 case UnavailableAttr::IR_ARCForbiddenConversion:
7922 flagARCError();
7923 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7924 break;
7925
7926 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7927 flagARCError();
7928 diag_available_here = diag::note_arc_init_returns_unrelated;
7929 break;
7930
7931 case UnavailableAttr::IR_ARCFieldWithOwnership:
7932 flagARCError();
7933 diag_available_here = diag::note_arc_field_with_ownership;
7934 break;
7935 }
7936 }
John McCallb61e14e2015-10-27 04:54:50 +00007937 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007938 break;
7939
Erik Pilkington796a3e22016-08-05 22:59:03 +00007940 case AR_Available:
7941 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007942 }
7943
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007944 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007945 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007946 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007947 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7948 Replacement = AL->getReplacement();
7949 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7950 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007951
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007952 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00007953 if (!Replacement.empty())
7954 UseRange =
7955 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007956 if (UseRange.isValid()) {
7957 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
7958 Selector Sel = MethodDecl->getSelector();
7959 SmallVector<StringRef, 12> SelectorSlotNames;
7960 Optional<unsigned> NumParams = tryParseObjCMethodName(
7961 Replacement, SelectorSlotNames, S.getLangOpts());
7962 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
7963 assert(SelectorSlotNames.size() == Locs.size());
7964 for (unsigned I = 0; I < Locs.size(); ++I) {
7965 if (!Sel.getNameForSlot(I).empty()) {
7966 CharSourceRange NameRange = CharSourceRange::getCharRange(
7967 Locs[I], S.getLocForEndOfToken(Locs[I]));
7968 FixIts.push_back(FixItHint::CreateReplacement(
7969 NameRange, SelectorSlotNames[I]));
7970 } else
7971 FixIts.push_back(
7972 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
7973 }
7974 } else
7975 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7976 } else
7977 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7978 }
Manman Renc7890fe2016-03-16 18:50:49 +00007979 }
7980
Aaron Ballmanfb237522014-10-15 15:37:51 +00007981 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007982 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007983 if (ObjCProperty)
7984 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7985 << ObjCProperty->getDeclName() << property_note_select;
7986 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007987 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007988 if (ObjCProperty)
7989 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7990 << ObjCProperty->getDeclName() << property_note_select;
7991 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007992 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007993 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7994 }
7995
Erik Pilkington8b352c42017-08-14 19:49:12 +00007996 S.Diag(NoteLocation, diag_available_here)
7997 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007998}
7999
8000static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
8001 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00008002 assert(DD.Kind == DelayedDiagnostic::Availability &&
8003 "Expected an availability diagnostic here");
8004
Aaron Ballmanfb237522014-10-15 15:37:51 +00008005 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00008006 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008007 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008008 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
8009 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
8010 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00008011}
8012
John McCall2ec85372012-05-07 06:16:41 +00008013void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
8014 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00008015 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00008016 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00008017
John McCall2ec85372012-05-07 06:16:41 +00008018 // When delaying diagnostics to run in the context of a parsed
8019 // declaration, we only want to actually emit anything if parsing
8020 // succeeds.
8021 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00008022
John McCall2ec85372012-05-07 06:16:41 +00008023 // We emit all the active diagnostics in this pool or any of its
8024 // parents. In general, we'll get one pool for the decl spec
8025 // and a child pool for each declarator; in a decl group like:
8026 // deprecated_typedef foo, *bar, baz();
8027 // only the declarator pops will be passed decls. This is correct;
8028 // we really do need to consider delayed diagnostics from the decl spec
8029 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00008030 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00008031 do {
Richard Smith5c9b3b72018-09-25 22:12:44 +00008032 bool AnyAccessFailures = false;
John McCall6347b682012-05-07 06:16:58 +00008033 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00008034 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
8035 // This const_cast is a bit lame. Really, Triggered should be mutable.
8036 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00008037 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00008038 continue;
8039
John McCallc1465822011-02-14 07:13:47 +00008040 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00008041 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00008042 // Don't bother giving deprecation/unavailable diagnostics if
8043 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00008044 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00008045 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00008046 break;
8047
8048 case DelayedDiagnostic::Access:
Richard Smith5c9b3b72018-09-25 22:12:44 +00008049 // Only produce one access control diagnostic for a structured binding
8050 // declaration: we don't need to tell the user that all the fields are
8051 // inaccessible one at a time.
8052 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
8053 continue;
John McCall2ec85372012-05-07 06:16:41 +00008054 HandleDelayedAccessCheck(diag, decl);
Richard Smith5c9b3b72018-09-25 22:12:44 +00008055 if (diag.Triggered)
8056 AnyAccessFailures = true;
John McCall86121512010-01-27 03:50:35 +00008057 break;
John McCall31168b02011-06-15 23:02:42 +00008058
8059 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00008060 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00008061 break;
John McCall86121512010-01-27 03:50:35 +00008062 }
8063 }
John McCall2ec85372012-05-07 06:16:41 +00008064 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00008065}
8066
John McCall6347b682012-05-07 06:16:58 +00008067/// Given a set of delayed diagnostics, re-emit them as if they had
8068/// been delayed in the current context instead of in the given pool.
8069/// Essentially, this just moves them to the current pool.
8070void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
8071 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
8072 assert(curPool && "re-emitting in undelayed context not supported");
8073 curPool->steal(pool);
8074}
8075
Erik Pilkington9f866a72017-07-18 20:32:07 +00008076static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
8077 const NamedDecl *ReferringDecl,
8078 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008079 StringRef Message,
8080 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008081 const ObjCInterfaceDecl *UnknownObjCClass,
8082 const ObjCPropertyDecl *ObjCProperty,
8083 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00008084 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00008085 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
8086 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008087 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008088 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008089 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00008090 return;
8091 }
8092
Erik Pilkington9f866a72017-07-18 20:32:07 +00008093 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
8094 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008095 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008096 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00008097}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00008098
Erik Pilkington5cd57172016-08-16 17:44:11 +00008099namespace {
8100
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008101/// Returns true if the given statement can be a body-like child of \p Parent.
8102bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
8103 switch (Parent->getStmtClass()) {
8104 case Stmt::IfStmtClass:
8105 return cast<IfStmt>(Parent)->getThen() == S ||
8106 cast<IfStmt>(Parent)->getElse() == S;
8107 case Stmt::WhileStmtClass:
8108 return cast<WhileStmt>(Parent)->getBody() == S;
8109 case Stmt::DoStmtClass:
8110 return cast<DoStmt>(Parent)->getBody() == S;
8111 case Stmt::ForStmtClass:
8112 return cast<ForStmt>(Parent)->getBody() == S;
8113 case Stmt::CXXForRangeStmtClass:
8114 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
8115 case Stmt::ObjCForCollectionStmtClass:
8116 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
8117 case Stmt::CaseStmtClass:
8118 case Stmt::DefaultStmtClass:
8119 return cast<SwitchCase>(Parent)->getSubStmt() == S;
8120 default:
8121 return false;
8122 }
8123}
8124
8125class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
8126 const Stmt *Target;
8127
8128public:
8129 bool VisitStmt(Stmt *S) { return S != Target; }
8130
8131 /// Returns true if the given statement is present in the given declaration.
8132 static bool isContained(const Stmt *Target, const Decl *D) {
8133 StmtUSEFinder Visitor;
8134 Visitor.Target = Target;
8135 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
8136 }
8137};
8138
8139/// Traverses the AST and finds the last statement that used a given
8140/// declaration.
8141class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
8142 const Decl *D;
8143
8144public:
8145 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
8146 if (DRE->getDecl() == D)
8147 return false;
8148 return true;
8149 }
8150
8151 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
8152 const CompoundStmt *Scope) {
8153 LastDeclUSEFinder Visitor;
8154 Visitor.D = D;
8155 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
8156 const Stmt *S = *I;
8157 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
8158 return S;
8159 }
8160 return nullptr;
8161 }
8162};
8163
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008164/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00008165///
8166/// This is done with a traversal of the AST of a function that makes reference
8167/// to a partially available declaration. Whenever we encounter an \c if of the
8168/// form: \c if(@available(...)), we use the version from the condition to visit
8169/// the then statement.
8170class DiagnoseUnguardedAvailability
8171 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
8172 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
8173
8174 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008175 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00008176
8177 /// Stack of potentially nested 'if (@available(...))'s.
8178 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008179 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00008180
Erik Pilkington42578572018-09-10 22:20:09 +00008181 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range,
8182 ObjCInterfaceDecl *ClassReceiver = nullptr);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008183
8184public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008185 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
8186 : SemaRef(SemaRef), Ctx(Ctx) {
8187 AvailabilityStack.push_back(
8188 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008189 }
8190
Alex Lorenz6f911122017-05-16 13:58:53 +00008191 bool TraverseDecl(Decl *D) {
8192 // Avoid visiting nested functions to prevent duplicate warnings.
8193 if (!D || isa<FunctionDecl>(D))
8194 return true;
8195 return Base::TraverseDecl(D);
8196 }
8197
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008198 bool TraverseStmt(Stmt *S) {
8199 if (!S)
8200 return true;
8201 StmtStack.push_back(S);
8202 bool Result = Base::TraverseStmt(S);
8203 StmtStack.pop_back();
8204 return Result;
8205 }
8206
Erik Pilkington5cd57172016-08-16 17:44:11 +00008207 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
8208
8209 bool TraverseIfStmt(IfStmt *If);
8210
Alex Lorenz6f911122017-05-16 13:58:53 +00008211 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
8212
Erik Pilkingtonba87c622017-08-18 20:20:56 +00008213 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
8214 // to any useful diagnostics.
8215 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
8216
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008217 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
8218 if (PRE->isClassReceiver())
8219 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
8220 return true;
8221 }
8222
Erik Pilkington5cd57172016-08-16 17:44:11 +00008223 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
Erik Pilkington42578572018-09-10 22:20:09 +00008224 if (ObjCMethodDecl *D = Msg->getMethodDecl()) {
8225 ObjCInterfaceDecl *ID = nullptr;
8226 QualType ReceiverTy = Msg->getClassReceiver();
8227 if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType())
8228 ID = ReceiverTy->getAsObjCInterfaceType()->getInterface();
8229
Erik Pilkington5cd57172016-08-16 17:44:11 +00008230 DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00008231 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID);
8232 }
Erik Pilkington5cd57172016-08-16 17:44:11 +00008233 return true;
8234 }
8235
8236 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
8237 DiagnoseDeclAvailability(DRE->getDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008238 SourceRange(DRE->getBeginLoc(), DRE->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00008239 return true;
8240 }
8241
8242 bool VisitMemberExpr(MemberExpr *ME) {
8243 DiagnoseDeclAvailability(ME->getMemberDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008244 SourceRange(ME->getBeginLoc(), ME->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00008245 return true;
8246 }
8247
Alex Lorenz0a484ba2017-05-24 15:15:29 +00008248 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008249 SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use)
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008250 << (!SemaRef.getLangOpts().ObjC);
Alex Lorenz0a484ba2017-05-24 15:15:29 +00008251 return true;
8252 }
8253
Erik Pilkington5cd57172016-08-16 17:44:11 +00008254 bool VisitTypeLoc(TypeLoc Ty);
8255};
8256
8257void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00008258 NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008259 AvailabilityResult Result;
8260 const NamedDecl *OffendingDecl;
8261 std::tie(Result, OffendingDecl) =
Erik Pilkington42578572018-09-10 22:20:09 +00008262 ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008263 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008264 // All other diagnostic kinds have already been handled in
8265 // DiagnoseAvailabilityOfDecl.
8266 if (Result != AR_NotYetIntroduced)
8267 return;
8268
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008269 const AvailabilityAttr *AA =
8270 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008271 VersionTuple Introduced = AA->getIntroduced();
8272
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008273 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008274 return;
8275
8276 // If the context of this function is less available than D, we should not
8277 // emit a diagnostic.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00008278 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx,
8279 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008280 return;
8281
Alex Lorenzc9a369f2017-06-22 17:02:24 +00008282 // We would like to emit the diagnostic even if -Wunguarded-availability is
8283 // not specified for deployment targets >= to iOS 11 or equivalent or
8284 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
8285 // later.
8286 unsigned DiagKind =
8287 shouldDiagnoseAvailabilityByDefault(
8288 SemaRef.Context,
8289 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
8290 ? diag::warn_unguarded_availability_new
8291 : diag::warn_unguarded_availability;
8292
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008293 std::string PlatformName = AvailabilityAttr::getPrettyPlatformName(
8294 SemaRef.getASTContext().getTargetInfo().getPlatformName());
8295
Alex Lorenzc9a369f2017-06-22 17:02:24 +00008296 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008297 << Range << D << PlatformName << Introduced.getAsString();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008298
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008299 SemaRef.Diag(OffendingDecl->getLocation(),
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008300 diag::note_partial_availability_specified_here)
8301 << OffendingDecl << PlatformName << Introduced.getAsString()
8302 << SemaRef.Context.getTargetInfo()
8303 .getPlatformMinVersion()
8304 .getAsString();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008305
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008306 auto FixitDiag =
8307 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
8308 << Range << D
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008309 << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0
8310 : /*__builtin_available*/ 1);
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008311
8312 // Find the statement which should be enclosed in the if @available check.
8313 if (StmtStack.empty())
8314 return;
8315 const Stmt *StmtOfUse = StmtStack.back();
8316 const CompoundStmt *Scope = nullptr;
8317 for (const Stmt *S : llvm::reverse(StmtStack)) {
8318 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
8319 Scope = CS;
8320 break;
8321 }
8322 if (isBodyLikeChildStmt(StmtOfUse, S)) {
8323 // The declaration won't be seen outside of the statement, so we don't
8324 // have to wrap the uses of any declared variables in if (@available).
8325 // Therefore we can avoid setting Scope here.
8326 break;
8327 }
8328 StmtOfUse = S;
8329 }
8330 const Stmt *LastStmtOfUse = nullptr;
8331 if (isa<DeclStmt>(StmtOfUse) && Scope) {
8332 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
8333 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
8334 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
8335 break;
8336 }
8337 }
8338 }
8339
8340 const SourceManager &SM = SemaRef.getSourceManager();
8341 SourceLocation IfInsertionLoc =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008342 SM.getExpansionLoc(StmtOfUse->getBeginLoc());
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008343 SourceLocation StmtEndLoc =
8344 SM.getExpansionRange(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008345 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc())
Richard Smithb5f81712018-04-30 05:25:48 +00008346 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008347 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
8348 return;
8349
8350 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
8351 const char *ExtraIndentation = " ";
8352 std::string FixItString;
8353 llvm::raw_string_ostream FixItOS(FixItString);
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008354 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available"
8355 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00008356 << "("
8357 << AvailabilityAttr::getPlatformNameSourceSpelling(
8358 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008359 << " " << Introduced.getAsString() << ", *)) {\n"
8360 << Indentation << ExtraIndentation;
8361 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
8362 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
8363 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
8364 /*SkipTrailingWhitespaceAndNewLine=*/false);
8365 if (ElseInsertionLoc.isInvalid())
8366 ElseInsertionLoc =
8367 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
8368 FixItOS.str().clear();
8369 FixItOS << "\n"
8370 << Indentation << "} else {\n"
8371 << Indentation << ExtraIndentation
8372 << "// Fallback on earlier versions\n"
8373 << Indentation << "}";
8374 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008375 }
8376}
8377
8378bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
8379 const Type *TyPtr = Ty.getTypePtr();
8380 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
8381
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008382 if (Range.isInvalid())
8383 return true;
8384
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008385 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008386 TagDecl *TD = TT->getDecl();
8387 DiagnoseDeclAvailability(TD, Range);
8388
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008389 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008390 TypedefNameDecl *D = TD->getDecl();
8391 DiagnoseDeclAvailability(D, Range);
8392
8393 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
8394 if (NamedDecl *D = ObjCO->getInterface())
8395 DiagnoseDeclAvailability(D, Range);
8396 }
8397
8398 return true;
8399}
8400
8401bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
8402 VersionTuple CondVersion;
8403 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
8404 CondVersion = E->getVersion();
8405
8406 // If we're using the '*' case here or if this check is redundant, then we
8407 // use the enclosing version to check both branches.
8408 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00008409 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008410 } else {
8411 // This isn't an availability checking 'if', we can just continue.
8412 return Base::TraverseIfStmt(If);
8413 }
8414
8415 AvailabilityStack.push_back(CondVersion);
8416 bool ShouldContinue = TraverseStmt(If->getThen());
8417 AvailabilityStack.pop_back();
8418
8419 return ShouldContinue && TraverseStmt(If->getElse());
8420}
8421
8422} // end anonymous namespace
8423
8424void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
8425 Stmt *Body = nullptr;
8426
8427 if (auto *FD = D->getAsFunction()) {
8428 // FIXME: We only examine the pattern decl for availability violations now,
8429 // but we should also examine instantiated templates.
8430 if (FD->isTemplateInstantiation())
8431 return;
8432
8433 Body = FD->getBody();
8434 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
8435 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00008436 else if (auto *BD = dyn_cast<BlockDecl>(D))
8437 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008438
8439 assert(Body && "Need a body here!");
8440
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008441 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008442}
Erik Pilkington9f866a72017-07-18 20:32:07 +00008443
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008444void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
8445 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008446 const ObjCInterfaceDecl *UnknownObjCClass,
8447 bool ObjCPropertyAccess,
Erik Pilkington42578572018-09-10 22:20:09 +00008448 bool AvoidPartialAvailabilityChecks,
8449 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008450 std::string Message;
8451 AvailabilityResult Result;
8452 const NamedDecl* OffendingDecl;
8453 // See if this declaration is unavailable, deprecated, or partial.
Erik Pilkington42578572018-09-10 22:20:09 +00008454 std::tie(Result, OffendingDecl) =
8455 ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver);
Erik Pilkington9f866a72017-07-18 20:32:07 +00008456 if (Result == AR_Available)
8457 return;
8458
8459 if (Result == AR_NotYetIntroduced) {
8460 if (AvoidPartialAvailabilityChecks)
8461 return;
8462
8463 // We need to know the @available context in the current function to
8464 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
8465 // when we're done parsing the current function.
8466 if (getCurFunctionOrMethodDecl()) {
8467 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
8468 return;
8469 } else if (getCurBlock() || getCurLambda()) {
8470 getCurFunction()->HasPotentialAvailabilityViolations = true;
8471 return;
8472 }
8473 }
8474
8475 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008476 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008477 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
8478 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
8479 if (PDeclResult == Result)
8480 ObjCPDecl = PD;
8481 }
8482 }
8483
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008484 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008485 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
8486}