blob: 393fdcb479d53182e4eec36b2ecf29b1c3cd461b [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"
Simon Tatham7c11da02019-09-02 15:35:09 +010026#include "clang/Basic/TargetBuiltins.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000028#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000029#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000030#include "clang/Sema/DelayedDiagnostic.h"
Artem Belevichbcec9da2016-06-06 22:54:57 +000031#include "clang/Sema/Initialization.h"
John McCallf1e8b342011-09-29 07:17:38 +000032#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000033#include "clang/Sema/Scope.h"
Reid Kleckner04f9bca2018-03-07 22:48:35 +000034#include "clang/Sema/ScopeInfo.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000035#include "clang/Sema/SemaInternal.h"
George Burgess IV177399e2017-01-09 04:12:14 +000036#include "llvm/ADT/STLExtras.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000037#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000038#include "llvm/Support/MathExtras.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000039
Chris Lattner2c6fcf52008-06-26 18:38:35 +000040using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000041using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000042
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000043namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000044 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000045 C,
46 Cpp,
47 ObjC
48 };
Eugene Zelenko1ced5092016-02-12 22:53:10 +000049} // end namespace AttributeLangSupport
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000050
Chris Lattner58418ff2008-06-29 00:16:31 +000051//===----------------------------------------------------------------------===//
52// Helper functions
53//===----------------------------------------------------------------------===//
54
Ted Kremenek527042b2009-08-14 20:49:40 +000055/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000056/// type (function or function-typed variable) or an Objective-C
57/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000058static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000059 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000060}
Eugene Zelenko1ced5092016-02-12 22:53:10 +000061
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000062/// Return true if the given decl has function type (function or
David Majnemer06864812015-04-07 06:01:53 +000063/// function-typed variable) or an Objective-C method or a block.
64static bool isFunctionOrMethodOrBlock(const Decl *D) {
65 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
66}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000067
John McCall3882ace2011-01-05 12:14:39 +000068/// Return true if the given decl has a declarator that should have
69/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000070static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000071 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000072 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
73 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000074}
75
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000076/// hasFunctionProto - Return true if the given decl has a argument
77/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000078/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000080 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000081 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000082 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000083}
84
Alp Toker601b22c2014-01-21 23:35:24 +000085/// getFunctionOrMethodNumParams - Return number of function or method
86/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000087/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000088static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000089 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000090 return cast<FunctionProtoType>(FnTy)->getNumParams();
Aaron Ballmana70c6b52018-02-15 16:20:20 +000091 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000092 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000094}
95
Erik Pilkington1e368822019-01-04 18:33:06 +000096static const ParmVarDecl *getFunctionOrMethodParam(const Decl *D,
97 unsigned Idx) {
98 if (const auto *FD = dyn_cast<FunctionDecl>(D))
99 return FD->getParamDecl(Idx);
100 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
101 return MD->getParamDecl(Idx);
102 if (const auto *BD = dyn_cast<BlockDecl>(D))
103 return BD->getParamDecl(Idx);
104 return nullptr;
105}
106
Alp Toker601b22c2014-01-21 23:35:24 +0000107static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000108 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +0000109 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000110 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000111 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000112
Alp Toker03376dc2014-07-07 09:02:20 +0000113 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000114}
115
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000116static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
Erik Pilkington1e368822019-01-04 18:33:06 +0000117 if (auto *PVD = getFunctionOrMethodParam(D, Idx))
118 return PVD->getSourceRange();
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000119 return SourceRange();
120}
121
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000123 if (const FunctionType *FnTy = D->getFunctionType())
George Burgess IV00f70bd2018-03-01 05:43:23 +0000124 return FnTy->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000125 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000126}
127
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000128static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
129 if (const auto *FD = dyn_cast<FunctionDecl>(D))
130 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000131 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000132 return MD->getReturnTypeSourceRange();
133 return SourceRange();
134}
135
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000137 if (const FunctionType *FnTy = D->getFunctionType())
138 return cast<FunctionProtoType>(FnTy)->isVariadic();
139 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballmane13d0092014-08-01 17:02:34 +0000140 return BD->isVariadic();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000141 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000142}
143
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144static bool isInstanceMethod(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000145 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000146 return MethodDecl->isInstance();
147 return false;
148}
149
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000150static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000151 const auto *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000152 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000153 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000154
John McCall96fa4842010-05-17 21:00:27 +0000155 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
156 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000157 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000158
John McCall96fa4842010-05-17 21:00:27 +0000159 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000160
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000161 // FIXME: Should we walk the chain of classes?
162 return ClsName == &Ctx.Idents.get("NSString") ||
163 ClsName == &Ctx.Idents.get("NSMutableString");
164}
165
Daniel Dunbar980c6692008-09-26 03:32:58 +0000166static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000167 const auto *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000168 if (!PT)
169 return false;
170
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000171 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000172 if (!RT)
173 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000174
Daniel Dunbar980c6692008-09-26 03:32:58 +0000175 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000176 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000177 return false;
178
179 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
180}
181
Erich Keanee891aa92018-07-13 15:07:47 +0000182static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
Richard Smithb87c4652013-10-31 21:23:20 +0000183 // FIXME: Include the type in the argument list.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000184 return AL.getNumArgs() + AL.hasParsedType();
Richard Smithb87c4652013-10-31 21:23:20 +0000185}
186
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000187template <typename Compare>
Erich Keanee891aa92018-07-13 15:07:47 +0000188static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000189 unsigned Num, unsigned Diag,
190 Compare Comp) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000191 if (Comp(getNumAttributeArgs(AL), Num)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000192 S.Diag(AL.getLoc(), Diag) << AL << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000193 return false;
194 }
195
196 return true;
197}
198
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000199/// Check if the attribute has exactly as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000200/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000201static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000202 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000203 diag::err_attribute_wrong_number_arguments,
204 std::not_equal_to<unsigned>());
205}
206
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000207/// Check if the attribute has at least as many args as Num. May
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000208/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000209static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL,
Richard Smithb87c4652013-10-31 21:23:20 +0000210 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000211 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000212 diag::err_attribute_too_few_arguments,
213 std::less<unsigned>());
214}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000215
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000216/// Check if the attribute has at most as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000217/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000218static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL,
219 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000220 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000221 diag::err_attribute_too_many_arguments,
222 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000223}
224
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000225/// A helper function to provide Attribute Location for the Attr types
Erich Keanee891aa92018-07-13 15:07:47 +0000226/// AND the ParsedAttr.
Erich Keane623efd82017-03-30 21:48:55 +0000227template <typename AttrInfo>
Justin Lebar027eb712020-02-10 23:23:44 -0800228static std::enable_if_t<std::is_base_of<Attr, AttrInfo>::value, SourceLocation>
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)
Michael Liao7557afa2019-02-26 18:49:36 +0000248 << &AI << Idx << AANT_ArgumentIntegerConstant
Erich Keane44bacdf2018-08-09 13:21:32 +0000249 << 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)
Michael Liao7557afa2019-02-26 18:49:36 +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)
Michael Liao7557afa2019-02-26 18:49:36 +0000264 << &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>
Erich Keane6a24e802019-09-13 17:39:31 +0000401static void handleSimpleAttribute(Sema &S, Decl *D,
402 const AttributeCommonInfo &CI) {
403 D->addAttr(::new (S.Context) AttrType(S.Context, CI));
George Karpenkov1657f362018-11-30 02:18:37 +0000404}
405
George Karpenkov1657f362018-11-30 02:18:37 +0000406template <typename... DiagnosticArgs>
407static const Sema::SemaDiagnosticBuilder&
408appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr) {
409 return Bldr;
410}
411
412template <typename T, typename... DiagnosticArgs>
413static const Sema::SemaDiagnosticBuilder&
414appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr, T &&ExtraArg,
415 DiagnosticArgs &&... ExtraArgs) {
416 return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
417 std::forward<DiagnosticArgs>(ExtraArgs)...);
418}
419
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000420/// Add an attribute {@code AttrType} to declaration {@code D}, provided that
421/// {@code PassesCheck} is true.
422/// Otherwise, emit diagnostic {@code DiagID}, passing in all parameters
423/// specified in {@code ExtraArgs}.
George Karpenkov1657f362018-11-30 02:18:37 +0000424template <typename AttrType, typename... DiagnosticArgs>
Erich Keane6a24e802019-09-13 17:39:31 +0000425static void handleSimpleAttributeOrDiagnose(Sema &S, Decl *D,
426 const AttributeCommonInfo &CI,
427 bool PassesCheck, unsigned DiagID,
428 DiagnosticArgs &&... ExtraArgs) {
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000429 if (!PassesCheck) {
George Karpenkov1657f362018-11-30 02:18:37 +0000430 Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
431 appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
432 return;
433 }
Erich Keane6a24e802019-09-13 17:39:31 +0000434 handleSimpleAttribute<AttrType>(S, D, CI);
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000435}
436
Justin Lebar3eaaf862016-01-13 01:07:35 +0000437template <typename AttrType>
438static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000439 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000440 handleSimpleAttribute<AttrType>(S, D, AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000441}
442
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000443/// Applies the given attribute to the Decl so long as the Decl doesn't
Justin Lebar3eaaf862016-01-13 01:07:35 +0000444/// already have one of the given incompatible attributes.
445template <typename AttrType, typename IncompatibleAttrType,
446 typename... IncompatibleAttrTypes>
447static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000448 const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000449 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
Justin Lebar3eaaf862016-01-13 01:07:35 +0000450 return;
451 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000452 AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000453}
454
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000455/// Check if the passed-in expression is of type int or bool.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000456static bool isIntOrBool(Expr *Exp) {
457 QualType QT = Exp->getType();
458 return QT->isBooleanType() || QT->isIntegerType();
459}
460
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000461
462// Check to see if the type is a smart pointer of some kind. We assume
463// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000464static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Trieu8d3fa392018-09-22 01:50:52 +0000465 auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record,
466 OverloadedOperatorKind Op) {
467 DeclContextLookupResult Result =
468 Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op));
469 return !Result.empty();
470 };
471
472 const RecordDecl *Record = RT->getDecl();
473 bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
474 bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
475 if (foundStarOperator && foundArrowOperator)
476 return true;
477
478 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
479 if (!CXXRecord)
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000480 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000481
Richard Trieu8d3fa392018-09-22 01:50:52 +0000482 for (auto BaseSpecifier : CXXRecord->bases()) {
483 if (!foundStarOperator)
484 foundStarOperator = IsOverloadedOperatorPresent(
485 BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
486 if (!foundArrowOperator)
487 foundArrowOperator = IsOverloadedOperatorPresent(
488 BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
489 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000490
Richard Trieu8d3fa392018-09-22 01:50:52 +0000491 if (foundStarOperator && foundArrowOperator)
492 return true;
493
494 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000495}
496
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000497/// Check if passed in Decl is a pointer type.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000498/// Note that this function may produce an error message.
499/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000500static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000501 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000502 const auto *VD = cast<ValueDecl>(D);
503 QualType QT = VD->getType();
Aaron Ballman553e6812013-12-26 14:54:11 +0000504 if (QT->isAnyPointerType())
505 return true;
506
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000507 if (const auto *RT = QT->getAs<RecordType>()) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000508 // If it's an incomplete type, it could be a smart pointer; skip it.
509 // (We don't want to force template instantiation if we can avoid it,
510 // since that would alter the order in which templates are instantiated.)
511 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000512 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000513
Aaron Ballman553e6812013-12-26 14:54:11 +0000514 if (threadSafetyCheckIsSmartPointer(S, RT))
515 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000516 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000517
Erich Keane44bacdf2018-08-09 13:21:32 +0000518 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000519 return false;
520}
521
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000522/// Checks that the passed in QualType either is of RecordType or points
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000523/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000524static const RecordType *getRecordType(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000525 if (const auto *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000526 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000527
528 // Now check if we point to record type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000529 if (const auto *PT = QT->getAs<PointerType>())
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000530 return PT->getPointeeType()->getAs<RecordType>();
531
Craig Topperc3ec1492014-05-26 06:22:03 +0000532 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000533}
534
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000535template <typename AttrType>
536static bool checkRecordDeclForAttr(const RecordDecl *RD) {
537 // Check if the record itself has the attribute.
538 if (RD->hasAttr<AttrType>())
539 return true;
540
541 // Else check if any base classes have the attribute.
542 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
543 CXXBasePaths BPaths(false, false);
544 if (CRD->lookupInBases(
545 [](const CXXBaseSpecifier *BS, CXXBasePath &) {
546 const auto &Ty = *BS->getType();
547 // If it's type-dependent, we assume it could have the attribute.
548 if (Ty.isDependentType())
549 return true;
Simon Pilgrim1cd399c2019-10-03 11:22:48 +0000550 return Ty.castAs<RecordType>()->getDecl()->hasAttr<AttrType>();
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000551 },
552 BPaths, true))
553 return true;
554 }
555 return false;
556}
557
Josh Gao55afa752017-08-11 07:54:35 +0000558static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000559 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000560
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000561 if (!RT)
562 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000563
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000564 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000565 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000566 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000567
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000568 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000569 // FIXME -- Check the type that the smart pointer points to.
570 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000571 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000572
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000573 return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl());
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000574}
575
Josh Gao55afa752017-08-11 07:54:35 +0000576static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000577 const auto *TD = Ty->getAs<TypedefType>();
578 if (!TD)
579 return false;
580
581 TypedefNameDecl *TN = TD->getDecl();
582 if (!TN)
583 return false;
584
Josh Gao55afa752017-08-11 07:54:35 +0000585 return TN->hasAttr<CapabilityAttr>();
Aaron Ballman76050722014-04-04 15:13:57 +0000586}
587
Josh Gaob40c1772017-08-08 19:44:35 +0000588static bool typeHasCapability(Sema &S, QualType Ty) {
Josh Gao55afa752017-08-11 07:54:35 +0000589 if (checkTypedefTypeForCapability(Ty))
590 return true;
Josh Gaob40c1772017-08-08 19:44:35 +0000591
Josh Gao55afa752017-08-11 07:54:35 +0000592 if (checkRecordTypeForCapability(S, Ty))
593 return true;
594
595 return false;
Josh Gaob40c1772017-08-08 19:44:35 +0000596}
597
Aaron Ballman76050722014-04-04 15:13:57 +0000598static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
599 // Capability expressions are simple expressions involving the boolean logic
600 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
601 // a DeclRefExpr is found, its type should be checked to determine whether it
602 // is a capability or not.
603
Yi Kong2d58d192017-12-14 22:24:45 +0000604 if (const auto *E = dyn_cast<CastExpr>(Ex))
Aaron Ballman76050722014-04-04 15:13:57 +0000605 return isCapabilityExpr(S, E->getSubExpr());
606 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
607 return isCapabilityExpr(S, E->getSubExpr());
608 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
Yi Kong2d58d192017-12-14 22:24:45 +0000609 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
610 E->getOpcode() == UO_Deref)
Aaron Ballman76050722014-04-04 15:13:57 +0000611 return isCapabilityExpr(S, E->getSubExpr());
612 return false;
613 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
614 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
615 return isCapabilityExpr(S, E->getLHS()) &&
616 isCapabilityExpr(S, E->getRHS());
617 return false;
618 }
619
Yi Kong2d58d192017-12-14 22:24:45 +0000620 return typeHasCapability(S, Ex->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000621}
622
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000623/// Checks that all attribute arguments, starting from Sidx, resolve to
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000624/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000625/// \param Sidx The attribute argument index to start checking with.
626/// \param ParamIdxOk Whether an argument can be indexing into a function
627/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000628static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000629 const ParsedAttr &AL,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000630 SmallVectorImpl<Expr *> &Args,
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000631 unsigned Sidx = 0,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000632 bool ParamIdxOk = false) {
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000633 if (Sidx == AL.getNumArgs()) {
634 // If we don't have any capability arguments, the attribute implicitly
635 // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
636 // a non-static method, and that the class is a (scoped) capability.
637 const auto *MD = dyn_cast<const CXXMethodDecl>(D);
638 if (MD && !MD->isStatic()) {
639 const CXXRecordDecl *RD = MD->getParent();
640 // FIXME -- need to check this again on template instantiation
641 if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
642 !checkRecordDeclForAttr<ScopedLockableAttr>(RD))
643 S.Diag(AL.getLoc(),
644 diag::warn_thread_attribute_not_on_capability_member)
645 << AL << MD->getParent();
646 } else {
647 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
648 << AL;
649 }
650 }
651
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000652 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
653 Expr *ArgExp = AL.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000654
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000655 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000656 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000657 Args.push_back(ArgExp);
658 continue;
659 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000660
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000661 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000662 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000663 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000664 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000665 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000666 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000667 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000668 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000669
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000670 // We allow constant strings to be used as a placeholder for expressions
671 // that are not valid C++ syntax, but warn that they are ignored.
Erich Keane44bacdf2018-08-09 13:21:32 +0000672 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000673 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000674 continue;
675 }
676
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000677 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000678
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000679 // A pointer to member expression of the form &MyClass::mu is treated
680 // specially -- we need to look at the type of the member.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000681 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000682 if (UOp->getOpcode() == UO_AddrOf)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000683 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000684 if (DRE->getDecl()->isCXXInstanceMember())
685 ArgTy = DRE->getDecl()->getType();
686
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000687 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000688 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000689
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000690 // Now check if we index into a record type function param.
Josh Gao55afa752017-08-11 07:54:35 +0000691 if(!RT && ParamIdxOk) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000692 const auto *FD = dyn_cast<FunctionDecl>(D);
693 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
Josh Gao55afa752017-08-11 07:54:35 +0000694 if(FD && IL) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000695 unsigned int NumParams = FD->getNumParams();
696 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000697 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
698 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000699 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Aaron Ballmance667f62019-02-12 13:19:02 +0000700 S.Diag(AL.getLoc(),
701 diag::err_attribute_argument_out_of_bounds_extra_info)
Erich Keane44bacdf2018-08-09 13:21:32 +0000702 << AL << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000703 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000704 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000705 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000706 }
707 }
708
Aaron Ballman76050722014-04-04 15:13:57 +0000709 // If the type does not have a capability, see if the components of the
710 // expression have capabilities. This allows for writing C code where the
711 // capability may be on the type, and the expression is a capability
712 // boolean logic expression. Eg) requires_capability(A || B && !C)
713 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000714 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Erich Keane44bacdf2018-08-09 13:21:32 +0000715 << AL << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000716
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000717 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000718 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000719}
720
Chris Lattner58418ff2008-06-29 00:16:31 +0000721//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000722// Attribute Implementations
723//===----------------------------------------------------------------------===//
724
Erich Keanee891aa92018-07-13 15:07:47 +0000725static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000726 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000727 return;
728
Erich Keane6a24e802019-09-13 17:39:31 +0000729 D->addAttr(::new (S.Context) PtGuardedVarAttr(S.Context, AL));
Michael Han3be3b442012-07-23 18:48:41 +0000730}
731
Erich Keanee891aa92018-07-13 15:07:47 +0000732static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000733 Expr *&Arg) {
734 SmallVector<Expr *, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000735 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000736 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000737 unsigned Size = Args.size();
738 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000739 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000740
Michael Han3be3b442012-07-23 18:48:41 +0000741 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000742
Michael Han3be3b442012-07-23 18:48:41 +0000743 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000744}
745
Erich Keanee891aa92018-07-13 15:07:47 +0000746static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000747 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000748 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000749 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000750
Erich Keane6a24e802019-09-13 17:39:31 +0000751 D->addAttr(::new (S.Context) GuardedByAttr(S.Context, AL, Arg));
Michael Han3be3b442012-07-23 18:48:41 +0000752}
753
Erich Keanee891aa92018-07-13 15:07:47 +0000754static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000755 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000756 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000757 return;
758
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000759 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000760 return;
761
Erich Keane6a24e802019-09-13 17:39:31 +0000762 D->addAttr(::new (S.Context) PtGuardedByAttr(S.Context, AL, Arg));
Michael Han3be3b442012-07-23 18:48:41 +0000763}
764
Erich Keanee891aa92018-07-13 15:07:47 +0000765static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000766 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000767 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000768 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000769
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000770 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000771 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000772 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000773 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000774 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000775 }
776
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000777 // Check that all arguments are lockable objects.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000778 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000779 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000780 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000781
Michael Han3be3b442012-07-23 18:48:41 +0000782 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000783}
784
Erich Keanee891aa92018-07-13 15:07:47 +0000785static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000786 SmallVector<Expr *, 1> Args;
787 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000788 return;
789
790 Expr **StartArg = &Args[0];
Erich Keane6a24e802019-09-13 17:39:31 +0000791 D->addAttr(::new (S.Context)
792 AcquiredAfterAttr(S.Context, AL, StartArg, Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000793}
794
Erich Keanee891aa92018-07-13 15:07:47 +0000795static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000796 SmallVector<Expr *, 1> Args;
797 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000798 return;
799
800 Expr **StartArg = &Args[0];
Erich Keane6a24e802019-09-13 17:39:31 +0000801 D->addAttr(::new (S.Context)
802 AcquiredBeforeAttr(S.Context, AL, StartArg, Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000803}
804
Erich Keanee891aa92018-07-13 15:07:47 +0000805static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000806 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000807 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000808 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000809 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000810
Michael Han3be3b442012-07-23 18:48:41 +0000811 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000812}
813
Erich Keanee891aa92018-07-13 15:07:47 +0000814static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000815 SmallVector<Expr *, 1> Args;
816 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000817 return;
818
819 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000820 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000821 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +0000822 AssertSharedLockAttr(S.Context, AL, StartArg, Size));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000823}
824
825static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000826 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000827 SmallVector<Expr *, 1> Args;
828 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000829 return;
830
831 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000832 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
Erich Keane6a24e802019-09-13 17:39:31 +0000833 D->addAttr(::new (S.Context)
834 AssertExclusiveLockAttr(S.Context, AL, StartArg, Size));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000835}
836
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000837/// Checks to be sure that the given parameter number is in bounds, and
Aaron Ballman836684a2018-02-25 20:40:06 +0000838/// is an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000839/// false.
840///
Aaron Ballman836684a2018-02-25 20:40:06 +0000841/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000842template <typename AttrInfo>
843static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Joel E. Denny81508102018-03-13 14:51:22 +0000844 const AttrInfo &AI, unsigned AttrArgNo) {
Aaron Ballman836684a2018-02-25 20:40:06 +0000845 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
846 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
Joel E. Denny81508102018-03-13 14:51:22 +0000847 ParamIdx Idx;
Aaron Ballman836684a2018-02-25 20:40:06 +0000848 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000849 Idx))
850 return false;
851
Joel E. Denny81508102018-03-13 14:51:22 +0000852 const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +0000853 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000854 SourceLocation SrcLoc = AttrArg->getBeginLoc();
Erich Keane623efd82017-03-30 21:48:55 +0000855 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +0000856 << AI << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000857 return false;
858 }
859 return true;
860}
861
Erich Keanee891aa92018-07-13 15:07:47 +0000862static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000863 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
864 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000865 return;
866
867 const auto *FD = cast<FunctionDecl>(D);
868 if (!FD->getReturnType()->isPointerType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000869 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
George Burgess IVe3763372016-12-22 02:50:20 +0000870 return;
871 }
872
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000873 const Expr *SizeExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +0000874 int SizeArgNoVal;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000875 // Parameter indices are 1-indexed, hence Index=1
Rui Ueyama49a3ad22019-07-16 04:46:31 +0000876 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Idx=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000877 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000878 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0))
George Burgess IVe3763372016-12-22 02:50:20 +0000879 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000880 ParamIdx SizeArgNo(SizeArgNoVal, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000881
Joel E. Denny81508102018-03-13 14:51:22 +0000882 ParamIdx NumberArgNo;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000883 if (AL.getNumArgs() == 2) {
884 const Expr *NumberExpr = AL.getArgAsExpr(1);
Joel E. Denny81508102018-03-13 14:51:22 +0000885 int Val;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000886 // Parameter indices are 1-based, hence Index=2
Rui Ueyama49a3ad22019-07-16 04:46:31 +0000887 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Idx=*/2))
George Burgess IVe3763372016-12-22 02:50:20 +0000888 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000889 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000890 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000891 NumberArgNo = ParamIdx(Val, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000892 }
893
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000894 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +0000895 AllocSizeAttr(S.Context, AL, SizeArgNo, NumberArgNo));
George Burgess IVe3763372016-12-22 02:50:20 +0000896}
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000897
Erich Keanee891aa92018-07-13 15:07:47 +0000898static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000899 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000900 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000901 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000902
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000903 if (!isIntOrBool(AL.getArgAsExpr(0))) {
904 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000905 << AL << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000906 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000907 }
908
909 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000910 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000911
Michael Han3be3b442012-07-23 18:48:41 +0000912 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000913}
914
Michael Hana9171bc2012-08-03 17:40:43 +0000915static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000916 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000917 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000918 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000919 return;
920
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000921 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
Erich Keane6a24e802019-09-13 17:39:31 +0000922 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000923}
924
Michael Hana9171bc2012-08-03 17:40:43 +0000925static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000926 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000927 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000928 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000929 return;
930
Nico Weber462fd1e2015-01-07 23:50:05 +0000931 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
Erich Keane6a24e802019-09-13 17:39:31 +0000932 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000933}
934
Erich Keanee891aa92018-07-13 15:07:47 +0000935static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000936 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000937 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000938 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000939 unsigned Size = Args.size();
940 if (Size == 0)
941 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000942
Erich Keane6a24e802019-09-13 17:39:31 +0000943 D->addAttr(::new (S.Context) LockReturnedAttr(S.Context, AL, Args[0]));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000944}
945
Erich Keanee891aa92018-07-13 15:07:47 +0000946static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000947 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000948 return;
949
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000950 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000951 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000952 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000953 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000954 if (Size == 0)
955 return;
956 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000957
Michael Han99315932013-01-24 16:46:58 +0000958 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +0000959 LocksExcludedAttr(S.Context, AL, StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000960}
961
Erich Keanee891aa92018-07-13 15:07:47 +0000962static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
George Burgess IV177399e2017-01-09 04:12:14 +0000963 Expr *&Cond, StringRef &Msg) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000964 Cond = AL.getArgAsExpr(0);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000965 if (!Cond->isTypeDependent()) {
966 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
967 if (Converted.isInvalid())
George Burgess IV177399e2017-01-09 04:12:14 +0000968 return false;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000969 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000970 }
971
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000972 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000973 return false;
974
975 if (Msg.empty())
976 Msg = "<no message provided>";
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000977
978 SmallVector<PartialDiagnosticAt, 8> Diags;
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000979 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000980 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
981 Diags)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000982 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
George Burgess IV0d546532016-11-10 21:47:12 +0000983 for (const PartialDiagnosticAt &PDiag : Diags)
984 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +0000985 return false;
986 }
987 return true;
988}
989
Erich Keanee891aa92018-07-13 15:07:47 +0000990static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000991 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +0000992
993 Expr *Cond;
994 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000995 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
Erich Keane6a24e802019-09-13 17:39:31 +0000996 D->addAttr(::new (S.Context) EnableIfAttr(S.Context, AL, Cond, Msg));
George Burgess IV177399e2017-01-09 04:12:14 +0000997}
998
999namespace {
1000/// Determines if a given Expr references any of the given function's
1001/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
1002class ArgumentDependenceChecker
1003 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
1004#ifndef NDEBUG
1005 const CXXRecordDecl *ClassType;
1006#endif
1007 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
1008 bool Result;
1009
1010public:
1011 ArgumentDependenceChecker(const FunctionDecl *FD) {
1012#ifndef NDEBUG
1013 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1014 ClassType = MD->getParent();
1015 else
1016 ClassType = nullptr;
1017#endif
1018 Parms.insert(FD->param_begin(), FD->param_end());
1019 }
1020
1021 bool referencesArgs(Expr *E) {
1022 Result = false;
1023 TraverseStmt(E);
1024 return Result;
1025 }
1026
1027 bool VisitCXXThisExpr(CXXThisExpr *E) {
1028 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
1029 "`this` doesn't refer to the enclosing class?");
1030 Result = true;
1031 return false;
1032 }
1033
1034 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1035 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1036 if (Parms.count(PVD)) {
1037 Result = true;
1038 return false;
1039 }
1040 return true;
1041 }
1042};
1043}
1044
Erich Keanee891aa92018-07-13 15:07:47 +00001045static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001046 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001047
1048 Expr *Cond;
1049 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001050 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001051 return;
1052
1053 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001054 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +00001055 return;
1056
1057 DiagnoseIfAttr::DiagnosticType DiagType;
1058 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001059 S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
George Burgess IV177399e2017-01-09 04:12:14 +00001060 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001061 return;
1062 }
1063
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001064 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +00001065 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001066 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +00001067 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00001068 S.Context, AL, Cond, Msg, DiagType, ArgDependent, cast<NamedDecl>(D)));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001069}
1070
Guillaume Chatelet98f31512019-09-25 11:31:28 +02001071static void handleNoBuiltinAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1072 static constexpr const StringRef kWildcard = "*";
1073
1074 llvm::SmallVector<StringRef, 16> Names;
1075 bool HasWildcard = false;
1076
1077 const auto AddBuiltinName = [&Names, &HasWildcard](StringRef Name) {
1078 if (Name == kWildcard)
1079 HasWildcard = true;
1080 Names.push_back(Name);
1081 };
1082
1083 // Add previously defined attributes.
1084 if (const auto *NBA = D->getAttr<NoBuiltinAttr>())
1085 for (StringRef BuiltinName : NBA->builtinNames())
1086 AddBuiltinName(BuiltinName);
1087
1088 // Add current attributes.
1089 if (AL.getNumArgs() == 0)
1090 AddBuiltinName(kWildcard);
1091 else
1092 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
1093 StringRef BuiltinName;
1094 SourceLocation LiteralLoc;
1095 if (!S.checkStringLiteralArgumentAttr(AL, I, BuiltinName, &LiteralLoc))
1096 return;
1097
Guillaume Chatelet1c85a2e2019-10-29 17:28:34 +01001098 if (Builtin::Context::isBuiltinFunc(BuiltinName))
Guillaume Chatelet98f31512019-09-25 11:31:28 +02001099 AddBuiltinName(BuiltinName);
1100 else
1101 S.Diag(LiteralLoc, diag::warn_attribute_no_builtin_invalid_builtin_name)
Aaron Ballmandab43c82020-03-14 17:00:45 -04001102 << BuiltinName << AL;
Guillaume Chatelet98f31512019-09-25 11:31:28 +02001103 }
1104
1105 // Repeating the same attribute is fine.
1106 llvm::sort(Names);
1107 Names.erase(std::unique(Names.begin(), Names.end()), Names.end());
1108
1109 // Empty no_builtin must be on its own.
1110 if (HasWildcard && Names.size() > 1)
1111 S.Diag(D->getLocation(),
1112 diag::err_attribute_no_builtin_wildcard_or_builtin_name)
Aaron Ballmandab43c82020-03-14 17:00:45 -04001113 << AL;
Guillaume Chatelet98f31512019-09-25 11:31:28 +02001114
1115 if (D->hasAttr<NoBuiltinAttr>())
1116 D->dropAttr<NoBuiltinAttr>();
1117 D->addAttr(::new (S.Context)
1118 NoBuiltinAttr(S.Context, AL, Names.data(), Names.size()));
1119}
1120
Erich Keanee891aa92018-07-13 15:07:47 +00001121static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001122 if (D->hasAttr<PassObjectSizeAttr>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001123 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001124 return;
1125 }
1126
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001127 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001128 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001129 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001130 return;
1131
1132 // pass_object_size's argument is passed in as the second argument of
1133 // __builtin_object_size. So, it has the same constraints as that second
1134 // argument; namely, it must be in the range [0, 3].
1135 if (Type > 3) {
Aaron Ballman52c9ad22019-02-12 13:04:11 +00001136 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00001137 << AL << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001138 return;
1139 }
1140
1141 // pass_object_size is only supported on constant pointer parameters; as a
1142 // kindness to users, we allow the parameter to be non-const for declarations.
1143 // At this point, we have no clue if `D` belongs to a function declaration or
1144 // definition, so we defer the constness check until later.
1145 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001146 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001147 return;
1148 }
1149
Erich Keane6a24e802019-09-13 17:39:31 +00001150 D->addAttr(::new (S.Context) PassObjectSizeAttr(S.Context, AL, (int)Type));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001151}
1152
Erich Keanee891aa92018-07-13 15:07:47 +00001153static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001154 ConsumableAttr::ConsumedState DefaultState;
1155
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001156 if (AL.isArgIdent(0)) {
1157 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001158 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1159 DefaultState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001160 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1161 << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001162 return;
1163 }
David Blaikie16f76d22013-09-06 01:28:43 +00001164 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001165 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001166 << AL << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001167 return;
1168 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001169
Erich Keane6a24e802019-09-13 17:39:31 +00001170 D->addAttr(::new (S.Context) ConsumableAttr(S.Context, AL, DefaultState));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001171}
1172
1173static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Erich Keanee891aa92018-07-13 15:07:47 +00001174 const ParsedAttr &AL) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001175 QualType ThisType = MD->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001176
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001177 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1178 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmandab43c82020-03-14 17:00:45 -04001179 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) << RD;
Fangrui Song6907ce22018-07-30 19:24:48 +00001180
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001181 return false;
1182 }
1183 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001184
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001185 return true;
1186}
1187
Erich Keanee891aa92018-07-13 15:07:47 +00001188static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001189 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001190 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001191
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001192 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001193 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001194
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001195 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001196 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001197 CallableWhenAttr::ConsumedState CallableState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001198
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001199 StringRef StateString;
1200 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001201 if (AL.isArgIdent(ArgIndex)) {
1202 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001203 StateString = Ident->Ident->getName();
1204 Loc = Ident->Loc;
1205 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001206 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001207 return;
1208 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001209
1210 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001211 CallableState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001212 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001213 return;
1214 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001215
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001216 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001217 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001218
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001219 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00001220 CallableWhenAttr(S.Context, AL, States.data(), States.size()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001221}
1222
Erich Keanee891aa92018-07-13 15:07:47 +00001223static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001224 ParamTypestateAttr::ConsumedState ParamState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001225
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001226 if (AL.isArgIdent(0)) {
1227 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001228 StringRef StateString = Ident->Ident->getName();
1229
1230 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1231 ParamState)) {
1232 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00001233 << AL << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001234 return;
1235 }
1236 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001237 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1238 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001239 return;
1240 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001241
DeLesley Hutchins69391772013-10-17 23:23:53 +00001242 // FIXME: This check is currently being done in the analysis. It can be
1243 // enabled here only after the parser propagates attributes at
1244 // template specialization definition, not declaration.
1245 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1246 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1247 //
1248 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001249 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001250 // ReturnType.getAsString();
1251 // return;
1252 //}
Fangrui Song6907ce22018-07-30 19:24:48 +00001253
Erich Keane6a24e802019-09-13 17:39:31 +00001254 D->addAttr(::new (S.Context) ParamTypestateAttr(S.Context, AL, ParamState));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001255}
1256
Erich Keanee891aa92018-07-13 15:07:47 +00001257static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001258 ReturnTypestateAttr::ConsumedState ReturnState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001259
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001260 if (AL.isArgIdent(0)) {
1261 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001262 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1263 ReturnState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001264 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1265 << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001266 return;
1267 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001268 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001269 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1270 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001271 return;
1272 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001273
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001274 // FIXME: This check is currently being done in the analysis. It can be
1275 // enabled here only after the parser propagates attributes at
1276 // template specialization definition, not declaration.
1277 //QualType ReturnType;
1278 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001279 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1280 // ReturnType = Param->getType();
1281 //
1282 //} else if (const CXXConstructorDecl *Constructor =
1283 // dyn_cast<CXXConstructorDecl>(D)) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001284 // ReturnType = Constructor->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001285 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001286 //} else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001287 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001288 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1289 //}
1290 //
1291 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1292 //
1293 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1294 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1295 // ReturnType.getAsString();
1296 // return;
1297 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001298
Erich Keane6a24e802019-09-13 17:39:31 +00001299 D->addAttr(::new (S.Context) ReturnTypestateAttr(S.Context, AL, ReturnState));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001300}
1301
Erich Keanee891aa92018-07-13 15:07:47 +00001302static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001303 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001304 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001305
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001306 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001307 if (AL.isArgIdent(0)) {
1308 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001309 StringRef Param = Ident->Ident->getName();
1310 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001311 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1312 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001313 return;
1314 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001315 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001316 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1317 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001318 return;
1319 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001320
Erich Keane6a24e802019-09-13 17:39:31 +00001321 D->addAttr(::new (S.Context) SetTypestateAttr(S.Context, AL, NewState));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001322}
1323
Erich Keanee891aa92018-07-13 15:07:47 +00001324static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001325 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001326 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001327
1328 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001329 if (AL.isArgIdent(0)) {
1330 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001331 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001332 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001333 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1334 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001335 return;
1336 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001337 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001338 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1339 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001340 return;
1341 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001342
Erich Keane6a24e802019-09-13 17:39:31 +00001343 D->addAttr(::new (S.Context) TestTypestateAttr(S.Context, AL, TestState));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001344}
1345
Erich Keanee891aa92018-07-13 15:07:47 +00001346static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001347 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001348 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001349}
1350
Erich Keanee891aa92018-07-13 15:07:47 +00001351static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001352 if (auto *TD = dyn_cast<TagDecl>(D))
Erich Keane6a24e802019-09-13 17:39:31 +00001353 TD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001354 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001355 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1356 !FD->getType()->isIncompleteType() &&
1357 FD->isBitField() &&
1358 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001359
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001360 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1361 if (BitfieldByteAligned)
1362 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001363 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001364 << AL << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001365 else
Erich Keane6a24e802019-09-13 17:39:31 +00001366 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
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
Erich Keane6a24e802019-09-13 17:39:31 +00001372 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001373 }
1374
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001375 } else
Erich Keane44bacdf2018-08-09 13:21:32 +00001376 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001377}
1378
Erich Keanee891aa92018-07-13 15:07:47 +00001379static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001380 // The IBOutlet/IBOutletCollection attributes only apply to instance
1381 // variables or properties of Objective-C classes. The outlet must also
1382 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001383 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001384 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001385 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001386 << AL << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001387 return false;
1388 }
1389 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001390 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001391 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001392 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001393 << AL << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001394 return false;
1395 }
1396 }
1397 else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001398 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001399 return false;
1400 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001401
Ted Kremenek7fd17232011-09-29 07:02:25 +00001402 return true;
1403}
1404
Erich Keanee891aa92018-07-13 15:07:47 +00001405static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001406 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001407 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001408
Erich Keane6a24e802019-09-13 17:39:31 +00001409 D->addAttr(::new (S.Context) IBOutletAttr(S.Context, AL));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001410}
1411
Erich Keanee891aa92018-07-13 15:07:47 +00001412static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001413
1414 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001415 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001416 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001417 return;
1418 }
1419
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001420 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001421 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001422
Richard Smithb1f9a282013-10-31 01:56:18 +00001423 ParsedType PT;
1424
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001425 if (AL.hasParsedType())
1426 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001427 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001428 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001429 S.getScopeForContext(D->getDeclContext()->getParent()));
1430 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001431 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001432 return;
1433 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001434 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001435
Craig Topperc3ec1492014-05-26 06:22:03 +00001436 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001437 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1438 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001439 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001440
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001441 // Diagnose use of non-object type in iboutletcollection attribute.
1442 // FIXME. Gnu attribute extension ignores use of builtin types in
1443 // attributes. So, __attribute__((iboutletcollection(char))) will be
1444 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001445 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001446 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001447 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1448 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001449 return;
1450 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001451
Erich Keane6a24e802019-09-13 17:39:31 +00001452 D->addAttr(::new (S.Context) IBOutletCollectionAttr(S.Context, AL, QTLoc));
Ted Kremenek26bde772010-05-19 17:38:06 +00001453}
1454
Hal Finkelee90a222014-09-26 05:04:30 +00001455bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1456 if (RefOkay) {
1457 if (T->isReferenceType())
1458 return true;
1459 } else {
1460 T = T.getNonReferenceType();
1461 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001462
Hal Finkelee90a222014-09-26 05:04:30 +00001463 // The nonnull attribute, and other similar attributes, can be applied to a
1464 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001465 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001466 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1467 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001468 for (const auto *I : UD->fields()) {
1469 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001470 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1471 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001472 }
1473 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001474 }
1475
1476 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001477}
1478
Erich Keanee891aa92018-07-13 15:07:47 +00001479static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001480 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001481 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001482 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001483 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001484 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001485 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001486 << AL << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001487 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001488 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001489 << AL << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001490 return false;
1491 }
1492 return true;
1493}
1494
Erich Keanee891aa92018-07-13 15:07:47 +00001495static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001496 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001497 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1498 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001499 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001500 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001501 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001502
1503 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001504 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1505 !attrNonNullArgCheck(
1506 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1507 Ex->getSourceRange(),
1508 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001509 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001510
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001511 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001512 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001513
1514 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001515 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1516 // check if the attribute came from a macro expansion or a template
1517 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001518 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001519 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001520 bool AnyPointers = isFunctionOrMethodVariadic(D);
1521 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1522 I != E && !AnyPointers; ++I) {
1523 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001524 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001525 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001526 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001527
Richard Smith588bd9b2014-08-27 04:59:42 +00001528 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001529 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001530 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001531
Joel E. Denny81508102018-03-13 14:51:22 +00001532 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001533 unsigned Size = NonNullArgs.size();
1534 llvm::array_pod_sort(Start, Start + Size);
Erich Keane6a24e802019-09-13 17:39:31 +00001535 D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, Start, Size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001536}
1537
Jordan Rosec9399072014-02-11 17:27:59 +00001538static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00001539 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001540 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001541 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001542 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001543 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001544 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001545 << D->getSourceRange();
1546 }
1547 return;
1548 }
1549
1550 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001551 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001552 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001553 return;
1554
Erich Keane6a24e802019-09-13 17:39:31 +00001555 D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, nullptr, 0));
Jordan Rosec9399072014-02-11 17:27:59 +00001556}
1557
Erich Keanee891aa92018-07-13 15:07:47 +00001558static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001559 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001560 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001561 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001562 /* isReturnValue */ true))
1563 return;
1564
Erich Keane6a24e802019-09-13 17:39:31 +00001565 D->addAttr(::new (S.Context) ReturnsNonNullAttr(S.Context, AL));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001566}
1567
Erich Keanee891aa92018-07-13 15:07:47 +00001568static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001569 if (D->isInvalidDecl())
1570 return;
1571
1572 // noescape only applies to pointer types.
1573 QualType T = cast<ParmVarDecl>(D)->getType();
1574 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001575 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001576 << AL << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001577 return;
1578 }
1579
Erich Keane6a24e802019-09-13 17:39:31 +00001580 D->addAttr(::new (S.Context) NoEscapeAttr(S.Context, AL));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001581}
1582
Erich Keanee891aa92018-07-13 15:07:47 +00001583static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001584 Expr *E = AL.getArgAsExpr(0),
1585 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
Erich Keane6a24e802019-09-13 17:39:31 +00001586 S.AddAssumeAlignedAttr(D, AL, E, OE);
Hal Finkelee90a222014-09-26 05:04:30 +00001587}
1588
Erich Keanee891aa92018-07-13 15:07:47 +00001589static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00001590 S.AddAllocAlignAttr(D, AL, AL.getArgAsExpr(0));
Erich Keane623efd82017-03-30 21:48:55 +00001591}
1592
Erich Keane6a24e802019-09-13 17:39:31 +00001593void Sema::AddAssumeAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
1594 Expr *OE) {
Hal Finkelee90a222014-09-26 05:04:30 +00001595 QualType ResultType = getFunctionOrMethodResultType(D);
1596 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1597
Erich Keane6a24e802019-09-13 17:39:31 +00001598 AssumeAlignedAttr TmpAttr(Context, CI, E, OE);
1599 SourceLocation AttrLoc = TmpAttr.getLocation();
Hal Finkelee90a222014-09-26 05:04:30 +00001600
1601 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1602 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
Erich Keane6a24e802019-09-13 17:39:31 +00001603 << &TmpAttr << TmpAttr.getRange() << SR;
Hal Finkelee90a222014-09-26 05:04:30 +00001604 return;
1605 }
1606
1607 if (!E->isValueDependent()) {
1608 llvm::APSInt I(64);
1609 if (!E->isIntegerConstantExpr(I, Context)) {
1610 if (OE)
1611 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1612 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1613 << E->getSourceRange();
1614 else
1615 Diag(AttrLoc, diag::err_attribute_argument_type)
1616 << &TmpAttr << AANT_ArgumentIntegerConstant
1617 << E->getSourceRange();
1618 return;
1619 }
1620
1621 if (!I.isPowerOf2()) {
1622 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1623 << E->getSourceRange();
1624 return;
1625 }
Roman Lebedev0a002f62020-01-23 22:48:57 +03001626
Roman Lebedev1d0972f2020-01-24 17:01:27 +03001627 if (I > Sema::MaximumAlignment)
Roman Lebedev0a002f62020-01-23 22:48:57 +03001628 Diag(CI.getLoc(), diag::warn_assume_aligned_too_great)
Roman Lebedev1d0972f2020-01-24 17:01:27 +03001629 << CI.getRange() << Sema::MaximumAlignment;
Hal Finkelee90a222014-09-26 05:04:30 +00001630 }
1631
1632 if (OE) {
1633 if (!OE->isValueDependent()) {
1634 llvm::APSInt I(64);
1635 if (!OE->isIntegerConstantExpr(I, Context)) {
1636 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1637 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1638 << OE->getSourceRange();
1639 return;
1640 }
1641 }
1642 }
1643
Erich Keane6a24e802019-09-13 17:39:31 +00001644 D->addAttr(::new (Context) AssumeAlignedAttr(Context, CI, E, OE));
Hal Finkelee90a222014-09-26 05:04:30 +00001645}
1646
Erich Keane6a24e802019-09-13 17:39:31 +00001647void Sema::AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI,
1648 Expr *ParamExpr) {
Erich Keane623efd82017-03-30 21:48:55 +00001649 QualType ResultType = getFunctionOrMethodResultType(D);
1650
Erich Keane6a24e802019-09-13 17:39:31 +00001651 AllocAlignAttr TmpAttr(Context, CI, ParamIdx());
1652 SourceLocation AttrLoc = CI.getLoc();
Erich Keane623efd82017-03-30 21:48:55 +00001653
1654 if (!ResultType->isDependentType() &&
1655 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1656 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
Erich Keane6a24e802019-09-13 17:39:31 +00001657 << &TmpAttr << CI.getRange() << getFunctionOrMethodResultSourceRange(D);
Erich Keane623efd82017-03-30 21:48:55 +00001658 return;
1659 }
1660
Joel E. Denny81508102018-03-13 14:51:22 +00001661 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001662 const auto *FuncDecl = cast<FunctionDecl>(D);
1663 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001664 /*AttrArgNum=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001665 return;
1666
Joel E. Denny81508102018-03-13 14:51:22 +00001667 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Roman Lebedevb749af62020-01-23 22:50:34 +03001668 if (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1669 !Ty->isAlignValT()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001670 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001671 << &TmpAttr
1672 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001673 return;
1674 }
1675
Erich Keane6a24e802019-09-13 17:39:31 +00001676 D->addAttr(::new (Context) AllocAlignAttr(Context, CI, Idx));
Erich Keane623efd82017-03-30 21:48:55 +00001677}
1678
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001679/// Normalize the attribute, __foo__ becomes foo.
1680/// Returns true if normalization was applied.
1681static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001682 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1683 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001684 AttrName = AttrName.drop_front(2).drop_back(2);
1685 return true;
1686 }
1687 return false;
1688}
1689
Erich Keanee891aa92018-07-13 15:07:47 +00001690static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001691 // This attribute must be applied to a function declaration. The first
1692 // argument to the attribute must be an identifier, the name of the resource,
1693 // for example: malloc. The following arguments must be argument indexes, the
1694 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001695 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001696 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001697 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001698
Aaron Ballman00e99962013-08-31 01:11:41 +00001699 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001700 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001701 << AL << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001702 return;
1703 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001704
Richard Smith852e9ce2013-11-27 01:46:48 +00001705 // Figure out our Kind.
1706 OwnershipAttr::OwnershipKind K =
Erich Keane6a24e802019-09-13 17:39:31 +00001707 OwnershipAttr(S.Context, AL, nullptr, nullptr, 0).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001708
Richard Smith852e9ce2013-11-27 01:46:48 +00001709 // Check arguments.
1710 switch (K) {
1711 case OwnershipAttr::Takes:
1712 case OwnershipAttr::Holds:
1713 if (AL.getNumArgs() < 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001714 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001715 return;
1716 }
1717 break;
1718 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001719 if (AL.getNumArgs() > 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001720 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001721 return;
1722 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001723 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001724 }
1725
Richard Smith852e9ce2013-11-27 01:46:48 +00001726 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001727
Richard Smith852e9ce2013-11-27 01:46:48 +00001728 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001729 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001730 Module = &S.PP.getIdentifierTable().get(ModuleName);
1731 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001732
Joel E. Denny81508102018-03-13 14:51:22 +00001733 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001734 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1735 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001736 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001737 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001738 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001739
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001740 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001741 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001742 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001743 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001744 case OwnershipAttr::Takes:
1745 case OwnershipAttr::Holds:
1746 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1747 Err = 0;
1748 break;
1749 case OwnershipAttr::Returns:
1750 if (!T->isIntegerType())
1751 Err = 1;
1752 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001753 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001754 if (-1 != Err) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001755 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1756 << Ex->getSourceRange();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001757 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001758 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001759
1760 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001761 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001762 // Cannot have two ownership attributes of different kinds for the same
1763 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001764 if (I->getOwnKind() != K && I->args_end() !=
1765 std::find(I->args_begin(), I->args_end(), Idx)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001766 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001767 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001768 } else if (K == OwnershipAttr::Returns &&
1769 I->getOwnKind() == OwnershipAttr::Returns) {
1770 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001771 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001772 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1773 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001774 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001775 if (I->args_size())
1776 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001777 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001778 return;
1779 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001780 }
1781 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001782 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001783 }
1784
Joel E. Denny81508102018-03-13 14:51:22 +00001785 ParamIdx *Start = OwnershipArgs.data();
1786 unsigned Size = OwnershipArgs.size();
1787 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001788 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00001789 OwnershipAttr(S.Context, AL, Module, Start, Size));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001790}
1791
Erich Keanee891aa92018-07-13 15:07:47 +00001792static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001793 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001794 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001795 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001796 return;
1797 }
1798
1799 // gcc rejects
1800 // class c {
1801 // static int a __attribute__((weakref ("v2")));
1802 // static int b() __attribute__((weakref ("f3")));
1803 // };
1804 // and ignores the attributes of
1805 // void f(void) {
1806 // static int a __attribute__((weakref ("v2")));
1807 // }
1808 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001809 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001810 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001811 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1812 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001813 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001814 }
1815
1816 // The GCC manual says
1817 //
1818 // At present, a declaration to which `weakref' is attached can only
1819 // be `static'.
1820 //
1821 // It also says
1822 //
1823 // Without a TARGET,
1824 // given as an argument to `weakref' or to `alias', `weakref' is
1825 // equivalent to `weak'.
1826 //
1827 // gcc 4.4.1 will accept
1828 // int a7 __attribute__((weakref));
1829 // as
1830 // int a7 __attribute__((weak));
1831 // This looks like a bug in gcc. We reject that for now. We should revisit
1832 // it if this behaviour is actually used.
1833
Rafael Espindolac18086a2010-02-23 22:00:30 +00001834 // GCC rejects
1835 // static ((alias ("y"), weakref)).
1836 // Should we? How to check that weakref is before or after alias?
1837
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001838 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1839 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1840 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001841 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001842 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001843 // GCC will accept anything as the argument of weakref. Should we
1844 // check for an existing decl?
Erich Keane6a24e802019-09-13 17:39:31 +00001845 D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001846
Erich Keane6a24e802019-09-13 17:39:31 +00001847 D->addAttr(::new (S.Context) WeakRefAttr(S.Context, AL));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001848}
1849
Erich Keanee891aa92018-07-13 15:07:47 +00001850static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001851 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001852 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001853 return;
1854
1855 // Aliases should be on declarations, not definitions.
1856 const auto *FD = cast<FunctionDecl>(D);
1857 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001858 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001859 return;
1860 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001861
Erich Keane6a24e802019-09-13 17:39:31 +00001862 D->addAttr(::new (S.Context) IFuncAttr(S.Context, AL, Str));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001863}
1864
Erich Keanee891aa92018-07-13 15:07:47 +00001865static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001866 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001867 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001868 return;
1869
Douglas Gregore8bbc122011-09-02 00:18:52 +00001870 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001871 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001872 return;
1873 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001874 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001875 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001876 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001877
David Majnemer2dc81462015-01-19 09:00:28 +00001878 // Aliases should be on declarations, not definitions.
1879 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1880 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001881 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001882 return;
1883 }
1884 } else {
1885 const auto *VD = cast<VarDecl>(D);
1886 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001887 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001888 return;
1889 }
1890 }
1891
Nick Desaulniers9b9fe412019-01-09 23:54:55 +00001892 // Mark target used to prevent unneeded-internal-declaration warnings.
1893 if (!S.LangOpts.CPlusPlus) {
1894 // FIXME: demangle Str for C++, as the attribute refers to the mangled
1895 // linkage name, not the pre-mangled identifier.
1896 const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc());
1897 LookupResult LR(S, target, Sema::LookupOrdinaryName);
1898 if (S.LookupQualifiedName(LR, S.getCurLexicalContext()))
1899 for (NamedDecl *ND : LR)
1900 ND->markUsed(S.Context);
1901 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001902
Erich Keane6a24e802019-09-13 17:39:31 +00001903 D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001904}
1905
Erich Keanee891aa92018-07-13 15:07:47 +00001906static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001907 StringRef Model;
1908 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001909 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001910 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001911 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001912
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001913 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001914 if (Model != "global-dynamic" && Model != "local-dynamic"
1915 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001916 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001917 return;
1918 }
1919
Erich Keane6a24e802019-09-13 17:39:31 +00001920 D->addAttr(::new (S.Context) TLSModelAttr(S.Context, AL, Model));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001921}
1922
Erich Keanee891aa92018-07-13 15:07:47 +00001923static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001924 QualType ResultType = getFunctionOrMethodResultType(D);
1925 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
Erich Keane6a24e802019-09-13 17:39:31 +00001926 D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL));
David Majnemer631a90b2015-02-04 07:23:21 +00001927 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001928 }
1929
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001930 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001931 << AL << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001932}
1933
Erich Keane3efe0022018-07-20 14:13:28 +00001934static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1935 FunctionDecl *FD = cast<FunctionDecl>(D);
Erich Keane659c8712018-09-10 14:31:56 +00001936
1937 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1938 if (MD->getParent()->isLambda()) {
1939 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
1940 return;
1941 }
1942 }
1943
Erich Keane3efe0022018-07-20 14:13:28 +00001944 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1945 return;
1946
1947 SmallVector<IdentifierInfo *, 8> CPUs;
1948 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
1949 if (!AL.isArgIdent(ArgNo)) {
1950 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001951 << AL << AANT_ArgumentIdentifier;
Erich Keane3efe0022018-07-20 14:13:28 +00001952 return;
1953 }
1954
1955 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1956 StringRef CPUName = CPUArg->Ident->getName().trim();
1957
1958 if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1959 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1960 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1961 return;
1962 }
1963
1964 const TargetInfo &Target = S.Context.getTargetInfo();
1965 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1966 return Target.CPUSpecificManglingCharacter(CPUName) ==
1967 Target.CPUSpecificManglingCharacter(Cur->getName());
1968 })) {
1969 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1970 return;
1971 }
1972 CPUs.push_back(CPUArg->Ident);
1973 }
1974
1975 FD->setIsMultiVersion(true);
1976 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
Erich Keane6a24e802019-09-13 17:39:31 +00001977 D->addAttr(::new (S.Context)
1978 CPUSpecificAttr(S.Context, AL, CPUs.data(), CPUs.size()));
Erich Keane3efe0022018-07-20 14:13:28 +00001979 else
Erich Keane6a24e802019-09-13 17:39:31 +00001980 D->addAttr(::new (S.Context)
1981 CPUDispatchAttr(S.Context, AL, CPUs.data(), CPUs.size()));
Erich Keane3efe0022018-07-20 14:13:28 +00001982}
1983
Erich Keanee891aa92018-07-13 15:07:47 +00001984static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001985 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001986 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00001987 << AL << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001988 return;
1989 }
1990
Erich Keane44bacdf2018-08-09 13:21:32 +00001991 if (CommonAttr *CA = S.mergeCommonAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001992 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001993}
1994
Erich Keanee891aa92018-07-13 15:07:47 +00001995static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001996 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
Charles Davis0e379112016-08-08 21:19:08 +00001997 return;
1998
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001999 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002000 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
2001 const auto &Arch = Triple.getArch();
2002 if (Arch != llvm::Triple::x86 &&
2003 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002004 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
Erich Keane44bacdf2018-08-09 13:21:32 +00002005 << AL << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002006 return;
2007 }
2008 }
2009
Erich Keane6a24e802019-09-13 17:39:31 +00002010 D->addAttr(::new (S.Context) NakedAttr(S.Context, AL));
Charles Davis0e379112016-08-08 21:19:08 +00002011}
2012
Erich Keanee891aa92018-07-13 15:07:47 +00002013static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002014 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00002015
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002016 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002017 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002018 << Attrs << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002019 return;
2020 }
2021
Erich Keane6a24e802019-09-13 17:39:31 +00002022 D->addAttr(::new (S.Context) NoReturnAttr(S.Context, Attrs));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002023}
2024
Erich Keanee891aa92018-07-13 15:07:47 +00002025static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00002026 if (!S.getLangOpts().CFProtectionBranch)
2027 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
2028 else
2029 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00002030}
2031
Erich Keanee891aa92018-07-13 15:07:47 +00002032bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002033 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
2034 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002035 return true;
2036 }
2037
2038 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002039}
2040
Erich Keanee891aa92018-07-13 15:07:47 +00002041bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002042 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002043 if (!AL.existsInTarget(Context.getTargetInfo())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002044 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002045 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002046 return true;
2047 }
2048
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002049 return false;
2050}
2051
Erich Keanee891aa92018-07-13 15:07:47 +00002052static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2053
Ted Kremenek5295ce82010-08-19 00:51:58 +00002054 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2055 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00002056 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002057 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00002058 if (!VD || (!VD->getType()->isBlockPointerType() &&
2059 !VD->getType()->isFunctionPointerType())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002060 S.Diag(AL.getLoc(), AL.isCXX11Attribute()
2061 ? diag::err_attribute_wrong_decl_type
2062 : diag::warn_attribute_wrong_decl_type)
2063 << AL << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00002064 return;
2065 }
2066 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002067
Erich Keane6a24e802019-09-13 17:39:31 +00002068 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(S.Context, AL));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002069}
2070
John Thompsoncdb847ba2010-08-09 21:53:52 +00002071// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00002072static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2073 /*
2074 Returning a Vector Class in Registers
2075
2076 According to the PPU ABI specifications, a class with a single member of
2077 vector type is returned in memory when used as the return value of a
2078 function.
2079 This results in inefficient code when implementing vector classes. To return
2080 the value in a single vector register, add the vecreturn attribute to the
2081 class definition. This attribute is also applicable to struct types.
2082
2083 Example:
2084
2085 struct Vector
2086 {
2087 __vector float xyzw;
2088 } __attribute__((vecreturn));
2089
2090 Vector Add(Vector lhs, Vector rhs)
2091 {
2092 Vector result;
2093 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2094 return result; // This will be returned in a register
2095 }
2096 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002097 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002098 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002099 return;
2100 }
2101
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002102 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002103 int count = 0;
2104
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002105 if (!isa<CXXRecordDecl>(R)) {
2106 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002107 return;
2108 }
2109
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002110 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2111 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002112 return;
2113 }
2114
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002115 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002116 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002117 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002118 return;
2119 }
2120 count++;
2121 }
2122
Erich Keane6a24e802019-09-13 17:39:31 +00002123 D->addAttr(::new (S.Context) VecReturnAttr(S.Context, AL));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002124}
2125
Richard Smithe233fbf2013-01-28 22:42:45 +00002126static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002127 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002128 if (isa<ParmVarDecl>(D)) {
2129 // [[carries_dependency]] can only be applied to a parameter if it is a
2130 // parameter of a function declaration or lambda.
2131 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002132 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002133 diag::err_carries_dependency_param_not_function_decl);
2134 return;
2135 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002136 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002137
Erich Keane6a24e802019-09-13 17:39:31 +00002138 D->addAttr(::new (S.Context) CarriesDependencyAttr(S.Context, AL));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002139}
2140
Erich Keanee891aa92018-07-13 15:07:47 +00002141static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002142 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002143
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002144 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002145 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002146 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Erich Keane44bacdf2018-08-09 13:21:32 +00002147 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002148
Erich Keane6a24e802019-09-13 17:39:31 +00002149 D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002150}
2151
Erich Keanee891aa92018-07-13 15:07:47 +00002152static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002153 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002154 if (AL.getNumArgs() &&
2155 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002156 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002157
Erich Keane6a24e802019-09-13 17:39:31 +00002158 D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00002159}
2160
Erich Keanee891aa92018-07-13 15:07:47 +00002161static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002162 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002163 if (AL.getNumArgs() &&
2164 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002165 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002166
Erich Keane6a24e802019-09-13 17:39:31 +00002167 D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00002168}
2169
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002170template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002171static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002172 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002173 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002174 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002175 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002176
Erich Keane6a24e802019-09-13 17:39:31 +00002177 D->addAttr(::new (S.Context) AttrTy(S.Context, AL, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002178}
2179
Ted Kremenek438f8db2014-02-22 01:06:05 +00002180static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002181 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002182 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002183 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
Erich Keane44bacdf2018-08-09 13:21:32 +00002184 << AL << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002185 return;
2186 }
2187
Erich Keane6a24e802019-09-13 17:39:31 +00002188 D->addAttr(::new (S.Context) ObjCExplicitProtocolImplAttr(S.Context, AL));
Ted Kremenek28eace62013-11-23 01:01:34 +00002189}
2190
Jordy Rose740b0c22012-05-08 03:27:22 +00002191static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2192 IdentifierInfo *Platform,
2193 VersionTuple Introduced,
2194 VersionTuple Deprecated,
2195 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002196 StringRef PlatformName
2197 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2198 if (PlatformName.empty())
2199 PlatformName = Platform->getName();
2200
2201 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2202 // of these steps are needed).
2203 if (!Introduced.empty() && !Deprecated.empty() &&
2204 !(Introduced <= Deprecated)) {
2205 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2206 << 1 << PlatformName << Deprecated.getAsString()
2207 << 0 << Introduced.getAsString();
2208 return true;
2209 }
2210
2211 if (!Introduced.empty() && !Obsoleted.empty() &&
2212 !(Introduced <= Obsoleted)) {
2213 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2214 << 2 << PlatformName << Obsoleted.getAsString()
2215 << 0 << Introduced.getAsString();
2216 return true;
2217 }
2218
2219 if (!Deprecated.empty() && !Obsoleted.empty() &&
2220 !(Deprecated <= Obsoleted)) {
2221 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2222 << 2 << PlatformName << Obsoleted.getAsString()
2223 << 1 << Deprecated.getAsString();
2224 return true;
2225 }
2226
2227 return false;
2228}
2229
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002230/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002231///
2232/// If either version tuple is empty, then they are assumed to match. If
2233/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2234static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2235 bool BeforeIsOkay) {
2236 if (X.empty() || Y.empty())
2237 return true;
2238
2239 if (X == Y)
2240 return true;
2241
2242 if (BeforeIsOkay && X < Y)
2243 return true;
2244
2245 return false;
2246}
2247
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002248AvailabilityAttr *Sema::mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002249 NamedDecl *D, const AttributeCommonInfo &CI, IdentifierInfo *Platform,
2250 bool Implicit, VersionTuple Introduced, VersionTuple Deprecated,
2251 VersionTuple Obsoleted, bool IsUnavailable, StringRef Message,
2252 bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK,
2253 int Priority) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002254 VersionTuple MergedIntroduced = Introduced;
2255 VersionTuple MergedDeprecated = Deprecated;
2256 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002257 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002258 bool OverrideOrImpl = false;
2259 switch (AMK) {
2260 case AMK_None:
2261 case AMK_Redeclaration:
2262 OverrideOrImpl = false;
2263 break;
2264
2265 case AMK_Override:
2266 case AMK_ProtocolImplementation:
2267 OverrideOrImpl = true;
2268 break;
2269 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002270
Rafael Espindolac67f2232012-05-10 02:50:16 +00002271 if (D->hasAttrs()) {
2272 AttrVec &Attrs = D->getAttrs();
2273 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002274 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002275 if (!OldAA) {
2276 ++i;
2277 continue;
2278 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002279
Rafael Espindolac67f2232012-05-10 02:50:16 +00002280 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2281 if (OldPlatform != Platform) {
2282 ++i;
2283 continue;
2284 }
2285
Tim Northover7a73cc72015-10-30 16:30:49 +00002286 // If there is an existing availability attribute for this platform that
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002287 // has a lower priority use the existing one and discard the new
2288 // attribute.
2289 if (OldAA->getPriority() < Priority)
Tim Northover7a73cc72015-10-30 16:30:49 +00002290 return nullptr;
Tim Northover7a73cc72015-10-30 16:30:49 +00002291
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002292 // If there is an existing attribute for this platform that has a higher
2293 // priority than the new attribute then erase the old one and continue
2294 // processing the attributes.
2295 if (OldAA->getPriority() > Priority) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002296 Attrs.erase(Attrs.begin() + i);
2297 --e;
2298 continue;
2299 }
2300
Rafael Espindolac67f2232012-05-10 02:50:16 +00002301 FoundAny = true;
2302 VersionTuple OldIntroduced = OldAA->getIntroduced();
2303 VersionTuple OldDeprecated = OldAA->getDeprecated();
2304 VersionTuple OldObsoleted = OldAA->getObsoleted();
2305 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002306
Douglas Gregord2a713e2015-09-30 21:27:42 +00002307 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2308 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2309 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002310 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002311 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2312 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002313 int Which = -1;
2314 VersionTuple FirstVersion;
2315 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002316 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002317 Which = 0;
2318 FirstVersion = OldIntroduced;
2319 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002320 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002321 Which = 1;
2322 FirstVersion = Deprecated;
2323 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002324 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002325 Which = 2;
2326 FirstVersion = Obsoleted;
2327 SecondVersion = OldObsoleted;
2328 }
2329
2330 if (Which == -1) {
2331 Diag(OldAA->getLocation(),
2332 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002333 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2334 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002335 } else {
2336 Diag(OldAA->getLocation(),
2337 diag::warn_mismatched_availability_override)
2338 << Which
2339 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002340 << FirstVersion.getAsString() << SecondVersion.getAsString()
2341 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002342 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002343 if (AMK == AMK_Override)
Erich Keane6a24e802019-09-13 17:39:31 +00002344 Diag(CI.getLoc(), diag::note_overridden_method);
Douglas Gregord2a713e2015-09-30 21:27:42 +00002345 else
Erich Keane6a24e802019-09-13 17:39:31 +00002346 Diag(CI.getLoc(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002347 } else {
2348 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
Erich Keane6a24e802019-09-13 17:39:31 +00002349 Diag(CI.getLoc(), diag::note_previous_attribute);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002350 }
2351
Rafael Espindolac67f2232012-05-10 02:50:16 +00002352 Attrs.erase(Attrs.begin() + i);
2353 --e;
2354 continue;
2355 }
2356
2357 VersionTuple MergedIntroduced2 = MergedIntroduced;
2358 VersionTuple MergedDeprecated2 = MergedDeprecated;
2359 VersionTuple MergedObsoleted2 = MergedObsoleted;
2360
2361 if (MergedIntroduced2.empty())
2362 MergedIntroduced2 = OldIntroduced;
2363 if (MergedDeprecated2.empty())
2364 MergedDeprecated2 = OldDeprecated;
2365 if (MergedObsoleted2.empty())
2366 MergedObsoleted2 = OldObsoleted;
2367
2368 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2369 MergedIntroduced2, MergedDeprecated2,
2370 MergedObsoleted2)) {
2371 Attrs.erase(Attrs.begin() + i);
2372 --e;
2373 continue;
2374 }
2375
2376 MergedIntroduced = MergedIntroduced2;
2377 MergedDeprecated = MergedDeprecated2;
2378 MergedObsoleted = MergedObsoleted2;
2379 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002380 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002381 }
2382
2383 if (FoundAny &&
2384 MergedIntroduced == Introduced &&
2385 MergedDeprecated == Deprecated &&
2386 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002387 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002388
Douglas Gregord2a713e2015-09-30 21:27:42 +00002389 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002390 // the checking.
Erich Keane6a24e802019-09-13 17:39:31 +00002391 if (!checkAvailabilityAttr(*this, CI.getRange(), Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002392 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002393 !OverrideOrImpl) {
Erich Keane6a24e802019-09-13 17:39:31 +00002394 auto *Avail = ::new (Context) AvailabilityAttr(
2395 Context, CI, Platform, Introduced, Deprecated, Obsoleted, IsUnavailable,
2396 Message, IsStrict, Replacement, Priority);
Manman Ren719a8642016-05-06 21:04:01 +00002397 Avail->setImplicit(Implicit);
2398 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002399 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002400 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002401}
2402
Erich Keanee891aa92018-07-13 15:07:47 +00002403static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002404 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002405 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002406 IdentifierLoc *Platform = AL.getArgAsIdent(0);
Fangrui Song6907ce22018-07-30 19:24:48 +00002407
Aaron Ballman00e99962013-08-31 01:11:41 +00002408 IdentifierInfo *II = Platform->Ident;
2409 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2410 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2411 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002412
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002413 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002414 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002415 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002416
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002417 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2418 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2419 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2420 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2421 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002422 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002423 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002424 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002425 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002426 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002427 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002428
Michael Wu260e9622018-11-12 02:44:33 +00002429 if (II->isStr("swift")) {
2430 if (Introduced.isValid() || Obsoleted.isValid() ||
2431 (!IsUnavailable && !Deprecated.isValid())) {
2432 S.Diag(AL.getLoc(),
2433 diag::warn_availability_swift_unavailable_deprecated_only);
2434 return;
2435 }
2436 }
2437
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002438 int PriorityModifier = AL.isPragmaClangAttribute()
2439 ? Sema::AP_PragmaClangAttribute
2440 : Sema::AP_Explicit;
2441 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002442 ND, AL, II, false /*Implicit*/, Introduced.Version, Deprecated.Version,
2443 Obsoleted.Version, IsUnavailable, Str, IsStrict, Replacement,
2444 Sema::AMK_None, PriorityModifier);
Rafael Espindola19de5612013-01-12 06:42:30 +00002445 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002446 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002447
2448 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2449 // matches before the start of the watchOS platform.
2450 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2451 IdentifierInfo *NewII = nullptr;
2452 if (II->getName() == "ios")
2453 NewII = &S.Context.Idents.get("watchos");
2454 else if (II->getName() == "ios_app_extension")
2455 NewII = &S.Context.Idents.get("watchos_app_extension");
2456
2457 if (NewII) {
2458 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2459 if (Version.empty())
2460 return Version;
2461 auto Major = Version.getMajor();
2462 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2463 if (NewMajor >= 2) {
2464 if (Version.getMinor().hasValue()) {
2465 if (Version.getSubminor().hasValue())
2466 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2467 Version.getSubminor().getValue());
2468 else
2469 return VersionTuple(NewMajor, Version.getMinor().getValue());
2470 }
Alex Lorenz0b436482019-03-20 20:02:00 +00002471 return VersionTuple(NewMajor);
Tim Northover7a73cc72015-10-30 16:30:49 +00002472 }
2473
2474 return VersionTuple(2, 0);
2475 };
2476
2477 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2478 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2479 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2480
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002481 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002482 ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated,
2483 NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
2484 Sema::AMK_None,
2485 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
Tim Northover7a73cc72015-10-30 16:30:49 +00002486 if (NewAttr)
2487 D->addAttr(NewAttr);
2488 }
2489 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2490 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2491 // matches before the start of the tvOS platform.
2492 IdentifierInfo *NewII = nullptr;
2493 if (II->getName() == "ios")
2494 NewII = &S.Context.Idents.get("tvos");
2495 else if (II->getName() == "ios_app_extension")
2496 NewII = &S.Context.Idents.get("tvos_app_extension");
2497
2498 if (NewII) {
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002499 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002500 ND, AL, NewII, true /*Implicit*/, Introduced.Version,
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002501 Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict,
2502 Replacement, Sema::AMK_None,
Erich Keane6a24e802019-09-13 17:39:31 +00002503 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002504 if (NewAttr)
2505 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002506 }
2507 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002508}
2509
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002510static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002511 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002512 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002513 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002514 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002515 "Invalid number of arguments in an external_source_symbol attribute");
2516
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002517 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002518 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002519 Language = SE->getString();
2520 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002521 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002522 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002523 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002524
2525 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002526 S.Context, AL, Language, DefinedIn, IsGeneratedDeclaration));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002527}
2528
John McCalld041a9b2013-02-20 01:54:26 +00002529template <class T>
Erich Keane6a24e802019-09-13 17:39:31 +00002530static T *mergeVisibilityAttr(Sema &S, Decl *D, const AttributeCommonInfo &CI,
2531 typename T::VisibilityType value) {
John McCalld041a9b2013-02-20 01:54:26 +00002532 T *existingAttr = D->getAttr<T>();
2533 if (existingAttr) {
2534 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2535 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002536 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002537 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
Erich Keane6a24e802019-09-13 17:39:31 +00002538 S.Diag(CI.getLoc(), diag::note_previous_attribute);
John McCalld041a9b2013-02-20 01:54:26 +00002539 D->dropAttr<T>();
2540 }
Erich Keane6a24e802019-09-13 17:39:31 +00002541 return ::new (S.Context) T(S.Context, CI, value);
John McCalld041a9b2013-02-20 01:54:26 +00002542}
2543
Erich Keane6a24e802019-09-13 17:39:31 +00002544VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D,
2545 const AttributeCommonInfo &CI,
2546 VisibilityAttr::VisibilityType Vis) {
2547 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, CI, Vis);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002548}
2549
Erich Keane6a24e802019-09-13 17:39:31 +00002550TypeVisibilityAttr *
2551Sema::mergeTypeVisibilityAttr(Decl *D, const AttributeCommonInfo &CI,
2552 TypeVisibilityAttr::VisibilityType Vis) {
2553 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, CI, Vis);
John McCalld041a9b2013-02-20 01:54:26 +00002554}
2555
Erich Keanee891aa92018-07-13 15:07:47 +00002556static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002557 bool isTypeVisibility) {
2558 // Visibility attributes don't mean anything on a typedef.
2559 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002560 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002561 return;
2562 }
2563
2564 // 'type_visibility' can only go on a type or namespace.
2565 if (isTypeVisibility &&
2566 !(isa<TagDecl>(D) ||
2567 isa<ObjCInterfaceDecl>(D) ||
2568 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002569 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002570 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002571 return;
2572 }
2573
Benjamin Kramer70370212013-09-09 15:08:57 +00002574 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002575 StringRef TypeStr;
2576 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002577 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002578 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002579
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002580 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002581 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002582 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2583 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002584 return;
2585 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002586
Aaron Ballman682ee422013-09-11 19:47:58 +00002587 // Complain about attempts to use protected visibility on targets
2588 // (like Darwin) that don't support it.
2589 if (type == VisibilityAttr::Protected &&
2590 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002591 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002592 type = VisibilityAttr::Default;
2593 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002594
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002595 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002596 if (isTypeVisibility) {
Erich Keane6a24e802019-09-13 17:39:31 +00002597 newAttr = S.mergeTypeVisibilityAttr(
2598 D, AL, (TypeVisibilityAttr::VisibilityType)type);
John McCalld041a9b2013-02-20 01:54:26 +00002599 } else {
Erich Keane6a24e802019-09-13 17:39:31 +00002600 newAttr = S.mergeVisibilityAttr(D, AL, type);
John McCalld041a9b2013-02-20 01:54:26 +00002601 }
2602 if (newAttr)
2603 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002604}
2605
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08002606static void handleObjCDirectAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2607 // objc_direct cannot be set on methods declared in the context of a protocol
2608 if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
2609 S.Diag(AL.getLoc(), diag::err_objc_direct_on_protocol) << false;
2610 return;
2611 }
2612
2613 if (S.getLangOpts().ObjCRuntime.allowsDirectDispatch()) {
2614 handleSimpleAttribute<ObjCDirectAttr>(S, D, AL);
2615 } else {
2616 S.Diag(AL.getLoc(), diag::warn_objc_direct_ignored) << AL;
2617 }
2618}
2619
2620static void handleObjCDirectMembersAttr(Sema &S, Decl *D,
2621 const ParsedAttr &AL) {
2622 if (S.getLangOpts().ObjCRuntime.allowsDirectDispatch()) {
2623 handleSimpleAttribute<ObjCDirectMembersAttr>(S, D, AL);
2624 } else {
2625 S.Diag(AL.getLoc(), diag::warn_objc_direct_ignored) << AL;
2626 }
2627}
2628
Erich Keanee891aa92018-07-13 15:07:47 +00002629static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002630 const auto *M = cast<ObjCMethodDecl>(D);
2631 if (!AL.isArgIdent(0)) {
2632 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002633 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002634 return;
2635 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002636
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002637 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002638 ObjCMethodFamilyAttr::FamilyKind F;
2639 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002640 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002641 return;
2642 }
2643
Alp Toker314cc812014-01-25 16:55:45 +00002644 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002645 !M->getReturnType()->isObjCObjectPointerType()) {
2646 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2647 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002648 // Ignore the attribute.
2649 return;
2650 }
2651
Erich Keane6a24e802019-09-13 17:39:31 +00002652 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(S.Context, AL, F));
John McCall86bc21f2011-03-02 11:33:24 +00002653}
2654
Erich Keanee891aa92018-07-13 15:07:47 +00002655static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002656 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002657 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002658 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002659 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2660 return;
2661 }
2662 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002663 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002664 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002665 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002666 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2667 return;
2668 }
2669 }
2670 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002671 // It is okay to include this attribute on properties, e.g.:
2672 //
2673 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2674 //
2675 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002676 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002677 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002678 }
Erich Keane6a24e802019-09-13 17:39:31 +00002679 D->addAttr(::new (S.Context) ObjCNSObjectAttr(S.Context, AL));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002680}
2681
Erich Keanee891aa92018-07-13 15:07:47 +00002682static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002683 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002684 QualType T = TD->getUnderlyingType();
2685 if (!T->isObjCObjectPointerType()) {
2686 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2687 return;
2688 }
2689 } else {
2690 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2691 return;
2692 }
Erich Keane6a24e802019-09-13 17:39:31 +00002693 D->addAttr(::new (S.Context) ObjCIndependentClassAttr(S.Context, AL));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002694}
2695
Erich Keanee891aa92018-07-13 15:07:47 +00002696static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002697 if (!AL.isArgIdent(0)) {
2698 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002699 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002700 return;
2701 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002702
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002703 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002704 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002705 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002706 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002707 return;
2708 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002709
Erich Keane6a24e802019-09-13 17:39:31 +00002710 D->addAttr(::new (S.Context) BlocksAttr(S.Context, AL, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002711}
2712
Erich Keanee891aa92018-07-13 15:07:47 +00002713static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002714 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002715 if (AL.getNumArgs() > 0) {
2716 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002717 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002718 if (E->isTypeDependent() || E->isValueDependent() ||
2719 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002720 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002721 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002722 return;
2723 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002724
John McCallb46f2872011-09-09 07:56:05 +00002725 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002726 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002727 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002728 return;
2729 }
John McCallb46f2872011-09-09 07:56:05 +00002730
2731 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002732 }
2733
Aaron Ballman18a78382013-11-21 00:28:23 +00002734 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002735 if (AL.getNumArgs() > 1) {
2736 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002737 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002738 if (E->isTypeDependent() || E->isValueDependent() ||
2739 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002740 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002741 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002742 return;
2743 }
2744 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002745
John McCallb46f2872011-09-09 07:56:05 +00002746 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002747 // FIXME: This error message could be improved, it would be nice
2748 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002749 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002750 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002751 return;
2752 }
2753 }
2754
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002755 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002756 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002757 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002758 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002759 return;
2760 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002761
Chris Lattner9363e312009-03-17 23:03:47 +00002762 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002763 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002764 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002765 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002766 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002767 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002768 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002769 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002770 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002771 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002772 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002773 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002774 return;
2775 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002776 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002777 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002778 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002779 const FunctionType *FT = Ty->isFunctionPointerType()
2780 ? D->getFunctionType()
Simon Pilgrim237d0af2019-10-04 15:02:46 +00002781 : Ty->castAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002782 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002783 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002784 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002785 return;
2786 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002787 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002788 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002789 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002790 return;
2791 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002792 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002793 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002794 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002795 return;
2796 }
Erich Keane6a24e802019-09-13 17:39:31 +00002797 D->addAttr(::new (S.Context) SentinelAttr(S.Context, AL, sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002798}
2799
Erich Keanee891aa92018-07-13 15:07:47 +00002800static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002801 if (D->getFunctionType() &&
Erich Keane46441fd2019-07-25 15:10:56 +00002802 D->getFunctionType()->getReturnType()->isVoidType() &&
2803 !isa<CXXConstructorDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002804 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002805 return;
2806 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002807 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002808 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002809 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002810 return;
2811 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002812
Aaron Ballman3bef0142019-07-20 07:56:34 +00002813 StringRef Str;
2814 if ((AL.isCXX11Attribute() || AL.isC2xAttribute()) && !AL.getScopeName()) {
2815 // If this is spelled as the standard C++17 attribute, but not in C++17,
2816 // warn about using it as an extension. If there are attribute arguments,
2817 // then claim it's a C++2a extension instead.
2818 // FIXME: If WG14 does not seem likely to adopt the same feature, add an
2819 // extension warning for C2x mode.
2820 const LangOptions &LO = S.getLangOpts();
2821 if (AL.getNumArgs() == 1) {
2822 if (LO.CPlusPlus && !LO.CPlusPlus2a)
2823 S.Diag(AL.getLoc(), diag::ext_cxx2a_attr) << AL;
2824
2825 // Since this this is spelled [[nodiscard]], get the optional string
2826 // literal. If in C++ mode, but not in C++2a mode, diagnose as an
2827 // extension.
2828 // FIXME: C2x should support this feature as well, even as an extension.
2829 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
2830 return;
2831 } else if (LO.CPlusPlus && !LO.CPlusPlus17)
2832 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
2833 }
Aaron Ballmane7964782016-03-07 22:44:55 +00002834
Erich Keane6a24e802019-09-13 17:39:31 +00002835 D->addAttr(::new (S.Context) WarnUnusedResultAttr(S.Context, AL, Str));
Chris Lattner237f2752009-02-14 07:37:35 +00002836}
2837
Erich Keanee891aa92018-07-13 15:07:47 +00002838static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002839 // weak_import only applies to variable & function declarations.
2840 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002841 if (!D->canBeWeakImported(isDef)) {
2842 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002843 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002844 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002845 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002846 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002847 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002848 // Nothing to warn about here.
2849 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002850 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002851 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002852
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002853 return;
2854 }
2855
Erich Keane6a24e802019-09-13 17:39:31 +00002856 D->addAttr(::new (S.Context) WeakImportAttr(S.Context, AL));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002857}
2858
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002859// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002860template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002861static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002862 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002863 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002864 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002865 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2866 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002867 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002868 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002869 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002870 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002871 return;
2872 }
2873 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002874
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002875 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2876 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2877 Existing->getYDim() == WGSize[1] &&
2878 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002879 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002880
Erich Keane6a24e802019-09-13 17:39:31 +00002881 D->addAttr(::new (S.Context)
2882 WorkGroupAttr(S.Context, AL, WGSize[0], WGSize[1], WGSize[2]));
Nate Begemanf2758702009-06-26 06:32:41 +00002883}
2884
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002885// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002886static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002887 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002888 const Expr *E = AL.getArgAsExpr(0);
2889 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002890 return;
2891 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002892 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002893 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002894 return;
2895 }
2896
2897 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2898 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2899 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002900 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002901
Erich Keane6a24e802019-09-13 17:39:31 +00002902 D->addAttr(::new (S.Context)
2903 OpenCLIntelReqdSubGroupSizeAttr(S.Context, AL, SGSize));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002904}
2905
Erich Keanee891aa92018-07-13 15:07:47 +00002906static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002907 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002908 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002909 return;
2910 }
2911
Craig Topperc3ec1492014-05-26 06:22:03 +00002912 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002913 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002914 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002915
2916 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2917 (ParmType->isBooleanType() ||
2918 !ParmType->isIntegralType(S.getASTContext()))) {
Gabor Horvath247a6032020-01-02 11:57:42 -08002919 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument) << 2 << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002920 return;
2921 }
2922
Aaron Ballmana9e05402013-12-02 22:16:55 +00002923 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002924 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002925 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002926 return;
2927 }
2928 }
2929
Erich Keane6a24e802019-09-13 17:39:31 +00002930 D->addAttr(::new (S.Context) VecTypeHintAttr(S.Context, AL, ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002931}
2932
Erich Keane6a24e802019-09-13 17:39:31 +00002933SectionAttr *Sema::mergeSectionAttr(Decl *D, const AttributeCommonInfo &CI,
2934 StringRef Name) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002935 // Explicit or partial specializations do not inherit
2936 // the section attribute from the primary template.
2937 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keane6a24e802019-09-13 17:39:31 +00002938 if (CI.getAttributeSpellingListIndex() == SectionAttr::Declspec_allocate &&
Erich Keane7963e8b2018-07-18 20:04:48 +00002939 FD->isFunctionTemplateSpecialization())
2940 return nullptr;
2941 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002942 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2943 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002944 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002945 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2946 << 1 /*section*/;
Erich Keane6a24e802019-09-13 17:39:31 +00002947 Diag(CI.getLoc(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002948 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002949 }
Erich Keane6a24e802019-09-13 17:39:31 +00002950 return ::new (Context) SectionAttr(Context, CI, Name);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002951}
2952
Reid Kleckner2a133222015-03-04 23:39:17 +00002953bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2954 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2955 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002956 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002957 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002958 return false;
2959 }
2960 return true;
2961}
2962
Erich Keanee891aa92018-07-13 15:07:47 +00002963static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002964 // Make sure that there is a string literal as the sections's single
2965 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002966 StringRef Str;
2967 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002968 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002969 return;
Mike Stump11289f42009-09-09 15:08:12 +00002970
Reid Kleckner2a133222015-03-04 23:39:17 +00002971 if (!S.checkSectionName(LiteralLoc, Str))
2972 return;
2973
Chris Lattner30ba6742009-08-10 19:03:04 +00002974 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002975 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002976 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002977 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002978 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002979 return;
2980 }
Mike Stump11289f42009-09-09 15:08:12 +00002981
Erich Keane6a24e802019-09-13 17:39:31 +00002982 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002983 if (NewAttr)
2984 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002985}
2986
Nico Weber98016212019-07-09 00:02:23 +00002987// This is used for `__declspec(code_seg("segname"))` on a decl.
2988// `#pragma code_seg("segname")` uses checkSectionName() instead.
2989static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc,
2990 StringRef CodeSegName) {
2991 std::string Error =
2992 S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
Erich Keane7963e8b2018-07-18 20:04:48 +00002993 if (!Error.empty()) {
Nico Weber98016212019-07-09 00:02:23 +00002994 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
2995 << Error << 0 /*'code-seg'*/;
Erich Keane7963e8b2018-07-18 20:04:48 +00002996 return false;
2997 }
Nico Weber98016212019-07-09 00:02:23 +00002998
Erich Keane7963e8b2018-07-18 20:04:48 +00002999 return true;
3000}
3001
Erich Keane6a24e802019-09-13 17:39:31 +00003002CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, const AttributeCommonInfo &CI,
3003 StringRef Name) {
Erich Keane7963e8b2018-07-18 20:04:48 +00003004 // Explicit or partial specializations do not inherit
3005 // the code_seg attribute from the primary template.
3006 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3007 if (FD->isFunctionTemplateSpecialization())
3008 return nullptr;
3009 }
3010 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3011 if (ExistingAttr->getName() == Name)
3012 return nullptr;
3013 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3014 << 0 /*codeseg*/;
Erich Keane6a24e802019-09-13 17:39:31 +00003015 Diag(CI.getLoc(), diag::note_previous_attribute);
Erich Keane7963e8b2018-07-18 20:04:48 +00003016 return nullptr;
3017 }
Erich Keane6a24e802019-09-13 17:39:31 +00003018 return ::new (Context) CodeSegAttr(Context, CI, Name);
Erich Keane7963e8b2018-07-18 20:04:48 +00003019}
3020
3021static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3022 StringRef Str;
3023 SourceLocation LiteralLoc;
3024 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3025 return;
3026 if (!checkCodeSegName(S, LiteralLoc, Str))
3027 return;
3028 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3029 if (!ExistingAttr->isImplicit()) {
3030 S.Diag(AL.getLoc(),
3031 ExistingAttr->getName() == Str
3032 ? diag::warn_duplicate_codeseg_attribute
3033 : diag::err_conflicting_codeseg_attribute);
3034 return;
3035 }
3036 D->dropAttr<CodeSegAttr>();
3037 }
Erich Keane6a24e802019-09-13 17:39:31 +00003038 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL, Str))
Erich Keane7963e8b2018-07-18 20:04:48 +00003039 D->addAttr(CSA);
3040}
3041
Erich Keane57e15cd2017-07-19 22:06:33 +00003042// Check for things we'd like to warn about. Multiversioning issues are
3043// handled later in the process, once we know how many exist.
3044bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3045 enum FirstParam { Unsupported, Duplicate };
3046 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00003047 for (auto Str : {"tune=", "fpmath="})
3048 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00003049 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3050 << Unsupported << None << Str;
3051
Craig Topper505aa242019-12-09 10:34:24 -08003052 ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
Erich Keane57e15cd2017-07-19 22:06:33 +00003053
3054 if (!ParsedAttrs.Architecture.empty() &&
3055 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3056 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3057 << Unsupported << Architecture << ParsedAttrs.Architecture;
3058
3059 if (ParsedAttrs.DuplicateArchitecture)
3060 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3061 << Duplicate << None << "arch=";
3062
3063 for (const auto &Feature : ParsedAttrs.Features) {
3064 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3065 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3066 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3067 << Unsupported << None << CurFeature;
3068 }
3069
Momchil Velikovaa6d48f2019-11-15 11:34:47 +00003070 TargetInfo::BranchProtectionInfo BPI;
3071 StringRef Error;
3072 if (!ParsedAttrs.BranchProtection.empty() &&
3073 !Context.getTargetInfo().validateBranchProtection(
3074 ParsedAttrs.BranchProtection, BPI, Error)) {
3075 if (Error.empty())
3076 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3077 << Unsupported << None << "branch-protection";
3078 else
3079 return Diag(LiteralLoc, diag::err_invalid_branch_protection_spec)
3080 << Error;
3081 }
3082
Erich Keane29636aa2018-02-16 17:31:59 +00003083 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003084}
3085
Erich Keanee891aa92018-07-13 15:07:47 +00003086static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003087 StringRef Str;
3088 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003089 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003090 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003091 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003092
Erich Keane6a24e802019-09-13 17:39:31 +00003093 TargetAttr *NewAttr = ::new (S.Context) TargetAttr(S.Context, AL, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00003094 D->addAttr(NewAttr);
3095}
3096
Erich Keanee891aa92018-07-13 15:07:47 +00003097static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003098 Expr *E = AL.getArgAsExpr(0);
3099 uint32_t VecWidth;
3100 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3101 AL.setInvalid();
3102 return;
3103 }
3104
3105 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3106 if (Existing && Existing->getVectorWidth() != VecWidth) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003107 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Craig Topper74c10e32018-07-09 19:00:16 +00003108 return;
3109 }
3110
Erich Keane6a24e802019-09-13 17:39:31 +00003111 D->addAttr(::new (S.Context) MinVectorWidthAttr(S.Context, AL, VecWidth));
Craig Topper74c10e32018-07-09 19:00:16 +00003112}
3113
Erich Keanee891aa92018-07-13 15:07:47 +00003114static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003115 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003116 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003117 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003118 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003119
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003120 // gcc only allows for simple identifiers. Since we support more than gcc, we
3121 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003122 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003123 if (DRE->hasQualifier())
3124 S.Diag(Loc, diag::warn_cleanup_ext);
3125 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3126 NI = DRE->getNameInfo();
3127 if (!FD) {
3128 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3129 << NI.getName();
3130 return;
3131 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003132 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003133 if (ULE->hasExplicitTemplateArgs())
3134 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003135 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3136 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003137 if (!FD) {
3138 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3139 << NI.getName();
3140 if (ULE->getType() == S.Context.OverloadTy)
3141 S.NoteAllOverloadCandidates(ULE);
3142 return;
3143 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003144 } else {
3145 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003146 return;
3147 }
3148
Anders Carlssond277d792009-01-31 01:16:18 +00003149 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003150 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3151 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003152 return;
3153 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003154
Anders Carlsson723f55d2009-02-07 23:16:50 +00003155 // We're currently more strict than GCC about what function types we accept.
3156 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003157 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003158 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003159 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3160 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003161 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3162 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003163 return;
3164 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003165
Erich Keane6a24e802019-09-13 17:39:31 +00003166 D->addAttr(::new (S.Context) CleanupAttr(S.Context, AL, FD));
Anders Carlssond277d792009-01-31 01:16:18 +00003167}
3168
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003169static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003170 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003171 if (!AL.isArgIdent(0)) {
3172 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003173 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003174 return;
3175 }
3176
3177 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003178 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003179 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3180 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003181 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003182 return;
3183 }
3184
Erich Keane6a24e802019-09-13 17:39:31 +00003185 D->addAttr(::new (S.Context)
3186 EnumExtensibilityAttr(S.Context, AL, ExtensibilityKind));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003187}
3188
Mike Stumpd3bb5572009-07-24 19:02:52 +00003189/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003190/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003191static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003192 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003193 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003194 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003195 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003196
Eric Christopherb64963e2015-08-13 21:34:35 +00003197 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003198 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003199
Eric Christopherb64963e2015-08-13 21:34:35 +00003200 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3201 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003202 !isCFStringType(Ty, S.Context) &&
3203 (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003204 !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003205 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003206 << "a string type" << IdxExpr->getSourceRange()
3207 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003208 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003209 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003210 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003211 if (!isNSStringType(Ty, S.Context) &&
3212 !isCFStringType(Ty, S.Context) &&
3213 (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003214 !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003215 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003216 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003217 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003218 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003219 }
3220
Erich Keane6a24e802019-09-13 17:39:31 +00003221 D->addAttr(::new (S.Context) FormatArgAttr(S.Context, AL, Idx));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003222}
3223
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003224enum FormatAttrKind {
3225 CFStringFormat,
3226 NSStringFormat,
3227 StrftimeFormat,
3228 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003229 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003230 InvalidFormat
3231};
3232
3233/// getFormatAttrKind - Map from format attribute names to supported format
3234/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003235static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003236 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003237 // Check for formats that get handled specially.
3238 .Case("NSString", NSStringFormat)
3239 .Case("CFString", CFStringFormat)
3240 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003241
Mehdi Amini06d367c2016-10-24 20:39:34 +00003242 // Otherwise, check for supported formats.
3243 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3244 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3245 .Case("kprintf", SupportedFormat) // OpenBSD.
3246 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3247 .Case("os_trace", SupportedFormat)
3248 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003249
Mehdi Amini06d367c2016-10-24 20:39:34 +00003250 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3251 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003252}
3253
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003254/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003255/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003256static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003257 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003258 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003259 return;
3260 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003261
Aaron Ballman4a611152013-11-27 16:34:09 +00003262 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003263 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3264 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003265 return;
3266 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003267 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003268 if (S.Context.getAsArrayType(T))
3269 T = S.Context.getBaseElementType(T);
3270 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003271 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3272 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003273 return;
3274 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003275
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003276 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003277 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003278 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3279 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003280 return;
3281 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003282
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003283 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballman52c9ad22019-02-12 13:04:11 +00003284 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003285 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003286 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003287 return;
3288 }
Erich Keane6a24e802019-09-13 17:39:31 +00003289 D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003290}
3291
Erich Keane6a24e802019-09-13 17:39:31 +00003292FormatAttr *Sema::mergeFormatAttr(Decl *D, const AttributeCommonInfo &CI,
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003293 IdentifierInfo *Format, int FormatIdx,
Erich Keane6a24e802019-09-13 17:39:31 +00003294 int FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003295 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003296 for (auto *F : D->specific_attrs<FormatAttr>()) {
3297 if (F->getType() == Format &&
3298 F->getFormatIdx() == FormatIdx &&
3299 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003300 // If we don't have a valid location for this attribute, adopt the
3301 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003302 if (F->getLocation().isInvalid())
Erich Keane6a24e802019-09-13 17:39:31 +00003303 F->setRange(CI.getRange());
Craig Topperc3ec1492014-05-26 06:22:03 +00003304 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003305 }
3306 }
3307
Erich Keane6a24e802019-09-13 17:39:31 +00003308 return ::new (Context) FormatAttr(Context, CI, Format, FormatIdx, FirstArg);
Rafael Espindola92d49452012-05-11 00:36:07 +00003309}
3310
Mike Stumpd3bb5572009-07-24 19:02:52 +00003311/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003312/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003313static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003314 if (!AL.isArgIdent(0)) {
3315 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003316 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003317 return;
3318 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003319
Chandler Carruth743682b2010-11-16 08:35:43 +00003320 // In C++ the implicit 'this' function parameter also counts, and they are
3321 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003322 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003323 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003324
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003325 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003326 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003327
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003328 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003329 // If we've modified the string name, we need a new identifier for it.
3330 II = &S.Context.Idents.get(Format);
3331 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003332
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003333 // Check for supported formats.
3334 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003335
Chris Lattner12161d32010-03-22 21:08:50 +00003336 if (Kind == IgnoredFormat)
3337 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003338
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003339 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003340 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003341 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003342 return;
3343 }
3344
3345 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003346 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003347 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003348 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003349 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003350
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003351 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003352 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003353 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003354 return;
3355 }
3356
3357 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003358 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003359
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003360 if (HasImplicitThisParam) {
3361 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003362 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003363 diag::err_format_attribute_implicit_this_format_string)
3364 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003365 return;
3366 }
3367 ArgIdx--;
3368 }
Mike Stump11289f42009-09-09 15:08:12 +00003369
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003370 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003371 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003372
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003373 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003374 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003375 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003376 << "a CFString" << IdxExpr->getSourceRange()
3377 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003378 return;
3379 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003380 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003381 // FIXME: do we need to check if the type is NSString*? What are the
3382 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003383 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003384 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003385 << "an NSString" << IdxExpr->getSourceRange()
3386 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003387 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003388 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003389 } else if (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003390 !Ty->castAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003391 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003392 << "a string type" << IdxExpr->getSourceRange()
3393 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003394 return;
3395 }
3396
3397 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003398 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003399 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003400 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003401 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003402
3403 // check if the function is variadic if the 3rd argument non-zero
3404 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003405 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003406 ++NumArgs; // +1 for ...
3407 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003408 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003409 return;
3410 }
3411 }
3412
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003413 // strftime requires FirstArg to be 0 because it doesn't read from any
3414 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003415 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003416 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003417 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003418 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003419 return;
3420 }
3421 // if 0 it disables parameter checking (to use with e.g. va_list)
3422 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003423 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003424 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003425 return;
3426 }
3427
Erich Keane6a24e802019-09-13 17:39:31 +00003428 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL, II, Idx, FirstArg);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003429 if (NewAttr)
3430 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003431}
3432
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003433/// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
3434static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3435 // The index that identifies the callback callee is mandatory.
3436 if (AL.getNumArgs() == 0) {
3437 S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee)
3438 << AL.getRange();
3439 return;
3440 }
3441
3442 bool HasImplicitThisParam = isInstanceMethod(D);
3443 int32_t NumArgs = getFunctionOrMethodNumParams(D);
3444
3445 FunctionDecl *FD = D->getAsFunction();
3446 assert(FD && "Expected a function declaration!");
3447
3448 llvm::StringMap<int> NameIdxMapping;
3449 NameIdxMapping["__"] = -1;
3450
3451 NameIdxMapping["this"] = 0;
3452
3453 int Idx = 1;
3454 for (const ParmVarDecl *PVD : FD->parameters())
3455 NameIdxMapping[PVD->getName()] = Idx++;
3456
3457 auto UnknownName = NameIdxMapping.end();
3458
3459 SmallVector<int, 8> EncodingIndices;
3460 for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) {
3461 SourceRange SR;
3462 int32_t ArgIdx;
3463
3464 if (AL.isArgIdent(I)) {
3465 IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
3466 auto It = NameIdxMapping.find(IdLoc->Ident->getName());
3467 if (It == UnknownName) {
3468 S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown)
3469 << IdLoc->Ident << IdLoc->Loc;
3470 return;
3471 }
3472
3473 SR = SourceRange(IdLoc->Loc);
3474 ArgIdx = It->second;
3475 } else if (AL.isArgExpr(I)) {
3476 Expr *IdxExpr = AL.getArgAsExpr(I);
3477
3478 // If the expression is not parseable as an int32_t we have a problem.
3479 if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1,
3480 false)) {
3481 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3482 << AL << (I + 1) << IdxExpr->getSourceRange();
3483 return;
3484 }
3485
3486 // Check oob, excluding the special values, 0 and -1.
3487 if (ArgIdx < -1 || ArgIdx > NumArgs) {
3488 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3489 << AL << (I + 1) << IdxExpr->getSourceRange();
3490 return;
3491 }
3492
3493 SR = IdxExpr->getSourceRange();
3494 } else {
3495 llvm_unreachable("Unexpected ParsedAttr argument type!");
3496 }
3497
3498 if (ArgIdx == 0 && !HasImplicitThisParam) {
3499 S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
3500 << (I + 1) << SR;
3501 return;
3502 }
3503
3504 // Adjust for the case we do not have an implicit "this" parameter. In this
3505 // case we decrease all positive values by 1 to get LLVM argument indices.
3506 if (!HasImplicitThisParam && ArgIdx > 0)
3507 ArgIdx -= 1;
3508
3509 EncodingIndices.push_back(ArgIdx);
3510 }
3511
3512 int CalleeIdx = EncodingIndices.front();
3513 // Check if the callee index is proper, thus not "this" and not "unknown".
Johannes Doerferte068d052019-01-21 14:23:46 +00003514 // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam"
3515 // is false and positive if "HasImplicitThisParam" is true.
3516 if (CalleeIdx < (int)HasImplicitThisParam) {
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003517 S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee)
3518 << AL.getRange();
3519 return;
3520 }
3521
3522 // Get the callee type, note the index adjustment as the AST doesn't contain
3523 // the this type (which the callee cannot reference anyway!).
3524 const Type *CalleeType =
3525 getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam)
3526 .getTypePtr();
3527 if (!CalleeType || !CalleeType->isFunctionPointerType()) {
3528 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3529 << AL.getRange();
3530 return;
3531 }
3532
3533 const Type *CalleeFnType =
3534 CalleeType->getPointeeType()->getUnqualifiedDesugaredType();
3535
3536 // TODO: Check the type of the callee arguments.
3537
3538 const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
3539 if (!CalleeFnProtoType) {
3540 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3541 << AL.getRange();
3542 return;
3543 }
3544
3545 if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
3546 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3547 << AL << (unsigned)(EncodingIndices.size() - 1);
3548 return;
3549 }
3550
3551 if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
3552 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3553 << AL << (unsigned)(EncodingIndices.size() - 1);
3554 return;
3555 }
3556
3557 if (CalleeFnProtoType->isVariadic()) {
3558 S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
3559 return;
3560 }
3561
3562 // Do not allow multiple callback attributes.
3563 if (D->hasAttr<CallbackAttr>()) {
3564 S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
3565 return;
3566 }
3567
3568 D->addAttr(::new (S.Context) CallbackAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00003569 S.Context, AL, EncodingIndices.data(), EncodingIndices.size()));
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003570}
3571
Erich Keanee891aa92018-07-13 15:07:47 +00003572static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003573 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003574 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003575 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003576 if (TD && TD->getUnderlyingType()->isUnionType())
3577 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3578 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003579 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003580
3581 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003582 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3583 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003584 return;
3585 }
3586
John McCallf937c022011-10-07 06:10:15 +00003587 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003588 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003589 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003590 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003591 return;
3592 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003593
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003594 RecordDecl::field_iterator Field = RD->field_begin(),
3595 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003596 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003597 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003598 return;
3599 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003600
David Blaikie40ed2972012-06-06 20:45:41 +00003601 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003602 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003603 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003604 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003605 diag::warn_transparent_union_attribute_floating)
3606 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003607 return;
3608 }
3609
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003610 if (FirstType->isIncompleteType())
3611 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003612 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3613 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3614 for (; Field != FieldEnd; ++Field) {
3615 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003616 if (FieldType->isIncompleteType())
3617 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003618 // FIXME: this isn't fully correct; we also need to test whether the
3619 // members of the union would all have the same calling convention as the
3620 // first member of the union. Checking just the size and alignment isn't
3621 // sufficient (consider structs passed on the stack instead of in registers
3622 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003623 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003624 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003625 // Warn if we drop the attribute.
3626 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003627 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003628 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003629 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003630 diag::warn_transparent_union_attribute_field_size_align)
3631 << isSize << Field->getDeclName() << FieldBits;
3632 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003633 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003634 diag::note_transparent_union_first_field_size_align)
3635 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003636 return;
3637 }
3638 }
3639
Erich Keane6a24e802019-09-13 17:39:31 +00003640 RD->addAttr(::new (S.Context) TransparentUnionAttr(S.Context, AL));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003641}
3642
Erich Keanee891aa92018-07-13 15:07:47 +00003643static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003644 // Make sure that there is a string literal as the annotation's single
3645 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003646 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003647 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003648 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003649
3650 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003651 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3652 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003653 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003654 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003655
Erich Keane6a24e802019-09-13 17:39:31 +00003656 D->addAttr(::new (S.Context) AnnotateAttr(S.Context, AL, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003657}
3658
Erich Keanee891aa92018-07-13 15:07:47 +00003659static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00003660 S.AddAlignValueAttr(D, AL, AL.getArgAsExpr(0));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003661}
3662
Erich Keane6a24e802019-09-13 17:39:31 +00003663void Sema::AddAlignValueAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E) {
3664 AlignValueAttr TmpAttr(Context, CI, E);
3665 SourceLocation AttrLoc = CI.getLoc();
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003666
3667 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003668 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003669 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003670 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003671 T = VD->getType();
3672 else
3673 llvm_unreachable("Unknown decl type for align_value");
3674
3675 if (!T->isDependentType() && !T->isAnyPointerType() &&
3676 !T->isReferenceType() && !T->isMemberPointerType()) {
3677 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
Aaron Ballmandab43c82020-03-14 17:00:45 -04003678 << &TmpAttr << T << D->getSourceRange();
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003679 return;
3680 }
3681
3682 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003683 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003684 ExprResult ICE
3685 = VerifyIntegerConstantExpression(E, &Alignment,
3686 diag::err_align_value_attribute_argument_not_int,
3687 /*AllowFold*/ false);
3688 if (ICE.isInvalid())
3689 return;
3690
3691 if (!Alignment.isPowerOf2()) {
3692 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3693 << E->getSourceRange();
3694 return;
3695 }
3696
Erich Keane6a24e802019-09-13 17:39:31 +00003697 D->addAttr(::new (Context) AlignValueAttr(Context, CI, ICE.get()));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003698 return;
3699 }
3700
3701 // Save dependent expressions in the AST to be instantiated.
Erich Keane6a24e802019-09-13 17:39:31 +00003702 D->addAttr(::new (Context) AlignValueAttr(Context, CI, E));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003703}
3704
Erich Keanee891aa92018-07-13 15:07:47 +00003705static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003706 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003707 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003708 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003709 return;
3710 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003711
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003712 if (AL.getNumArgs() == 0) {
Erich Keane6a24e802019-09-13 17:39:31 +00003713 D->addAttr(::new (S.Context) AlignedAttr(S.Context, AL, true, nullptr));
Richard Smith848e1f12013-02-01 08:12:08 +00003714 return;
3715 }
3716
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003717 Expr *E = AL.getArgAsExpr(0);
3718 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3719 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003720 diag::err_pack_expansion_without_parameter_packs);
3721 return;
3722 }
3723
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003724 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003725 return;
3726
Erich Keane6a24e802019-09-13 17:39:31 +00003727 S.AddAlignedAttr(D, AL, E, AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003728}
3729
Erich Keane6a24e802019-09-13 17:39:31 +00003730void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
3731 bool IsPackExpansion) {
3732 AlignedAttr TmpAttr(Context, CI, true, E);
3733 SourceLocation AttrLoc = CI.getLoc();
Richard Smith848e1f12013-02-01 08:12:08 +00003734
Richard Smith1dba27c2013-01-29 09:02:09 +00003735 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003736 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003737 // C++11 [dcl.align]p1:
3738 // An alignment-specifier may be applied to a variable or to a class
3739 // data member, but it shall not be applied to a bit-field, a function
3740 // parameter, the formal parameter of a catch clause, or a variable
3741 // declared with the register storage class specifier. An
3742 // alignment-specifier may also be applied to the declaration of a class
3743 // or enumeration type.
3744 // C11 6.7.5/2:
3745 // An alignment attribute shall not be specified in a declaration of
3746 // a typedef, or a bit-field, or a function, or a parameter, or an
3747 // object declared with the register storage-class specifier.
3748 int DiagKind = -1;
3749 if (isa<ParmVarDecl>(D)) {
3750 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003751 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003752 if (VD->getStorageClass() == SC_Register)
3753 DiagKind = 1;
3754 if (VD->isExceptionVariable())
3755 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003756 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003757 if (FD->isBitField())
3758 DiagKind = 3;
3759 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003760 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003761 << (TmpAttr.isC11() ? ExpectedVariableOrField
3762 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003763 return;
3764 }
3765 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003766 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003767 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003768 return;
3769 }
3770 }
3771
Richard Smith90ae9672018-06-20 23:36:55 +00003772 if (E->isValueDependent()) {
3773 // We can't support a dependent alignment on a non-dependent type,
3774 // because we have no way to model that a type is "alignment-dependent"
3775 // but not dependent in any other way.
3776 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3777 if (!TND->getUnderlyingType()->isDependentType()) {
3778 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3779 << E->getSourceRange();
3780 return;
3781 }
3782 }
3783
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003784 // Save dependent expressions in the AST to be instantiated.
Erich Keane6a24e802019-09-13 17:39:31 +00003785 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, E);
Richard Smith44c247f2013-02-22 08:32:16 +00003786 AA->setPackExpansion(IsPackExpansion);
3787 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003788 return;
3789 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003790
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003791 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003792 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003793 ExprResult ICE
3794 = VerifyIntegerConstantExpression(E, &Alignment,
3795 diag::err_aligned_attribute_argument_not_int,
3796 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003797 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003798 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003799
David Majnemer0be6bd02015-07-26 09:02:21 +00003800 uint64_t AlignVal = Alignment.getZExtValue();
3801
Richard Smith848e1f12013-02-01 08:12:08 +00003802 // C++11 [dcl.align]p2:
3803 // -- if the constant expression evaluates to zero, the alignment
3804 // specifier shall have no effect
3805 // C11 6.7.5p6:
3806 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003807 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003808 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003809 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3810 << E->getSourceRange();
3811 return;
3812 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003813 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003814
Roman Lebedev1d0972f2020-01-24 17:01:27 +03003815 unsigned MaximumAlignment = Sema::MaximumAlignment;
3816 if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
3817 MaximumAlignment = std::min(MaximumAlignment, 8192u);
3818 if (AlignVal > MaximumAlignment) {
Roman Lebedevd096f8d2020-01-23 22:50:06 +03003819 Diag(AttrLoc, diag::err_attribute_aligned_too_great)
Roman Lebedev1d0972f2020-01-24 17:01:27 +03003820 << MaximumAlignment << E->getSourceRange();
David Majnemerabecae72014-02-12 20:36:10 +00003821 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003822 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003823
David Majnemer0be6bd02015-07-26 09:02:21 +00003824 if (Context.getTargetInfo().isTLSSupported()) {
3825 unsigned MaxTLSAlign =
3826 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3827 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003828 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003829 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3830 VD->getTLSKind() != VarDecl::TLS_None) {
3831 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3832 << (unsigned)AlignVal << VD << MaxTLSAlign;
3833 return;
3834 }
3835 }
3836
Erich Keane6a24e802019-09-13 17:39:31 +00003837 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, ICE.get());
Richard Smith44c247f2013-02-22 08:32:16 +00003838 AA->setPackExpansion(IsPackExpansion);
3839 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003840}
3841
Erich Keane6a24e802019-09-13 17:39:31 +00003842void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI,
3843 TypeSourceInfo *TS, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003844 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003845 // FIXME: Perform checking of type validity
Erich Keane6a24e802019-09-13 17:39:31 +00003846 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, false, TS);
Richard Smith44c247f2013-02-22 08:32:16 +00003847 AA->setPackExpansion(IsPackExpansion);
3848 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003849}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003850
Richard Smith848e1f12013-02-01 08:12:08 +00003851void Sema::CheckAlignasUnderalignment(Decl *D) {
3852 assert(D->hasAttrs() && "no attributes on decl");
3853
David Majnemer475b25e2015-01-21 10:54:38 +00003854 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003855 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003856 UnderlyingTy = DiagTy = VD->getType();
3857 } else {
3858 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003859 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003860 UnderlyingTy = ED->getIntegerType();
3861 }
3862 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003863 return;
3864
3865 // C++11 [dcl.align]p5, C11 6.7.5/4:
3866 // The combined effect of all alignment attributes in a declaration shall
3867 // not specify an alignment that is less strict than the alignment that
3868 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003869 AlignedAttr *AlignasAttr = nullptr;
Richard Sandiford627b5c12020-03-02 17:37:58 +00003870 AlignedAttr *LastAlignedAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003871 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003872 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003873 if (I->isAlignmentDependent())
3874 return;
3875 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003876 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003877 Align = std::max(Align, I->getAlignment(Context));
Richard Sandiford627b5c12020-03-02 17:37:58 +00003878 LastAlignedAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003879 }
3880
Richard Sandiford627b5c12020-03-02 17:37:58 +00003881 if (Align && DiagTy->isSizelessType()) {
3882 Diag(LastAlignedAttr->getLocation(), diag::err_attribute_sizeless_type)
3883 << LastAlignedAttr << DiagTy;
3884 } else if (AlignasAttr && Align) {
Richard Smith848e1f12013-02-01 08:12:08 +00003885 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003886 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003887 if (NaturalAlign > RequestedAlign)
3888 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003889 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003890 }
3891}
3892
David Majnemer2c4e00a2014-01-29 22:07:36 +00003893bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003894 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
Reid Klecknera9cc64e2019-11-15 18:49:32 -08003895 MSInheritanceModel ExplicitModel) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00003896 assert(RD->hasDefinition() && "RD has no definition!");
3897
David Majnemer98c9ee22014-02-07 00:43:07 +00003898 // We may not have seen base specifiers or any virtual methods yet. We will
3899 // have to wait until the record is defined to catch any mismatches.
3900 if (!RD->getDefinition()->isCompleteDefinition())
3901 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003902
David Majnemer98c9ee22014-02-07 00:43:07 +00003903 // The unspecified model never matches what a definition could need.
Reid Klecknera9cc64e2019-11-15 18:49:32 -08003904 if (ExplicitModel == MSInheritanceModel::Unspecified)
David Majnemer98c9ee22014-02-07 00:43:07 +00003905 return false;
3906
David Majnemer4bb09802014-02-10 19:50:15 +00003907 if (BestCase) {
Reid Klecknera9cc64e2019-11-15 18:49:32 -08003908 if (RD->calculateInheritanceModel() == ExplicitModel)
David Majnemer4bb09802014-02-10 19:50:15 +00003909 return false;
3910 } else {
Reid Klecknera9cc64e2019-11-15 18:49:32 -08003911 if (RD->calculateInheritanceModel() <= ExplicitModel)
David Majnemer4bb09802014-02-10 19:50:15 +00003912 return false;
3913 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003914
3915 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3916 << 0 /*definition*/;
Aaron Ballmandab43c82020-03-14 17:00:45 -04003917 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) << RD;
David Majnemer98c9ee22014-02-07 00:43:07 +00003918 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003919}
3920
Alexey Bataevf278eb12015-11-19 10:13:11 +00003921/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3922/// attribute.
3923static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3924 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003925 IntegerMode = true;
3926 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003927 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003928 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003929 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003930 case 'Q':
3931 DestWidth = 8;
3932 break;
3933 case 'H':
3934 DestWidth = 16;
3935 break;
3936 case 'S':
3937 DestWidth = 32;
3938 break;
3939 case 'D':
3940 DestWidth = 64;
3941 break;
3942 case 'X':
3943 DestWidth = 96;
3944 break;
3945 case 'T':
3946 DestWidth = 128;
3947 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003948 }
3949 if (Str[1] == 'F') {
3950 IntegerMode = false;
3951 } else if (Str[1] == 'C') {
3952 IntegerMode = false;
3953 ComplexMode = true;
3954 } else if (Str[1] != 'I') {
3955 DestWidth = 0;
3956 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003957 break;
3958 case 4:
3959 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3960 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003961 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003962 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003963 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003964 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003965 break;
3966 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003967 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003968 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003969 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003970 case 11:
3971 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003972 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003973 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003974 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003975}
3976
3977/// handleModeAttr - This attribute modifies the width of a decl with primitive
3978/// type.
3979///
3980/// Despite what would be logical, the mode attribute is a decl attribute, not a
3981/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3982/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00003983static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003984 // This attribute isn't documented, but glibc uses it. It changes
3985 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003986 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003987 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
3988 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003989 return;
3990 }
3991
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003992 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003993
Erich Keane6a24e802019-09-13 17:39:31 +00003994 S.AddModeAttr(D, AL, Name);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003995}
3996
Erich Keane6a24e802019-09-13 17:39:31 +00003997void Sema::AddModeAttr(Decl *D, const AttributeCommonInfo &CI,
3998 IdentifierInfo *Name, bool InInstantiation) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003999 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00004000 normalizeName(Str);
Erich Keane6a24e802019-09-13 17:39:31 +00004001 SourceLocation AttrLoc = CI.getLoc();
Alexey Bataevf278eb12015-11-19 10:13:11 +00004002
4003 unsigned DestWidth = 0;
4004 bool IntegerMode = true;
4005 bool ComplexMode = false;
4006 llvm::APInt VectorSize(64, 0);
4007 if (Str.size() >= 4 && Str[0] == 'V') {
4008 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
4009 size_t StrSize = Str.size();
4010 size_t VectorStringLength = 0;
4011 while ((VectorStringLength + 1) < StrSize &&
4012 isdigit(Str[VectorStringLength + 1]))
4013 ++VectorStringLength;
4014 if (VectorStringLength &&
4015 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
4016 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004017 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00004018 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004019 // Avoid duplicate warning from template instantiation.
4020 if (!InInstantiation)
4021 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00004022 } else {
4023 VectorSize = 0;
4024 }
4025 }
4026
4027 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004028 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
4029
4030 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
4031 // and friends, at least with glibc.
4032 // FIXME: Make sure floating-point mappings are accurate
4033 // FIXME: Support XF and TF types
4034 if (!DestWidth) {
4035 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
4036 return;
4037 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00004038
4039 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004040 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00004041 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004042 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004043 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
4044 // Try to get type from enum declaration, default to int.
4045 OldTy = ED->getIntegerType();
4046 if (OldTy.isNull())
4047 OldTy = Context.IntTy;
4048 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004049 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00004050
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004051 if (OldTy->isDependentType()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004052 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004053 return;
4054 }
4055
Alexey Bataev326057d2015-06-19 07:46:21 +00004056 // Base type can also be a vector type (see PR17453).
4057 // Distinguish between base type and base element type.
4058 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004059 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00004060 OldElemTy = VT->getElementType();
4061
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004062 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
4063 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
4064 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
4065 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
4066 VectorSize.getBoolValue()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004067 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << CI.getRange();
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004068 return;
4069 }
4070 bool IntegralOrAnyEnumType =
4071 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
4072
4073 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
4074 !IntegralOrAnyEnumType)
4075 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00004076 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004077 if (!IntegralOrAnyEnumType)
4078 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004079 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004080 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004081 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004082 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00004083 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004084 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004085 }
4086
Alexey Bataev326057d2015-06-19 07:46:21 +00004087 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004088
4089 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004090 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
4091 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004092 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004093 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004094
Alexey Bataev326057d2015-06-19 07:46:21 +00004095 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004096 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004097 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004098 }
4099
Eli Friedman4735374e2009-03-03 06:41:03 +00004100 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004101 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004102 }
4103
4104 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004105 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004106 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
4107 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004108 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004109 // Complex machine mode does not support base vector types.
4110 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004111 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004112 return;
4113 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004114 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00004115 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004116 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004117 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004118 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00004119 }
4120
4121 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004122 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004123 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004124 }
4125
4126 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004127 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004128 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004129 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004130 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004131 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004132 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004133
Erich Keane6a24e802019-09-13 17:39:31 +00004134 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
Chris Lattneracbc2d22008-06-27 22:18:37 +00004135}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004136
Erich Keanee891aa92018-07-13 15:07:47 +00004137static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004138 D->addAttr(::new (S.Context) NoDebugAttr(S.Context, AL));
Anders Carlsson76187b42009-02-13 06:46:13 +00004139}
4140
Erich Keane6a24e802019-09-13 17:39:31 +00004141AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D,
4142 const AttributeCommonInfo &CI,
4143 const IdentifierInfo *Ident) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004144 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004145 Diag(CI.getLoc(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00004146 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4147 return nullptr;
4148 }
4149
4150 if (D->hasAttr<AlwaysInlineAttr>())
4151 return nullptr;
4152
Erich Keane6a24e802019-09-13 17:39:31 +00004153 return ::new (Context) AlwaysInlineAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004154}
4155
Erich Keane44bacdf2018-08-09 13:21:32 +00004156CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
4157 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004158 return nullptr;
4159
Erich Keane6a24e802019-09-13 17:39:31 +00004160 return ::new (Context) CommonAttr(Context, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004161}
4162
Erich Keane44bacdf2018-08-09 13:21:32 +00004163CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
4164 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
4165 return nullptr;
4166
Erich Keane6a24e802019-09-13 17:39:31 +00004167 return ::new (Context) CommonAttr(Context, AL);
Erich Keane44bacdf2018-08-09 13:21:32 +00004168}
4169
4170InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4171 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004172 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004173 // Attribute applies to Var but not any subclass of it (like ParmVar,
4174 // ImplicitParm or VarTemplateSpecialization).
4175 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004176 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4177 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4178 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004179 return nullptr;
4180 }
4181 // Attribute does not apply to non-static local variables.
4182 if (VD->hasLocalStorage()) {
4183 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4184 return nullptr;
4185 }
4186 }
4187
Erich Keane44bacdf2018-08-09 13:21:32 +00004188 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
4189 return nullptr;
4190
Erich Keane6a24e802019-09-13 17:39:31 +00004191 return ::new (Context) InternalLinkageAttr(Context, AL);
Erich Keane44bacdf2018-08-09 13:21:32 +00004192}
4193InternalLinkageAttr *
4194Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4195 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4196 // Attribute applies to Var but not any subclass of it (like ParmVar,
4197 // ImplicitParm or VarTemplateSpecialization).
4198 if (VD->getKind() != Decl::Var) {
4199 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4200 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4201 : ExpectedVariableOrFunction);
4202 return nullptr;
4203 }
4204 // Attribute does not apply to non-static local variables.
4205 if (VD->hasLocalStorage()) {
4206 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4207 return nullptr;
4208 }
4209 }
4210
4211 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004212 return nullptr;
4213
Erich Keane6a24e802019-09-13 17:39:31 +00004214 return ::new (Context) InternalLinkageAttr(Context, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004215}
4216
Erich Keane6a24e802019-09-13 17:39:31 +00004217MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, const AttributeCommonInfo &CI) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004218 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004219 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'minsize'";
Paul Robinson30e41fb2014-12-15 18:57:28 +00004220 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4221 return nullptr;
4222 }
4223
4224 if (D->hasAttr<MinSizeAttr>())
4225 return nullptr;
4226
Erich Keane6a24e802019-09-13 17:39:31 +00004227 return ::new (Context) MinSizeAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004228}
4229
Zola Bridges826ef592019-01-18 17:20:46 +00004230NoSpeculativeLoadHardeningAttr *Sema::mergeNoSpeculativeLoadHardeningAttr(
4231 Decl *D, const NoSpeculativeLoadHardeningAttr &AL) {
4232 if (checkAttrMutualExclusion<SpeculativeLoadHardeningAttr>(*this, D, AL))
4233 return nullptr;
4234
Erich Keane6a24e802019-09-13 17:39:31 +00004235 return ::new (Context) NoSpeculativeLoadHardeningAttr(Context, AL);
Zola Bridges826ef592019-01-18 17:20:46 +00004236}
4237
Erich Keane6a24e802019-09-13 17:39:31 +00004238OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D,
4239 const AttributeCommonInfo &CI) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004240 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4241 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
Erich Keane6a24e802019-09-13 17:39:31 +00004242 Diag(CI.getLoc(), diag::note_conflicting_attribute);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004243 D->dropAttr<AlwaysInlineAttr>();
4244 }
4245 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4246 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
Erich Keane6a24e802019-09-13 17:39:31 +00004247 Diag(CI.getLoc(), diag::note_conflicting_attribute);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004248 D->dropAttr<MinSizeAttr>();
4249 }
4250
4251 if (D->hasAttr<OptimizeNoneAttr>())
4252 return nullptr;
4253
Erich Keane6a24e802019-09-13 17:39:31 +00004254 return ::new (Context) OptimizeNoneAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004255}
4256
Zola Bridges826ef592019-01-18 17:20:46 +00004257SpeculativeLoadHardeningAttr *Sema::mergeSpeculativeLoadHardeningAttr(
4258 Decl *D, const SpeculativeLoadHardeningAttr &AL) {
4259 if (checkAttrMutualExclusion<NoSpeculativeLoadHardeningAttr>(*this, D, AL))
4260 return nullptr;
4261
Erich Keane6a24e802019-09-13 17:39:31 +00004262 return ::new (Context) SpeculativeLoadHardeningAttr(Context, AL);
Zola Bridges826ef592019-01-18 17:20:46 +00004263}
4264
Erich Keanee891aa92018-07-13 15:07:47 +00004265static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004266 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004267 return;
4268
Erich Keane6a24e802019-09-13 17:39:31 +00004269 if (AlwaysInlineAttr *Inline =
4270 S.mergeAlwaysInlineAttr(D, AL, AL.getAttrName()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004271 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004272}
4273
Erich Keanee891aa92018-07-13 15:07:47 +00004274static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004275 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(D, AL))
Paul Robinson080b1f32015-01-13 18:34:56 +00004276 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004277}
4278
Erich Keanee891aa92018-07-13 15:07:47 +00004279static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004280 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(D, AL))
Paul Robinson080b1f32015-01-13 18:34:56 +00004281 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004282}
4283
Erich Keanee891aa92018-07-13 15:07:47 +00004284static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004285 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004286 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004287 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004288 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004289 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004290 return;
4291 }
Erich Keane6a24e802019-09-13 17:39:31 +00004292 D->addAttr(::new (S.Context) CUDAConstantAttr(S.Context, AL));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004293}
4294
Erich Keanee891aa92018-07-13 15:07:47 +00004295static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004296 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004297 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004298 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004299 // extern __shared__ is only allowed on arrays with no length (e.g.
4300 // "int x[]").
Yaxun Liu97670892018-10-02 17:48:54 +00004301 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004302 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004303 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004304 return;
4305 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004306 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004307 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004308 << S.CurrentCUDATarget())
4309 return;
Erich Keane6a24e802019-09-13 17:39:31 +00004310 D->addAttr(::new (S.Context) CUDASharedAttr(S.Context, AL));
Justin Lebar10411012016-09-30 23:57:30 +00004311}
4312
Erich Keanee891aa92018-07-13 15:07:47 +00004313static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004314 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4315 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004316 return;
4317 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004318 const auto *FD = cast<FunctionDecl>(D);
Michael Liao24337db2019-09-25 16:51:45 +00004319 if (!FD->getReturnType()->isVoidType() &&
4320 !FD->getReturnType()->getAs<AutoType>() &&
4321 !FD->getReturnType()->isInstantiationDependentType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004322 SourceRange RTRange = FD->getReturnTypeSourceRange();
4323 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004324 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004325 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4326 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004327 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004328 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004329 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4330 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004331 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004332 << Method;
4333 return;
4334 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004335 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004336 }
4337 // Only warn for "inline" when compiling for host, to cut down on noise.
4338 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004339 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004340
Erich Keane6a24e802019-09-13 17:39:31 +00004341 D->addAttr(::new (S.Context) CUDAGlobalAttr(S.Context, AL));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004342}
4343
Erich Keanee891aa92018-07-13 15:07:47 +00004344static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004345 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004346 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004347 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004348 return;
4349 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004350
Martin Storsjo71decf82019-09-27 12:25:19 +00004351 if (S.LangOpts.CPlusPlus && Fn->getStorageClass() != SC_Extern)
4352 S.Diag(AL.getLoc(), diag::warn_gnu_inline_cplusplus_without_extern);
4353
Erich Keane6a24e802019-09-13 17:39:31 +00004354 D->addAttr(::new (S.Context) GNUInlineAttr(S.Context, AL));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004355}
4356
Erich Keanee891aa92018-07-13 15:07:47 +00004357static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004358 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004359
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004360 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004361 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4362 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004363 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004364 return;
4365
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004366 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004367 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004368 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004369 return;
4370 }
4371
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004372 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004373 case ParsedAttr::AT_FastCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004374 D->addAttr(::new (S.Context) FastCallAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004375 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004376 case ParsedAttr::AT_StdCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004377 D->addAttr(::new (S.Context) StdCallAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004378 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004379 case ParsedAttr::AT_ThisCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004380 D->addAttr(::new (S.Context) ThisCallAttr(S.Context, AL));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004381 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004382 case ParsedAttr::AT_CDecl:
Erich Keane6a24e802019-09-13 17:39:31 +00004383 D->addAttr(::new (S.Context) CDeclAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004384 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004385 case ParsedAttr::AT_Pascal:
Erich Keane6a24e802019-09-13 17:39:31 +00004386 D->addAttr(::new (S.Context) PascalAttr(S.Context, AL));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004387 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004388 case ParsedAttr::AT_SwiftCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004389 D->addAttr(::new (S.Context) SwiftCallAttr(S.Context, AL));
John McCall477f2bb2016-03-03 06:39:32 +00004390 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004391 case ParsedAttr::AT_VectorCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004392 D->addAttr(::new (S.Context) VectorCallAttr(S.Context, AL));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004393 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004394 case ParsedAttr::AT_MSABI:
Erich Keane6a24e802019-09-13 17:39:31 +00004395 D->addAttr(::new (S.Context) MSABIAttr(S.Context, AL));
Charles Davisb5a214e2013-08-30 04:39:01 +00004396 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004397 case ParsedAttr::AT_SysVABI:
Erich Keane6a24e802019-09-13 17:39:31 +00004398 D->addAttr(::new (S.Context) SysVABIAttr(S.Context, AL));
Charles Davisb5a214e2013-08-30 04:39:01 +00004399 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004400 case ParsedAttr::AT_RegCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004401 D->addAttr(::new (S.Context) RegCallAttr(S.Context, AL));
Erich Keane757d3172016-11-02 18:29:35 +00004402 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004403 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004404 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004405 switch (CC) {
4406 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004407 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004408 break;
4409 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004410 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004411 break;
4412 default:
4413 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004414 }
4415
Erich Keane6a24e802019-09-13 17:39:31 +00004416 D->addAttr(::new (S.Context) PcsAttr(S.Context, AL, PCS));
Derek Schuffa2020962012-10-16 22:30:41 +00004417 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004418 }
Sander de Smalen44a22532018-11-26 16:38:37 +00004419 case ParsedAttr::AT_AArch64VectorPcs:
Erich Keane6a24e802019-09-13 17:39:31 +00004420 D->addAttr(::new (S.Context) AArch64VectorPcsAttr(S.Context, AL));
Sander de Smalen44a22532018-11-26 16:38:37 +00004421 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004422 case ParsedAttr::AT_IntelOclBicc:
Erich Keane6a24e802019-09-13 17:39:31 +00004423 D->addAttr(::new (S.Context) IntelOclBiccAttr(S.Context, AL));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004424 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004425 case ParsedAttr::AT_PreserveMost:
Erich Keane6a24e802019-09-13 17:39:31 +00004426 D->addAttr(::new (S.Context) PreserveMostAttr(S.Context, AL));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004427 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004428 case ParsedAttr::AT_PreserveAll:
Erich Keane6a24e802019-09-13 17:39:31 +00004429 D->addAttr(::new (S.Context) PreserveAllAttr(S.Context, AL));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004430 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004431 default:
4432 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004433 }
4434}
4435
Erich Keanee891aa92018-07-13 15:07:47 +00004436static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004437 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004438 return;
4439
4440 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004441 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004442 StringRef RuleName;
4443
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004444 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004445 return;
4446
4447 // FIXME: Warn if the rule name is unknown. This is tricky because only
4448 // clang-tidy knows about available rules.
4449 DiagnosticIdentifiers.push_back(RuleName);
4450 }
Erich Keane6a24e802019-09-13 17:39:31 +00004451 D->addAttr(::new (S.Context)
4452 SuppressAttr(S.Context, AL, DiagnosticIdentifiers.data(),
4453 DiagnosticIdentifiers.size()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004454}
4455
Matthias Gehred293cbd2019-07-25 17:50:51 +00004456static void handleLifetimeCategoryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4457 TypeSourceInfo *DerefTypeLoc = nullptr;
4458 QualType ParmType;
4459 if (AL.hasParsedType()) {
4460 ParmType = S.GetTypeFromParser(AL.getTypeArg(), &DerefTypeLoc);
4461
4462 unsigned SelectIdx = ~0U;
Gabor Horvath247a6032020-01-02 11:57:42 -08004463 if (ParmType->isReferenceType())
Matthias Gehred293cbd2019-07-25 17:50:51 +00004464 SelectIdx = 0;
Matthias Gehred293cbd2019-07-25 17:50:51 +00004465 else if (ParmType->isArrayType())
Gabor Horvath247a6032020-01-02 11:57:42 -08004466 SelectIdx = 1;
Matthias Gehred293cbd2019-07-25 17:50:51 +00004467
4468 if (SelectIdx != ~0U) {
4469 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument)
4470 << SelectIdx << AL;
4471 return;
4472 }
4473 }
4474
4475 // To check if earlier decl attributes do not conflict the newly parsed ones
4476 // we always add (and check) the attribute to the cannonical decl.
4477 D = D->getCanonicalDecl();
4478 if (AL.getKind() == ParsedAttr::AT_Owner) {
4479 if (checkAttrMutualExclusion<PointerAttr>(S, D, AL))
4480 return;
4481 if (const auto *OAttr = D->getAttr<OwnerAttr>()) {
4482 const Type *ExistingDerefType = OAttr->getDerefTypeLoc()
4483 ? OAttr->getDerefType().getTypePtr()
4484 : nullptr;
4485 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
4486 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
4487 << AL << OAttr;
4488 S.Diag(OAttr->getLocation(), diag::note_conflicting_attribute);
4489 }
4490 return;
4491 }
Matthias Gehref64f4882019-09-06 08:56:30 +00004492 for (Decl *Redecl : D->redecls()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004493 Redecl->addAttr(::new (S.Context) OwnerAttr(S.Context, AL, DerefTypeLoc));
Matthias Gehref64f4882019-09-06 08:56:30 +00004494 }
Matthias Gehred293cbd2019-07-25 17:50:51 +00004495 } else {
4496 if (checkAttrMutualExclusion<OwnerAttr>(S, D, AL))
4497 return;
4498 if (const auto *PAttr = D->getAttr<PointerAttr>()) {
4499 const Type *ExistingDerefType = PAttr->getDerefTypeLoc()
4500 ? PAttr->getDerefType().getTypePtr()
4501 : nullptr;
4502 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
4503 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
4504 << AL << PAttr;
4505 S.Diag(PAttr->getLocation(), diag::note_conflicting_attribute);
4506 }
4507 return;
4508 }
Matthias Gehref64f4882019-09-06 08:56:30 +00004509 for (Decl *Redecl : D->redecls()) {
4510 Redecl->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00004511 PointerAttr(S.Context, AL, DerefTypeLoc));
Matthias Gehref64f4882019-09-06 08:56:30 +00004512 }
Matthias Gehred293cbd2019-07-25 17:50:51 +00004513 }
4514}
4515
Erich Keanee891aa92018-07-13 15:07:47 +00004516bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004517 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004518 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004519 return true;
4520
Erich Keaneb11ebc52017-09-27 03:20:13 +00004521 if (Attrs.hasProcessingCache()) {
4522 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004523 return false;
4524 }
4525
Erich Keanee891aa92018-07-13 15:07:47 +00004526 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004527 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4528 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004529 return true;
4530 }
4531
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004532 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004533 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004534 case ParsedAttr::AT_CDecl:
4535 CC = CC_C;
4536 break;
4537 case ParsedAttr::AT_FastCall:
4538 CC = CC_X86FastCall;
4539 break;
4540 case ParsedAttr::AT_StdCall:
4541 CC = CC_X86StdCall;
4542 break;
4543 case ParsedAttr::AT_ThisCall:
4544 CC = CC_X86ThisCall;
4545 break;
4546 case ParsedAttr::AT_Pascal:
4547 CC = CC_X86Pascal;
4548 break;
4549 case ParsedAttr::AT_SwiftCall:
4550 CC = CC_Swift;
4551 break;
4552 case ParsedAttr::AT_VectorCall:
4553 CC = CC_X86VectorCall;
4554 break;
Sander de Smalen44a22532018-11-26 16:38:37 +00004555 case ParsedAttr::AT_AArch64VectorPcs:
4556 CC = CC_AArch64VectorCall;
4557 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004558 case ParsedAttr::AT_RegCall:
4559 CC = CC_X86RegCall;
4560 break;
4561 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004562 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004563 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004564 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004565 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004566 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4567 CC_C;
4568 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004569 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004570 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004571 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4572 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004573 return true;
4574 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004575 if (StrRef == "aapcs") {
4576 CC = CC_AAPCS;
4577 break;
4578 } else if (StrRef == "aapcs-vfp") {
4579 CC = CC_AAPCS_VFP;
4580 break;
4581 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004582
Erich Keaneb11ebc52017-09-27 03:20:13 +00004583 Attrs.setInvalid();
4584 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004585 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004586 }
Erich Keanee891aa92018-07-13 15:07:47 +00004587 case ParsedAttr::AT_IntelOclBicc:
4588 CC = CC_IntelOclBicc;
4589 break;
4590 case ParsedAttr::AT_PreserveMost:
4591 CC = CC_PreserveMost;
4592 break;
4593 case ParsedAttr::AT_PreserveAll:
4594 CC = CC_PreserveAll;
4595 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004596 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004597 }
4598
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004599 TargetInfo::CallingConvCheckResult A = TargetInfo::CCCR_OK;
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004600 const TargetInfo &TI = Context.getTargetInfo();
Yaxun Liu785cbd82019-02-27 15:46:29 +00004601 // CUDA functions may have host and/or device attributes which indicate
4602 // their targeted execution environment, therefore the calling convention
4603 // of functions in CUDA should be checked against the target deduced based
4604 // on their host/device attributes.
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004605 if (LangOpts.CUDA) {
Yaxun Liu785cbd82019-02-27 15:46:29 +00004606 auto *Aux = Context.getAuxTargetInfo();
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004607 auto CudaTarget = IdentifyCUDATarget(FD);
4608 bool CheckHost = false, CheckDevice = false;
4609 switch (CudaTarget) {
4610 case CFT_HostDevice:
4611 CheckHost = true;
4612 CheckDevice = true;
4613 break;
4614 case CFT_Host:
4615 CheckHost = true;
4616 break;
4617 case CFT_Device:
4618 case CFT_Global:
4619 CheckDevice = true;
4620 break;
4621 case CFT_InvalidTarget:
4622 llvm_unreachable("unexpected cuda target");
4623 }
4624 auto *HostTI = LangOpts.CUDAIsDevice ? Aux : &TI;
4625 auto *DeviceTI = LangOpts.CUDAIsDevice ? &TI : Aux;
4626 if (CheckHost && HostTI)
4627 A = HostTI->checkCallingConvention(CC);
4628 if (A == TargetInfo::CCCR_OK && CheckDevice && DeviceTI)
4629 A = DeviceTI->checkCallingConvention(CC);
4630 } else {
4631 A = TI.checkCallingConvention(CC);
4632 }
Reid Kleckner4586a192019-07-09 23:17:43 +00004633
4634 switch (A) {
4635 case TargetInfo::CCCR_OK:
4636 break;
4637
4638 case TargetInfo::CCCR_Ignore:
4639 // Treat an ignored convention as if it was an explicit C calling convention
4640 // attribute. For example, __stdcall on Win x64 functions as __cdecl, so
4641 // that command line flags that change the default convention to
4642 // __vectorcall don't affect declarations marked __stdcall.
4643 CC = CC_C;
4644 break;
4645
Sunil Srivastavaf4038e72019-07-19 21:38:34 +00004646 case TargetInfo::CCCR_Error:
4647 Diag(Attrs.getLoc(), diag::error_cconv_unsupported)
4648 << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget;
4649 break;
4650
Reid Kleckner4586a192019-07-09 23:17:43 +00004651 case TargetInfo::CCCR_Warning: {
Sunil Srivastava85d667f2019-07-17 20:41:26 +00004652 Diag(Attrs.getLoc(), diag::warn_cconv_unsupported)
Reid Kleckner4586a192019-07-09 23:17:43 +00004653 << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004654
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004655 // This convention is not valid for the target. Use the default function or
4656 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004657 bool IsCXXMethod = false, IsVariadic = false;
4658 if (FD) {
4659 IsCXXMethod = FD->isCXXInstanceMember();
4660 IsVariadic = FD->isVariadic();
4661 }
4662 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Reid Kleckner4586a192019-07-09 23:17:43 +00004663 break;
4664 }
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004665 }
4666
Erich Keaneb11ebc52017-09-27 03:20:13 +00004667 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004668 return false;
4669}
4670
John McCall477f2bb2016-03-03 06:39:32 +00004671/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004672static bool isValidSwiftContextType(QualType Ty) {
4673 if (!Ty->hasPointerRepresentation())
4674 return Ty->isDependentType();
4675 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004676}
4677
4678/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004679static bool isValidSwiftIndirectResultType(QualType Ty) {
4680 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4681 Ty = PtrType->getPointeeType();
4682 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4683 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004684 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004685 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004686 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004687 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004688}
4689
4690/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004691static bool isValidSwiftErrorResultType(QualType Ty) {
4692 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4693 Ty = PtrType->getPointeeType();
4694 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4695 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004696 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004697 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004698 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004699 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004700 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004701 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004702}
4703
Erich Keane6a24e802019-09-13 17:39:31 +00004704void Sema::AddParameterABIAttr(Decl *D, const AttributeCommonInfo &CI,
4705 ParameterABI abi) {
John McCall477f2bb2016-03-03 06:39:32 +00004706
4707 QualType type = cast<ParmVarDecl>(D)->getType();
4708
4709 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4710 if (existingAttr->getABI() != abi) {
Erich Keane6a24e802019-09-13 17:39:31 +00004711 Diag(CI.getLoc(), diag::err_attributes_are_not_compatible)
4712 << getParameterABISpelling(abi) << existingAttr;
John McCall477f2bb2016-03-03 06:39:32 +00004713 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4714 return;
4715 }
4716 }
4717
4718 switch (abi) {
4719 case ParameterABI::Ordinary:
4720 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4721
4722 case ParameterABI::SwiftContext:
4723 if (!isValidSwiftContextType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004724 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4725 << getParameterABISpelling(abi) << /*pointer to pointer */ 0 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004726 }
Erich Keane6a24e802019-09-13 17:39:31 +00004727 D->addAttr(::new (Context) SwiftContextAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004728 return;
4729
4730 case ParameterABI::SwiftErrorResult:
4731 if (!isValidSwiftErrorResultType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004732 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4733 << getParameterABISpelling(abi) << /*pointer to pointer */ 1 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004734 }
Erich Keane6a24e802019-09-13 17:39:31 +00004735 D->addAttr(::new (Context) SwiftErrorResultAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004736 return;
4737
4738 case ParameterABI::SwiftIndirectResult:
4739 if (!isValidSwiftIndirectResultType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004740 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4741 << getParameterABISpelling(abi) << /*pointer*/ 0 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004742 }
Erich Keane6a24e802019-09-13 17:39:31 +00004743 D->addAttr(::new (Context) SwiftIndirectResultAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004744 return;
4745 }
4746 llvm_unreachable("bad parameter ABI attribute");
4747}
4748
John McCall3882ace2011-01-05 12:14:39 +00004749/// Checks a regparm attribute, returning true if it is ill-formed and
4750/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004751bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004752 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004753 return true;
4754
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004755 if (!checkAttributeNumArgs(*this, AL, 1)) {
4756 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004757 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004758 }
Eli Friedman7044b762009-03-27 21:06:47 +00004759
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004760 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004761 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4762 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4763 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004764 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004765 }
4766
Douglas Gregore8bbc122011-09-02 00:18:52 +00004767 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004768 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004769 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004770 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004771 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004772 }
4773
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004774 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004775 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004776 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004777 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004778 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004779 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004780 }
4781
John McCall3882ace2011-01-05 12:14:39 +00004782 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004783}
4784
Artem Belevichbcec9da2016-06-06 22:54:57 +00004785// Checks whether an argument of launch_bounds attribute is
4786// acceptable, performs implicit conversion to Rvalue, and returns
4787// non-nullptr Expr result on success. Otherwise, it returns nullptr
4788// and may output an error.
4789static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004790 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004791 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004792 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004793 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004794
4795 // Accept template arguments for now as they depend on something else.
4796 // We'll get to check them when they eventually get instantiated.
4797 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004798 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004799
4800 llvm::APSInt I(64);
4801 if (!E->isIntegerConstantExpr(I, S.Context)) {
4802 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004803 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004804 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004805 }
4806 // Make sure we can fit it in 32 bits.
4807 if (!I.isIntN(32)) {
4808 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4809 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004810 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004811 }
4812 if (I < 0)
4813 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004814 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004815
Artem Belevichbcec9da2016-06-06 22:54:57 +00004816 // We may need to perform implicit conversion of the argument.
4817 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4818 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4819 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4820 assert(!ValArg.isInvalid() &&
4821 "Unexpected PerformCopyInitialization() failure.");
4822
4823 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004824}
4825
Erich Keane6a24e802019-09-13 17:39:31 +00004826void Sema::AddLaunchBoundsAttr(Decl *D, const AttributeCommonInfo &CI,
4827 Expr *MaxThreads, Expr *MinBlocks) {
4828 CUDALaunchBoundsAttr TmpAttr(Context, CI, MaxThreads, MinBlocks);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004829 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4830 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004831 return;
4832
Artem Belevichbcec9da2016-06-06 22:54:57 +00004833 if (MinBlocks) {
4834 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4835 if (MinBlocks == nullptr)
4836 return;
4837 }
Artem Belevich7093e402015-04-21 22:55:54 +00004838
Erich Keane6a24e802019-09-13 17:39:31 +00004839 D->addAttr(::new (Context)
4840 CUDALaunchBoundsAttr(Context, CI, MaxThreads, MinBlocks));
Artem Belevich7093e402015-04-21 22:55:54 +00004841}
4842
Erich Keanee891aa92018-07-13 15:07:47 +00004843static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004844 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4845 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004846 return;
4847
Erich Keane6a24e802019-09-13 17:39:31 +00004848 S.AddLaunchBoundsAttr(D, AL, AL.getArgAsExpr(0),
4849 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004850}
4851
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004852static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004853 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004854 if (!AL.isArgIdent(0)) {
4855 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004856 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004857 return;
4858 }
Joel E. Denny81508102018-03-13 14:51:22 +00004859
4860 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004861 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004862 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004863 return;
4864
Joel E. Denny81508102018-03-13 14:51:22 +00004865 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004866 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004867 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004868 return;
4869
Erich Keane6a24e802019-09-13 17:39:31 +00004870 bool IsPointer = AL.getAttrName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004871 if (IsPointer) {
4872 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004873 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4874 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4875 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004876 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004877 }
4878
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004879 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00004880 S.Context, AL, AL.getArgAsIdent(0)->Ident, ArgumentIdx, TypeTagIdx,
4881 IsPointer));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004882}
4883
4884static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004885 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004886 if (!AL.isArgIdent(0)) {
4887 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004888 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004889 return;
4890 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004891
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004892 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004893 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004894
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004895 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004896 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004897 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004898 return;
4899 }
4900
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004901 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004902 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004903 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004904 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004905
Erich Keane6a24e802019-09-13 17:39:31 +00004906 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
4907 S.Context, AL, PointerKind, MatchingCTypeLoc, AL.getLayoutCompatible(),
4908 AL.getMustBeNull()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004909}
4910
Erich Keanee891aa92018-07-13 15:07:47 +00004911static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004912 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004913
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004914 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004915 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004916 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004917 return;
4918
Joel E. Denny81508102018-03-13 14:51:22 +00004919 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
Erich Keane6a24e802019-09-13 17:39:31 +00004920 D->addAttr(::new (S.Context)
4921 XRayLogArgsAttr(S.Context, AL, ArgCount.getSourceIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004922}
4923
Fangrui Songa44c4342020-01-04 15:39:19 -08004924static void handlePatchableFunctionEntryAttr(Sema &S, Decl *D,
4925 const ParsedAttr &AL) {
4926 uint32_t Count = 0, Offset = 0;
4927 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Count, 0, true))
4928 return;
4929 if (AL.getNumArgs() == 2) {
4930 Expr *Arg = AL.getArgAsExpr(1);
4931 if (!checkUInt32Argument(S, AL, Arg, Offset, 1, true))
4932 return;
Fangrui Song69bf40c2020-01-20 14:30:06 -08004933 if (Count < Offset) {
Fangrui Songa44c4342020-01-04 15:39:19 -08004934 S.Diag(getAttrLoc(AL), diag::err_attribute_argument_out_of_range)
Fangrui Song69bf40c2020-01-20 14:30:06 -08004935 << &AL << 0 << Count << Arg->getBeginLoc();
Fangrui Songa44c4342020-01-04 15:39:19 -08004936 return;
4937 }
4938 }
4939 D->addAttr(::new (S.Context)
4940 PatchableFunctionEntryAttr(S.Context, AL, Count, Offset));
4941}
4942
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00004943namespace {
4944struct IntrinToName {
4945 uint32_t Id;
4946 int32_t FullName;
4947 int32_t ShortName;
4948};
4949} // unnamed namespace
4950
4951static bool ArmBuiltinAliasValid(unsigned BuiltinID, StringRef AliasName,
4952 ArrayRef<IntrinToName> Map,
4953 const char *IntrinNames) {
Simon Tatham08074cc2019-09-02 15:50:50 +01004954 if (AliasName.startswith("__arm_"))
4955 AliasName = AliasName.substr(6);
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00004956 const IntrinToName *It = std::lower_bound(
4957 Map.begin(), Map.end(), BuiltinID,
4958 [](const IntrinToName &L, unsigned Id) { return L.Id < Id; });
4959 if (It == Map.end() || It->Id != BuiltinID)
4960 return false;
4961 StringRef FullName(&IntrinNames[It->FullName]);
4962 if (AliasName == FullName)
4963 return true;
4964 if (It->ShortName == -1)
4965 return false;
4966 StringRef ShortName(&IntrinNames[It->ShortName]);
4967 return AliasName == ShortName;
Simon Tatham7c11da02019-09-02 15:35:09 +01004968}
4969
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00004970static bool ArmMveAliasValid(unsigned BuiltinID, StringRef AliasName) {
4971#include "clang/Basic/arm_mve_builtin_aliases.inc"
4972 // The included file defines:
4973 // - ArrayRef<IntrinToName> Map
4974 // - const char IntrinNames[]
4975 return ArmBuiltinAliasValid(BuiltinID, AliasName, Map, IntrinNames);
4976}
4977
4978static bool ArmCdeAliasValid(unsigned BuiltinID, StringRef AliasName) {
4979#include "clang/Basic/arm_cde_builtin_aliases.inc"
4980 return ArmBuiltinAliasValid(BuiltinID, AliasName, Map, IntrinNames);
4981}
4982
Sander de Smalen981f0802020-03-18 15:05:08 +00004983static bool ArmSveAliasValid(unsigned BuiltinID, StringRef AliasName) {
4984 switch (BuiltinID) {
4985 default:
4986 return false;
4987#define GET_SVE_BUILTINS
4988#define BUILTIN(name, types, attr) case SVE::BI##name:
4989#include "clang/Basic/arm_sve_builtins.inc"
4990 return true;
4991 }
4992}
4993
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00004994static void handleArmBuiltinAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Simon Tatham7c11da02019-09-02 15:35:09 +01004995 if (!AL.isArgIdent(0)) {
4996 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4997 << AL << 1 << AANT_ArgumentIdentifier;
4998 return;
4999 }
5000
5001 IdentifierInfo *Ident = AL.getArgAsIdent(0)->Ident;
5002 unsigned BuiltinID = Ident->getBuiltinID();
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00005003 StringRef AliasName = cast<FunctionDecl>(D)->getIdentifier()->getName();
Simon Tatham7c11da02019-09-02 15:35:09 +01005004
Sander de Smalen981f0802020-03-18 15:05:08 +00005005 bool IsAArch64 = S.Context.getTargetInfo().getTriple().isAArch64();
5006 if ((IsAArch64 && !ArmSveAliasValid(BuiltinID, AliasName)) ||
5007 (!IsAArch64 && !ArmMveAliasValid(BuiltinID, AliasName) &&
5008 !ArmCdeAliasValid(BuiltinID, AliasName))) {
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00005009 S.Diag(AL.getLoc(), diag::err_attribute_arm_builtin_alias);
Simon Tatham7c11da02019-09-02 15:35:09 +01005010 return;
5011 }
5012
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00005013 D->addAttr(::new (S.Context) ArmBuiltinAliasAttr(S.Context, AL, Ident));
Simon Tatham7c11da02019-09-02 15:35:09 +01005014}
5015
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005016//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005017// Checker-specific attribute handlers.
5018//===----------------------------------------------------------------------===//
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005019static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
5020 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00005021}
5022
George Karpenkov1657f362018-11-30 02:18:37 +00005023static bool isValidSubjectOfNSAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005024 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00005025 QT->isObjCNSObjectType();
John McCalled433932011-01-25 03:31:58 +00005026}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00005027
George Karpenkov1657f362018-11-30 02:18:37 +00005028static bool isValidSubjectOfCFAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005029 return QT->isDependentType() || QT->isPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00005030 isValidSubjectOfNSAttribute(QT);
John McCalled433932011-01-25 03:31:58 +00005031}
5032
George Karpenkov1657f362018-11-30 02:18:37 +00005033static bool isValidSubjectOfOSAttribute(QualType QT) {
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005034 if (QT->isDependentType())
5035 return true;
5036 QualType PT = QT->getPointeeType();
5037 return !PT.isNull() && PT->getAsCXXRecordDecl() != nullptr;
John McCall3b5a8f52016-03-03 00:10:03 +00005038}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005039
Erich Keane6a24e802019-09-13 17:39:31 +00005040void Sema::AddXConsumedAttr(Decl *D, const AttributeCommonInfo &CI,
George Karpenkov1657f362018-11-30 02:18:37 +00005041 RetainOwnershipKind K,
5042 bool IsTemplateInstantiation) {
5043 ValueDecl *VD = cast<ValueDecl>(D);
5044 switch (K) {
5045 case RetainOwnershipKind::OS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005046 handleSimpleAttributeOrDiagnose<OSConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00005047 *this, VD, CI, isValidSubjectOfOSAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00005048 diag::warn_ns_attribute_wrong_parameter_type,
Erich Keane6a24e802019-09-13 17:39:31 +00005049 /*ExtraArgs=*/CI.getRange(), "os_consumed", /*pointers*/ 1);
George Karpenkov1657f362018-11-30 02:18:37 +00005050 return;
5051 case RetainOwnershipKind::NS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005052 handleSimpleAttributeOrDiagnose<NSConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00005053 *this, VD, CI, isValidSubjectOfNSAttribute(VD->getType()),
John McCall3b5a8f52016-03-03 00:10:03 +00005054
George Karpenkov1657f362018-11-30 02:18:37 +00005055 // These attributes are normally just advisory, but in ARC, ns_consumed
5056 // is significant. Allow non-dependent code to contain inappropriate
5057 // attributes even in ARC, but require template instantiations to be
5058 // set up correctly.
5059 ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount)
5060 ? diag::err_ns_attribute_wrong_parameter_type
5061 : diag::warn_ns_attribute_wrong_parameter_type),
Erich Keane6a24e802019-09-13 17:39:31 +00005062 /*ExtraArgs=*/CI.getRange(), "ns_consumed", /*objc pointers*/ 0);
George Karpenkov1657f362018-11-30 02:18:37 +00005063 return;
5064 case RetainOwnershipKind::CF:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005065 handleSimpleAttributeOrDiagnose<CFConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00005066 *this, VD, CI, isValidSubjectOfCFAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00005067 diag::warn_ns_attribute_wrong_parameter_type,
Erich Keane6a24e802019-09-13 17:39:31 +00005068 /*ExtraArgs=*/CI.getRange(), "cf_consumed", /*pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00005069 return;
5070 }
George Karpenkov1657f362018-11-30 02:18:37 +00005071}
John McCalled433932011-01-25 03:31:58 +00005072
George Karpenkov1657f362018-11-30 02:18:37 +00005073static Sema::RetainOwnershipKind
5074parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) {
5075 switch (AL.getKind()) {
5076 case ParsedAttr::AT_CFConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005077 case ParsedAttr::AT_CFReturnsRetained:
5078 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005079 return Sema::RetainOwnershipKind::CF;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005080 case ParsedAttr::AT_OSConsumesThis:
George Karpenkov1657f362018-11-30 02:18:37 +00005081 case ParsedAttr::AT_OSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005082 case ParsedAttr::AT_OSReturnsRetained:
5083 case ParsedAttr::AT_OSReturnsNotRetained:
5084 case ParsedAttr::AT_OSReturnsRetainedOnZero:
5085 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
George Karpenkov1657f362018-11-30 02:18:37 +00005086 return Sema::RetainOwnershipKind::OS;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005087 case ParsedAttr::AT_NSConsumesSelf:
George Karpenkov1657f362018-11-30 02:18:37 +00005088 case ParsedAttr::AT_NSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005089 case ParsedAttr::AT_NSReturnsRetained:
5090 case ParsedAttr::AT_NSReturnsNotRetained:
5091 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005092 return Sema::RetainOwnershipKind::NS;
5093 default:
5094 llvm_unreachable("Wrong argument supplied");
5095 }
John McCalled433932011-01-25 03:31:58 +00005096}
5097
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005098bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
5099 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00005100 return false;
5101
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005102 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
5103 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00005104 return true;
5105}
5106
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005107/// \return whether the parameter is a pointer to OSObject pointer.
5108static bool isValidOSObjectOutParameter(const Decl *D) {
5109 const auto *PVD = dyn_cast<ParmVarDecl>(D);
5110 if (!PVD)
5111 return false;
5112 QualType QT = PVD->getType();
5113 QualType PT = QT->getPointeeType();
5114 return !PT.isNull() && isValidSubjectOfOSAttribute(PT);
5115}
5116
George Karpenkov1657f362018-11-30 02:18:37 +00005117static void handleXReturnsXRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005118 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005119 QualType ReturnType;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005120 Sema::RetainOwnershipKind K = parsedAttrToRetainOwnershipKind(AL);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005121
George Karpenkov1657f362018-11-30 02:18:37 +00005122 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005123 ReturnType = MD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005124 } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
5125 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) {
John McCall31168b02011-06-15 23:02:42 +00005126 return; // ignore: was handled as a type attribute
George Karpenkov1657f362018-11-30 02:18:37 +00005127 } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005128 ReturnType = PD->getType();
George Karpenkov1657f362018-11-30 02:18:37 +00005129 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005130 ReturnType = FD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005131 } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
5132 // Attributes on parameters are used for out-parameters,
5133 // passed as pointers-to-pointers.
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005134 unsigned DiagID = K == Sema::RetainOwnershipKind::CF
5135 ? /*pointer-to-CF-pointer*/2
5136 : /*pointer-to-OSObject-pointer*/3;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005137 ReturnType = Param->getType()->getPointeeType();
5138 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005139 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005140 << AL << DiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005141 return;
5142 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005143 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00005144 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005145 } else {
5146 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005147 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005148 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005149 case ParsedAttr::AT_NSReturnsRetained:
5150 case ParsedAttr::AT_NSReturnsAutoreleased:
5151 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005152 ExpectedDeclKind = ExpectedFunctionOrMethod;
5153 break;
5154
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005155 case ParsedAttr::AT_OSReturnsRetained:
5156 case ParsedAttr::AT_OSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00005157 case ParsedAttr::AT_CFReturnsRetained:
5158 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005159 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
5160 break;
5161 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005162 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005163 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005164 return;
5165 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005166
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005167 bool TypeOK;
5168 bool Cf;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005169 unsigned ParmDiagID = 2; // Pointer-to-CF-pointer
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005170 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00005171 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005172 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005173 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
5174 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00005175 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005176
5177 case ParsedAttr::AT_NSReturnsAutoreleased:
5178 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005179 TypeOK = isValidSubjectOfNSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005180 Cf = false;
John McCalled433932011-01-25 03:31:58 +00005181 break;
5182
Erich Keanee891aa92018-07-13 15:07:47 +00005183 case ParsedAttr::AT_CFReturnsRetained:
5184 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005185 TypeOK = isValidSubjectOfCFAttribute(ReturnType);
5186 Cf = true;
5187 break;
5188
5189 case ParsedAttr::AT_OSReturnsRetained:
5190 case ParsedAttr::AT_OSReturnsNotRetained:
5191 TypeOK = isValidSubjectOfOSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005192 Cf = true;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005193 ParmDiagID = 3; // Pointer-to-OSObject-pointer
John McCalled433932011-01-25 03:31:58 +00005194 break;
5195 }
5196
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005197 if (!TypeOK) {
5198 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00005199 return;
5200
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005201 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005202 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005203 << AL << ParmDiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005204 } else {
5205 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
5206 enum : unsigned {
5207 Function,
5208 Method,
5209 Property
5210 } SubjectKind = Function;
5211 if (isa<ObjCMethodDecl>(D))
5212 SubjectKind = Method;
5213 else if (isa<ObjCPropertyDecl>(D))
5214 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005215 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005216 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005217 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005218 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00005219 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005220
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005221 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005222 default:
David Blaikie83d382b2011-09-23 05:06:16 +00005223 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005224 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005225 handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL);
John McCalled433932011-01-25 03:31:58 +00005226 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005227 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005228 handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005229 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005230 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005231 handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005232 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005233 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005234 handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005235 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005236 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005237 handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL);
5238 return;
5239 case ParsedAttr::AT_OSReturnsRetained:
5240 handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL);
5241 return;
5242 case ParsedAttr::AT_OSReturnsNotRetained:
5243 handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005244 return;
5245 };
5246}
5247
John McCallcf166702011-07-22 08:53:00 +00005248static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005249 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00005250 const int EP_ObjCMethod = 1;
5251 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00005252
Erich Keaneb11ebc52017-09-27 03:20:13 +00005253 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005254 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005255 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00005256 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005257 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005258 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00005259
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00005260 if (!resultType->isReferenceType() &&
5261 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005262 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005263 << SourceRange(loc) << Attrs
5264 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
5265 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00005266
5267 // Drop the attribute.
5268 return;
5269 }
5270
Erich Keane6a24e802019-09-13 17:39:31 +00005271 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(S.Context, Attrs));
John McCallcf166702011-07-22 08:53:00 +00005272}
5273
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005274static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005275 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005276 const auto *Method = cast<ObjCMethodDecl>(D);
5277
5278 const DeclContext *DC = Method->getDeclContext();
5279 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005280 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005281 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005282 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
5283 return;
5284 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005285 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005286 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005287 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005288 return;
5289 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005290
Erich Keane6a24e802019-09-13 17:39:31 +00005291 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(S.Context, Attrs));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005292}
5293
Erich Keanee891aa92018-07-13 15:07:47 +00005294static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005295 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00005296
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005297 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005298 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005299 return;
5300 }
John McCall28592582015-02-01 22:34:06 +00005301
5302 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005303 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00005304 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005305 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00005306 return;
5307 }
5308
5309 // Only allow 'cv void *'.
5310 QualType T = TD->getUnderlyingType();
5311 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005312 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00005313 return;
5314 }
5315 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005316
Erich Keane6a24e802019-09-13 17:39:31 +00005317 D->addAttr(::new (S.Context) ObjCBridgeAttr(S.Context, AL, Parm->Ident));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005318}
5319
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005320static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005321 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005322 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00005323
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005324 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005325 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005326 return;
5327 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005328
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005329 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005330 ObjCBridgeMutableAttr(S.Context, AL, Parm->Ident));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005331}
5332
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005333static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005334 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005335 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005336 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005337 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005338 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005339 return;
5340 }
5341 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005342 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005343 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005344 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Erich Keane6a24e802019-09-13 17:39:31 +00005345 D->addAttr(::new (S.Context) ObjCBridgeRelatedAttr(
5346 S.Context, AL, RelatedClass, ClassMethod, InstanceMethod));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005347}
5348
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005349static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005350 const ParsedAttr &AL) {
Erik Pilkington81d3f452019-02-13 20:32:37 +00005351 DeclContext *Ctx = D->getDeclContext();
5352
5353 // This attribute can only be applied to methods in interfaces or class
5354 // extensions.
5355 if (!isa<ObjCInterfaceDecl>(Ctx) &&
5356 !(isa<ObjCCategoryDecl>(Ctx) &&
5357 cast<ObjCCategoryDecl>(Ctx)->IsClassExtension())) {
5358 S.Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
5359 return;
5360 }
5361
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005362 ObjCInterfaceDecl *IFace;
Erik Pilkington81d3f452019-02-13 20:32:37 +00005363 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(Ctx))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005364 IFace = CatDecl->getClassInterface();
5365 else
Erik Pilkington81d3f452019-02-13 20:32:37 +00005366 IFace = cast<ObjCInterfaceDecl>(Ctx);
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005367
5368 if (!IFace)
5369 return;
5370
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00005371 IFace->setHasDesignatedInitializers();
Erich Keane6a24e802019-09-13 17:39:31 +00005372 D->addAttr(::new (S.Context) ObjCDesignatedInitializerAttr(S.Context, AL));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005373}
5374
Erich Keanee891aa92018-07-13 15:07:47 +00005375static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005376 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005377 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005378 return;
5379 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005380 ObjCRuntimeNameAttr(S.Context, AL, MetaDataName));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005381}
5382
Nico Webera6916892016-06-10 18:53:04 +00005383// When a user wants to use objc_boxable with a union or struct
5384// but they don't have access to the declaration (legacy/third-party code)
5385// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00005386// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00005387static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00005388 bool notify = false;
5389
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005390 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00005391 if (RD && RD->getDefinition()) {
5392 RD = RD->getDefinition();
5393 notify = true;
5394 }
5395
5396 if (RD) {
Erich Keane6a24e802019-09-13 17:39:31 +00005397 ObjCBoxableAttr *BoxableAttr =
5398 ::new (S.Context) ObjCBoxableAttr(S.Context, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00005399 RD->addAttr(BoxableAttr);
5400 if (notify) {
5401 // we need to notify ASTReader/ASTWriter about
5402 // modification of existing declaration
5403 if (ASTMutationListener *L = S.getASTMutationListener())
5404 L->AddedAttributeToRecord(BoxableAttr, RD);
5405 }
5406 }
5407}
5408
Erich Keanee891aa92018-07-13 15:07:47 +00005409static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005410 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005411
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005412 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005413 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005414}
5415
Chandler Carruthedc2c642011-07-02 00:01:44 +00005416static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005417 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005418 const auto *VD = cast<ValueDecl>(D);
5419 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005420
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005421 if (!QT->isDependentType() &&
5422 !QT->isObjCLifetimeType()) {
5423 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5424 << QT;
John McCall31168b02011-06-15 23:02:42 +00005425 return;
5426 }
5427
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005428 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005429
5430 // If we have no lifetime yet, check the lifetime we're presumably
5431 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005432 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5433 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005434
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005435 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005436 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005437 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005438 "didn't infer lifetime for non-dependent type?");
5439 break;
5440
5441 case Qualifiers::OCL_Weak: // meaningful
5442 case Qualifiers::OCL_Strong: // meaningful
5443 break;
5444
5445 case Qualifiers::OCL_ExplicitNone:
5446 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005447 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5448 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005449 break;
5450 }
5451
Erich Keane6a24e802019-09-13 17:39:31 +00005452 D->addAttr(::new (S.Context) ObjCPreciseLifetimeAttr(S.Context, AL));
John McCall31168b02011-06-15 23:02:42 +00005453}
5454
Francois Picheta83957a2010-12-19 06:50:37 +00005455//===----------------------------------------------------------------------===//
5456// Microsoft specific attribute handlers.
5457//===----------------------------------------------------------------------===//
5458
Erich Keane6a24e802019-09-13 17:39:31 +00005459UuidAttr *Sema::mergeUuidAttr(Decl *D, const AttributeCommonInfo &CI,
5460 StringRef Uuid) {
Nico Weber88f5ed92016-09-13 18:55:26 +00005461 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005462 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005463 return nullptr;
Zachary Henkel0acfc492019-12-28 13:06:13 -08005464 if (!UA->getGuid().empty()) {
5465 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5466 Diag(CI.getLoc(), diag::note_previous_uuid);
5467 D->dropAttr<UuidAttr>();
5468 }
Nico Weber88f5ed92016-09-13 18:55:26 +00005469 }
5470
Erich Keane6a24e802019-09-13 17:39:31 +00005471 return ::new (Context) UuidAttr(Context, CI, Uuid);
Nico Weber88f5ed92016-09-13 18:55:26 +00005472}
5473
Erich Keanee891aa92018-07-13 15:07:47 +00005474static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005475 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005476 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005477 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005478 return;
5479 }
5480
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005481 StringRef StrRef;
5482 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005483 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005484 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005485
David Majnemer89085342013-08-09 08:56:20 +00005486 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5487 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005488 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5489 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005490
Reid Kleckner140c4a72013-05-17 14:04:52 +00005491 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005492 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005493 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005494 return;
5495 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005496
David Majnemer89085342013-08-09 08:56:20 +00005497 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005498 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005499 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005500 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005501 return;
5502 }
David Majnemer89085342013-08-09 08:56:20 +00005503 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005504 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005505 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005506 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005507 }
Francois Picheta83957a2010-12-19 06:50:37 +00005508
Nico Weber469891e2017-05-05 17:05:56 +00005509 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5510 // the only thing in the [] list, the [] too), and add an insertion of
5511 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5512 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005513 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005514 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005515 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5516 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005517
Erich Keane6a24e802019-09-13 17:39:31 +00005518 UuidAttr *UA = S.mergeUuidAttr(D, AL, StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005519 if (UA)
5520 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005521}
5522
Erich Keanee891aa92018-07-13 15:07:47 +00005523static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005524 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005525 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005526 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005527 return;
5528 }
5529 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Reid Klecknera9cc64e2019-11-15 18:49:32 -08005530 D, AL, /*BestCase=*/true, (MSInheritanceModel)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005531 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005532 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005533 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5534 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005535}
5536
Erich Keanee891aa92018-07-13 15:07:47 +00005537static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005538 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005539 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005540 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005541 return;
5542 }
5543 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005544 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005545 return;
5546 }
5547 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005548 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005549 return;
5550 }
Erich Keane6a24e802019-09-13 17:39:31 +00005551 D->addAttr(::new (S.Context) ThreadAttr(S.Context, AL));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005552}
5553
Erich Keanee891aa92018-07-13 15:07:47 +00005554static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005555 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005556 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005557 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005558 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005559 return;
5560 Tags.push_back(Tag);
5561 }
5562
5563 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5564 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005565 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005566 return;
5567 }
5568 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005569 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005570 return;
5571 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005572 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005573 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005574 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005575 return;
5576
5577 // Store tags sorted and without duplicates.
Fangrui Song55fab262018-09-26 22:16:28 +00005578 llvm::sort(Tags);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005579 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5580
5581 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005582 AbiTagAttr(S.Context, AL, Tags.data(), Tags.size()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005583}
5584
Erich Keanee891aa92018-07-13 15:07:47 +00005585static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005586 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005587 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005588 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005589 return;
5590 }
5591
5592 StringRef Str;
5593 SourceLocation ArgLoc;
5594
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005595 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005596 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005597 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005598 return;
5599
5600 ARMInterruptAttr::InterruptType Kind;
5601 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005602 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5603 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005604 return;
5605 }
5606
Erich Keane6a24e802019-09-13 17:39:31 +00005607 D->addAttr(::new (S.Context) ARMInterruptAttr(S.Context, AL, Kind));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005608}
5609
Erich Keanee891aa92018-07-13 15:07:47 +00005610static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005611 // MSP430 'interrupt' attribute is applied to
5612 // a function with no parameters and void return type.
5613 if (!isFunctionOrMethod(D)) {
5614 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5615 << "'interrupt'" << ExpectedFunctionOrMethod;
5616 return;
5617 }
5618
5619 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005620 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5621 << /*MSP430*/ 1 << 0;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005622 return;
5623 }
5624
5625 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005626 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5627 << /*MSP430*/ 1 << 1;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005628 return;
5629 }
5630
5631 // The attribute takes one integer argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005632 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005633 return;
5634
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005635 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005636 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5637 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005638 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005639 }
5640
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005641 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005642 llvm::APSInt NumParams(32);
5643 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005644 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005645 << AL << AANT_ArgumentIntegerConstant
5646 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005647 return;
5648 }
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005649 // The argument should be in range 0..63.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005650 unsigned Num = NumParams.getLimitedValue(255);
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005651 if (Num > 63) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005652 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005653 << AL << (int)NumParams.getSExtValue()
5654 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005655 return;
5656 }
5657
Erich Keane6a24e802019-09-13 17:39:31 +00005658 D->addAttr(::new (S.Context) MSP430InterruptAttr(S.Context, AL, Num));
Aaron Ballman36a53502014-01-16 13:03:14 +00005659 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005660}
5661
Erich Keanee891aa92018-07-13 15:07:47 +00005662static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005663 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005664 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005665 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005666 return;
5667 }
5668
5669 StringRef Str;
5670 SourceLocation ArgLoc;
5671
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005672 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005673 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005674 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005675 return;
5676
5677 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5678 // a) Must be a function.
5679 // b) Must have no parameters.
5680 // c) Must have the 'void' return type.
5681 // d) Cannot have the 'mips16' attribute, as that instruction set
5682 // lacks the 'eret' instruction.
5683 // e) The attribute itself must either have no argument or one of the
5684 // valid interrupt types, see [MipsInterruptDocs].
5685
5686 if (!isFunctionOrMethod(D)) {
5687 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5688 << "'interrupt'" << ExpectedFunctionOrMethod;
5689 return;
5690 }
5691
5692 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005693 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5694 << /*MIPS*/ 0 << 0;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005695 return;
5696 }
5697
5698 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005699 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5700 << /*MIPS*/ 0 << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005701 return;
5702 }
5703
Erich Keane44bacdf2018-08-09 13:21:32 +00005704 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005705 return;
5706
5707 MipsInterruptAttr::InterruptType Kind;
5708 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005709 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005710 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005711 return;
5712 }
5713
Erich Keane6a24e802019-09-13 17:39:31 +00005714 D->addAttr(::new (S.Context) MipsInterruptAttr(S.Context, AL, Kind));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005715}
5716
Erich Keanee891aa92018-07-13 15:07:47 +00005717static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005718 // Semantic checks for a function with the 'interrupt' attribute.
5719 // a) Must be a function.
5720 // b) Must have the 'void' return type.
5721 // c) Must take 1 or 2 arguments.
5722 // d) The 1st argument must be a pointer.
5723 // e) The 2nd argument (if any) must be an unsigned integer.
5724 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5725 CXXMethodDecl::isStaticOverloadedOperator(
5726 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005727 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005728 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005729 return;
5730 }
5731 // Interrupt handler must have void return type.
5732 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5733 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5734 diag::err_anyx86_interrupt_attribute)
5735 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5736 ? 0
5737 : 1)
5738 << 0;
5739 return;
5740 }
5741 // Interrupt handler must have 1 or 2 parameters.
5742 unsigned NumParams = getFunctionOrMethodNumParams(D);
5743 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005744 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005745 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5746 ? 0
5747 : 1)
5748 << 1;
5749 return;
5750 }
5751 // The first argument must be a pointer.
5752 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5753 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5754 diag::err_anyx86_interrupt_attribute)
5755 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5756 ? 0
5757 : 1)
5758 << 2;
5759 return;
5760 }
5761 // The second argument, if present, must be an unsigned integer.
5762 unsigned TypeSize =
5763 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5764 ? 64
5765 : 32;
5766 if (NumParams == 2 &&
5767 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5768 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5769 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5770 diag::err_anyx86_interrupt_attribute)
5771 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5772 ? 0
5773 : 1)
5774 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5775 return;
5776 }
Erich Keane6a24e802019-09-13 17:39:31 +00005777 D->addAttr(::new (S.Context) AnyX86InterruptAttr(S.Context, AL));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005778 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5779}
5780
Erich Keanee891aa92018-07-13 15:07:47 +00005781static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005782 if (!isFunctionOrMethod(D)) {
5783 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5784 << "'interrupt'" << ExpectedFunction;
5785 return;
5786 }
5787
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005788 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005789 return;
5790
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005791 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005792}
5793
Erich Keanee891aa92018-07-13 15:07:47 +00005794static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005795 if (!isFunctionOrMethod(D)) {
5796 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5797 << "'signal'" << ExpectedFunction;
5798 return;
5799 }
5800
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005801 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005802 return;
5803
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005804 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005805}
5806
Yonghong Song4e2ce222019-11-01 22:16:59 -07005807static void handleBPFPreserveAIRecord(Sema &S, RecordDecl *RD) {
5808 // Add preserve_access_index attribute to all fields and inner records.
5809 for (auto D : RD->decls()) {
5810 if (D->hasAttr<BPFPreserveAccessIndexAttr>())
5811 continue;
5812
5813 D->addAttr(BPFPreserveAccessIndexAttr::CreateImplicit(S.Context));
5814 if (auto *Rec = dyn_cast<RecordDecl>(D))
5815 handleBPFPreserveAIRecord(S, Rec);
5816 }
5817}
5818
5819static void handleBPFPreserveAccessIndexAttr(Sema &S, Decl *D,
5820 const ParsedAttr &AL) {
5821 auto *Rec = cast<RecordDecl>(D);
5822 handleBPFPreserveAIRecord(S, Rec);
5823 Rec->addAttr(::new (S.Context) BPFPreserveAccessIndexAttr(S.Context, AL));
5824}
5825
Sam Clegg881d8772019-11-05 10:15:56 -08005826static void handleWebAssemblyExportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5827 if (!isFunctionOrMethod(D)) {
5828 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5829 << "'export_name'" << ExpectedFunction;
5830 return;
5831 }
5832
5833 auto *FD = cast<FunctionDecl>(D);
5834 if (FD->isThisDeclarationADefinition()) {
5835 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5836 return;
5837 }
5838
5839 StringRef Str;
5840 SourceLocation ArgLoc;
5841 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5842 return;
5843
Sam Clegg0a1e3492019-12-13 14:44:06 -08005844 D->addAttr(::new (S.Context) WebAssemblyExportNameAttr(S.Context, AL, Str));
5845 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Sam Clegg881d8772019-11-05 10:15:56 -08005846}
5847
Dan Gohmanb4323692019-01-24 21:08:30 +00005848static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5849 if (!isFunctionOrMethod(D)) {
5850 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5851 << "'import_module'" << ExpectedFunction;
5852 return;
5853 }
5854
5855 auto *FD = cast<FunctionDecl>(D);
5856 if (FD->isThisDeclarationADefinition()) {
5857 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5858 return;
5859 }
5860
5861 StringRef Str;
5862 SourceLocation ArgLoc;
5863 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5864 return;
5865
Erich Keane6a24e802019-09-13 17:39:31 +00005866 FD->addAttr(::new (S.Context)
5867 WebAssemblyImportModuleAttr(S.Context, AL, Str));
Dan Gohmanb4323692019-01-24 21:08:30 +00005868}
Ana Pazos1eee1b72018-07-26 17:37:45 +00005869
Dan Gohmancae84592019-02-01 22:25:23 +00005870static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5871 if (!isFunctionOrMethod(D)) {
5872 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5873 << "'import_name'" << ExpectedFunction;
5874 return;
5875 }
5876
5877 auto *FD = cast<FunctionDecl>(D);
5878 if (FD->isThisDeclarationADefinition()) {
5879 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5880 return;
5881 }
5882
5883 StringRef Str;
5884 SourceLocation ArgLoc;
5885 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5886 return;
5887
Erich Keane6a24e802019-09-13 17:39:31 +00005888 FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr(S.Context, AL, Str));
Dan Gohmancae84592019-02-01 22:25:23 +00005889}
5890
Ana Pazos1eee1b72018-07-26 17:37:45 +00005891static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5892 const ParsedAttr &AL) {
5893 // Warn about repeated attributes.
5894 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5895 S.Diag(AL.getRange().getBegin(),
5896 diag::warn_riscv_repeated_interrupt_attribute);
5897 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5898 return;
5899 }
5900
5901 // Check the attribute argument. Argument is optional.
5902 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5903 return;
5904
5905 StringRef Str;
5906 SourceLocation ArgLoc;
5907
5908 // 'machine'is the default interrupt mode.
5909 if (AL.getNumArgs() == 0)
5910 Str = "machine";
5911 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5912 return;
5913
5914 // Semantic checks for a function with the 'interrupt' attribute:
5915 // - Must be a function.
5916 // - Must have no parameters.
5917 // - Must have the 'void' return type.
5918 // - The attribute itself must either have no argument or one of the
5919 // valid interrupt types, see [RISCVInterruptDocs].
5920
5921 if (D->getFunctionType() == nullptr) {
5922 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5923 << "'interrupt'" << ExpectedFunction;
5924 return;
5925 }
5926
5927 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005928 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5929 << /*RISC-V*/ 2 << 0;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005930 return;
5931 }
5932
5933 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005934 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5935 << /*RISC-V*/ 2 << 1;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005936 return;
5937 }
5938
5939 RISCVInterruptAttr::InterruptType Kind;
5940 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005941 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5942 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005943 return;
5944 }
5945
Erich Keane6a24e802019-09-13 17:39:31 +00005946 D->addAttr(::new (S.Context) RISCVInterruptAttr(S.Context, AL, Kind));
Ana Pazos1eee1b72018-07-26 17:37:45 +00005947}
5948
Erich Keanee891aa92018-07-13 15:07:47 +00005949static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005950 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005951 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5952 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005953 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005954 break;
5955 case llvm::Triple::mipsel:
5956 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005957 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005958 break;
5959 case llvm::Triple::x86:
5960 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005961 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005962 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005963 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005964 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005965 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005966 case llvm::Triple::riscv32:
5967 case llvm::Triple::riscv64:
5968 handleRISCVInterruptAttr(S, D, AL);
5969 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005970 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005971 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005972 break;
5973 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005974}
5975
Michael Liao7557afa2019-02-26 18:49:36 +00005976static bool
5977checkAMDGPUFlatWorkGroupSizeArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr,
5978 const AMDGPUFlatWorkGroupSizeAttr &Attr) {
5979 // Accept template arguments for now as they depend on something else.
5980 // We'll get to check them when they eventually get instantiated.
5981 if (MinExpr->isValueDependent() || MaxExpr->isValueDependent())
5982 return false;
5983
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005984 uint32_t Min = 0;
Michael Liao7557afa2019-02-26 18:49:36 +00005985 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
5986 return true;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005987
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005988 uint32_t Max = 0;
Michael Liao7557afa2019-02-26 18:49:36 +00005989 if (!checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
5990 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005991
5992 if (Min == 0 && Max != 0) {
Michael Liao7557afa2019-02-26 18:49:36 +00005993 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5994 << &Attr << 0;
5995 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005996 }
5997 if (Min > Max) {
Michael Liao7557afa2019-02-26 18:49:36 +00005998 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5999 << &Attr << 1;
6000 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006001 }
6002
Michael Liao7557afa2019-02-26 18:49:36 +00006003 return false;
6004}
6005
Erich Keane6a24e802019-09-13 17:39:31 +00006006void Sema::addAMDGPUFlatWorkGroupSizeAttr(Decl *D,
6007 const AttributeCommonInfo &CI,
6008 Expr *MinExpr, Expr *MaxExpr) {
6009 AMDGPUFlatWorkGroupSizeAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00006010
6011 if (checkAMDGPUFlatWorkGroupSizeArguments(*this, MinExpr, MaxExpr, TmpAttr))
6012 return;
6013
Erich Keane6a24e802019-09-13 17:39:31 +00006014 D->addAttr(::new (Context)
6015 AMDGPUFlatWorkGroupSizeAttr(Context, CI, MinExpr, MaxExpr));
Michael Liao7557afa2019-02-26 18:49:36 +00006016}
6017
6018static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
6019 const ParsedAttr &AL) {
6020 Expr *MinExpr = AL.getArgAsExpr(0);
6021 Expr *MaxExpr = AL.getArgAsExpr(1);
6022
Erich Keane6a24e802019-09-13 17:39:31 +00006023 S.addAMDGPUFlatWorkGroupSizeAttr(D, AL, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00006024}
6025
6026static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr,
6027 Expr *MaxExpr,
6028 const AMDGPUWavesPerEUAttr &Attr) {
6029 if (S.DiagnoseUnexpandedParameterPack(MinExpr) ||
6030 (MaxExpr && S.DiagnoseUnexpandedParameterPack(MaxExpr)))
6031 return true;
6032
6033 // Accept template arguments for now as they depend on something else.
6034 // We'll get to check them when they eventually get instantiated.
6035 if (MinExpr->isValueDependent() || (MaxExpr && MaxExpr->isValueDependent()))
6036 return false;
6037
6038 uint32_t Min = 0;
6039 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
6040 return true;
6041
6042 uint32_t Max = 0;
6043 if (MaxExpr && !checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
6044 return true;
6045
6046 if (Min == 0 && Max != 0) {
6047 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
6048 << &Attr << 0;
6049 return true;
6050 }
6051 if (Max != 0 && Min > Max) {
6052 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
6053 << &Attr << 1;
6054 return true;
6055 }
6056
6057 return false;
6058}
6059
Erich Keane6a24e802019-09-13 17:39:31 +00006060void Sema::addAMDGPUWavesPerEUAttr(Decl *D, const AttributeCommonInfo &CI,
6061 Expr *MinExpr, Expr *MaxExpr) {
6062 AMDGPUWavesPerEUAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00006063
6064 if (checkAMDGPUWavesPerEUArguments(*this, MinExpr, MaxExpr, TmpAttr))
6065 return;
6066
Erich Keane6a24e802019-09-13 17:39:31 +00006067 D->addAttr(::new (Context)
6068 AMDGPUWavesPerEUAttr(Context, CI, MinExpr, MaxExpr));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006069}
6070
Erich Keanee891aa92018-07-13 15:07:47 +00006071static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Liao7557afa2019-02-26 18:49:36 +00006072 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
6073 !checkAttributeAtMostNumArgs(S, AL, 2))
6074 return;
6075
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006076 Expr *MinExpr = AL.getArgAsExpr(0);
Michael Liao7557afa2019-02-26 18:49:36 +00006077 Expr *MaxExpr = (AL.getNumArgs() > 1) ? AL.getArgAsExpr(1) : nullptr;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006078
Erich Keane6a24e802019-09-13 17:39:31 +00006079 S.addAMDGPUWavesPerEUAttr(D, AL, MinExpr, MaxExpr);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006080}
6081
Erich Keanee891aa92018-07-13 15:07:47 +00006082static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006083 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006084 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
6085 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006086 return;
6087
Erich Keane6a24e802019-09-13 17:39:31 +00006088 D->addAttr(::new (S.Context) AMDGPUNumSGPRAttr(S.Context, AL, NumSGPR));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006089}
6090
Erich Keanee891aa92018-07-13 15:07:47 +00006091static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006092 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006093 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
6094 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006095 return;
6096
Erich Keane6a24e802019-09-13 17:39:31 +00006097 D->addAttr(::new (S.Context) AMDGPUNumVGPRAttr(S.Context, AL, NumVGPR));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006098}
6099
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006100static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006101 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006102 // If we try to apply it to a function pointer, don't warn, but don't
6103 // do anything, either. It doesn't matter anyway, because there's nothing
6104 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006105 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006106 if (VD && VD->getType()->isFunctionPointerType())
6107 return;
6108 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006109 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006110 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
6111 TD->getUnderlyingType()->isFunctionType()))
6112 return;
6113 // Attribute can only be applied to function types.
6114 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006115 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006116 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006117 return;
6118 }
6119
Erich Keane6a24e802019-09-13 17:39:31 +00006120 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(S.Context, AL));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006121}
6122
Erich Keanee891aa92018-07-13 15:07:47 +00006123static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00006124 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006125 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
6126 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00006127 return;
6128
6129 // TODO: Investigate what happens with the next major version of MSVC.
Reid Kleckner1a94d872018-12-17 23:16:43 +00006130 if (Version != LangOptions::MSVC2015 / 100) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006131 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00006132 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00006133 return;
6134 }
6135
Reid Kleckner1a94d872018-12-17 23:16:43 +00006136 // The attribute expects a "major" version number like 19, but new versions of
6137 // MSVC have moved to updating the "minor", or less significant numbers, so we
6138 // have to multiply by 100 now.
6139 Version *= 100;
6140
Erich Keane6a24e802019-09-13 17:39:31 +00006141 D->addAttr(::new (S.Context) LayoutVersionAttr(S.Context, AL, Version));
David Majnemercd3ebfe2016-05-23 17:16:12 +00006142}
6143
Erich Keane6a24e802019-09-13 17:39:31 +00006144DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D,
6145 const AttributeCommonInfo &CI) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006146 if (D->hasAttr<DLLExportAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006147 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00006148 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006149 }
6150
6151 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00006152 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006153
Erich Keane6a24e802019-09-13 17:39:31 +00006154 return ::new (Context) DLLImportAttr(Context, CI);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006155}
6156
Erich Keane6a24e802019-09-13 17:39:31 +00006157DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D,
6158 const AttributeCommonInfo &CI) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006159 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00006160 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006161 D->dropAttr<DLLImportAttr>();
6162 }
6163
6164 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00006165 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006166
Erich Keane6a24e802019-09-13 17:39:31 +00006167 return ::new (Context) DLLExportAttr(Context, CI);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006168}
6169
Erich Keanee891aa92018-07-13 15:07:47 +00006170static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00006171 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
6172 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006173 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00006174 return;
6175 }
6176
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006177 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00006178 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00006179 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
6180 // MinGW doesn't allow dllimport on inline functions.
6181 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00006182 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00006183 return;
6184 }
6185 }
6186
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006187 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00006188 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
6189 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006190 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00006191 return;
6192 }
6193 }
6194
Erich Keanee891aa92018-07-13 15:07:47 +00006195 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Erich Keane6a24e802019-09-13 17:39:31 +00006196 ? (Attr *)S.mergeDLLExportAttr(D, A)
6197 : (Attr *)S.mergeDLLImportAttr(D, A);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006198 if (NewAttr)
6199 D->addAttr(NewAttr);
6200}
6201
David Majnemer2c4e00a2014-01-29 22:07:36 +00006202MSInheritanceAttr *
Erich Keane6a24e802019-09-13 17:39:31 +00006203Sema::mergeMSInheritanceAttr(Decl *D, const AttributeCommonInfo &CI,
6204 bool BestCase,
Reid Klecknera9cc64e2019-11-15 18:49:32 -08006205 MSInheritanceModel Model) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00006206 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
Reid Klecknera9cc64e2019-11-15 18:49:32 -08006207 if (IA->getInheritanceModel() == Model)
Craig Topperc3ec1492014-05-26 06:22:03 +00006208 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006209 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
6210 << 1 /*previous declaration*/;
Erich Keane6a24e802019-09-13 17:39:31 +00006211 Diag(CI.getLoc(), diag::note_previous_ms_inheritance);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006212 D->dropAttr<MSInheritanceAttr>();
6213 }
6214
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006215 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006216 if (RD->hasDefinition()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006217 if (checkMSInheritanceAttrOnDefinition(RD, CI.getRange(), BestCase,
Reid Klecknera9cc64e2019-11-15 18:49:32 -08006218 Model)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006219 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006220 }
6221 } else {
6222 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
Erich Keane6a24e802019-09-13 17:39:31 +00006223 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
David Majnemer2c4e00a2014-01-29 22:07:36 +00006224 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006225 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006226 }
6227 if (RD->getDescribedClassTemplate()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006228 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
David Majnemer2c4e00a2014-01-29 22:07:36 +00006229 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006230 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006231 }
6232 }
6233
Erich Keane6a24e802019-09-13 17:39:31 +00006234 return ::new (Context) MSInheritanceAttr(Context, CI, BestCase);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006235}
6236
Erich Keanee891aa92018-07-13 15:07:47 +00006237static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006238 // The capability attributes take a single string parameter for the name of
6239 // the capability they represent. The lockable attribute does not take any
6240 // parameters. However, semantically, both attributes represent the same
6241 // concept, and so they use the same semantic attribute. Eventually, the
6242 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00006243 //
Alp Toker958027b2014-07-14 19:42:55 +00006244 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00006245 // literal will be considered a "mutex."
6246 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006247 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00006248 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006249 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006250 return;
6251
Erich Keane6a24e802019-09-13 17:39:31 +00006252 D->addAttr(::new (S.Context) CapabilityAttr(S.Context, AL, N));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006253}
6254
Erich Keanee891aa92018-07-13 15:07:47 +00006255static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00006256 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006257 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00006258 return;
6259
Erich Keane6a24e802019-09-13 17:39:31 +00006260 D->addAttr(::new (S.Context)
6261 AssertCapabilityAttr(S.Context, AL, Args.data(), Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006262}
6263
6264static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006265 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006266 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006267 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006268 return;
6269
Erich Keane6a24e802019-09-13 17:39:31 +00006270 D->addAttr(::new (S.Context) AcquireCapabilityAttr(S.Context, AL, Args.data(),
6271 Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006272}
6273
6274static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006275 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006276 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006277 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006278 return;
6279
Erich Keane6a24e802019-09-13 17:39:31 +00006280 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(
6281 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006282}
6283
6284static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006285 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006286 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00006287 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006288 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006289
Erich Keane6a24e802019-09-13 17:39:31 +00006290 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(S.Context, AL, Args.data(),
6291 Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006292}
6293
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006294static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006295 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006296 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006297 return;
6298
6299 // check that all arguments are lockable objects
6300 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006301 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006302 if (Args.empty())
6303 return;
6304
6305 RequiresCapabilityAttr *RCA = ::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00006306 RequiresCapabilityAttr(S.Context, AL, Args.data(), Args.size());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006307
6308 D->addAttr(RCA);
6309}
6310
Erich Keanee891aa92018-07-13 15:07:47 +00006311static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006312 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00006313 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006314 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00006315 // Do not want to attach the attribute to the namespace because that will
6316 // cause confusing diagnostic reports for uses of declarations within the
6317 // namespace.
6318 return;
6319 }
6320 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00006321
Manman Renc7890fe2016-03-16 18:50:49 +00006322 // Handle the cases where the attribute has a text message.
6323 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006324 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
6325 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00006326 return;
6327
6328 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006329 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
6330 checkAttributeAtMostNumArgs(S, AL, 1);
6331 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
Sander de Smalen44a22532018-11-26 16:38:37 +00006332 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
6333 return;
6334
6335 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
6336 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
6337
Erich Keane6a24e802019-09-13 17:39:31 +00006338 D->addAttr(::new (S.Context) DeprecatedAttr(S.Context, AL, Str, Replacement));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006339}
6340
6341static bool isGlobalVar(const Decl *D) {
6342 if (const auto *S = dyn_cast<VarDecl>(D))
6343 return S->hasGlobalStorage();
6344 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00006345}
6346
Erich Keanee891aa92018-07-13 15:07:47 +00006347static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006348 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00006349 return;
6350
Benjamin Kramer1b582012016-02-13 18:11:49 +00006351 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00006352
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006353 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00006354 StringRef SanitizerName;
6355 SourceLocation LiteralLoc;
6356
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006357 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00006358 return;
6359
Pierre Gousseauae5303d2019-03-01 10:05:15 +00006360 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) ==
6361 SanitizerMask())
Peter Collingbourne915df992015-05-15 18:33:32 +00006362 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006363 else if (isGlobalVar(D) && SanitizerName != "address")
6364 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006365 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00006366 Sanitizers.push_back(SanitizerName);
6367 }
6368
Erich Keane6a24e802019-09-13 17:39:31 +00006369 D->addAttr(::new (S.Context) NoSanitizeAttr(S.Context, AL, Sanitizers.data(),
6370 Sanitizers.size()));
Peter Collingbourne915df992015-05-15 18:33:32 +00006371}
6372
6373static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006374 const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00006375 StringRef AttrName = AL.getAttrName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00006376 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006377 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
6378 .Case("no_address_safety_analysis", "address")
6379 .Case("no_sanitize_address", "address")
6380 .Case("no_sanitize_thread", "thread")
6381 .Case("no_sanitize_memory", "memory");
6382 if (isGlobalVar(D) && SanitizerName != "address")
6383 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006384 << AL << ExpectedFunction;
Aaron Ballman31ca49b2019-05-21 17:24:49 +00006385
6386 // FIXME: Rather than create a NoSanitizeSpecificAttr, this creates a
6387 // NoSanitizeAttr object; but we need to calculate the correct spelling list
6388 // index rather than incorrectly assume the index for NoSanitizeSpecificAttr
6389 // has the same spellings as the index for NoSanitizeAttr. We don't have a
6390 // general way to "translate" between the two, so this hack attempts to work
6391 // around the issue with hard-coded indicies. This is critical for calling
6392 // getSpelling() or prettyPrint() on the resulting semantic attribute object
6393 // without failing assertions.
6394 unsigned TranslatedSpellingIndex = 0;
6395 if (AL.isC2xAttribute() || AL.isCXX11Attribute())
6396 TranslatedSpellingIndex = 1;
6397
Erich Keane6a24e802019-09-13 17:39:31 +00006398 AttributeCommonInfo Info = AL;
6399 Info.setAttributeSpellingListIndex(TranslatedSpellingIndex);
6400 D->addAttr(::new (S.Context)
6401 NoSanitizeAttr(S.Context, Info, &SanitizerName, 1));
Peter Collingbourne915df992015-05-15 18:33:32 +00006402}
6403
Erich Keanee891aa92018-07-13 15:07:47 +00006404static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006405 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006406 D->addAttr(Internal);
6407}
6408
Erich Keanee891aa92018-07-13 15:07:47 +00006409static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006410 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006411 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00006412 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006413 else
Erich Keane44bacdf2018-08-09 13:21:32 +00006414 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
6415 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006416}
6417
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006418/// Handles semantic checking for features that are common to all attributes,
6419/// such as checking whether a parameter was properly specified, or the correct
6420/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006421static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006422 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006423 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00006424 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006425 //
6426 // We also bail on unknown and ignored attributes because those are handled
6427 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00006428 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006429 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006430 // Check whether the attribute requires specific language extensions to be
6431 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006432 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00006433 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00006434 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006435 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00006436 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006437 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00006438 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006439
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006440 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006441 // If there are no optional arguments, then checking for the argument count
6442 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006443 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006444 return true;
6445 } else {
6446 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006447 if (AL.getMinArgs() &&
6448 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006449 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006450 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
6451 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006452 return true;
6453 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00006454
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006455 if (S.CheckAttrTarget(AL))
6456 return true;
6457
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006458 return false;
6459}
6460
Erich Keanee891aa92018-07-13 15:07:47 +00006461static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006462 if (D->isInvalidDecl())
6463 return;
6464
6465 // Check if there is only one access qualifier.
6466 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006467 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
6468 AL.getSemanticSpelling()) {
6469 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
Erich Keane6a24e802019-09-13 17:39:31 +00006470 << AL.getAttrName()->getName() << AL.getRange();
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006471 } else {
6472 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
6473 << D->getSourceRange();
6474 D->setInvalidDecl(true);
6475 return;
6476 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00006477 }
6478
6479 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
6480 // image object can be read and written.
6481 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
6482 // object. Using the read_write (or __read_write) qualifier with the pipe
6483 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006484 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006485 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Erich Keane6a24e802019-09-13 17:39:31 +00006486 if (AL.getAttrName()->getName().find("read_write") != StringRef::npos) {
Anastasia Stulova2c4730d2019-02-15 12:07:57 +00006487 if ((!S.getLangOpts().OpenCLCPlusPlus &&
6488 S.getLangOpts().OpenCLVersion < 200) ||
6489 DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006490 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00006491 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00006492 D->setInvalidDecl(true);
6493 return;
6494 }
6495 }
6496 }
6497
Erich Keane6a24e802019-09-13 17:39:31 +00006498 D->addAttr(::new (S.Context) OpenCLAccessAttr(S.Context, AL));
Xiuli Pan11e13f62016-02-26 03:13:03 +00006499}
6500
Mariya Podchishchaevac094e7d2019-11-06 17:35:50 +03006501static void handleSYCLKernelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6502 // The 'sycl_kernel' attribute applies only to function templates.
6503 const auto *FD = cast<FunctionDecl>(D);
6504 const FunctionTemplateDecl *FT = FD->getDescribedFunctionTemplate();
6505 assert(FT && "Function template is expected");
6506
6507 // Function template must have at least two template parameters.
6508 const TemplateParameterList *TL = FT->getTemplateParameters();
6509 if (TL->size() < 2) {
6510 S.Diag(FT->getLocation(), diag::warn_sycl_kernel_num_of_template_params);
6511 return;
6512 }
6513
6514 // Template parameters must be typenames.
6515 for (unsigned I = 0; I < 2; ++I) {
6516 const NamedDecl *TParam = TL->getParam(I);
6517 if (isa<NonTypeTemplateParmDecl>(TParam)) {
6518 S.Diag(FT->getLocation(),
6519 diag::warn_sycl_kernel_invalid_template_param_type);
6520 return;
6521 }
6522 }
6523
6524 // Function must have at least one argument.
6525 if (getFunctionOrMethodNumParams(D) != 1) {
6526 S.Diag(FT->getLocation(), diag::warn_sycl_kernel_num_of_function_params);
6527 return;
6528 }
6529
6530 // Function must return void.
6531 QualType RetTy = getFunctionOrMethodResultType(D);
6532 if (!RetTy->isVoidType()) {
6533 S.Diag(FT->getLocation(), diag::warn_sycl_kernel_return_type);
6534 return;
6535 }
6536
6537 handleSimpleAttribute<SYCLKernelAttr>(S, D, AL);
6538}
6539
Erik Pilkington5a559e62018-08-21 17:24:06 +00006540static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006541 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00006542 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
6543 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
6544 return;
6545 }
6546
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006547 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00006548 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006549 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00006550 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00006551}
6552
JF Bastien14daa202018-12-18 05:12:21 +00006553static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6554 assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic &&
6555 "uninitialized is only valid on automatic duration variables");
Erich Keane6a24e802019-09-13 17:39:31 +00006556 D->addAttr(::new (S.Context) UninitializedAttr(S.Context, AL));
JF Bastien14daa202018-12-18 05:12:21 +00006557}
6558
Erik Pilkington1e368822019-01-04 18:33:06 +00006559static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD,
6560 bool DiagnoseFailure) {
6561 QualType Ty = VD->getType();
6562 if (!Ty->isObjCRetainableType()) {
6563 if (DiagnoseFailure) {
6564 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6565 << 0;
6566 }
6567 return false;
6568 }
6569
6570 Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime();
6571
6572 // Sema::inferObjCARCLifetime must run after processing decl attributes
6573 // (because __block lowers to an attribute), so if the lifetime hasn't been
6574 // explicitly specified, infer it locally now.
6575 if (LifetimeQual == Qualifiers::OCL_None)
6576 LifetimeQual = Ty->getObjCARCImplicitLifetime();
6577
6578 // The attributes only really makes sense for __strong variables; ignore any
6579 // attempts to annotate a parameter with any other lifetime qualifier.
6580 if (LifetimeQual != Qualifiers::OCL_Strong) {
6581 if (DiagnoseFailure) {
6582 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6583 << 1;
6584 }
6585 return false;
6586 }
6587
6588 // Tampering with the type of a VarDecl here is a bit of a hack, but we need
6589 // to ensure that the variable is 'const' so that we can error on
6590 // modification, which can otherwise over-release.
6591 VD->setType(Ty.withConst());
6592 VD->setARCPseudoStrong(true);
6593 return true;
6594}
6595
6596static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D,
6597 const ParsedAttr &AL) {
6598 if (auto *VD = dyn_cast<VarDecl>(D)) {
6599 assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically");
6600 if (!VD->hasLocalStorage()) {
6601 S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6602 << 0;
6603 return;
6604 }
6605
6606 if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true))
6607 return;
6608
6609 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6610 return;
6611 }
6612
6613 // If D is a function-like declaration (method, block, or function), then we
6614 // make every parameter psuedo-strong.
Erik Pilkington2e4f5e62020-02-28 15:24:23 -08006615 unsigned NumParams =
6616 hasFunctionProto(D) ? getFunctionOrMethodNumParams(D) : 0;
6617 for (unsigned I = 0; I != NumParams; ++I) {
Erik Pilkington1e368822019-01-04 18:33:06 +00006618 auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I));
6619 QualType Ty = PVD->getType();
6620
6621 // If a user wrote a parameter with __strong explicitly, then assume they
6622 // want "real" strong semantics for that parameter. This works because if
6623 // the parameter was written with __strong, then the strong qualifier will
6624 // be non-local.
6625 if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() ==
6626 Qualifiers::OCL_Strong)
6627 continue;
6628
6629 tryMakeVariablePseudoStrong(S, PVD, /*DiagnoseFailure=*/false);
6630 }
6631 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6632}
6633
Artem Dergachevc333d772019-02-21 00:01:02 +00006634static void handleMIGServerRoutineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6635 // Check that the return type is a `typedef int kern_return_t` or a typedef
6636 // around it, because otherwise MIG convention checks make no sense.
6637 // BlockDecl doesn't store a return type, so it's annoying to check,
6638 // so let's skip it for now.
6639 if (!isa<BlockDecl>(D)) {
6640 QualType T = getFunctionOrMethodResultType(D);
6641 bool IsKernReturnT = false;
6642 while (const auto *TT = T->getAs<TypedefType>()) {
6643 IsKernReturnT = (TT->getDecl()->getName() == "kern_return_t");
6644 T = TT->desugar();
6645 }
6646 if (!IsKernReturnT || T.getCanonicalType() != S.getASTContext().IntTy) {
6647 S.Diag(D->getBeginLoc(),
6648 diag::warn_mig_server_routine_does_not_return_kern_return_t);
6649 return;
6650 }
6651 }
6652
6653 handleSimpleAttribute<MIGServerRoutineAttr>(S, D, AL);
6654}
6655
Reid Kleckner1181c9f2019-03-25 23:20:18 +00006656static void handleMSAllocatorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6657 // Warn if the return type is not a pointer or reference type.
6658 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6659 QualType RetTy = FD->getReturnType();
6660 if (!RetTy->isPointerType() && !RetTy->isReferenceType()) {
6661 S.Diag(AL.getLoc(), diag::warn_declspec_allocator_nonpointer)
6662 << AL.getRange() << RetTy;
6663 return;
6664 }
6665 }
6666
6667 handleSimpleAttribute<MSAllocatorAttr>(S, D, AL);
6668}
6669
Gabor Horvathfe17b302019-12-04 16:12:50 -08006670static void handeAcquireHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6671 if (AL.isUsedAsTypeAttr())
6672 return;
6673 // Warn if the parameter is definitely not an output parameter.
6674 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
6675 if (PVD->getType()->isIntegerType()) {
6676 S.Diag(AL.getLoc(), diag::err_attribute_output_parameter)
6677 << AL.getRange();
6678 return;
6679 }
6680 }
6681 StringRef Argument;
6682 if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument))
6683 return;
6684 D->addAttr(AcquireHandleAttr::Create(S.Context, Argument, AL));
6685}
6686
6687template<typename Attr>
6688static void handleHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6689 StringRef Argument;
6690 if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument))
6691 return;
6692 D->addAttr(Attr::Create(S.Context, Argument, AL));
6693}
6694
Andrew Paverdbdd88b72020-01-10 11:08:18 +00006695static void handleCFGuardAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6696 // The guard attribute takes a single identifier argument.
6697
6698 if (!AL.isArgIdent(0)) {
6699 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
6700 << AL << AANT_ArgumentIdentifier;
6701 return;
6702 }
6703
6704 CFGuardAttr::GuardArg Arg;
6705 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
6706 if (!CFGuardAttr::ConvertStrToGuardArg(II->getName(), Arg)) {
6707 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
6708 return;
6709 }
6710
6711 D->addAttr(::new (S.Context) CFGuardAttr(S.Context, AL, Arg));
6712}
6713
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00006714//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006715// Top Level Sema Entry Points
6716//===----------------------------------------------------------------------===//
6717
Richard Smithf8a75c32013-08-29 00:47:48 +00006718/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
6719/// the attribute applies to decls. If the attribute is a type attribute, just
6720/// silently ignore it if a GNU attribute.
6721static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006722 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00006723 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00006724 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00006725 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00006726
Richard Smithf8a75c32013-08-29 00:47:48 +00006727 // Ignore C++11 attributes on declarator chunks: they appertain to the type
6728 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006729 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00006730 return;
6731
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006732 // Unknown attributes are automatically warned on. Target-specific attributes
6733 // which do not apply to the current target architecture are treated as
6734 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006735 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006736 !AL.existsInTarget(S.Context.getTargetInfo())) {
Simon Pilgrimfc0ff612018-12-17 12:17:37 +00006737 S.Diag(AL.getLoc(),
6738 AL.isDeclspecAttribute()
6739 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
6740 : (unsigned)diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006741 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006742 return;
6743 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006744
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006745 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006746 return;
6747
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006748 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006749 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006750 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00006751 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006752 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00006753 break;
6754 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006755 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00006756 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006757 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006758 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006759 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006760 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006761 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006762 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006763 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006764 case ParsedAttr::AT_DLLExport:
6765 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006766 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006767 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006768 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006769 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006770 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006771 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006772 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006773 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006774 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006775 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006776 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006777 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006778 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006779 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006780 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006781 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006782 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006783 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006784 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006785 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006786 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006787 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006788 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006789 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006790 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006791 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006792 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006793 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006794 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006795 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006796 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006797 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006798 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006799 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006800 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006801 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006802 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006803 break;
Yonghong Song4e2ce222019-11-01 22:16:59 -07006804 case ParsedAttr::AT_BPFPreserveAccessIndex:
6805 handleBPFPreserveAccessIndexAttr(S, D, AL);
6806 break;
Sam Clegg881d8772019-11-05 10:15:56 -08006807 case ParsedAttr::AT_WebAssemblyExportName:
6808 handleWebAssemblyExportNameAttr(S, D, AL);
6809 break;
Dan Gohmanb4323692019-01-24 21:08:30 +00006810 case ParsedAttr::AT_WebAssemblyImportModule:
6811 handleWebAssemblyImportModuleAttr(S, D, AL);
6812 break;
Dan Gohmancae84592019-02-01 22:25:23 +00006813 case ParsedAttr::AT_WebAssemblyImportName:
6814 handleWebAssemblyImportNameAttr(S, D, AL);
6815 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006816 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006817 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006818 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006819 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006820 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006821 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006822 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006823 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006824 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006825 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006826 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006827 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006828 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006829 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006830 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006831 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006832 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006833 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006834 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006835 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006836 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006837 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006838 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006839 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006840 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006841 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006842 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006843 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006844 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006845 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006846 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006847 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006848 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006849 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006850 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006851 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006852 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006853 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006854 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006855 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006856 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006857 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006858 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006859 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006860 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006861 case ParsedAttr::AT_CPUDispatch:
6862 case ParsedAttr::AT_CPUSpecific:
6863 handleCPUSpecificAttr(S, D, AL);
6864 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006865 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006866 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006867 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006868 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006869 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006870 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006871 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006872 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006873 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006874 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006875 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006876 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006877 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006878 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006879 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006880 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006881 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006882 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006883 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006884 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006885 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006886 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006887 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006888 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006889 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006890 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006891 break;
Guillaume Chatelet98f31512019-09-25 11:31:28 +02006892 case ParsedAttr::AT_NoBuiltin:
6893 handleNoBuiltinAttr(S, D, AL);
6894 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006895 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006896 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006897 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006898 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006899 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006900 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006901 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006902 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006903 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006904 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006905 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006906 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006907 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006908 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006909 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006910 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006911 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006912 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006913 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006914 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006915 break;
Mariya Podchishchaevac094e7d2019-11-06 17:35:50 +03006916 case ParsedAttr::AT_SYCLKernel:
6917 handleSYCLKernelAttr(S, D, AL);
6918 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006919 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006920 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006921 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006922 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006923 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006924 break;
Johannes Doerfertac991bb2019-01-19 05:36:54 +00006925 case ParsedAttr::AT_Callback:
6926 handleCallbackAttr(S, D, AL);
6927 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006928 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006929 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006930 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006931 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006932 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006933 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006934 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006935 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006936 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006937 break;
Yaxun Liuc3dfe902019-06-26 03:47:37 +00006938 case ParsedAttr::AT_HIPPinnedShadow:
6939 handleSimpleAttributeWithExclusions<HIPPinnedShadowAttr, CUDADeviceAttr,
6940 CUDAConstantAttr>(S, D, AL);
6941 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006942 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006943 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006944 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006945 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006946 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006947 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006948 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006949 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006950 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006951 case ParsedAttr::AT_LifetimeBound:
6952 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6953 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006954 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006955 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006956 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006957 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006958 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006959 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006960 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006961 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006962 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006963 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006964 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006965 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006966 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006967 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006968 break;
Richard Smith78b239e2019-06-20 20:44:45 +00006969 case ParsedAttr::AT_NoUniqueAddress:
6970 handleSimpleAttribute<NoUniqueAddressAttr>(S, D, AL);
6971 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006972 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006973 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6974 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006975 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006976 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006977 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006978 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006979 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006980 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006981 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006982 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006983 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006984 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006985 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006986 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006987 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006988 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006989 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006990 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006991 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006992 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006993 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006994 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006995 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006996 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006997 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006998 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006999 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00007000 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007001 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007002 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007003 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007004 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007005 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007006 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007007 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007008 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00007009 handleNoCfCheckAttr(S, D, AL);
7010 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007011 case ParsedAttr::AT_NoThrow:
Erich Keaned02f4a12019-05-30 17:31:54 +00007012 if (!AL.isUsedAsTypeAttr())
7013 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007014 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007015 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007016 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007017 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007018 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007019 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007020 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007021 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007022 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007023 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007024 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007025 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007026 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007027 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007028 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007029 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007030 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007031 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007032 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007033 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007034 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007035 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007036 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007037 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007038 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007039 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007040 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007041 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007042 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007043 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007044 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007045 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007046 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00007047 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007048 case ParsedAttr::AT_ObjCRuntimeVisible:
7049 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
7050 break;
7051 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007052 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00007053 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007054 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00007055 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
7056 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007057 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007058 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00007059 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
7060 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007061 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007062 case ParsedAttr::AT_CFConsumed:
7063 case ParsedAttr::AT_NSConsumed:
George Karpenkov1657f362018-11-30 02:18:37 +00007064 case ParsedAttr::AT_OSConsumed:
Erich Keane6a24e802019-09-13 17:39:31 +00007065 S.AddXConsumedAttr(D, AL, parsedAttrToRetainOwnershipKind(AL),
7066 /*IsTemplateInstantiation=*/false);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007067 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007068 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007069 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007070 break;
George Karpenkovda2c77f2018-12-06 22:06:59 +00007071 case ParsedAttr::AT_OSConsumesThis:
7072 handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL);
7073 break;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00007074 case ParsedAttr::AT_OSReturnsRetainedOnZero:
7075 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnZeroAttr>(
7076 S, D, AL, isValidOSObjectOutParameter(D),
7077 diag::warn_ns_attribute_wrong_parameter_type,
7078 /*Extra Args=*/AL, /*pointer-to-OSObject-pointer*/ 3, AL.getRange());
7079 break;
7080 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
7081 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnNonZeroAttr>(
7082 S, D, AL, isValidOSObjectOutParameter(D),
7083 diag::warn_ns_attribute_wrong_parameter_type,
7084 /*Extra Args=*/AL, /*pointer-to-OSObject-poointer*/ 3, AL.getRange());
7085 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007086 case ParsedAttr::AT_NSReturnsAutoreleased:
7087 case ParsedAttr::AT_NSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00007088 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00007089 case ParsedAttr::AT_CFReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00007090 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00007091 case ParsedAttr::AT_OSReturnsNotRetained:
7092 case ParsedAttr::AT_OSReturnsRetained:
7093 handleXReturnsXRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007094 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007095 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007096 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007097 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007098 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007099 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007100 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007101 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007102 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00007103 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007104 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007105 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007106 break;
Richard Smitha6e8b682019-09-04 20:30:37 +00007107 case ParsedAttr::AT_ConstInit:
7108 handleSimpleAttribute<ConstInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00007109 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007110 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007111 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007112 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007113 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007114 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007115 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007116 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007117 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007118 break;
Zola Bridgescbac3ad2018-11-27 19:56:46 +00007119 case ParsedAttr::AT_SpeculativeLoadHardening:
Zola Bridges826ef592019-01-18 17:20:46 +00007120 handleSimpleAttributeWithExclusions<SpeculativeLoadHardeningAttr,
7121 NoSpeculativeLoadHardeningAttr>(S, D,
7122 AL);
7123 break;
7124 case ParsedAttr::AT_NoSpeculativeLoadHardening:
7125 handleSimpleAttributeWithExclusions<NoSpeculativeLoadHardeningAttr,
7126 SpeculativeLoadHardeningAttr>(S, D, AL);
Zola Bridgescbac3ad2018-11-27 19:56:46 +00007127 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00007128 case ParsedAttr::AT_CodeSeg:
7129 handleCodeSegAttr(S, D, AL);
7130 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007131 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007132 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00007133 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007134 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00007135 handleMinVectorWidthAttr(S, D, AL);
7136 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007137 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007138 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00007139 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007140 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007141 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007142 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007143 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007144 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007145 break;
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08007146 case ParsedAttr::AT_ObjCDirect:
7147 handleObjCDirectAttr(S, D, AL);
7148 break;
7149 case ParsedAttr::AT_ObjCDirectMembers:
7150 handleObjCDirectMembersAttr(S, D, AL);
7151 handleSimpleAttribute<ObjCDirectMembersAttr>(S, D, AL);
7152 break;
Joe Danielsf7393d22019-02-04 23:32:55 +00007153 case ParsedAttr::AT_ObjCNonLazyClass:
7154 handleSimpleAttribute<ObjCNonLazyClassAttr>(S, D, AL);
7155 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007156 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007157 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00007158 break;
John McCall2c91c3b2019-05-30 04:09:01 +00007159 case ParsedAttr::AT_ObjCClassStub:
7160 handleSimpleAttribute<ObjCClassStubAttr>(S, D, AL);
7161 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007162 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007163 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00007164 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007165 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007166 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007167 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007168 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007169 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007170 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007171 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007172 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007173 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007174 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00007175 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
7176 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00007177 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007178 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00007179 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
7180 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00007181 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007182 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00007183 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007184 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007185 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007186 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00007187 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007188 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007189 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00007190 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007191 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007192 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007193 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007194 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007195 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00007196 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007197 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007198 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007199 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007200 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007201 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007202 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007203 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007204 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007205 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007206 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007207 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007208 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007209 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007210 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007211 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007212 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007213 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00007214 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007215 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007216 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007217 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007218 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007219 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00007220 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007221 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007222 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007223 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007224 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007225 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007226 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007227 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007228 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007229 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007230 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007231 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007232 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007233 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007234 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007235 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007236 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007237 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007238 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007239 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007240 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007241 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007242 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007243 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00007244 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007245 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007246 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007247 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007248 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007249 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007250 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007251 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00007252 // Interacts with -fstack-protector options.
7253 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
7254 break;
Peter Collingbourne0e497d12019-08-09 22:31:59 +00007255 case ParsedAttr::AT_CFICanonicalJumpTable:
7256 handleSimpleAttribute<CFICanonicalJumpTableAttr>(S, D, AL);
7257 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007258 case ParsedAttr::AT_StdCall:
7259 case ParsedAttr::AT_CDecl:
7260 case ParsedAttr::AT_FastCall:
7261 case ParsedAttr::AT_ThisCall:
7262 case ParsedAttr::AT_Pascal:
7263 case ParsedAttr::AT_RegCall:
7264 case ParsedAttr::AT_SwiftCall:
7265 case ParsedAttr::AT_VectorCall:
7266 case ParsedAttr::AT_MSABI:
7267 case ParsedAttr::AT_SysVABI:
7268 case ParsedAttr::AT_Pcs:
7269 case ParsedAttr::AT_IntelOclBicc:
7270 case ParsedAttr::AT_PreserveMost:
7271 case ParsedAttr::AT_PreserveAll:
Sander de Smalen44a22532018-11-26 16:38:37 +00007272 case ParsedAttr::AT_AArch64VectorPcs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007273 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00007274 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007275 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007276 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00007277 break;
Matthias Gehred293cbd2019-07-25 17:50:51 +00007278 case ParsedAttr::AT_Owner:
7279 case ParsedAttr::AT_Pointer:
7280 handleLifetimeCategoryAttr(S, D, AL);
7281 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007282 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007283 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007284 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007285 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007286 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007287 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007288 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007289 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00007290 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007291 case ParsedAttr::AT_SwiftContext:
Erich Keane6a24e802019-09-13 17:39:31 +00007292 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00007293 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007294 case ParsedAttr::AT_SwiftErrorResult:
Erich Keane6a24e802019-09-13 17:39:31 +00007295 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00007296 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007297 case ParsedAttr::AT_SwiftIndirectResult:
Erich Keane6a24e802019-09-13 17:39:31 +00007298 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00007299 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007300 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007301 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00007302 break;
Louis Dionned2695792018-10-04 15:49:42 +00007303 case ParsedAttr::AT_ExcludeFromExplicitInstantiation:
7304 handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL);
7305 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007306 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007307 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00007308 break;
John McCall8d32c052012-05-22 21:28:12 +00007309
7310 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007311 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007312 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007313 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007314 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007315 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007316 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007317 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007318 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00007319 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007320 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007321 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00007322 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007323 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007324 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00007325 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007326 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007327 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00007328 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007329 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007330 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007331 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007332 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007333 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007334 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007335 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007336 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00007337 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00007338
Erich Keanee891aa92018-07-13 15:07:47 +00007339 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007340 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00007341 break;
Andrew Paverdbdd88b72020-01-10 11:08:18 +00007342 case ParsedAttr::AT_CFGuard:
7343 handleCFGuardAttr(S, D, AL);
7344 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007345
7346 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007347 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007348 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007349 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007350 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007351 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007352 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007353 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007354 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007355 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007356 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007357 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007358 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007359 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007360 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007361 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007362 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007363 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00007364 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007365 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007366 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00007367 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007368 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007369 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00007370 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007371 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007372 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007373 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007374 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007375 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007376 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007377 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007378 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007379 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007380 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007381 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007382 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007383 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007384 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007385 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007386 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007387 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007388 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007389 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007390 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007391 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007392 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007393 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007394 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007395
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007396 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007397 case ParsedAttr::AT_Capability:
7398 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007399 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007400 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007401 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007402 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007403 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00007404
Erich Keanee891aa92018-07-13 15:07:47 +00007405 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007406 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007407 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007408 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007409 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007410 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007411 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007412 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007413 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007414 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007415 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007416 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007417
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00007418 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007419 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007420 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00007421 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007422 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007423 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007424 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007425 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007426 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007427 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007428 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007429 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007430 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007431 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007432 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00007433 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007434 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007435 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00007436 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007437 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007438 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007439 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007440 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007441 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007442 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007443
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007444 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007445 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007446 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007447 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007448 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007449 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007450 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007451 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00007452 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00007453 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007454 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007455 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00007456 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007457 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007458 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007459 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007460 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007461 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007462 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00007463 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00007464
Fangrui Songa44c4342020-01-04 15:39:19 -08007465 case ParsedAttr::AT_PatchableFunctionEntry:
7466 handlePatchableFunctionEntryAttr(S, D, AL);
7467 break;
7468
Martin Bohme4e1293b2018-08-13 14:11:03 +00007469 // Move semantics attribute.
7470 case ParsedAttr::AT_Reinitializes:
7471 handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
7472 break;
Erik Pilkington5a559e62018-08-21 17:24:06 +00007473
7474 case ParsedAttr::AT_AlwaysDestroy:
7475 case ParsedAttr::AT_NoDestroy:
7476 handleDestroyAttr(S, D, AL);
7477 break;
JF Bastien14daa202018-12-18 05:12:21 +00007478
7479 case ParsedAttr::AT_Uninitialized:
7480 handleUninitializedAttr(S, D, AL);
7481 break;
Erik Pilkington1e368822019-01-04 18:33:06 +00007482
Jon Chesterfieldc45eaea2020-03-17 21:22:04 +00007483 case ParsedAttr::AT_LoaderUninitialized:
7484 handleSimpleAttribute<LoaderUninitializedAttr>(S, D, AL);
7485 break;
7486
Erik Pilkington1e368822019-01-04 18:33:06 +00007487 case ParsedAttr::AT_ObjCExternallyRetained:
7488 handleObjCExternallyRetainedAttr(S, D, AL);
7489 break;
Erik Pilkingtone3cd7352019-02-11 23:21:39 +00007490
Artem Dergachevc333d772019-02-21 00:01:02 +00007491 case ParsedAttr::AT_MIGServerRoutine:
7492 handleMIGServerRoutineAttr(S, D, AL);
7493 break;
Reid Kleckner1181c9f2019-03-25 23:20:18 +00007494
7495 case ParsedAttr::AT_MSAllocator:
7496 handleMSAllocatorAttr(S, D, AL);
7497 break;
Simon Tatham7c11da02019-09-02 15:35:09 +01007498
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00007499 case ParsedAttr::AT_ArmBuiltinAlias:
7500 handleArmBuiltinAliasAttr(S, D, AL);
Simon Tatham7c11da02019-09-02 15:35:09 +01007501 break;
Gabor Horvathfe17b302019-12-04 16:12:50 -08007502
7503 case ParsedAttr::AT_AcquireHandle:
7504 handeAcquireHandleAttr(S, D, AL);
7505 break;
7506
7507 case ParsedAttr::AT_ReleaseHandle:
7508 handleHandleAttr<ReleaseHandleAttr>(S, D, AL);
7509 break;
7510
7511 case ParsedAttr::AT_UseHandle:
7512 handleHandleAttr<UseHandleAttr>(S, D, AL);
7513 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007514 }
7515}
7516
7517/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
7518/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00007519void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00007520 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00007521 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00007522 if (AttrList.empty())
7523 return;
7524
Erich Keanee891aa92018-07-13 15:07:47 +00007525 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00007526 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00007527
Joey Gouly2cd9db12013-12-13 16:15:28 +00007528 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00007529 // GCC accepts
7530 // static int a9 __attribute__((weakref));
7531 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00007532 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00007533 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
7534 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00007535 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00007536 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007537 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007538
Aaron Ballmanbe243a72014-12-04 22:45:31 +00007539 // FIXME: We should be able to handle this in TableGen as well. It would be
7540 // good to have a way to specify "these attributes must appear as a group",
7541 // for these. Additionally, it would be good to have a way to specify "these
7542 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00007543 if (!D->hasAttr<OpenCLKernelAttr>()) {
7544 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007545 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00007546 // FIXME: This emits a different error message than
7547 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00007548 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007549 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007550 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007551 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007552 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007553 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007554 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007555 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007556 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00007557 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
7558 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00007559 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
7560 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
7561 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7562 << A << ExpectedKernelFunction;
7563 D->setInvalidDecl();
7564 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
7565 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7566 << A << ExpectedKernelFunction;
7567 D->setInvalidDecl();
7568 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
7569 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7570 << A << ExpectedKernelFunction;
7571 D->setInvalidDecl();
7572 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
7573 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7574 << A << ExpectedKernelFunction;
7575 D->setInvalidDecl();
7576 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007577 }
7578 }
Erik Pilkington81d3f452019-02-13 20:32:37 +00007579
7580 // Do this check after processing D's attributes because the attribute
7581 // objc_method_family can change whether the given method is in the init
7582 // family, and it can be applied after objc_designated_initializer. This is a
7583 // bit of a hack, but we need it to be compatible with versions of clang that
7584 // processed the attribute list in the wrong order.
7585 if (D->hasAttr<ObjCDesignatedInitializerAttr>() &&
7586 cast<ObjCMethodDecl>(D)->getMethodFamily() != OMF_init) {
7587 Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
7588 D->dropAttr<ObjCDesignatedInitializerAttr>();
7589 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007590}
7591
Yonghong Song4e2ce222019-11-01 22:16:59 -07007592// Helper for delayed processing TransparentUnion or BPFPreserveAccessIndexAttr
7593// attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007594void Sema::ProcessDeclAttributeDelayed(Decl *D,
7595 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007596 for (const ParsedAttr &AL : AttrList)
7597 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00007598 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00007599 break;
7600 }
Yonghong Song4e2ce222019-11-01 22:16:59 -07007601
7602 // For BPFPreserveAccessIndexAttr, we want to populate the attributes
7603 // to fields and inner records as well.
7604 if (D && D->hasAttr<BPFPreserveAccessIndexAttr>())
7605 handleBPFPreserveAIRecord(*this, cast<RecordDecl>(D));
Erich Keane2fe684b2017-02-28 20:44:39 +00007606}
7607
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007608// Annotation attributes are the only attributes allowed after an access
7609// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00007610bool Sema::ProcessAccessDeclAttributeList(
7611 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007612 for (const ParsedAttr &AL : AttrList) {
7613 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00007614 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007615 } else {
Erich Keanec480f302018-07-12 21:09:05 +00007616 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007617 return true;
7618 }
7619 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007620 return false;
7621}
7622
John McCall42856de2011-10-01 05:17:03 +00007623/// checkUnusedDeclAttributes - Check a list of attributes to see if it
7624/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00007625static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00007626 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00007627 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007628 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
7629 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00007630 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00007631 continue;
John McCall42856de2011-10-01 05:17:03 +00007632
Erich Keanee891aa92018-07-13 15:07:47 +00007633 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00007634 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00007635 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007636 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00007637 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
7638 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007639 }
7640 }
7641}
7642
7643/// checkUnusedDeclAttributes - Given a declarator which is not being
7644/// used to build a declaration, complain about any decl attributes
7645/// which might be lying around on it.
7646void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00007647 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00007648 ::checkUnusedDeclAttributes(*this, D.getAttributes());
7649 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
7650 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
7651}
7652
Ryan Flynn7d470f32009-07-30 03:15:39 +00007653/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00007654/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00007655NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
7656 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00007657 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00007658 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007659 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00007660 FunctionDecl *NewFD;
7661 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00007662 // FIXME: Mangling?
7663 // FIXME: Is the qualifier info correct?
7664 // FIXME: Is the DeclContext correct?
Gauthier Harnisch796ed032019-06-14 08:56:20 +00007665 NewFD = FunctionDecl::Create(
7666 FD->getASTContext(), FD->getDeclContext(), Loc, Loc,
7667 DeclarationName(II), FD->getType(), FD->getTypeSourceInfo(), SC_None,
Saar Razb65b1f32020-01-09 15:07:51 +02007668 false /*isInlineSpecified*/, FD->hasPrototype(), CSK_unspecified,
7669 FD->getTrailingRequiresClause());
Eli Friedmance3e2c82011-09-07 04:05:06 +00007670 NewD = NewFD;
7671
7672 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00007673 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00007674
7675 // Fake up parameter variables; they are declared as if this were
7676 // a typedef.
7677 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007678 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00007679 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00007680 for (const auto &AI : FT->param_types()) {
7681 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007682 Param->setScopeInfo(0, Params.size());
7683 Params.push_back(Param);
7684 }
David Blaikie9c70e042011-09-21 18:16:56 +00007685 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00007686 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007687 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00007688 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00007689 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00007690 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00007691 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007692 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00007693 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00007694 }
7695 return NewD;
7696}
7697
James Dennett634962f2012-06-14 21:40:34 +00007698/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00007699/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00007700void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00007701 if (W.getUsed()) return; // only do this once
7702 W.setUsed(true);
7703 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
7704 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00007705 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Erich Keane6a24e802019-09-13 17:39:31 +00007706 NewD->addAttr(
7707 AliasAttr::CreateImplicit(Context, NDId->getName(), W.getLocation()));
7708 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation(),
7709 AttributeCommonInfo::AS_Pragma));
Chris Lattnere6eab982009-09-08 18:10:11 +00007710 WeakTopLevelDecl.push_back(NewD);
7711 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
7712 // to insert Decl at TU scope, sorry.
7713 DeclContext *SavedContext = CurContext;
7714 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00007715 NewD->setDeclContext(CurContext);
7716 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00007717 PushOnScopeChains(NewD, S);
7718 CurContext = SavedContext;
7719 } else { // just add weak to existing
Erich Keane6a24e802019-09-13 17:39:31 +00007720 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation(),
7721 AttributeCommonInfo::AS_Pragma));
Ryan Flynn7d470f32009-07-30 03:15:39 +00007722 }
7723}
7724
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007725void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
7726 // It's valid to "forward-declare" #pragma weak, in which case we
7727 // have to do this.
7728 LoadExternalWeakUndeclaredIdentifiers();
7729 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00007730 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007731 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007732 if (VD->isExternC())
7733 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007734 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007735 if (FD->isExternC())
7736 ND = FD;
7737 if (ND) {
7738 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00007739 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007740 if (I != WeakUndeclaredIdentifiers.end()) {
7741 WeakInfo W = I->second;
7742 DeclApplyPragmaWeak(S, ND, W);
7743 WeakUndeclaredIdentifiers[Id] = W;
7744 }
7745 }
7746 }
7747 }
7748}
7749
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007750/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
7751/// it, apply them to D. This is a bit tricky because PD can have attributes
7752/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00007753void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007754 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00007755 if (!PD.getDeclSpec().getAttributes().empty())
7756 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00007757
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007758 // Walk the declarator structure, applying decl attributes that were in a type
7759 // position to the decl itself. This handles cases like:
7760 // int *__attr__(x)** D;
7761 // when X is a decl attribute.
7762 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00007763 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
7764 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00007765
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007766 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00007767 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00007768
7769 // Apply additional attributes specified by '#pragma clang attribute'.
7770 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007771}
John McCall28a6aea2009-11-04 02:18:39 +00007772
John McCall31168b02011-06-15 23:02:42 +00007773/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00007774/// If so, it'll still be annotated with an attribute that makes it
7775/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007776static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00007777 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00007778 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00007779 // Private ivars are always okay. Unfortunately, people don't
7780 // always properly make their ivars private, even in system headers.
7781 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007782 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
7783 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00007784 return false;
7785
John McCallc6af8c62015-10-28 05:03:19 +00007786 // Silently accept unsupported uses of __weak in both user and system
7787 // declarations when it's been disabled, for ease of integration with
7788 // -fno-objc-arc files. We do have to take some care against attempts
7789 // to define such things; for now, we've only done that for ivars
7790 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007791 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00007792 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
7793 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
7794 reason = UnavailableAttr::IR_ForbiddenWeak;
7795 return true;
7796 }
John McCallb61e14e2015-10-27 04:54:50 +00007797 }
7798
John McCallc6af8c62015-10-28 05:03:19 +00007799 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007800 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00007801 // Currently, all the failures dealt with this way are due to ARC
7802 // restrictions.
7803 reason = UnavailableAttr::IR_ARCForbiddenType;
7804 return true;
John McCallb61e14e2015-10-27 04:54:50 +00007805 }
7806
7807 return false;
John McCall31168b02011-06-15 23:02:42 +00007808}
7809
7810/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007811static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
7812 Decl *D) {
7813 auto Reason = UnavailableAttr::IR_None;
7814 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
7815 assert(Reason && "didn't set reason?");
7816 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00007817 return;
7818 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00007819 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007820 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00007821 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007822 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007823 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007824 DD.getForbiddenTypeDiagnostic() ==
7825 diag::err_arc_array_param_no_ownership) {
7826 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007827 return;
7828 }
7829 }
John McCall31168b02011-06-15 23:02:42 +00007830
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007831 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
7832 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
7833 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00007834}
7835
Aaron Ballmanfb237522014-10-15 15:37:51 +00007836
John McCall2ec85372012-05-07 06:16:41 +00007837void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7838 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007839 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007840 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007841
John McCall2ec85372012-05-07 06:16:41 +00007842 // When delaying diagnostics to run in the context of a parsed
7843 // declaration, we only want to actually emit anything if parsing
7844 // succeeds.
7845 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007846
John McCall2ec85372012-05-07 06:16:41 +00007847 // We emit all the active diagnostics in this pool or any of its
7848 // parents. In general, we'll get one pool for the decl spec
7849 // and a child pool for each declarator; in a decl group like:
7850 // deprecated_typedef foo, *bar, baz();
7851 // only the declarator pops will be passed decls. This is correct;
7852 // we really do need to consider delayed diagnostics from the decl spec
7853 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007854 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007855 do {
Richard Smith5c9b3b72018-09-25 22:12:44 +00007856 bool AnyAccessFailures = false;
John McCall6347b682012-05-07 06:16:58 +00007857 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007858 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7859 // This const_cast is a bit lame. Really, Triggered should be mutable.
7860 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007861 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007862 continue;
7863
John McCallc1465822011-02-14 07:13:47 +00007864 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007865 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007866 // Don't bother giving deprecation/unavailable diagnostics if
7867 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007868 if (!decl->isInvalidDecl())
Reid Klecknerdd8e0a02020-01-24 15:16:22 -08007869 handleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00007870 break;
7871
7872 case DelayedDiagnostic::Access:
Richard Smith5c9b3b72018-09-25 22:12:44 +00007873 // Only produce one access control diagnostic for a structured binding
7874 // declaration: we don't need to tell the user that all the fields are
7875 // inaccessible one at a time.
7876 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
7877 continue;
John McCall2ec85372012-05-07 06:16:41 +00007878 HandleDelayedAccessCheck(diag, decl);
Richard Smith5c9b3b72018-09-25 22:12:44 +00007879 if (diag.Triggered)
7880 AnyAccessFailures = true;
John McCall86121512010-01-27 03:50:35 +00007881 break;
John McCall31168b02011-06-15 23:02:42 +00007882
7883 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007884 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007885 break;
John McCall86121512010-01-27 03:50:35 +00007886 }
7887 }
John McCall2ec85372012-05-07 06:16:41 +00007888 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007889}
7890
John McCall6347b682012-05-07 06:16:58 +00007891/// Given a set of delayed diagnostics, re-emit them as if they had
7892/// been delayed in the current context instead of in the given pool.
7893/// Essentially, this just moves them to the current pool.
7894void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7895 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7896 assert(curPool && "re-emitting in undelayed context not supported");
7897 curPool->steal(pool);
7898}