blob: 5aacb2fd64a24cc7ec4c0ccde1385d0fe640e3d9 [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
Momchil Velikov080d0462020-03-24 09:32:51 +00001995static void handleCmseNSEntryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1996 if (S.LangOpts.CPlusPlus && !D->getDeclContext()->isExternCContext()) {
1997 S.Diag(AL.getLoc(), diag::err_attribute_not_clinkage) << AL;
1998 return;
1999 }
2000
2001 if (cast<FunctionDecl>(D)->getStorageClass() == SC_Static) {
2002 S.Diag(AL.getLoc(), diag::warn_attribute_cmse_entry_static);
2003 return;
2004 }
2005
2006 D->addAttr(::new (S.Context) CmseNSEntryAttr(S.Context, AL));
2007}
2008
Erich Keanee891aa92018-07-13 15:07:47 +00002009static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002010 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
Charles Davis0e379112016-08-08 21:19:08 +00002011 return;
2012
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002013 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002014 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
2015 const auto &Arch = Triple.getArch();
2016 if (Arch != llvm::Triple::x86 &&
2017 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002018 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
Erich Keane44bacdf2018-08-09 13:21:32 +00002019 << AL << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002020 return;
2021 }
2022 }
2023
Erich Keane6a24e802019-09-13 17:39:31 +00002024 D->addAttr(::new (S.Context) NakedAttr(S.Context, AL));
Charles Davis0e379112016-08-08 21:19:08 +00002025}
2026
Erich Keanee891aa92018-07-13 15:07:47 +00002027static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002028 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00002029
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002030 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002031 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002032 << Attrs << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002033 return;
2034 }
2035
Erich Keane6a24e802019-09-13 17:39:31 +00002036 D->addAttr(::new (S.Context) NoReturnAttr(S.Context, Attrs));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002037}
2038
Erich Keanee891aa92018-07-13 15:07:47 +00002039static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00002040 if (!S.getLangOpts().CFProtectionBranch)
2041 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
2042 else
2043 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00002044}
2045
Erich Keanee891aa92018-07-13 15:07:47 +00002046bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002047 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
2048 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002049 return true;
2050 }
2051
2052 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002053}
2054
Erich Keanee891aa92018-07-13 15:07:47 +00002055bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002056 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002057 if (!AL.existsInTarget(Context.getTargetInfo())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002058 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002059 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002060 return true;
2061 }
2062
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002063 return false;
2064}
2065
Erich Keanee891aa92018-07-13 15:07:47 +00002066static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2067
Ted Kremenek5295ce82010-08-19 00:51:58 +00002068 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2069 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00002070 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002071 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00002072 if (!VD || (!VD->getType()->isBlockPointerType() &&
2073 !VD->getType()->isFunctionPointerType())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002074 S.Diag(AL.getLoc(), AL.isCXX11Attribute()
2075 ? diag::err_attribute_wrong_decl_type
2076 : diag::warn_attribute_wrong_decl_type)
2077 << AL << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00002078 return;
2079 }
2080 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002081
Erich Keane6a24e802019-09-13 17:39:31 +00002082 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(S.Context, AL));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002083}
2084
John Thompsoncdb847ba2010-08-09 21:53:52 +00002085// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00002086static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2087 /*
2088 Returning a Vector Class in Registers
2089
2090 According to the PPU ABI specifications, a class with a single member of
2091 vector type is returned in memory when used as the return value of a
2092 function.
2093 This results in inefficient code when implementing vector classes. To return
2094 the value in a single vector register, add the vecreturn attribute to the
2095 class definition. This attribute is also applicable to struct types.
2096
2097 Example:
2098
2099 struct Vector
2100 {
2101 __vector float xyzw;
2102 } __attribute__((vecreturn));
2103
2104 Vector Add(Vector lhs, Vector rhs)
2105 {
2106 Vector result;
2107 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2108 return result; // This will be returned in a register
2109 }
2110 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002111 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002112 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002113 return;
2114 }
2115
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002116 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002117 int count = 0;
2118
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002119 if (!isa<CXXRecordDecl>(R)) {
2120 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002121 return;
2122 }
2123
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002124 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2125 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002126 return;
2127 }
2128
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002129 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002130 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002131 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002132 return;
2133 }
2134 count++;
2135 }
2136
Erich Keane6a24e802019-09-13 17:39:31 +00002137 D->addAttr(::new (S.Context) VecReturnAttr(S.Context, AL));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002138}
2139
Richard Smithe233fbf2013-01-28 22:42:45 +00002140static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002141 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002142 if (isa<ParmVarDecl>(D)) {
2143 // [[carries_dependency]] can only be applied to a parameter if it is a
2144 // parameter of a function declaration or lambda.
2145 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002146 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002147 diag::err_carries_dependency_param_not_function_decl);
2148 return;
2149 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002150 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002151
Erich Keane6a24e802019-09-13 17:39:31 +00002152 D->addAttr(::new (S.Context) CarriesDependencyAttr(S.Context, AL));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002153}
2154
Erich Keanee891aa92018-07-13 15:07:47 +00002155static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002156 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002157
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002158 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002159 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002160 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Erich Keane44bacdf2018-08-09 13:21:32 +00002161 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002162
Erich Keane6a24e802019-09-13 17:39:31 +00002163 D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002164}
2165
Erich Keanee891aa92018-07-13 15:07:47 +00002166static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002167 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002168 if (AL.getNumArgs() &&
2169 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002170 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002171
Erich Keane6a24e802019-09-13 17:39:31 +00002172 D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00002173}
2174
Erich Keanee891aa92018-07-13 15:07:47 +00002175static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002176 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002177 if (AL.getNumArgs() &&
2178 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002179 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002180
Erich Keane6a24e802019-09-13 17:39:31 +00002181 D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00002182}
2183
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002184template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002185static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002186 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002187 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002188 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002189 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002190
Erich Keane6a24e802019-09-13 17:39:31 +00002191 D->addAttr(::new (S.Context) AttrTy(S.Context, AL, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002192}
2193
Ted Kremenek438f8db2014-02-22 01:06:05 +00002194static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002195 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002196 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002197 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
Erich Keane44bacdf2018-08-09 13:21:32 +00002198 << AL << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002199 return;
2200 }
2201
Erich Keane6a24e802019-09-13 17:39:31 +00002202 D->addAttr(::new (S.Context) ObjCExplicitProtocolImplAttr(S.Context, AL));
Ted Kremenek28eace62013-11-23 01:01:34 +00002203}
2204
Jordy Rose740b0c22012-05-08 03:27:22 +00002205static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2206 IdentifierInfo *Platform,
2207 VersionTuple Introduced,
2208 VersionTuple Deprecated,
2209 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002210 StringRef PlatformName
2211 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2212 if (PlatformName.empty())
2213 PlatformName = Platform->getName();
2214
2215 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2216 // of these steps are needed).
2217 if (!Introduced.empty() && !Deprecated.empty() &&
2218 !(Introduced <= Deprecated)) {
2219 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2220 << 1 << PlatformName << Deprecated.getAsString()
2221 << 0 << Introduced.getAsString();
2222 return true;
2223 }
2224
2225 if (!Introduced.empty() && !Obsoleted.empty() &&
2226 !(Introduced <= Obsoleted)) {
2227 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2228 << 2 << PlatformName << Obsoleted.getAsString()
2229 << 0 << Introduced.getAsString();
2230 return true;
2231 }
2232
2233 if (!Deprecated.empty() && !Obsoleted.empty() &&
2234 !(Deprecated <= Obsoleted)) {
2235 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2236 << 2 << PlatformName << Obsoleted.getAsString()
2237 << 1 << Deprecated.getAsString();
2238 return true;
2239 }
2240
2241 return false;
2242}
2243
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002244/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002245///
2246/// If either version tuple is empty, then they are assumed to match. If
2247/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2248static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2249 bool BeforeIsOkay) {
2250 if (X.empty() || Y.empty())
2251 return true;
2252
2253 if (X == Y)
2254 return true;
2255
2256 if (BeforeIsOkay && X < Y)
2257 return true;
2258
2259 return false;
2260}
2261
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002262AvailabilityAttr *Sema::mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002263 NamedDecl *D, const AttributeCommonInfo &CI, IdentifierInfo *Platform,
2264 bool Implicit, VersionTuple Introduced, VersionTuple Deprecated,
2265 VersionTuple Obsoleted, bool IsUnavailable, StringRef Message,
2266 bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK,
2267 int Priority) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002268 VersionTuple MergedIntroduced = Introduced;
2269 VersionTuple MergedDeprecated = Deprecated;
2270 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002271 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002272 bool OverrideOrImpl = false;
2273 switch (AMK) {
2274 case AMK_None:
2275 case AMK_Redeclaration:
2276 OverrideOrImpl = false;
2277 break;
2278
2279 case AMK_Override:
2280 case AMK_ProtocolImplementation:
2281 OverrideOrImpl = true;
2282 break;
2283 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002284
Rafael Espindolac67f2232012-05-10 02:50:16 +00002285 if (D->hasAttrs()) {
2286 AttrVec &Attrs = D->getAttrs();
2287 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002288 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002289 if (!OldAA) {
2290 ++i;
2291 continue;
2292 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002293
Rafael Espindolac67f2232012-05-10 02:50:16 +00002294 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2295 if (OldPlatform != Platform) {
2296 ++i;
2297 continue;
2298 }
2299
Tim Northover7a73cc72015-10-30 16:30:49 +00002300 // If there is an existing availability attribute for this platform that
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002301 // has a lower priority use the existing one and discard the new
2302 // attribute.
2303 if (OldAA->getPriority() < Priority)
Tim Northover7a73cc72015-10-30 16:30:49 +00002304 return nullptr;
Tim Northover7a73cc72015-10-30 16:30:49 +00002305
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002306 // If there is an existing attribute for this platform that has a higher
2307 // priority than the new attribute then erase the old one and continue
2308 // processing the attributes.
2309 if (OldAA->getPriority() > Priority) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002310 Attrs.erase(Attrs.begin() + i);
2311 --e;
2312 continue;
2313 }
2314
Rafael Espindolac67f2232012-05-10 02:50:16 +00002315 FoundAny = true;
2316 VersionTuple OldIntroduced = OldAA->getIntroduced();
2317 VersionTuple OldDeprecated = OldAA->getDeprecated();
2318 VersionTuple OldObsoleted = OldAA->getObsoleted();
2319 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002320
Douglas Gregord2a713e2015-09-30 21:27:42 +00002321 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2322 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2323 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002324 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002325 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2326 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002327 int Which = -1;
2328 VersionTuple FirstVersion;
2329 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002330 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002331 Which = 0;
2332 FirstVersion = OldIntroduced;
2333 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002334 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002335 Which = 1;
2336 FirstVersion = Deprecated;
2337 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002338 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002339 Which = 2;
2340 FirstVersion = Obsoleted;
2341 SecondVersion = OldObsoleted;
2342 }
2343
2344 if (Which == -1) {
2345 Diag(OldAA->getLocation(),
2346 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002347 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2348 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002349 } else {
2350 Diag(OldAA->getLocation(),
2351 diag::warn_mismatched_availability_override)
2352 << Which
2353 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002354 << FirstVersion.getAsString() << SecondVersion.getAsString()
2355 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002356 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002357 if (AMK == AMK_Override)
Erich Keane6a24e802019-09-13 17:39:31 +00002358 Diag(CI.getLoc(), diag::note_overridden_method);
Douglas Gregord2a713e2015-09-30 21:27:42 +00002359 else
Erich Keane6a24e802019-09-13 17:39:31 +00002360 Diag(CI.getLoc(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002361 } else {
2362 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
Erich Keane6a24e802019-09-13 17:39:31 +00002363 Diag(CI.getLoc(), diag::note_previous_attribute);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002364 }
2365
Rafael Espindolac67f2232012-05-10 02:50:16 +00002366 Attrs.erase(Attrs.begin() + i);
2367 --e;
2368 continue;
2369 }
2370
2371 VersionTuple MergedIntroduced2 = MergedIntroduced;
2372 VersionTuple MergedDeprecated2 = MergedDeprecated;
2373 VersionTuple MergedObsoleted2 = MergedObsoleted;
2374
2375 if (MergedIntroduced2.empty())
2376 MergedIntroduced2 = OldIntroduced;
2377 if (MergedDeprecated2.empty())
2378 MergedDeprecated2 = OldDeprecated;
2379 if (MergedObsoleted2.empty())
2380 MergedObsoleted2 = OldObsoleted;
2381
2382 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2383 MergedIntroduced2, MergedDeprecated2,
2384 MergedObsoleted2)) {
2385 Attrs.erase(Attrs.begin() + i);
2386 --e;
2387 continue;
2388 }
2389
2390 MergedIntroduced = MergedIntroduced2;
2391 MergedDeprecated = MergedDeprecated2;
2392 MergedObsoleted = MergedObsoleted2;
2393 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002394 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002395 }
2396
2397 if (FoundAny &&
2398 MergedIntroduced == Introduced &&
2399 MergedDeprecated == Deprecated &&
2400 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002401 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002402
Douglas Gregord2a713e2015-09-30 21:27:42 +00002403 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002404 // the checking.
Erich Keane6a24e802019-09-13 17:39:31 +00002405 if (!checkAvailabilityAttr(*this, CI.getRange(), Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002406 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002407 !OverrideOrImpl) {
Erich Keane6a24e802019-09-13 17:39:31 +00002408 auto *Avail = ::new (Context) AvailabilityAttr(
2409 Context, CI, Platform, Introduced, Deprecated, Obsoleted, IsUnavailable,
2410 Message, IsStrict, Replacement, Priority);
Manman Ren719a8642016-05-06 21:04:01 +00002411 Avail->setImplicit(Implicit);
2412 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002413 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002414 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002415}
2416
Erich Keanee891aa92018-07-13 15:07:47 +00002417static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002418 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002419 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002420 IdentifierLoc *Platform = AL.getArgAsIdent(0);
Fangrui Song6907ce22018-07-30 19:24:48 +00002421
Aaron Ballman00e99962013-08-31 01:11:41 +00002422 IdentifierInfo *II = Platform->Ident;
2423 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2424 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2425 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002426
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002427 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002428 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002429 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002430
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002431 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2432 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2433 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2434 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2435 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002436 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002437 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002438 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002439 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002440 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002441 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002442
Michael Wu260e9622018-11-12 02:44:33 +00002443 if (II->isStr("swift")) {
2444 if (Introduced.isValid() || Obsoleted.isValid() ||
2445 (!IsUnavailable && !Deprecated.isValid())) {
2446 S.Diag(AL.getLoc(),
2447 diag::warn_availability_swift_unavailable_deprecated_only);
2448 return;
2449 }
2450 }
2451
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002452 int PriorityModifier = AL.isPragmaClangAttribute()
2453 ? Sema::AP_PragmaClangAttribute
2454 : Sema::AP_Explicit;
2455 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002456 ND, AL, II, false /*Implicit*/, Introduced.Version, Deprecated.Version,
2457 Obsoleted.Version, IsUnavailable, Str, IsStrict, Replacement,
2458 Sema::AMK_None, PriorityModifier);
Rafael Espindola19de5612013-01-12 06:42:30 +00002459 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002460 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002461
2462 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2463 // matches before the start of the watchOS platform.
2464 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2465 IdentifierInfo *NewII = nullptr;
2466 if (II->getName() == "ios")
2467 NewII = &S.Context.Idents.get("watchos");
2468 else if (II->getName() == "ios_app_extension")
2469 NewII = &S.Context.Idents.get("watchos_app_extension");
2470
2471 if (NewII) {
2472 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2473 if (Version.empty())
2474 return Version;
2475 auto Major = Version.getMajor();
2476 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2477 if (NewMajor >= 2) {
2478 if (Version.getMinor().hasValue()) {
2479 if (Version.getSubminor().hasValue())
2480 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2481 Version.getSubminor().getValue());
2482 else
2483 return VersionTuple(NewMajor, Version.getMinor().getValue());
2484 }
Alex Lorenz0b436482019-03-20 20:02:00 +00002485 return VersionTuple(NewMajor);
Tim Northover7a73cc72015-10-30 16:30:49 +00002486 }
2487
2488 return VersionTuple(2, 0);
2489 };
2490
2491 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2492 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2493 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2494
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002495 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002496 ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated,
2497 NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
2498 Sema::AMK_None,
2499 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
Tim Northover7a73cc72015-10-30 16:30:49 +00002500 if (NewAttr)
2501 D->addAttr(NewAttr);
2502 }
2503 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2504 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2505 // matches before the start of the tvOS platform.
2506 IdentifierInfo *NewII = nullptr;
2507 if (II->getName() == "ios")
2508 NewII = &S.Context.Idents.get("tvos");
2509 else if (II->getName() == "ios_app_extension")
2510 NewII = &S.Context.Idents.get("tvos_app_extension");
2511
2512 if (NewII) {
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002513 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002514 ND, AL, NewII, true /*Implicit*/, Introduced.Version,
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002515 Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict,
2516 Replacement, Sema::AMK_None,
Erich Keane6a24e802019-09-13 17:39:31 +00002517 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002518 if (NewAttr)
2519 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002520 }
2521 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002522}
2523
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002524static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002525 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002526 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002527 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002528 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002529 "Invalid number of arguments in an external_source_symbol attribute");
2530
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002531 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002532 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002533 Language = SE->getString();
2534 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002535 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002536 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002537 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002538
2539 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002540 S.Context, AL, Language, DefinedIn, IsGeneratedDeclaration));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002541}
2542
John McCalld041a9b2013-02-20 01:54:26 +00002543template <class T>
Erich Keane6a24e802019-09-13 17:39:31 +00002544static T *mergeVisibilityAttr(Sema &S, Decl *D, const AttributeCommonInfo &CI,
2545 typename T::VisibilityType value) {
John McCalld041a9b2013-02-20 01:54:26 +00002546 T *existingAttr = D->getAttr<T>();
2547 if (existingAttr) {
2548 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2549 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002550 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002551 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
Erich Keane6a24e802019-09-13 17:39:31 +00002552 S.Diag(CI.getLoc(), diag::note_previous_attribute);
John McCalld041a9b2013-02-20 01:54:26 +00002553 D->dropAttr<T>();
2554 }
Erich Keane6a24e802019-09-13 17:39:31 +00002555 return ::new (S.Context) T(S.Context, CI, value);
John McCalld041a9b2013-02-20 01:54:26 +00002556}
2557
Erich Keane6a24e802019-09-13 17:39:31 +00002558VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D,
2559 const AttributeCommonInfo &CI,
2560 VisibilityAttr::VisibilityType Vis) {
2561 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, CI, Vis);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002562}
2563
Erich Keane6a24e802019-09-13 17:39:31 +00002564TypeVisibilityAttr *
2565Sema::mergeTypeVisibilityAttr(Decl *D, const AttributeCommonInfo &CI,
2566 TypeVisibilityAttr::VisibilityType Vis) {
2567 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, CI, Vis);
John McCalld041a9b2013-02-20 01:54:26 +00002568}
2569
Erich Keanee891aa92018-07-13 15:07:47 +00002570static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002571 bool isTypeVisibility) {
2572 // Visibility attributes don't mean anything on a typedef.
2573 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002574 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002575 return;
2576 }
2577
2578 // 'type_visibility' can only go on a type or namespace.
2579 if (isTypeVisibility &&
2580 !(isa<TagDecl>(D) ||
2581 isa<ObjCInterfaceDecl>(D) ||
2582 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002583 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002584 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002585 return;
2586 }
2587
Benjamin Kramer70370212013-09-09 15:08:57 +00002588 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002589 StringRef TypeStr;
2590 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002591 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002592 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002593
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002594 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002595 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002596 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2597 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002598 return;
2599 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002600
Aaron Ballman682ee422013-09-11 19:47:58 +00002601 // Complain about attempts to use protected visibility on targets
2602 // (like Darwin) that don't support it.
2603 if (type == VisibilityAttr::Protected &&
2604 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002605 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002606 type = VisibilityAttr::Default;
2607 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002608
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002609 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002610 if (isTypeVisibility) {
Erich Keane6a24e802019-09-13 17:39:31 +00002611 newAttr = S.mergeTypeVisibilityAttr(
2612 D, AL, (TypeVisibilityAttr::VisibilityType)type);
John McCalld041a9b2013-02-20 01:54:26 +00002613 } else {
Erich Keane6a24e802019-09-13 17:39:31 +00002614 newAttr = S.mergeVisibilityAttr(D, AL, type);
John McCalld041a9b2013-02-20 01:54:26 +00002615 }
2616 if (newAttr)
2617 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002618}
2619
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08002620static void handleObjCDirectAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2621 // objc_direct cannot be set on methods declared in the context of a protocol
2622 if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
2623 S.Diag(AL.getLoc(), diag::err_objc_direct_on_protocol) << false;
2624 return;
2625 }
2626
2627 if (S.getLangOpts().ObjCRuntime.allowsDirectDispatch()) {
2628 handleSimpleAttribute<ObjCDirectAttr>(S, D, AL);
2629 } else {
2630 S.Diag(AL.getLoc(), diag::warn_objc_direct_ignored) << AL;
2631 }
2632}
2633
2634static void handleObjCDirectMembersAttr(Sema &S, Decl *D,
2635 const ParsedAttr &AL) {
2636 if (S.getLangOpts().ObjCRuntime.allowsDirectDispatch()) {
2637 handleSimpleAttribute<ObjCDirectMembersAttr>(S, D, AL);
2638 } else {
2639 S.Diag(AL.getLoc(), diag::warn_objc_direct_ignored) << AL;
2640 }
2641}
2642
Erich Keanee891aa92018-07-13 15:07:47 +00002643static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002644 const auto *M = cast<ObjCMethodDecl>(D);
2645 if (!AL.isArgIdent(0)) {
2646 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002647 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002648 return;
2649 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002650
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002651 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002652 ObjCMethodFamilyAttr::FamilyKind F;
2653 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002654 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002655 return;
2656 }
2657
Alp Toker314cc812014-01-25 16:55:45 +00002658 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002659 !M->getReturnType()->isObjCObjectPointerType()) {
2660 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2661 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002662 // Ignore the attribute.
2663 return;
2664 }
2665
Erich Keane6a24e802019-09-13 17:39:31 +00002666 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(S.Context, AL, F));
John McCall86bc21f2011-03-02 11:33:24 +00002667}
2668
Erich Keanee891aa92018-07-13 15:07:47 +00002669static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002670 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002671 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002672 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002673 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2674 return;
2675 }
2676 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002677 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002678 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002679 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002680 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2681 return;
2682 }
2683 }
2684 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002685 // It is okay to include this attribute on properties, e.g.:
2686 //
2687 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2688 //
2689 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002690 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002691 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002692 }
Erich Keane6a24e802019-09-13 17:39:31 +00002693 D->addAttr(::new (S.Context) ObjCNSObjectAttr(S.Context, AL));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002694}
2695
Erich Keanee891aa92018-07-13 15:07:47 +00002696static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002697 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002698 QualType T = TD->getUnderlyingType();
2699 if (!T->isObjCObjectPointerType()) {
2700 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2701 return;
2702 }
2703 } else {
2704 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2705 return;
2706 }
Erich Keane6a24e802019-09-13 17:39:31 +00002707 D->addAttr(::new (S.Context) ObjCIndependentClassAttr(S.Context, AL));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002708}
2709
Erich Keanee891aa92018-07-13 15:07:47 +00002710static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002711 if (!AL.isArgIdent(0)) {
2712 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002713 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002714 return;
2715 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002716
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002717 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002718 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002719 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002720 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002721 return;
2722 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002723
Erich Keane6a24e802019-09-13 17:39:31 +00002724 D->addAttr(::new (S.Context) BlocksAttr(S.Context, AL, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002725}
2726
Erich Keanee891aa92018-07-13 15:07:47 +00002727static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002728 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002729 if (AL.getNumArgs() > 0) {
2730 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002731 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002732 if (E->isTypeDependent() || E->isValueDependent() ||
2733 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002734 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002735 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002736 return;
2737 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002738
John McCallb46f2872011-09-09 07:56:05 +00002739 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002740 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002741 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002742 return;
2743 }
John McCallb46f2872011-09-09 07:56:05 +00002744
2745 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002746 }
2747
Aaron Ballman18a78382013-11-21 00:28:23 +00002748 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002749 if (AL.getNumArgs() > 1) {
2750 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002751 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002752 if (E->isTypeDependent() || E->isValueDependent() ||
2753 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002754 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002755 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002756 return;
2757 }
2758 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002759
John McCallb46f2872011-09-09 07:56:05 +00002760 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002761 // FIXME: This error message could be improved, it would be nice
2762 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002763 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002764 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002765 return;
2766 }
2767 }
2768
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002769 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002770 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002771 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002772 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002773 return;
2774 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002775
Chris Lattner9363e312009-03-17 23:03:47 +00002776 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002777 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002778 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002779 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002780 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002781 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002782 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002783 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002784 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002785 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002786 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002787 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002788 return;
2789 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002790 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002791 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002792 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002793 const FunctionType *FT = Ty->isFunctionPointerType()
2794 ? D->getFunctionType()
Simon Pilgrim237d0af2019-10-04 15:02:46 +00002795 : Ty->castAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002796 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002797 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002798 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002799 return;
2800 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002801 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002802 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002803 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002804 return;
2805 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002806 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002807 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002808 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002809 return;
2810 }
Erich Keane6a24e802019-09-13 17:39:31 +00002811 D->addAttr(::new (S.Context) SentinelAttr(S.Context, AL, sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002812}
2813
Erich Keanee891aa92018-07-13 15:07:47 +00002814static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002815 if (D->getFunctionType() &&
Erich Keane46441fd2019-07-25 15:10:56 +00002816 D->getFunctionType()->getReturnType()->isVoidType() &&
2817 !isa<CXXConstructorDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002818 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002819 return;
2820 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002821 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002822 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002823 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002824 return;
2825 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002826
Aaron Ballman3bef0142019-07-20 07:56:34 +00002827 StringRef Str;
2828 if ((AL.isCXX11Attribute() || AL.isC2xAttribute()) && !AL.getScopeName()) {
2829 // If this is spelled as the standard C++17 attribute, but not in C++17,
2830 // warn about using it as an extension. If there are attribute arguments,
2831 // then claim it's a C++2a extension instead.
2832 // FIXME: If WG14 does not seem likely to adopt the same feature, add an
2833 // extension warning for C2x mode.
2834 const LangOptions &LO = S.getLangOpts();
2835 if (AL.getNumArgs() == 1) {
2836 if (LO.CPlusPlus && !LO.CPlusPlus2a)
2837 S.Diag(AL.getLoc(), diag::ext_cxx2a_attr) << AL;
2838
2839 // Since this this is spelled [[nodiscard]], get the optional string
2840 // literal. If in C++ mode, but not in C++2a mode, diagnose as an
2841 // extension.
2842 // FIXME: C2x should support this feature as well, even as an extension.
2843 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
2844 return;
2845 } else if (LO.CPlusPlus && !LO.CPlusPlus17)
2846 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
2847 }
Aaron Ballmane7964782016-03-07 22:44:55 +00002848
Erich Keane6a24e802019-09-13 17:39:31 +00002849 D->addAttr(::new (S.Context) WarnUnusedResultAttr(S.Context, AL, Str));
Chris Lattner237f2752009-02-14 07:37:35 +00002850}
2851
Erich Keanee891aa92018-07-13 15:07:47 +00002852static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002853 // weak_import only applies to variable & function declarations.
2854 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002855 if (!D->canBeWeakImported(isDef)) {
2856 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002857 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002858 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002859 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002860 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002861 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002862 // Nothing to warn about here.
2863 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002864 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002865 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002866
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002867 return;
2868 }
2869
Erich Keane6a24e802019-09-13 17:39:31 +00002870 D->addAttr(::new (S.Context) WeakImportAttr(S.Context, AL));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002871}
2872
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002873// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002874template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002875static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002876 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002877 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002878 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002879 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2880 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002881 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002882 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002883 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002884 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002885 return;
2886 }
2887 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002888
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002889 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2890 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2891 Existing->getYDim() == WGSize[1] &&
2892 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002893 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002894
Erich Keane6a24e802019-09-13 17:39:31 +00002895 D->addAttr(::new (S.Context)
2896 WorkGroupAttr(S.Context, AL, WGSize[0], WGSize[1], WGSize[2]));
Nate Begemanf2758702009-06-26 06:32:41 +00002897}
2898
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002899// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002900static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002901 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002902 const Expr *E = AL.getArgAsExpr(0);
2903 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002904 return;
2905 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002906 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002907 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002908 return;
2909 }
2910
2911 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2912 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2913 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002914 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002915
Erich Keane6a24e802019-09-13 17:39:31 +00002916 D->addAttr(::new (S.Context)
2917 OpenCLIntelReqdSubGroupSizeAttr(S.Context, AL, SGSize));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002918}
2919
Erich Keanee891aa92018-07-13 15:07:47 +00002920static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002921 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002922 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002923 return;
2924 }
2925
Craig Topperc3ec1492014-05-26 06:22:03 +00002926 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002927 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002928 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002929
2930 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2931 (ParmType->isBooleanType() ||
2932 !ParmType->isIntegralType(S.getASTContext()))) {
Gabor Horvath247a6032020-01-02 11:57:42 -08002933 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument) << 2 << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002934 return;
2935 }
2936
Aaron Ballmana9e05402013-12-02 22:16:55 +00002937 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002938 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002939 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002940 return;
2941 }
2942 }
2943
Erich Keane6a24e802019-09-13 17:39:31 +00002944 D->addAttr(::new (S.Context) VecTypeHintAttr(S.Context, AL, ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002945}
2946
Erich Keane6a24e802019-09-13 17:39:31 +00002947SectionAttr *Sema::mergeSectionAttr(Decl *D, const AttributeCommonInfo &CI,
2948 StringRef Name) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002949 // Explicit or partial specializations do not inherit
2950 // the section attribute from the primary template.
2951 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keane6a24e802019-09-13 17:39:31 +00002952 if (CI.getAttributeSpellingListIndex() == SectionAttr::Declspec_allocate &&
Erich Keane7963e8b2018-07-18 20:04:48 +00002953 FD->isFunctionTemplateSpecialization())
2954 return nullptr;
2955 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002956 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2957 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002958 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002959 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2960 << 1 /*section*/;
Erich Keane6a24e802019-09-13 17:39:31 +00002961 Diag(CI.getLoc(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002962 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002963 }
Erich Keane6a24e802019-09-13 17:39:31 +00002964 return ::new (Context) SectionAttr(Context, CI, Name);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002965}
2966
Reid Kleckner2a133222015-03-04 23:39:17 +00002967bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2968 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2969 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002970 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002971 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002972 return false;
2973 }
2974 return true;
2975}
2976
Erich Keanee891aa92018-07-13 15:07:47 +00002977static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002978 // Make sure that there is a string literal as the sections's single
2979 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002980 StringRef Str;
2981 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002982 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002983 return;
Mike Stump11289f42009-09-09 15:08:12 +00002984
Reid Kleckner2a133222015-03-04 23:39:17 +00002985 if (!S.checkSectionName(LiteralLoc, Str))
2986 return;
2987
Chris Lattner30ba6742009-08-10 19:03:04 +00002988 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002989 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002990 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002991 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002992 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002993 return;
2994 }
Mike Stump11289f42009-09-09 15:08:12 +00002995
Erich Keane6a24e802019-09-13 17:39:31 +00002996 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002997 if (NewAttr)
2998 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002999}
3000
Nico Weber98016212019-07-09 00:02:23 +00003001// This is used for `__declspec(code_seg("segname"))` on a decl.
3002// `#pragma code_seg("segname")` uses checkSectionName() instead.
3003static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc,
3004 StringRef CodeSegName) {
3005 std::string Error =
3006 S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
Erich Keane7963e8b2018-07-18 20:04:48 +00003007 if (!Error.empty()) {
Nico Weber98016212019-07-09 00:02:23 +00003008 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
3009 << Error << 0 /*'code-seg'*/;
Erich Keane7963e8b2018-07-18 20:04:48 +00003010 return false;
3011 }
Nico Weber98016212019-07-09 00:02:23 +00003012
Erich Keane7963e8b2018-07-18 20:04:48 +00003013 return true;
3014}
3015
Erich Keane6a24e802019-09-13 17:39:31 +00003016CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, const AttributeCommonInfo &CI,
3017 StringRef Name) {
Erich Keane7963e8b2018-07-18 20:04:48 +00003018 // Explicit or partial specializations do not inherit
3019 // the code_seg attribute from the primary template.
3020 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3021 if (FD->isFunctionTemplateSpecialization())
3022 return nullptr;
3023 }
3024 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3025 if (ExistingAttr->getName() == Name)
3026 return nullptr;
3027 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3028 << 0 /*codeseg*/;
Erich Keane6a24e802019-09-13 17:39:31 +00003029 Diag(CI.getLoc(), diag::note_previous_attribute);
Erich Keane7963e8b2018-07-18 20:04:48 +00003030 return nullptr;
3031 }
Erich Keane6a24e802019-09-13 17:39:31 +00003032 return ::new (Context) CodeSegAttr(Context, CI, Name);
Erich Keane7963e8b2018-07-18 20:04:48 +00003033}
3034
3035static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3036 StringRef Str;
3037 SourceLocation LiteralLoc;
3038 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3039 return;
3040 if (!checkCodeSegName(S, LiteralLoc, Str))
3041 return;
3042 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3043 if (!ExistingAttr->isImplicit()) {
3044 S.Diag(AL.getLoc(),
3045 ExistingAttr->getName() == Str
3046 ? diag::warn_duplicate_codeseg_attribute
3047 : diag::err_conflicting_codeseg_attribute);
3048 return;
3049 }
3050 D->dropAttr<CodeSegAttr>();
3051 }
Erich Keane6a24e802019-09-13 17:39:31 +00003052 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL, Str))
Erich Keane7963e8b2018-07-18 20:04:48 +00003053 D->addAttr(CSA);
3054}
3055
Erich Keane57e15cd2017-07-19 22:06:33 +00003056// Check for things we'd like to warn about. Multiversioning issues are
3057// handled later in the process, once we know how many exist.
3058bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3059 enum FirstParam { Unsupported, Duplicate };
3060 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00003061 for (auto Str : {"tune=", "fpmath="})
3062 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00003063 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3064 << Unsupported << None << Str;
3065
Craig Topper505aa242019-12-09 10:34:24 -08003066 ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
Erich Keane57e15cd2017-07-19 22:06:33 +00003067
3068 if (!ParsedAttrs.Architecture.empty() &&
3069 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3070 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3071 << Unsupported << Architecture << ParsedAttrs.Architecture;
3072
3073 if (ParsedAttrs.DuplicateArchitecture)
3074 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3075 << Duplicate << None << "arch=";
3076
3077 for (const auto &Feature : ParsedAttrs.Features) {
3078 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3079 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3080 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3081 << Unsupported << None << CurFeature;
3082 }
3083
Momchil Velikovaa6d48f2019-11-15 11:34:47 +00003084 TargetInfo::BranchProtectionInfo BPI;
3085 StringRef Error;
3086 if (!ParsedAttrs.BranchProtection.empty() &&
3087 !Context.getTargetInfo().validateBranchProtection(
3088 ParsedAttrs.BranchProtection, BPI, Error)) {
3089 if (Error.empty())
3090 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3091 << Unsupported << None << "branch-protection";
3092 else
3093 return Diag(LiteralLoc, diag::err_invalid_branch_protection_spec)
3094 << Error;
3095 }
3096
Erich Keane29636aa2018-02-16 17:31:59 +00003097 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003098}
3099
Erich Keanee891aa92018-07-13 15:07:47 +00003100static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003101 StringRef Str;
3102 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003103 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003104 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003105 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003106
Erich Keane6a24e802019-09-13 17:39:31 +00003107 TargetAttr *NewAttr = ::new (S.Context) TargetAttr(S.Context, AL, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00003108 D->addAttr(NewAttr);
3109}
3110
Erich Keanee891aa92018-07-13 15:07:47 +00003111static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003112 Expr *E = AL.getArgAsExpr(0);
3113 uint32_t VecWidth;
3114 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3115 AL.setInvalid();
3116 return;
3117 }
3118
3119 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3120 if (Existing && Existing->getVectorWidth() != VecWidth) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003121 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Craig Topper74c10e32018-07-09 19:00:16 +00003122 return;
3123 }
3124
Erich Keane6a24e802019-09-13 17:39:31 +00003125 D->addAttr(::new (S.Context) MinVectorWidthAttr(S.Context, AL, VecWidth));
Craig Topper74c10e32018-07-09 19:00:16 +00003126}
3127
Erich Keanee891aa92018-07-13 15:07:47 +00003128static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003129 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003130 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003131 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003132 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003133
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003134 // gcc only allows for simple identifiers. Since we support more than gcc, we
3135 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003136 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003137 if (DRE->hasQualifier())
3138 S.Diag(Loc, diag::warn_cleanup_ext);
3139 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3140 NI = DRE->getNameInfo();
3141 if (!FD) {
3142 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3143 << NI.getName();
3144 return;
3145 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003146 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003147 if (ULE->hasExplicitTemplateArgs())
3148 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003149 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3150 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003151 if (!FD) {
3152 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3153 << NI.getName();
3154 if (ULE->getType() == S.Context.OverloadTy)
3155 S.NoteAllOverloadCandidates(ULE);
3156 return;
3157 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003158 } else {
3159 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003160 return;
3161 }
3162
Anders Carlssond277d792009-01-31 01:16:18 +00003163 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003164 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3165 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003166 return;
3167 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003168
Anders Carlsson723f55d2009-02-07 23:16:50 +00003169 // We're currently more strict than GCC about what function types we accept.
3170 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003171 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003172 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003173 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3174 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003175 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3176 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003177 return;
3178 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003179
Erich Keane6a24e802019-09-13 17:39:31 +00003180 D->addAttr(::new (S.Context) CleanupAttr(S.Context, AL, FD));
Anders Carlssond277d792009-01-31 01:16:18 +00003181}
3182
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003183static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003184 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003185 if (!AL.isArgIdent(0)) {
3186 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003187 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003188 return;
3189 }
3190
3191 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003192 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003193 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3194 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003195 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003196 return;
3197 }
3198
Erich Keane6a24e802019-09-13 17:39:31 +00003199 D->addAttr(::new (S.Context)
3200 EnumExtensibilityAttr(S.Context, AL, ExtensibilityKind));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003201}
3202
Mike Stumpd3bb5572009-07-24 19:02:52 +00003203/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003204/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003205static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003206 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003207 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003208 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003209 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003210
Eric Christopherb64963e2015-08-13 21:34:35 +00003211 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003212 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003213
Eric Christopherb64963e2015-08-13 21:34:35 +00003214 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3215 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003216 !isCFStringType(Ty, S.Context) &&
3217 (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003218 !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003219 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003220 << "a string type" << IdxExpr->getSourceRange()
3221 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003222 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003223 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003224 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003225 if (!isNSStringType(Ty, S.Context) &&
3226 !isCFStringType(Ty, S.Context) &&
3227 (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003228 !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003229 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003230 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003231 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003232 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003233 }
3234
Erich Keane6a24e802019-09-13 17:39:31 +00003235 D->addAttr(::new (S.Context) FormatArgAttr(S.Context, AL, Idx));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003236}
3237
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003238enum FormatAttrKind {
3239 CFStringFormat,
3240 NSStringFormat,
3241 StrftimeFormat,
3242 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003243 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003244 InvalidFormat
3245};
3246
3247/// getFormatAttrKind - Map from format attribute names to supported format
3248/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003249static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003250 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003251 // Check for formats that get handled specially.
3252 .Case("NSString", NSStringFormat)
3253 .Case("CFString", CFStringFormat)
3254 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003255
Mehdi Amini06d367c2016-10-24 20:39:34 +00003256 // Otherwise, check for supported formats.
3257 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3258 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3259 .Case("kprintf", SupportedFormat) // OpenBSD.
3260 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3261 .Case("os_trace", SupportedFormat)
3262 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003263
Mehdi Amini06d367c2016-10-24 20:39:34 +00003264 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3265 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003266}
3267
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003268/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003269/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003270static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003271 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003272 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003273 return;
3274 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003275
Aaron Ballman4a611152013-11-27 16:34:09 +00003276 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003277 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3278 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003279 return;
3280 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003281 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003282 if (S.Context.getAsArrayType(T))
3283 T = S.Context.getBaseElementType(T);
3284 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003285 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3286 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003287 return;
3288 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003289
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003290 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003291 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003292 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3293 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003294 return;
3295 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003296
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003297 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballman52c9ad22019-02-12 13:04:11 +00003298 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003299 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003300 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003301 return;
3302 }
Erich Keane6a24e802019-09-13 17:39:31 +00003303 D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003304}
3305
Erich Keane6a24e802019-09-13 17:39:31 +00003306FormatAttr *Sema::mergeFormatAttr(Decl *D, const AttributeCommonInfo &CI,
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003307 IdentifierInfo *Format, int FormatIdx,
Erich Keane6a24e802019-09-13 17:39:31 +00003308 int FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003309 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003310 for (auto *F : D->specific_attrs<FormatAttr>()) {
3311 if (F->getType() == Format &&
3312 F->getFormatIdx() == FormatIdx &&
3313 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003314 // If we don't have a valid location for this attribute, adopt the
3315 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003316 if (F->getLocation().isInvalid())
Erich Keane6a24e802019-09-13 17:39:31 +00003317 F->setRange(CI.getRange());
Craig Topperc3ec1492014-05-26 06:22:03 +00003318 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003319 }
3320 }
3321
Erich Keane6a24e802019-09-13 17:39:31 +00003322 return ::new (Context) FormatAttr(Context, CI, Format, FormatIdx, FirstArg);
Rafael Espindola92d49452012-05-11 00:36:07 +00003323}
3324
Mike Stumpd3bb5572009-07-24 19:02:52 +00003325/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003326/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003327static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003328 if (!AL.isArgIdent(0)) {
3329 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003330 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003331 return;
3332 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003333
Chandler Carruth743682b2010-11-16 08:35:43 +00003334 // In C++ the implicit 'this' function parameter also counts, and they are
3335 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003336 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003337 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003338
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003339 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003340 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003341
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003342 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003343 // If we've modified the string name, we need a new identifier for it.
3344 II = &S.Context.Idents.get(Format);
3345 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003346
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003347 // Check for supported formats.
3348 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003349
Chris Lattner12161d32010-03-22 21:08:50 +00003350 if (Kind == IgnoredFormat)
3351 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003352
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003353 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003354 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003355 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003356 return;
3357 }
3358
3359 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003360 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003361 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003362 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003363 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003364
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003365 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003366 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003367 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003368 return;
3369 }
3370
3371 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003372 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003373
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003374 if (HasImplicitThisParam) {
3375 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003376 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003377 diag::err_format_attribute_implicit_this_format_string)
3378 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003379 return;
3380 }
3381 ArgIdx--;
3382 }
Mike Stump11289f42009-09-09 15:08:12 +00003383
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003384 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003385 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003386
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003387 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003388 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003389 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003390 << "a CFString" << IdxExpr->getSourceRange()
3391 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003392 return;
3393 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003394 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003395 // FIXME: do we need to check if the type is NSString*? What are the
3396 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003397 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003398 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003399 << "an NSString" << IdxExpr->getSourceRange()
3400 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003401 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003402 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003403 } else if (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003404 !Ty->castAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003405 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003406 << "a string type" << IdxExpr->getSourceRange()
3407 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003408 return;
3409 }
3410
3411 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003412 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003413 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003414 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003415 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003416
3417 // check if the function is variadic if the 3rd argument non-zero
3418 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003419 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003420 ++NumArgs; // +1 for ...
3421 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003422 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003423 return;
3424 }
3425 }
3426
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003427 // strftime requires FirstArg to be 0 because it doesn't read from any
3428 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003429 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003430 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003431 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003432 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003433 return;
3434 }
3435 // if 0 it disables parameter checking (to use with e.g. va_list)
3436 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003437 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003438 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003439 return;
3440 }
3441
Erich Keane6a24e802019-09-13 17:39:31 +00003442 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL, II, Idx, FirstArg);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003443 if (NewAttr)
3444 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003445}
3446
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003447/// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
3448static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3449 // The index that identifies the callback callee is mandatory.
3450 if (AL.getNumArgs() == 0) {
3451 S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee)
3452 << AL.getRange();
3453 return;
3454 }
3455
3456 bool HasImplicitThisParam = isInstanceMethod(D);
3457 int32_t NumArgs = getFunctionOrMethodNumParams(D);
3458
3459 FunctionDecl *FD = D->getAsFunction();
3460 assert(FD && "Expected a function declaration!");
3461
3462 llvm::StringMap<int> NameIdxMapping;
3463 NameIdxMapping["__"] = -1;
3464
3465 NameIdxMapping["this"] = 0;
3466
3467 int Idx = 1;
3468 for (const ParmVarDecl *PVD : FD->parameters())
3469 NameIdxMapping[PVD->getName()] = Idx++;
3470
3471 auto UnknownName = NameIdxMapping.end();
3472
3473 SmallVector<int, 8> EncodingIndices;
3474 for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) {
3475 SourceRange SR;
3476 int32_t ArgIdx;
3477
3478 if (AL.isArgIdent(I)) {
3479 IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
3480 auto It = NameIdxMapping.find(IdLoc->Ident->getName());
3481 if (It == UnknownName) {
3482 S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown)
3483 << IdLoc->Ident << IdLoc->Loc;
3484 return;
3485 }
3486
3487 SR = SourceRange(IdLoc->Loc);
3488 ArgIdx = It->second;
3489 } else if (AL.isArgExpr(I)) {
3490 Expr *IdxExpr = AL.getArgAsExpr(I);
3491
3492 // If the expression is not parseable as an int32_t we have a problem.
3493 if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1,
3494 false)) {
3495 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3496 << AL << (I + 1) << IdxExpr->getSourceRange();
3497 return;
3498 }
3499
3500 // Check oob, excluding the special values, 0 and -1.
3501 if (ArgIdx < -1 || ArgIdx > NumArgs) {
3502 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3503 << AL << (I + 1) << IdxExpr->getSourceRange();
3504 return;
3505 }
3506
3507 SR = IdxExpr->getSourceRange();
3508 } else {
3509 llvm_unreachable("Unexpected ParsedAttr argument type!");
3510 }
3511
3512 if (ArgIdx == 0 && !HasImplicitThisParam) {
3513 S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
3514 << (I + 1) << SR;
3515 return;
3516 }
3517
3518 // Adjust for the case we do not have an implicit "this" parameter. In this
3519 // case we decrease all positive values by 1 to get LLVM argument indices.
3520 if (!HasImplicitThisParam && ArgIdx > 0)
3521 ArgIdx -= 1;
3522
3523 EncodingIndices.push_back(ArgIdx);
3524 }
3525
3526 int CalleeIdx = EncodingIndices.front();
3527 // Check if the callee index is proper, thus not "this" and not "unknown".
Johannes Doerferte068d052019-01-21 14:23:46 +00003528 // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam"
3529 // is false and positive if "HasImplicitThisParam" is true.
3530 if (CalleeIdx < (int)HasImplicitThisParam) {
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003531 S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee)
3532 << AL.getRange();
3533 return;
3534 }
3535
3536 // Get the callee type, note the index adjustment as the AST doesn't contain
3537 // the this type (which the callee cannot reference anyway!).
3538 const Type *CalleeType =
3539 getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam)
3540 .getTypePtr();
3541 if (!CalleeType || !CalleeType->isFunctionPointerType()) {
3542 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3543 << AL.getRange();
3544 return;
3545 }
3546
3547 const Type *CalleeFnType =
3548 CalleeType->getPointeeType()->getUnqualifiedDesugaredType();
3549
3550 // TODO: Check the type of the callee arguments.
3551
3552 const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
3553 if (!CalleeFnProtoType) {
3554 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3555 << AL.getRange();
3556 return;
3557 }
3558
3559 if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
3560 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3561 << AL << (unsigned)(EncodingIndices.size() - 1);
3562 return;
3563 }
3564
3565 if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
3566 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3567 << AL << (unsigned)(EncodingIndices.size() - 1);
3568 return;
3569 }
3570
3571 if (CalleeFnProtoType->isVariadic()) {
3572 S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
3573 return;
3574 }
3575
3576 // Do not allow multiple callback attributes.
3577 if (D->hasAttr<CallbackAttr>()) {
3578 S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
3579 return;
3580 }
3581
3582 D->addAttr(::new (S.Context) CallbackAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00003583 S.Context, AL, EncodingIndices.data(), EncodingIndices.size()));
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003584}
3585
Erich Keanee891aa92018-07-13 15:07:47 +00003586static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003587 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003588 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003589 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003590 if (TD && TD->getUnderlyingType()->isUnionType())
3591 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3592 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003593 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003594
3595 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003596 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3597 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003598 return;
3599 }
3600
John McCallf937c022011-10-07 06:10:15 +00003601 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003602 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003603 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003604 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003605 return;
3606 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003607
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003608 RecordDecl::field_iterator Field = RD->field_begin(),
3609 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003610 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003611 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003612 return;
3613 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003614
David Blaikie40ed2972012-06-06 20:45:41 +00003615 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003616 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003617 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003618 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003619 diag::warn_transparent_union_attribute_floating)
3620 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003621 return;
3622 }
3623
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003624 if (FirstType->isIncompleteType())
3625 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003626 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3627 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3628 for (; Field != FieldEnd; ++Field) {
3629 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003630 if (FieldType->isIncompleteType())
3631 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003632 // FIXME: this isn't fully correct; we also need to test whether the
3633 // members of the union would all have the same calling convention as the
3634 // first member of the union. Checking just the size and alignment isn't
3635 // sufficient (consider structs passed on the stack instead of in registers
3636 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003637 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003638 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003639 // Warn if we drop the attribute.
3640 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003641 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003642 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003643 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003644 diag::warn_transparent_union_attribute_field_size_align)
3645 << isSize << Field->getDeclName() << FieldBits;
3646 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003647 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003648 diag::note_transparent_union_first_field_size_align)
3649 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003650 return;
3651 }
3652 }
3653
Erich Keane6a24e802019-09-13 17:39:31 +00003654 RD->addAttr(::new (S.Context) TransparentUnionAttr(S.Context, AL));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003655}
3656
Erich Keanee891aa92018-07-13 15:07:47 +00003657static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003658 // Make sure that there is a string literal as the annotation's single
3659 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003660 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003661 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003662 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003663
3664 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003665 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3666 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003667 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003668 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003669
Erich Keane6a24e802019-09-13 17:39:31 +00003670 D->addAttr(::new (S.Context) AnnotateAttr(S.Context, AL, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003671}
3672
Erich Keanee891aa92018-07-13 15:07:47 +00003673static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00003674 S.AddAlignValueAttr(D, AL, AL.getArgAsExpr(0));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003675}
3676
Erich Keane6a24e802019-09-13 17:39:31 +00003677void Sema::AddAlignValueAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E) {
3678 AlignValueAttr TmpAttr(Context, CI, E);
3679 SourceLocation AttrLoc = CI.getLoc();
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003680
3681 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003682 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003683 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003684 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003685 T = VD->getType();
3686 else
3687 llvm_unreachable("Unknown decl type for align_value");
3688
3689 if (!T->isDependentType() && !T->isAnyPointerType() &&
3690 !T->isReferenceType() && !T->isMemberPointerType()) {
3691 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
Aaron Ballmandab43c82020-03-14 17:00:45 -04003692 << &TmpAttr << T << D->getSourceRange();
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003693 return;
3694 }
3695
3696 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003697 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003698 ExprResult ICE
3699 = VerifyIntegerConstantExpression(E, &Alignment,
3700 diag::err_align_value_attribute_argument_not_int,
3701 /*AllowFold*/ false);
3702 if (ICE.isInvalid())
3703 return;
3704
3705 if (!Alignment.isPowerOf2()) {
3706 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3707 << E->getSourceRange();
3708 return;
3709 }
3710
Erich Keane6a24e802019-09-13 17:39:31 +00003711 D->addAttr(::new (Context) AlignValueAttr(Context, CI, ICE.get()));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003712 return;
3713 }
3714
3715 // Save dependent expressions in the AST to be instantiated.
Erich Keane6a24e802019-09-13 17:39:31 +00003716 D->addAttr(::new (Context) AlignValueAttr(Context, CI, E));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003717}
3718
Erich Keanee891aa92018-07-13 15:07:47 +00003719static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003720 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003721 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003722 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003723 return;
3724 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003725
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003726 if (AL.getNumArgs() == 0) {
Erich Keane6a24e802019-09-13 17:39:31 +00003727 D->addAttr(::new (S.Context) AlignedAttr(S.Context, AL, true, nullptr));
Richard Smith848e1f12013-02-01 08:12:08 +00003728 return;
3729 }
3730
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003731 Expr *E = AL.getArgAsExpr(0);
3732 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3733 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003734 diag::err_pack_expansion_without_parameter_packs);
3735 return;
3736 }
3737
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003738 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003739 return;
3740
Erich Keane6a24e802019-09-13 17:39:31 +00003741 S.AddAlignedAttr(D, AL, E, AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003742}
3743
Erich Keane6a24e802019-09-13 17:39:31 +00003744void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
3745 bool IsPackExpansion) {
3746 AlignedAttr TmpAttr(Context, CI, true, E);
3747 SourceLocation AttrLoc = CI.getLoc();
Richard Smith848e1f12013-02-01 08:12:08 +00003748
Richard Smith1dba27c2013-01-29 09:02:09 +00003749 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003750 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003751 // C++11 [dcl.align]p1:
3752 // An alignment-specifier may be applied to a variable or to a class
3753 // data member, but it shall not be applied to a bit-field, a function
3754 // parameter, the formal parameter of a catch clause, or a variable
3755 // declared with the register storage class specifier. An
3756 // alignment-specifier may also be applied to the declaration of a class
3757 // or enumeration type.
3758 // C11 6.7.5/2:
3759 // An alignment attribute shall not be specified in a declaration of
3760 // a typedef, or a bit-field, or a function, or a parameter, or an
3761 // object declared with the register storage-class specifier.
3762 int DiagKind = -1;
3763 if (isa<ParmVarDecl>(D)) {
3764 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003765 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003766 if (VD->getStorageClass() == SC_Register)
3767 DiagKind = 1;
3768 if (VD->isExceptionVariable())
3769 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003770 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003771 if (FD->isBitField())
3772 DiagKind = 3;
3773 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003774 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003775 << (TmpAttr.isC11() ? ExpectedVariableOrField
3776 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003777 return;
3778 }
3779 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003780 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003781 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003782 return;
3783 }
3784 }
3785
Richard Smith90ae9672018-06-20 23:36:55 +00003786 if (E->isValueDependent()) {
3787 // We can't support a dependent alignment on a non-dependent type,
3788 // because we have no way to model that a type is "alignment-dependent"
3789 // but not dependent in any other way.
3790 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3791 if (!TND->getUnderlyingType()->isDependentType()) {
3792 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3793 << E->getSourceRange();
3794 return;
3795 }
3796 }
3797
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003798 // Save dependent expressions in the AST to be instantiated.
Erich Keane6a24e802019-09-13 17:39:31 +00003799 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, E);
Richard Smith44c247f2013-02-22 08:32:16 +00003800 AA->setPackExpansion(IsPackExpansion);
3801 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003802 return;
3803 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003804
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003805 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003806 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003807 ExprResult ICE
3808 = VerifyIntegerConstantExpression(E, &Alignment,
3809 diag::err_aligned_attribute_argument_not_int,
3810 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003811 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003812 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003813
David Majnemer0be6bd02015-07-26 09:02:21 +00003814 uint64_t AlignVal = Alignment.getZExtValue();
3815
Richard Smith848e1f12013-02-01 08:12:08 +00003816 // C++11 [dcl.align]p2:
3817 // -- if the constant expression evaluates to zero, the alignment
3818 // specifier shall have no effect
3819 // C11 6.7.5p6:
3820 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003821 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003822 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003823 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3824 << E->getSourceRange();
3825 return;
3826 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003827 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003828
Roman Lebedev1d0972f2020-01-24 17:01:27 +03003829 unsigned MaximumAlignment = Sema::MaximumAlignment;
3830 if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
3831 MaximumAlignment = std::min(MaximumAlignment, 8192u);
3832 if (AlignVal > MaximumAlignment) {
Roman Lebedevd096f8d2020-01-23 22:50:06 +03003833 Diag(AttrLoc, diag::err_attribute_aligned_too_great)
Roman Lebedev1d0972f2020-01-24 17:01:27 +03003834 << MaximumAlignment << E->getSourceRange();
David Majnemerabecae72014-02-12 20:36:10 +00003835 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003836 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003837
David Majnemer0be6bd02015-07-26 09:02:21 +00003838 if (Context.getTargetInfo().isTLSSupported()) {
3839 unsigned MaxTLSAlign =
3840 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3841 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003842 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003843 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3844 VD->getTLSKind() != VarDecl::TLS_None) {
3845 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3846 << (unsigned)AlignVal << VD << MaxTLSAlign;
3847 return;
3848 }
3849 }
3850
Erich Keane6a24e802019-09-13 17:39:31 +00003851 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, ICE.get());
Richard Smith44c247f2013-02-22 08:32:16 +00003852 AA->setPackExpansion(IsPackExpansion);
3853 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003854}
3855
Erich Keane6a24e802019-09-13 17:39:31 +00003856void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI,
3857 TypeSourceInfo *TS, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003858 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003859 // FIXME: Perform checking of type validity
Erich Keane6a24e802019-09-13 17:39:31 +00003860 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, false, TS);
Richard Smith44c247f2013-02-22 08:32:16 +00003861 AA->setPackExpansion(IsPackExpansion);
3862 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003863}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003864
Richard Smith848e1f12013-02-01 08:12:08 +00003865void Sema::CheckAlignasUnderalignment(Decl *D) {
3866 assert(D->hasAttrs() && "no attributes on decl");
3867
David Majnemer475b25e2015-01-21 10:54:38 +00003868 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003869 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003870 UnderlyingTy = DiagTy = VD->getType();
3871 } else {
3872 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003873 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003874 UnderlyingTy = ED->getIntegerType();
3875 }
3876 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003877 return;
3878
3879 // C++11 [dcl.align]p5, C11 6.7.5/4:
3880 // The combined effect of all alignment attributes in a declaration shall
3881 // not specify an alignment that is less strict than the alignment that
3882 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003883 AlignedAttr *AlignasAttr = nullptr;
Richard Sandiford627b5c12020-03-02 17:37:58 +00003884 AlignedAttr *LastAlignedAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003885 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003886 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003887 if (I->isAlignmentDependent())
3888 return;
3889 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003890 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003891 Align = std::max(Align, I->getAlignment(Context));
Richard Sandiford627b5c12020-03-02 17:37:58 +00003892 LastAlignedAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003893 }
3894
Richard Sandiford627b5c12020-03-02 17:37:58 +00003895 if (Align && DiagTy->isSizelessType()) {
3896 Diag(LastAlignedAttr->getLocation(), diag::err_attribute_sizeless_type)
3897 << LastAlignedAttr << DiagTy;
3898 } else if (AlignasAttr && Align) {
Richard Smith848e1f12013-02-01 08:12:08 +00003899 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003900 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003901 if (NaturalAlign > RequestedAlign)
3902 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003903 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003904 }
3905}
3906
David Majnemer2c4e00a2014-01-29 22:07:36 +00003907bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003908 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
Reid Klecknera9cc64e2019-11-15 18:49:32 -08003909 MSInheritanceModel ExplicitModel) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00003910 assert(RD->hasDefinition() && "RD has no definition!");
3911
David Majnemer98c9ee22014-02-07 00:43:07 +00003912 // We may not have seen base specifiers or any virtual methods yet. We will
3913 // have to wait until the record is defined to catch any mismatches.
3914 if (!RD->getDefinition()->isCompleteDefinition())
3915 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003916
David Majnemer98c9ee22014-02-07 00:43:07 +00003917 // The unspecified model never matches what a definition could need.
Reid Klecknera9cc64e2019-11-15 18:49:32 -08003918 if (ExplicitModel == MSInheritanceModel::Unspecified)
David Majnemer98c9ee22014-02-07 00:43:07 +00003919 return false;
3920
David Majnemer4bb09802014-02-10 19:50:15 +00003921 if (BestCase) {
Reid Klecknera9cc64e2019-11-15 18:49:32 -08003922 if (RD->calculateInheritanceModel() == ExplicitModel)
David Majnemer4bb09802014-02-10 19:50:15 +00003923 return false;
3924 } else {
Reid Klecknera9cc64e2019-11-15 18:49:32 -08003925 if (RD->calculateInheritanceModel() <= ExplicitModel)
David Majnemer4bb09802014-02-10 19:50:15 +00003926 return false;
3927 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003928
3929 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3930 << 0 /*definition*/;
Aaron Ballmandab43c82020-03-14 17:00:45 -04003931 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) << RD;
David Majnemer98c9ee22014-02-07 00:43:07 +00003932 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003933}
3934
Alexey Bataevf278eb12015-11-19 10:13:11 +00003935/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3936/// attribute.
3937static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3938 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003939 IntegerMode = true;
3940 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003941 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003942 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003943 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003944 case 'Q':
3945 DestWidth = 8;
3946 break;
3947 case 'H':
3948 DestWidth = 16;
3949 break;
3950 case 'S':
3951 DestWidth = 32;
3952 break;
3953 case 'D':
3954 DestWidth = 64;
3955 break;
3956 case 'X':
3957 DestWidth = 96;
3958 break;
3959 case 'T':
3960 DestWidth = 128;
3961 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003962 }
3963 if (Str[1] == 'F') {
3964 IntegerMode = false;
3965 } else if (Str[1] == 'C') {
3966 IntegerMode = false;
3967 ComplexMode = true;
3968 } else if (Str[1] != 'I') {
3969 DestWidth = 0;
3970 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003971 break;
3972 case 4:
3973 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3974 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003975 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003976 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003977 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003978 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003979 break;
3980 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003981 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003982 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003983 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003984 case 11:
3985 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003986 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003987 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003988 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003989}
3990
3991/// handleModeAttr - This attribute modifies the width of a decl with primitive
3992/// type.
3993///
3994/// Despite what would be logical, the mode attribute is a decl attribute, not a
3995/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3996/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00003997static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003998 // This attribute isn't documented, but glibc uses it. It changes
3999 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004000 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004001 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
4002 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004003 return;
4004 }
4005
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004006 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004007
Erich Keane6a24e802019-09-13 17:39:31 +00004008 S.AddModeAttr(D, AL, Name);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004009}
4010
Erich Keane6a24e802019-09-13 17:39:31 +00004011void Sema::AddModeAttr(Decl *D, const AttributeCommonInfo &CI,
4012 IdentifierInfo *Name, bool InInstantiation) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004013 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00004014 normalizeName(Str);
Erich Keane6a24e802019-09-13 17:39:31 +00004015 SourceLocation AttrLoc = CI.getLoc();
Alexey Bataevf278eb12015-11-19 10:13:11 +00004016
4017 unsigned DestWidth = 0;
4018 bool IntegerMode = true;
4019 bool ComplexMode = false;
4020 llvm::APInt VectorSize(64, 0);
4021 if (Str.size() >= 4 && Str[0] == 'V') {
4022 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
4023 size_t StrSize = Str.size();
4024 size_t VectorStringLength = 0;
4025 while ((VectorStringLength + 1) < StrSize &&
4026 isdigit(Str[VectorStringLength + 1]))
4027 ++VectorStringLength;
4028 if (VectorStringLength &&
4029 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
4030 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004031 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00004032 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004033 // Avoid duplicate warning from template instantiation.
4034 if (!InInstantiation)
4035 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00004036 } else {
4037 VectorSize = 0;
4038 }
4039 }
4040
4041 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004042 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
4043
4044 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
4045 // and friends, at least with glibc.
4046 // FIXME: Make sure floating-point mappings are accurate
4047 // FIXME: Support XF and TF types
4048 if (!DestWidth) {
4049 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
4050 return;
4051 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00004052
4053 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004054 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00004055 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004056 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004057 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
4058 // Try to get type from enum declaration, default to int.
4059 OldTy = ED->getIntegerType();
4060 if (OldTy.isNull())
4061 OldTy = Context.IntTy;
4062 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004063 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00004064
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004065 if (OldTy->isDependentType()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004066 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004067 return;
4068 }
4069
Alexey Bataev326057d2015-06-19 07:46:21 +00004070 // Base type can also be a vector type (see PR17453).
4071 // Distinguish between base type and base element type.
4072 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004073 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00004074 OldElemTy = VT->getElementType();
4075
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004076 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
4077 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
4078 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
4079 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
4080 VectorSize.getBoolValue()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004081 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << CI.getRange();
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004082 return;
4083 }
4084 bool IntegralOrAnyEnumType =
4085 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
4086
4087 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
4088 !IntegralOrAnyEnumType)
4089 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00004090 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004091 if (!IntegralOrAnyEnumType)
4092 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004093 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004094 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004095 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004096 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00004097 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004098 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004099 }
4100
Alexey Bataev326057d2015-06-19 07:46:21 +00004101 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004102
4103 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004104 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
4105 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004106 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004107 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004108
Alexey Bataev326057d2015-06-19 07:46:21 +00004109 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004110 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004111 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004112 }
4113
Eli Friedman4735374e2009-03-03 06:41:03 +00004114 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004115 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004116 }
4117
4118 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004119 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004120 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
4121 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004122 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004123 // Complex machine mode does not support base vector types.
4124 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004125 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004126 return;
4127 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004128 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00004129 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004130 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004131 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004132 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00004133 }
4134
4135 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004136 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004137 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004138 }
4139
4140 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004141 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004142 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004143 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004144 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004145 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004146 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004147
Erich Keane6a24e802019-09-13 17:39:31 +00004148 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
Chris Lattneracbc2d22008-06-27 22:18:37 +00004149}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004150
Erich Keanee891aa92018-07-13 15:07:47 +00004151static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004152 D->addAttr(::new (S.Context) NoDebugAttr(S.Context, AL));
Anders Carlsson76187b42009-02-13 06:46:13 +00004153}
4154
Erich Keane6a24e802019-09-13 17:39:31 +00004155AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D,
4156 const AttributeCommonInfo &CI,
4157 const IdentifierInfo *Ident) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004158 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004159 Diag(CI.getLoc(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00004160 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4161 return nullptr;
4162 }
4163
4164 if (D->hasAttr<AlwaysInlineAttr>())
4165 return nullptr;
4166
Erich Keane6a24e802019-09-13 17:39:31 +00004167 return ::new (Context) AlwaysInlineAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004168}
4169
Erich Keane44bacdf2018-08-09 13:21:32 +00004170CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
4171 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004172 return nullptr;
4173
Erich Keane6a24e802019-09-13 17:39:31 +00004174 return ::new (Context) CommonAttr(Context, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004175}
4176
Erich Keane44bacdf2018-08-09 13:21:32 +00004177CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
4178 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
4179 return nullptr;
4180
Erich Keane6a24e802019-09-13 17:39:31 +00004181 return ::new (Context) CommonAttr(Context, AL);
Erich Keane44bacdf2018-08-09 13:21:32 +00004182}
4183
4184InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4185 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004186 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004187 // Attribute applies to Var but not any subclass of it (like ParmVar,
4188 // ImplicitParm or VarTemplateSpecialization).
4189 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004190 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4191 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4192 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004193 return nullptr;
4194 }
4195 // Attribute does not apply to non-static local variables.
4196 if (VD->hasLocalStorage()) {
4197 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4198 return nullptr;
4199 }
4200 }
4201
Erich Keane44bacdf2018-08-09 13:21:32 +00004202 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
4203 return nullptr;
4204
Erich Keane6a24e802019-09-13 17:39:31 +00004205 return ::new (Context) InternalLinkageAttr(Context, AL);
Erich Keane44bacdf2018-08-09 13:21:32 +00004206}
4207InternalLinkageAttr *
4208Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4209 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4210 // Attribute applies to Var but not any subclass of it (like ParmVar,
4211 // ImplicitParm or VarTemplateSpecialization).
4212 if (VD->getKind() != Decl::Var) {
4213 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4214 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4215 : ExpectedVariableOrFunction);
4216 return nullptr;
4217 }
4218 // Attribute does not apply to non-static local variables.
4219 if (VD->hasLocalStorage()) {
4220 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4221 return nullptr;
4222 }
4223 }
4224
4225 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004226 return nullptr;
4227
Erich Keane6a24e802019-09-13 17:39:31 +00004228 return ::new (Context) InternalLinkageAttr(Context, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004229}
4230
Erich Keane6a24e802019-09-13 17:39:31 +00004231MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, const AttributeCommonInfo &CI) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004232 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004233 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'minsize'";
Paul Robinson30e41fb2014-12-15 18:57:28 +00004234 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4235 return nullptr;
4236 }
4237
4238 if (D->hasAttr<MinSizeAttr>())
4239 return nullptr;
4240
Erich Keane6a24e802019-09-13 17:39:31 +00004241 return ::new (Context) MinSizeAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004242}
4243
Zola Bridges826ef592019-01-18 17:20:46 +00004244NoSpeculativeLoadHardeningAttr *Sema::mergeNoSpeculativeLoadHardeningAttr(
4245 Decl *D, const NoSpeculativeLoadHardeningAttr &AL) {
4246 if (checkAttrMutualExclusion<SpeculativeLoadHardeningAttr>(*this, D, AL))
4247 return nullptr;
4248
Erich Keane6a24e802019-09-13 17:39:31 +00004249 return ::new (Context) NoSpeculativeLoadHardeningAttr(Context, AL);
Zola Bridges826ef592019-01-18 17:20:46 +00004250}
4251
Erich Keane6a24e802019-09-13 17:39:31 +00004252OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D,
4253 const AttributeCommonInfo &CI) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004254 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4255 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
Erich Keane6a24e802019-09-13 17:39:31 +00004256 Diag(CI.getLoc(), diag::note_conflicting_attribute);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004257 D->dropAttr<AlwaysInlineAttr>();
4258 }
4259 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4260 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
Erich Keane6a24e802019-09-13 17:39:31 +00004261 Diag(CI.getLoc(), diag::note_conflicting_attribute);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004262 D->dropAttr<MinSizeAttr>();
4263 }
4264
4265 if (D->hasAttr<OptimizeNoneAttr>())
4266 return nullptr;
4267
Erich Keane6a24e802019-09-13 17:39:31 +00004268 return ::new (Context) OptimizeNoneAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004269}
4270
Zola Bridges826ef592019-01-18 17:20:46 +00004271SpeculativeLoadHardeningAttr *Sema::mergeSpeculativeLoadHardeningAttr(
4272 Decl *D, const SpeculativeLoadHardeningAttr &AL) {
4273 if (checkAttrMutualExclusion<NoSpeculativeLoadHardeningAttr>(*this, D, AL))
4274 return nullptr;
4275
Erich Keane6a24e802019-09-13 17:39:31 +00004276 return ::new (Context) SpeculativeLoadHardeningAttr(Context, AL);
Zola Bridges826ef592019-01-18 17:20:46 +00004277}
4278
Erich Keanee891aa92018-07-13 15:07:47 +00004279static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004280 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004281 return;
4282
Erich Keane6a24e802019-09-13 17:39:31 +00004283 if (AlwaysInlineAttr *Inline =
4284 S.mergeAlwaysInlineAttr(D, AL, AL.getAttrName()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004285 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004286}
4287
Erich Keanee891aa92018-07-13 15:07:47 +00004288static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004289 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(D, AL))
Paul Robinson080b1f32015-01-13 18:34:56 +00004290 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004291}
4292
Erich Keanee891aa92018-07-13 15:07:47 +00004293static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004294 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(D, AL))
Paul Robinson080b1f32015-01-13 18:34:56 +00004295 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004296}
4297
Erich Keanee891aa92018-07-13 15:07:47 +00004298static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004299 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004300 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004301 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004302 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004303 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004304 return;
4305 }
Erich Keane6a24e802019-09-13 17:39:31 +00004306 D->addAttr(::new (S.Context) CUDAConstantAttr(S.Context, AL));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004307}
4308
Erich Keanee891aa92018-07-13 15:07:47 +00004309static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004310 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004311 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004312 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004313 // extern __shared__ is only allowed on arrays with no length (e.g.
4314 // "int x[]").
Yaxun Liu97670892018-10-02 17:48:54 +00004315 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004316 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004317 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004318 return;
4319 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004320 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004321 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004322 << S.CurrentCUDATarget())
4323 return;
Erich Keane6a24e802019-09-13 17:39:31 +00004324 D->addAttr(::new (S.Context) CUDASharedAttr(S.Context, AL));
Justin Lebar10411012016-09-30 23:57:30 +00004325}
4326
Erich Keanee891aa92018-07-13 15:07:47 +00004327static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004328 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4329 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004330 return;
4331 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004332 const auto *FD = cast<FunctionDecl>(D);
Michael Liao24337db2019-09-25 16:51:45 +00004333 if (!FD->getReturnType()->isVoidType() &&
4334 !FD->getReturnType()->getAs<AutoType>() &&
4335 !FD->getReturnType()->isInstantiationDependentType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004336 SourceRange RTRange = FD->getReturnTypeSourceRange();
4337 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004338 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004339 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4340 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004341 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004342 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004343 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4344 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004345 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004346 << Method;
4347 return;
4348 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004349 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004350 }
4351 // Only warn for "inline" when compiling for host, to cut down on noise.
4352 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004353 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004354
Erich Keane6a24e802019-09-13 17:39:31 +00004355 D->addAttr(::new (S.Context) CUDAGlobalAttr(S.Context, AL));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004356}
4357
Erich Keanee891aa92018-07-13 15:07:47 +00004358static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004359 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004360 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004361 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004362 return;
4363 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004364
Martin Storsjo71decf82019-09-27 12:25:19 +00004365 if (S.LangOpts.CPlusPlus && Fn->getStorageClass() != SC_Extern)
4366 S.Diag(AL.getLoc(), diag::warn_gnu_inline_cplusplus_without_extern);
4367
Erich Keane6a24e802019-09-13 17:39:31 +00004368 D->addAttr(::new (S.Context) GNUInlineAttr(S.Context, AL));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004369}
4370
Erich Keanee891aa92018-07-13 15:07:47 +00004371static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004372 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004373
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004374 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004375 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4376 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004377 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004378 return;
4379
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004380 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004381 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004382 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004383 return;
4384 }
4385
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004386 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004387 case ParsedAttr::AT_FastCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004388 D->addAttr(::new (S.Context) FastCallAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004389 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004390 case ParsedAttr::AT_StdCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004391 D->addAttr(::new (S.Context) StdCallAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004392 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004393 case ParsedAttr::AT_ThisCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004394 D->addAttr(::new (S.Context) ThisCallAttr(S.Context, AL));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004395 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004396 case ParsedAttr::AT_CDecl:
Erich Keane6a24e802019-09-13 17:39:31 +00004397 D->addAttr(::new (S.Context) CDeclAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004398 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004399 case ParsedAttr::AT_Pascal:
Erich Keane6a24e802019-09-13 17:39:31 +00004400 D->addAttr(::new (S.Context) PascalAttr(S.Context, AL));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004401 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004402 case ParsedAttr::AT_SwiftCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004403 D->addAttr(::new (S.Context) SwiftCallAttr(S.Context, AL));
John McCall477f2bb2016-03-03 06:39:32 +00004404 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004405 case ParsedAttr::AT_VectorCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004406 D->addAttr(::new (S.Context) VectorCallAttr(S.Context, AL));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004407 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004408 case ParsedAttr::AT_MSABI:
Erich Keane6a24e802019-09-13 17:39:31 +00004409 D->addAttr(::new (S.Context) MSABIAttr(S.Context, AL));
Charles Davisb5a214e2013-08-30 04:39:01 +00004410 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004411 case ParsedAttr::AT_SysVABI:
Erich Keane6a24e802019-09-13 17:39:31 +00004412 D->addAttr(::new (S.Context) SysVABIAttr(S.Context, AL));
Charles Davisb5a214e2013-08-30 04:39:01 +00004413 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004414 case ParsedAttr::AT_RegCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004415 D->addAttr(::new (S.Context) RegCallAttr(S.Context, AL));
Erich Keane757d3172016-11-02 18:29:35 +00004416 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004417 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004418 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004419 switch (CC) {
4420 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004421 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004422 break;
4423 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004424 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004425 break;
4426 default:
4427 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004428 }
4429
Erich Keane6a24e802019-09-13 17:39:31 +00004430 D->addAttr(::new (S.Context) PcsAttr(S.Context, AL, PCS));
Derek Schuffa2020962012-10-16 22:30:41 +00004431 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004432 }
Sander de Smalen44a22532018-11-26 16:38:37 +00004433 case ParsedAttr::AT_AArch64VectorPcs:
Erich Keane6a24e802019-09-13 17:39:31 +00004434 D->addAttr(::new (S.Context) AArch64VectorPcsAttr(S.Context, AL));
Sander de Smalen44a22532018-11-26 16:38:37 +00004435 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004436 case ParsedAttr::AT_IntelOclBicc:
Erich Keane6a24e802019-09-13 17:39:31 +00004437 D->addAttr(::new (S.Context) IntelOclBiccAttr(S.Context, AL));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004438 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004439 case ParsedAttr::AT_PreserveMost:
Erich Keane6a24e802019-09-13 17:39:31 +00004440 D->addAttr(::new (S.Context) PreserveMostAttr(S.Context, AL));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004441 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004442 case ParsedAttr::AT_PreserveAll:
Erich Keane6a24e802019-09-13 17:39:31 +00004443 D->addAttr(::new (S.Context) PreserveAllAttr(S.Context, AL));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004444 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004445 default:
4446 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004447 }
4448}
4449
Erich Keanee891aa92018-07-13 15:07:47 +00004450static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004451 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004452 return;
4453
4454 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004455 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004456 StringRef RuleName;
4457
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004458 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004459 return;
4460
4461 // FIXME: Warn if the rule name is unknown. This is tricky because only
4462 // clang-tidy knows about available rules.
4463 DiagnosticIdentifiers.push_back(RuleName);
4464 }
Erich Keane6a24e802019-09-13 17:39:31 +00004465 D->addAttr(::new (S.Context)
4466 SuppressAttr(S.Context, AL, DiagnosticIdentifiers.data(),
4467 DiagnosticIdentifiers.size()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004468}
4469
Matthias Gehred293cbd2019-07-25 17:50:51 +00004470static void handleLifetimeCategoryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4471 TypeSourceInfo *DerefTypeLoc = nullptr;
4472 QualType ParmType;
4473 if (AL.hasParsedType()) {
4474 ParmType = S.GetTypeFromParser(AL.getTypeArg(), &DerefTypeLoc);
4475
4476 unsigned SelectIdx = ~0U;
Gabor Horvath247a6032020-01-02 11:57:42 -08004477 if (ParmType->isReferenceType())
Matthias Gehred293cbd2019-07-25 17:50:51 +00004478 SelectIdx = 0;
Matthias Gehred293cbd2019-07-25 17:50:51 +00004479 else if (ParmType->isArrayType())
Gabor Horvath247a6032020-01-02 11:57:42 -08004480 SelectIdx = 1;
Matthias Gehred293cbd2019-07-25 17:50:51 +00004481
4482 if (SelectIdx != ~0U) {
4483 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument)
4484 << SelectIdx << AL;
4485 return;
4486 }
4487 }
4488
4489 // To check if earlier decl attributes do not conflict the newly parsed ones
4490 // we always add (and check) the attribute to the cannonical decl.
4491 D = D->getCanonicalDecl();
4492 if (AL.getKind() == ParsedAttr::AT_Owner) {
4493 if (checkAttrMutualExclusion<PointerAttr>(S, D, AL))
4494 return;
4495 if (const auto *OAttr = D->getAttr<OwnerAttr>()) {
4496 const Type *ExistingDerefType = OAttr->getDerefTypeLoc()
4497 ? OAttr->getDerefType().getTypePtr()
4498 : nullptr;
4499 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
4500 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
4501 << AL << OAttr;
4502 S.Diag(OAttr->getLocation(), diag::note_conflicting_attribute);
4503 }
4504 return;
4505 }
Matthias Gehref64f4882019-09-06 08:56:30 +00004506 for (Decl *Redecl : D->redecls()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004507 Redecl->addAttr(::new (S.Context) OwnerAttr(S.Context, AL, DerefTypeLoc));
Matthias Gehref64f4882019-09-06 08:56:30 +00004508 }
Matthias Gehred293cbd2019-07-25 17:50:51 +00004509 } else {
4510 if (checkAttrMutualExclusion<OwnerAttr>(S, D, AL))
4511 return;
4512 if (const auto *PAttr = D->getAttr<PointerAttr>()) {
4513 const Type *ExistingDerefType = PAttr->getDerefTypeLoc()
4514 ? PAttr->getDerefType().getTypePtr()
4515 : nullptr;
4516 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
4517 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
4518 << AL << PAttr;
4519 S.Diag(PAttr->getLocation(), diag::note_conflicting_attribute);
4520 }
4521 return;
4522 }
Matthias Gehref64f4882019-09-06 08:56:30 +00004523 for (Decl *Redecl : D->redecls()) {
4524 Redecl->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00004525 PointerAttr(S.Context, AL, DerefTypeLoc));
Matthias Gehref64f4882019-09-06 08:56:30 +00004526 }
Matthias Gehred293cbd2019-07-25 17:50:51 +00004527 }
4528}
4529
Erich Keanee891aa92018-07-13 15:07:47 +00004530bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004531 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004532 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004533 return true;
4534
Erich Keaneb11ebc52017-09-27 03:20:13 +00004535 if (Attrs.hasProcessingCache()) {
4536 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004537 return false;
4538 }
4539
Erich Keanee891aa92018-07-13 15:07:47 +00004540 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004541 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4542 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004543 return true;
4544 }
4545
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004546 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004547 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004548 case ParsedAttr::AT_CDecl:
4549 CC = CC_C;
4550 break;
4551 case ParsedAttr::AT_FastCall:
4552 CC = CC_X86FastCall;
4553 break;
4554 case ParsedAttr::AT_StdCall:
4555 CC = CC_X86StdCall;
4556 break;
4557 case ParsedAttr::AT_ThisCall:
4558 CC = CC_X86ThisCall;
4559 break;
4560 case ParsedAttr::AT_Pascal:
4561 CC = CC_X86Pascal;
4562 break;
4563 case ParsedAttr::AT_SwiftCall:
4564 CC = CC_Swift;
4565 break;
4566 case ParsedAttr::AT_VectorCall:
4567 CC = CC_X86VectorCall;
4568 break;
Sander de Smalen44a22532018-11-26 16:38:37 +00004569 case ParsedAttr::AT_AArch64VectorPcs:
4570 CC = CC_AArch64VectorCall;
4571 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004572 case ParsedAttr::AT_RegCall:
4573 CC = CC_X86RegCall;
4574 break;
4575 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004576 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004577 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004578 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004579 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004580 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4581 CC_C;
4582 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004583 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004584 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004585 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4586 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004587 return true;
4588 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004589 if (StrRef == "aapcs") {
4590 CC = CC_AAPCS;
4591 break;
4592 } else if (StrRef == "aapcs-vfp") {
4593 CC = CC_AAPCS_VFP;
4594 break;
4595 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004596
Erich Keaneb11ebc52017-09-27 03:20:13 +00004597 Attrs.setInvalid();
4598 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004599 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004600 }
Erich Keanee891aa92018-07-13 15:07:47 +00004601 case ParsedAttr::AT_IntelOclBicc:
4602 CC = CC_IntelOclBicc;
4603 break;
4604 case ParsedAttr::AT_PreserveMost:
4605 CC = CC_PreserveMost;
4606 break;
4607 case ParsedAttr::AT_PreserveAll:
4608 CC = CC_PreserveAll;
4609 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004610 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004611 }
4612
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004613 TargetInfo::CallingConvCheckResult A = TargetInfo::CCCR_OK;
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004614 const TargetInfo &TI = Context.getTargetInfo();
Yaxun Liu785cbd82019-02-27 15:46:29 +00004615 // CUDA functions may have host and/or device attributes which indicate
4616 // their targeted execution environment, therefore the calling convention
4617 // of functions in CUDA should be checked against the target deduced based
4618 // on their host/device attributes.
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004619 if (LangOpts.CUDA) {
Yaxun Liu785cbd82019-02-27 15:46:29 +00004620 auto *Aux = Context.getAuxTargetInfo();
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004621 auto CudaTarget = IdentifyCUDATarget(FD);
4622 bool CheckHost = false, CheckDevice = false;
4623 switch (CudaTarget) {
4624 case CFT_HostDevice:
4625 CheckHost = true;
4626 CheckDevice = true;
4627 break;
4628 case CFT_Host:
4629 CheckHost = true;
4630 break;
4631 case CFT_Device:
4632 case CFT_Global:
4633 CheckDevice = true;
4634 break;
4635 case CFT_InvalidTarget:
4636 llvm_unreachable("unexpected cuda target");
4637 }
4638 auto *HostTI = LangOpts.CUDAIsDevice ? Aux : &TI;
4639 auto *DeviceTI = LangOpts.CUDAIsDevice ? &TI : Aux;
4640 if (CheckHost && HostTI)
4641 A = HostTI->checkCallingConvention(CC);
4642 if (A == TargetInfo::CCCR_OK && CheckDevice && DeviceTI)
4643 A = DeviceTI->checkCallingConvention(CC);
4644 } else {
4645 A = TI.checkCallingConvention(CC);
4646 }
Reid Kleckner4586a192019-07-09 23:17:43 +00004647
4648 switch (A) {
4649 case TargetInfo::CCCR_OK:
4650 break;
4651
4652 case TargetInfo::CCCR_Ignore:
4653 // Treat an ignored convention as if it was an explicit C calling convention
4654 // attribute. For example, __stdcall on Win x64 functions as __cdecl, so
4655 // that command line flags that change the default convention to
4656 // __vectorcall don't affect declarations marked __stdcall.
4657 CC = CC_C;
4658 break;
4659
Sunil Srivastavaf4038e72019-07-19 21:38:34 +00004660 case TargetInfo::CCCR_Error:
4661 Diag(Attrs.getLoc(), diag::error_cconv_unsupported)
4662 << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget;
4663 break;
4664
Reid Kleckner4586a192019-07-09 23:17:43 +00004665 case TargetInfo::CCCR_Warning: {
Sunil Srivastava85d667f2019-07-17 20:41:26 +00004666 Diag(Attrs.getLoc(), diag::warn_cconv_unsupported)
Reid Kleckner4586a192019-07-09 23:17:43 +00004667 << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004668
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004669 // This convention is not valid for the target. Use the default function or
4670 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004671 bool IsCXXMethod = false, IsVariadic = false;
4672 if (FD) {
4673 IsCXXMethod = FD->isCXXInstanceMember();
4674 IsVariadic = FD->isVariadic();
4675 }
4676 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Reid Kleckner4586a192019-07-09 23:17:43 +00004677 break;
4678 }
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004679 }
4680
Erich Keaneb11ebc52017-09-27 03:20:13 +00004681 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004682 return false;
4683}
4684
John McCall477f2bb2016-03-03 06:39:32 +00004685/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004686static bool isValidSwiftContextType(QualType Ty) {
4687 if (!Ty->hasPointerRepresentation())
4688 return Ty->isDependentType();
4689 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004690}
4691
4692/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004693static bool isValidSwiftIndirectResultType(QualType Ty) {
4694 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4695 Ty = PtrType->getPointeeType();
4696 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4697 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004698 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004699 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004700 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004701 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004702}
4703
4704/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004705static bool isValidSwiftErrorResultType(QualType Ty) {
4706 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4707 Ty = PtrType->getPointeeType();
4708 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4709 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004710 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004711 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004712 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004713 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004714 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004715 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004716}
4717
Erich Keane6a24e802019-09-13 17:39:31 +00004718void Sema::AddParameterABIAttr(Decl *D, const AttributeCommonInfo &CI,
4719 ParameterABI abi) {
John McCall477f2bb2016-03-03 06:39:32 +00004720
4721 QualType type = cast<ParmVarDecl>(D)->getType();
4722
4723 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4724 if (existingAttr->getABI() != abi) {
Erich Keane6a24e802019-09-13 17:39:31 +00004725 Diag(CI.getLoc(), diag::err_attributes_are_not_compatible)
4726 << getParameterABISpelling(abi) << existingAttr;
John McCall477f2bb2016-03-03 06:39:32 +00004727 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4728 return;
4729 }
4730 }
4731
4732 switch (abi) {
4733 case ParameterABI::Ordinary:
4734 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4735
4736 case ParameterABI::SwiftContext:
4737 if (!isValidSwiftContextType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004738 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4739 << getParameterABISpelling(abi) << /*pointer to pointer */ 0 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004740 }
Erich Keane6a24e802019-09-13 17:39:31 +00004741 D->addAttr(::new (Context) SwiftContextAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004742 return;
4743
4744 case ParameterABI::SwiftErrorResult:
4745 if (!isValidSwiftErrorResultType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004746 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4747 << getParameterABISpelling(abi) << /*pointer to pointer */ 1 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004748 }
Erich Keane6a24e802019-09-13 17:39:31 +00004749 D->addAttr(::new (Context) SwiftErrorResultAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004750 return;
4751
4752 case ParameterABI::SwiftIndirectResult:
4753 if (!isValidSwiftIndirectResultType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004754 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4755 << getParameterABISpelling(abi) << /*pointer*/ 0 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004756 }
Erich Keane6a24e802019-09-13 17:39:31 +00004757 D->addAttr(::new (Context) SwiftIndirectResultAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004758 return;
4759 }
4760 llvm_unreachable("bad parameter ABI attribute");
4761}
4762
John McCall3882ace2011-01-05 12:14:39 +00004763/// Checks a regparm attribute, returning true if it is ill-formed and
4764/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004765bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004766 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004767 return true;
4768
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004769 if (!checkAttributeNumArgs(*this, AL, 1)) {
4770 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004771 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004772 }
Eli Friedman7044b762009-03-27 21:06:47 +00004773
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004774 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004775 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4776 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4777 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004778 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004779 }
4780
Douglas Gregore8bbc122011-09-02 00:18:52 +00004781 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004782 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004783 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004784 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004785 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004786 }
4787
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004788 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004789 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004790 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004791 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004792 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004793 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004794 }
4795
John McCall3882ace2011-01-05 12:14:39 +00004796 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004797}
4798
Artem Belevichbcec9da2016-06-06 22:54:57 +00004799// Checks whether an argument of launch_bounds attribute is
4800// acceptable, performs implicit conversion to Rvalue, and returns
4801// non-nullptr Expr result on success. Otherwise, it returns nullptr
4802// and may output an error.
4803static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004804 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004805 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004806 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004807 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004808
4809 // Accept template arguments for now as they depend on something else.
4810 // We'll get to check them when they eventually get instantiated.
4811 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004812 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004813
4814 llvm::APSInt I(64);
4815 if (!E->isIntegerConstantExpr(I, S.Context)) {
4816 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004817 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004818 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004819 }
4820 // Make sure we can fit it in 32 bits.
4821 if (!I.isIntN(32)) {
4822 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4823 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004824 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004825 }
4826 if (I < 0)
4827 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004828 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004829
Artem Belevichbcec9da2016-06-06 22:54:57 +00004830 // We may need to perform implicit conversion of the argument.
4831 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4832 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4833 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4834 assert(!ValArg.isInvalid() &&
4835 "Unexpected PerformCopyInitialization() failure.");
4836
4837 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004838}
4839
Erich Keane6a24e802019-09-13 17:39:31 +00004840void Sema::AddLaunchBoundsAttr(Decl *D, const AttributeCommonInfo &CI,
4841 Expr *MaxThreads, Expr *MinBlocks) {
4842 CUDALaunchBoundsAttr TmpAttr(Context, CI, MaxThreads, MinBlocks);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004843 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4844 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004845 return;
4846
Artem Belevichbcec9da2016-06-06 22:54:57 +00004847 if (MinBlocks) {
4848 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4849 if (MinBlocks == nullptr)
4850 return;
4851 }
Artem Belevich7093e402015-04-21 22:55:54 +00004852
Erich Keane6a24e802019-09-13 17:39:31 +00004853 D->addAttr(::new (Context)
4854 CUDALaunchBoundsAttr(Context, CI, MaxThreads, MinBlocks));
Artem Belevich7093e402015-04-21 22:55:54 +00004855}
4856
Erich Keanee891aa92018-07-13 15:07:47 +00004857static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004858 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4859 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004860 return;
4861
Erich Keane6a24e802019-09-13 17:39:31 +00004862 S.AddLaunchBoundsAttr(D, AL, AL.getArgAsExpr(0),
4863 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004864}
4865
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004866static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004867 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004868 if (!AL.isArgIdent(0)) {
4869 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004870 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004871 return;
4872 }
Joel E. Denny81508102018-03-13 14:51:22 +00004873
4874 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004875 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004876 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004877 return;
4878
Joel E. Denny81508102018-03-13 14:51:22 +00004879 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004880 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004881 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004882 return;
4883
Erich Keane6a24e802019-09-13 17:39:31 +00004884 bool IsPointer = AL.getAttrName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004885 if (IsPointer) {
4886 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004887 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4888 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4889 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004890 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004891 }
4892
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004893 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00004894 S.Context, AL, AL.getArgAsIdent(0)->Ident, ArgumentIdx, TypeTagIdx,
4895 IsPointer));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004896}
4897
4898static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004899 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004900 if (!AL.isArgIdent(0)) {
4901 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004902 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004903 return;
4904 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004905
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004906 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004907 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004908
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004909 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004910 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004911 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004912 return;
4913 }
4914
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004915 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004916 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004917 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004918 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004919
Erich Keane6a24e802019-09-13 17:39:31 +00004920 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
4921 S.Context, AL, PointerKind, MatchingCTypeLoc, AL.getLayoutCompatible(),
4922 AL.getMustBeNull()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004923}
4924
Erich Keanee891aa92018-07-13 15:07:47 +00004925static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004926 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004927
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004928 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004929 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004930 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004931 return;
4932
Joel E. Denny81508102018-03-13 14:51:22 +00004933 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
Erich Keane6a24e802019-09-13 17:39:31 +00004934 D->addAttr(::new (S.Context)
4935 XRayLogArgsAttr(S.Context, AL, ArgCount.getSourceIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004936}
4937
Fangrui Songa44c4342020-01-04 15:39:19 -08004938static void handlePatchableFunctionEntryAttr(Sema &S, Decl *D,
4939 const ParsedAttr &AL) {
4940 uint32_t Count = 0, Offset = 0;
4941 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Count, 0, true))
4942 return;
4943 if (AL.getNumArgs() == 2) {
4944 Expr *Arg = AL.getArgAsExpr(1);
4945 if (!checkUInt32Argument(S, AL, Arg, Offset, 1, true))
4946 return;
Fangrui Song69bf40c2020-01-20 14:30:06 -08004947 if (Count < Offset) {
Fangrui Songa44c4342020-01-04 15:39:19 -08004948 S.Diag(getAttrLoc(AL), diag::err_attribute_argument_out_of_range)
Fangrui Song69bf40c2020-01-20 14:30:06 -08004949 << &AL << 0 << Count << Arg->getBeginLoc();
Fangrui Songa44c4342020-01-04 15:39:19 -08004950 return;
4951 }
4952 }
4953 D->addAttr(::new (S.Context)
4954 PatchableFunctionEntryAttr(S.Context, AL, Count, Offset));
4955}
4956
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00004957namespace {
4958struct IntrinToName {
4959 uint32_t Id;
4960 int32_t FullName;
4961 int32_t ShortName;
4962};
4963} // unnamed namespace
4964
4965static bool ArmBuiltinAliasValid(unsigned BuiltinID, StringRef AliasName,
4966 ArrayRef<IntrinToName> Map,
4967 const char *IntrinNames) {
Simon Tatham08074cc2019-09-02 15:50:50 +01004968 if (AliasName.startswith("__arm_"))
4969 AliasName = AliasName.substr(6);
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00004970 const IntrinToName *It = std::lower_bound(
4971 Map.begin(), Map.end(), BuiltinID,
4972 [](const IntrinToName &L, unsigned Id) { return L.Id < Id; });
4973 if (It == Map.end() || It->Id != BuiltinID)
4974 return false;
4975 StringRef FullName(&IntrinNames[It->FullName]);
4976 if (AliasName == FullName)
4977 return true;
4978 if (It->ShortName == -1)
4979 return false;
4980 StringRef ShortName(&IntrinNames[It->ShortName]);
4981 return AliasName == ShortName;
Simon Tatham7c11da02019-09-02 15:35:09 +01004982}
4983
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00004984static bool ArmMveAliasValid(unsigned BuiltinID, StringRef AliasName) {
4985#include "clang/Basic/arm_mve_builtin_aliases.inc"
4986 // The included file defines:
4987 // - ArrayRef<IntrinToName> Map
4988 // - const char IntrinNames[]
4989 return ArmBuiltinAliasValid(BuiltinID, AliasName, Map, IntrinNames);
4990}
4991
4992static bool ArmCdeAliasValid(unsigned BuiltinID, StringRef AliasName) {
4993#include "clang/Basic/arm_cde_builtin_aliases.inc"
4994 return ArmBuiltinAliasValid(BuiltinID, AliasName, Map, IntrinNames);
4995}
4996
Sander de Smalen981f0802020-03-18 15:05:08 +00004997static bool ArmSveAliasValid(unsigned BuiltinID, StringRef AliasName) {
4998 switch (BuiltinID) {
4999 default:
5000 return false;
5001#define GET_SVE_BUILTINS
5002#define BUILTIN(name, types, attr) case SVE::BI##name:
5003#include "clang/Basic/arm_sve_builtins.inc"
5004 return true;
5005 }
5006}
5007
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00005008static void handleArmBuiltinAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Simon Tatham7c11da02019-09-02 15:35:09 +01005009 if (!AL.isArgIdent(0)) {
5010 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
5011 << AL << 1 << AANT_ArgumentIdentifier;
5012 return;
5013 }
5014
5015 IdentifierInfo *Ident = AL.getArgAsIdent(0)->Ident;
5016 unsigned BuiltinID = Ident->getBuiltinID();
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00005017 StringRef AliasName = cast<FunctionDecl>(D)->getIdentifier()->getName();
Simon Tatham7c11da02019-09-02 15:35:09 +01005018
Sander de Smalen981f0802020-03-18 15:05:08 +00005019 bool IsAArch64 = S.Context.getTargetInfo().getTriple().isAArch64();
5020 if ((IsAArch64 && !ArmSveAliasValid(BuiltinID, AliasName)) ||
5021 (!IsAArch64 && !ArmMveAliasValid(BuiltinID, AliasName) &&
5022 !ArmCdeAliasValid(BuiltinID, AliasName))) {
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00005023 S.Diag(AL.getLoc(), diag::err_attribute_arm_builtin_alias);
Simon Tatham7c11da02019-09-02 15:35:09 +01005024 return;
5025 }
5026
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00005027 D->addAttr(::new (S.Context) ArmBuiltinAliasAttr(S.Context, AL, Ident));
Simon Tatham7c11da02019-09-02 15:35:09 +01005028}
5029
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005030//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005031// Checker-specific attribute handlers.
5032//===----------------------------------------------------------------------===//
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005033static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
5034 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00005035}
5036
George Karpenkov1657f362018-11-30 02:18:37 +00005037static bool isValidSubjectOfNSAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005038 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00005039 QT->isObjCNSObjectType();
John McCalled433932011-01-25 03:31:58 +00005040}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00005041
George Karpenkov1657f362018-11-30 02:18:37 +00005042static bool isValidSubjectOfCFAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005043 return QT->isDependentType() || QT->isPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00005044 isValidSubjectOfNSAttribute(QT);
John McCalled433932011-01-25 03:31:58 +00005045}
5046
George Karpenkov1657f362018-11-30 02:18:37 +00005047static bool isValidSubjectOfOSAttribute(QualType QT) {
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005048 if (QT->isDependentType())
5049 return true;
5050 QualType PT = QT->getPointeeType();
5051 return !PT.isNull() && PT->getAsCXXRecordDecl() != nullptr;
John McCall3b5a8f52016-03-03 00:10:03 +00005052}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005053
Erich Keane6a24e802019-09-13 17:39:31 +00005054void Sema::AddXConsumedAttr(Decl *D, const AttributeCommonInfo &CI,
George Karpenkov1657f362018-11-30 02:18:37 +00005055 RetainOwnershipKind K,
5056 bool IsTemplateInstantiation) {
5057 ValueDecl *VD = cast<ValueDecl>(D);
5058 switch (K) {
5059 case RetainOwnershipKind::OS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005060 handleSimpleAttributeOrDiagnose<OSConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00005061 *this, VD, CI, isValidSubjectOfOSAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00005062 diag::warn_ns_attribute_wrong_parameter_type,
Erich Keane6a24e802019-09-13 17:39:31 +00005063 /*ExtraArgs=*/CI.getRange(), "os_consumed", /*pointers*/ 1);
George Karpenkov1657f362018-11-30 02:18:37 +00005064 return;
5065 case RetainOwnershipKind::NS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005066 handleSimpleAttributeOrDiagnose<NSConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00005067 *this, VD, CI, isValidSubjectOfNSAttribute(VD->getType()),
John McCall3b5a8f52016-03-03 00:10:03 +00005068
George Karpenkov1657f362018-11-30 02:18:37 +00005069 // These attributes are normally just advisory, but in ARC, ns_consumed
5070 // is significant. Allow non-dependent code to contain inappropriate
5071 // attributes even in ARC, but require template instantiations to be
5072 // set up correctly.
5073 ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount)
5074 ? diag::err_ns_attribute_wrong_parameter_type
5075 : diag::warn_ns_attribute_wrong_parameter_type),
Erich Keane6a24e802019-09-13 17:39:31 +00005076 /*ExtraArgs=*/CI.getRange(), "ns_consumed", /*objc pointers*/ 0);
George Karpenkov1657f362018-11-30 02:18:37 +00005077 return;
5078 case RetainOwnershipKind::CF:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005079 handleSimpleAttributeOrDiagnose<CFConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00005080 *this, VD, CI, isValidSubjectOfCFAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00005081 diag::warn_ns_attribute_wrong_parameter_type,
Erich Keane6a24e802019-09-13 17:39:31 +00005082 /*ExtraArgs=*/CI.getRange(), "cf_consumed", /*pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00005083 return;
5084 }
George Karpenkov1657f362018-11-30 02:18:37 +00005085}
John McCalled433932011-01-25 03:31:58 +00005086
George Karpenkov1657f362018-11-30 02:18:37 +00005087static Sema::RetainOwnershipKind
5088parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) {
5089 switch (AL.getKind()) {
5090 case ParsedAttr::AT_CFConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005091 case ParsedAttr::AT_CFReturnsRetained:
5092 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005093 return Sema::RetainOwnershipKind::CF;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005094 case ParsedAttr::AT_OSConsumesThis:
George Karpenkov1657f362018-11-30 02:18:37 +00005095 case ParsedAttr::AT_OSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005096 case ParsedAttr::AT_OSReturnsRetained:
5097 case ParsedAttr::AT_OSReturnsNotRetained:
5098 case ParsedAttr::AT_OSReturnsRetainedOnZero:
5099 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
George Karpenkov1657f362018-11-30 02:18:37 +00005100 return Sema::RetainOwnershipKind::OS;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005101 case ParsedAttr::AT_NSConsumesSelf:
George Karpenkov1657f362018-11-30 02:18:37 +00005102 case ParsedAttr::AT_NSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005103 case ParsedAttr::AT_NSReturnsRetained:
5104 case ParsedAttr::AT_NSReturnsNotRetained:
5105 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005106 return Sema::RetainOwnershipKind::NS;
5107 default:
5108 llvm_unreachable("Wrong argument supplied");
5109 }
John McCalled433932011-01-25 03:31:58 +00005110}
5111
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005112bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
5113 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00005114 return false;
5115
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005116 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
5117 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00005118 return true;
5119}
5120
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005121/// \return whether the parameter is a pointer to OSObject pointer.
5122static bool isValidOSObjectOutParameter(const Decl *D) {
5123 const auto *PVD = dyn_cast<ParmVarDecl>(D);
5124 if (!PVD)
5125 return false;
5126 QualType QT = PVD->getType();
5127 QualType PT = QT->getPointeeType();
5128 return !PT.isNull() && isValidSubjectOfOSAttribute(PT);
5129}
5130
George Karpenkov1657f362018-11-30 02:18:37 +00005131static void handleXReturnsXRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005132 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005133 QualType ReturnType;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005134 Sema::RetainOwnershipKind K = parsedAttrToRetainOwnershipKind(AL);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005135
George Karpenkov1657f362018-11-30 02:18:37 +00005136 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005137 ReturnType = MD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005138 } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
5139 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) {
John McCall31168b02011-06-15 23:02:42 +00005140 return; // ignore: was handled as a type attribute
George Karpenkov1657f362018-11-30 02:18:37 +00005141 } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005142 ReturnType = PD->getType();
George Karpenkov1657f362018-11-30 02:18:37 +00005143 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005144 ReturnType = FD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005145 } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
5146 // Attributes on parameters are used for out-parameters,
5147 // passed as pointers-to-pointers.
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005148 unsigned DiagID = K == Sema::RetainOwnershipKind::CF
5149 ? /*pointer-to-CF-pointer*/2
5150 : /*pointer-to-OSObject-pointer*/3;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005151 ReturnType = Param->getType()->getPointeeType();
5152 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005153 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005154 << AL << DiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005155 return;
5156 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005157 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00005158 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005159 } else {
5160 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005161 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005162 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005163 case ParsedAttr::AT_NSReturnsRetained:
5164 case ParsedAttr::AT_NSReturnsAutoreleased:
5165 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005166 ExpectedDeclKind = ExpectedFunctionOrMethod;
5167 break;
5168
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005169 case ParsedAttr::AT_OSReturnsRetained:
5170 case ParsedAttr::AT_OSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00005171 case ParsedAttr::AT_CFReturnsRetained:
5172 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005173 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
5174 break;
5175 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005176 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005177 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005178 return;
5179 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005180
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005181 bool TypeOK;
5182 bool Cf;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005183 unsigned ParmDiagID = 2; // Pointer-to-CF-pointer
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005184 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00005185 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005186 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005187 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
5188 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00005189 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005190
5191 case ParsedAttr::AT_NSReturnsAutoreleased:
5192 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005193 TypeOK = isValidSubjectOfNSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005194 Cf = false;
John McCalled433932011-01-25 03:31:58 +00005195 break;
5196
Erich Keanee891aa92018-07-13 15:07:47 +00005197 case ParsedAttr::AT_CFReturnsRetained:
5198 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005199 TypeOK = isValidSubjectOfCFAttribute(ReturnType);
5200 Cf = true;
5201 break;
5202
5203 case ParsedAttr::AT_OSReturnsRetained:
5204 case ParsedAttr::AT_OSReturnsNotRetained:
5205 TypeOK = isValidSubjectOfOSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005206 Cf = true;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005207 ParmDiagID = 3; // Pointer-to-OSObject-pointer
John McCalled433932011-01-25 03:31:58 +00005208 break;
5209 }
5210
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005211 if (!TypeOK) {
5212 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00005213 return;
5214
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005215 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005216 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005217 << AL << ParmDiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005218 } else {
5219 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
5220 enum : unsigned {
5221 Function,
5222 Method,
5223 Property
5224 } SubjectKind = Function;
5225 if (isa<ObjCMethodDecl>(D))
5226 SubjectKind = Method;
5227 else if (isa<ObjCPropertyDecl>(D))
5228 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005229 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005230 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005231 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005232 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00005233 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005234
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005235 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005236 default:
David Blaikie83d382b2011-09-23 05:06:16 +00005237 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005238 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005239 handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL);
John McCalled433932011-01-25 03:31:58 +00005240 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005241 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005242 handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005243 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005244 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005245 handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005246 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005247 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005248 handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005249 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005250 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005251 handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL);
5252 return;
5253 case ParsedAttr::AT_OSReturnsRetained:
5254 handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL);
5255 return;
5256 case ParsedAttr::AT_OSReturnsNotRetained:
5257 handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005258 return;
5259 };
5260}
5261
John McCallcf166702011-07-22 08:53:00 +00005262static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005263 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00005264 const int EP_ObjCMethod = 1;
5265 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00005266
Erich Keaneb11ebc52017-09-27 03:20:13 +00005267 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005268 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005269 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00005270 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005271 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005272 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00005273
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00005274 if (!resultType->isReferenceType() &&
5275 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005276 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005277 << SourceRange(loc) << Attrs
5278 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
5279 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00005280
5281 // Drop the attribute.
5282 return;
5283 }
5284
Erich Keane6a24e802019-09-13 17:39:31 +00005285 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(S.Context, Attrs));
John McCallcf166702011-07-22 08:53:00 +00005286}
5287
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005288static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005289 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005290 const auto *Method = cast<ObjCMethodDecl>(D);
5291
5292 const DeclContext *DC = Method->getDeclContext();
5293 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005294 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005295 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005296 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
5297 return;
5298 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005299 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005300 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005301 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005302 return;
5303 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005304
Erich Keane6a24e802019-09-13 17:39:31 +00005305 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(S.Context, Attrs));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005306}
5307
Erich Keanee891aa92018-07-13 15:07:47 +00005308static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005309 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00005310
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005311 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005312 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005313 return;
5314 }
John McCall28592582015-02-01 22:34:06 +00005315
5316 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005317 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00005318 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005319 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00005320 return;
5321 }
5322
5323 // Only allow 'cv void *'.
5324 QualType T = TD->getUnderlyingType();
5325 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005326 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00005327 return;
5328 }
5329 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005330
Erich Keane6a24e802019-09-13 17:39:31 +00005331 D->addAttr(::new (S.Context) ObjCBridgeAttr(S.Context, AL, Parm->Ident));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005332}
5333
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005334static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005335 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005336 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00005337
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005338 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005339 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005340 return;
5341 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005342
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005343 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005344 ObjCBridgeMutableAttr(S.Context, AL, Parm->Ident));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005345}
5346
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005347static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005348 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005349 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005350 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005351 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005352 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005353 return;
5354 }
5355 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005356 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005357 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005358 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Erich Keane6a24e802019-09-13 17:39:31 +00005359 D->addAttr(::new (S.Context) ObjCBridgeRelatedAttr(
5360 S.Context, AL, RelatedClass, ClassMethod, InstanceMethod));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005361}
5362
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005363static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005364 const ParsedAttr &AL) {
Erik Pilkington81d3f452019-02-13 20:32:37 +00005365 DeclContext *Ctx = D->getDeclContext();
5366
5367 // This attribute can only be applied to methods in interfaces or class
5368 // extensions.
5369 if (!isa<ObjCInterfaceDecl>(Ctx) &&
5370 !(isa<ObjCCategoryDecl>(Ctx) &&
5371 cast<ObjCCategoryDecl>(Ctx)->IsClassExtension())) {
5372 S.Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
5373 return;
5374 }
5375
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005376 ObjCInterfaceDecl *IFace;
Erik Pilkington81d3f452019-02-13 20:32:37 +00005377 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(Ctx))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005378 IFace = CatDecl->getClassInterface();
5379 else
Erik Pilkington81d3f452019-02-13 20:32:37 +00005380 IFace = cast<ObjCInterfaceDecl>(Ctx);
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005381
5382 if (!IFace)
5383 return;
5384
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00005385 IFace->setHasDesignatedInitializers();
Erich Keane6a24e802019-09-13 17:39:31 +00005386 D->addAttr(::new (S.Context) ObjCDesignatedInitializerAttr(S.Context, AL));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005387}
5388
Erich Keanee891aa92018-07-13 15:07:47 +00005389static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005390 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005391 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005392 return;
5393 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005394 ObjCRuntimeNameAttr(S.Context, AL, MetaDataName));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005395}
5396
Nico Webera6916892016-06-10 18:53:04 +00005397// When a user wants to use objc_boxable with a union or struct
5398// but they don't have access to the declaration (legacy/third-party code)
5399// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00005400// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00005401static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00005402 bool notify = false;
5403
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005404 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00005405 if (RD && RD->getDefinition()) {
5406 RD = RD->getDefinition();
5407 notify = true;
5408 }
5409
5410 if (RD) {
Erich Keane6a24e802019-09-13 17:39:31 +00005411 ObjCBoxableAttr *BoxableAttr =
5412 ::new (S.Context) ObjCBoxableAttr(S.Context, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00005413 RD->addAttr(BoxableAttr);
5414 if (notify) {
5415 // we need to notify ASTReader/ASTWriter about
5416 // modification of existing declaration
5417 if (ASTMutationListener *L = S.getASTMutationListener())
5418 L->AddedAttributeToRecord(BoxableAttr, RD);
5419 }
5420 }
5421}
5422
Erich Keanee891aa92018-07-13 15:07:47 +00005423static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005424 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005425
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005426 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005427 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005428}
5429
Chandler Carruthedc2c642011-07-02 00:01:44 +00005430static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005431 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005432 const auto *VD = cast<ValueDecl>(D);
5433 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005434
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005435 if (!QT->isDependentType() &&
5436 !QT->isObjCLifetimeType()) {
5437 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5438 << QT;
John McCall31168b02011-06-15 23:02:42 +00005439 return;
5440 }
5441
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005442 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005443
5444 // If we have no lifetime yet, check the lifetime we're presumably
5445 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005446 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5447 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005448
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005449 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005450 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005451 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005452 "didn't infer lifetime for non-dependent type?");
5453 break;
5454
5455 case Qualifiers::OCL_Weak: // meaningful
5456 case Qualifiers::OCL_Strong: // meaningful
5457 break;
5458
5459 case Qualifiers::OCL_ExplicitNone:
5460 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005461 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5462 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005463 break;
5464 }
5465
Erich Keane6a24e802019-09-13 17:39:31 +00005466 D->addAttr(::new (S.Context) ObjCPreciseLifetimeAttr(S.Context, AL));
John McCall31168b02011-06-15 23:02:42 +00005467}
5468
Francois Picheta83957a2010-12-19 06:50:37 +00005469//===----------------------------------------------------------------------===//
5470// Microsoft specific attribute handlers.
5471//===----------------------------------------------------------------------===//
5472
Erich Keane6a24e802019-09-13 17:39:31 +00005473UuidAttr *Sema::mergeUuidAttr(Decl *D, const AttributeCommonInfo &CI,
5474 StringRef Uuid) {
Nico Weber88f5ed92016-09-13 18:55:26 +00005475 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005476 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005477 return nullptr;
Zachary Henkel0acfc492019-12-28 13:06:13 -08005478 if (!UA->getGuid().empty()) {
5479 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5480 Diag(CI.getLoc(), diag::note_previous_uuid);
5481 D->dropAttr<UuidAttr>();
5482 }
Nico Weber88f5ed92016-09-13 18:55:26 +00005483 }
5484
Erich Keane6a24e802019-09-13 17:39:31 +00005485 return ::new (Context) UuidAttr(Context, CI, Uuid);
Nico Weber88f5ed92016-09-13 18:55:26 +00005486}
5487
Erich Keanee891aa92018-07-13 15:07:47 +00005488static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005489 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005490 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005491 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005492 return;
5493 }
5494
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005495 StringRef StrRef;
5496 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005497 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005498 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005499
David Majnemer89085342013-08-09 08:56:20 +00005500 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5501 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005502 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5503 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005504
Reid Kleckner140c4a72013-05-17 14:04:52 +00005505 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005506 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005507 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005508 return;
5509 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005510
David Majnemer89085342013-08-09 08:56:20 +00005511 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005512 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005513 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005514 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005515 return;
5516 }
David Majnemer89085342013-08-09 08:56:20 +00005517 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005518 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005519 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005520 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005521 }
Francois Picheta83957a2010-12-19 06:50:37 +00005522
Nico Weber469891e2017-05-05 17:05:56 +00005523 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5524 // the only thing in the [] list, the [] too), and add an insertion of
5525 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5526 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005527 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005528 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005529 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5530 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005531
Erich Keane6a24e802019-09-13 17:39:31 +00005532 UuidAttr *UA = S.mergeUuidAttr(D, AL, StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005533 if (UA)
5534 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005535}
5536
Erich Keanee891aa92018-07-13 15:07:47 +00005537static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005538 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005539 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005540 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005541 return;
5542 }
5543 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Reid Klecknera9cc64e2019-11-15 18:49:32 -08005544 D, AL, /*BestCase=*/true, (MSInheritanceModel)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005545 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005546 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005547 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5548 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005549}
5550
Erich Keanee891aa92018-07-13 15:07:47 +00005551static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005552 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005553 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005554 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005555 return;
5556 }
5557 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005558 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005559 return;
5560 }
5561 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005562 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005563 return;
5564 }
Erich Keane6a24e802019-09-13 17:39:31 +00005565 D->addAttr(::new (S.Context) ThreadAttr(S.Context, AL));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005566}
5567
Erich Keanee891aa92018-07-13 15:07:47 +00005568static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005569 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005570 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005571 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005572 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005573 return;
5574 Tags.push_back(Tag);
5575 }
5576
5577 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5578 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005579 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005580 return;
5581 }
5582 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005583 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005584 return;
5585 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005586 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005587 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005588 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005589 return;
5590
5591 // Store tags sorted and without duplicates.
Fangrui Song55fab262018-09-26 22:16:28 +00005592 llvm::sort(Tags);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005593 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5594
5595 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005596 AbiTagAttr(S.Context, AL, Tags.data(), Tags.size()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005597}
5598
Erich Keanee891aa92018-07-13 15:07:47 +00005599static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005600 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005601 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005602 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005603 return;
5604 }
5605
5606 StringRef Str;
5607 SourceLocation ArgLoc;
5608
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005609 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005610 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005611 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005612 return;
5613
5614 ARMInterruptAttr::InterruptType Kind;
5615 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005616 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5617 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005618 return;
5619 }
5620
Erich Keane6a24e802019-09-13 17:39:31 +00005621 D->addAttr(::new (S.Context) ARMInterruptAttr(S.Context, AL, Kind));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005622}
5623
Erich Keanee891aa92018-07-13 15:07:47 +00005624static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005625 // MSP430 'interrupt' attribute is applied to
5626 // a function with no parameters and void return type.
5627 if (!isFunctionOrMethod(D)) {
5628 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5629 << "'interrupt'" << ExpectedFunctionOrMethod;
5630 return;
5631 }
5632
5633 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005634 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5635 << /*MSP430*/ 1 << 0;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005636 return;
5637 }
5638
5639 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005640 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5641 << /*MSP430*/ 1 << 1;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005642 return;
5643 }
5644
5645 // The attribute takes one integer argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005646 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005647 return;
5648
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005649 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005650 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5651 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005652 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005653 }
5654
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005655 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005656 llvm::APSInt NumParams(32);
5657 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005658 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005659 << AL << AANT_ArgumentIntegerConstant
5660 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005661 return;
5662 }
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005663 // The argument should be in range 0..63.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005664 unsigned Num = NumParams.getLimitedValue(255);
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005665 if (Num > 63) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005666 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005667 << AL << (int)NumParams.getSExtValue()
5668 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005669 return;
5670 }
5671
Erich Keane6a24e802019-09-13 17:39:31 +00005672 D->addAttr(::new (S.Context) MSP430InterruptAttr(S.Context, AL, Num));
Aaron Ballman36a53502014-01-16 13:03:14 +00005673 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005674}
5675
Erich Keanee891aa92018-07-13 15:07:47 +00005676static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005677 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005678 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005679 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005680 return;
5681 }
5682
5683 StringRef Str;
5684 SourceLocation ArgLoc;
5685
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005686 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005687 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005688 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005689 return;
5690
5691 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5692 // a) Must be a function.
5693 // b) Must have no parameters.
5694 // c) Must have the 'void' return type.
5695 // d) Cannot have the 'mips16' attribute, as that instruction set
5696 // lacks the 'eret' instruction.
5697 // e) The attribute itself must either have no argument or one of the
5698 // valid interrupt types, see [MipsInterruptDocs].
5699
5700 if (!isFunctionOrMethod(D)) {
5701 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5702 << "'interrupt'" << ExpectedFunctionOrMethod;
5703 return;
5704 }
5705
5706 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005707 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5708 << /*MIPS*/ 0 << 0;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005709 return;
5710 }
5711
5712 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005713 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5714 << /*MIPS*/ 0 << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005715 return;
5716 }
5717
Erich Keane44bacdf2018-08-09 13:21:32 +00005718 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005719 return;
5720
5721 MipsInterruptAttr::InterruptType Kind;
5722 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005723 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005724 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005725 return;
5726 }
5727
Erich Keane6a24e802019-09-13 17:39:31 +00005728 D->addAttr(::new (S.Context) MipsInterruptAttr(S.Context, AL, Kind));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005729}
5730
Erich Keanee891aa92018-07-13 15:07:47 +00005731static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005732 // Semantic checks for a function with the 'interrupt' attribute.
5733 // a) Must be a function.
5734 // b) Must have the 'void' return type.
5735 // c) Must take 1 or 2 arguments.
5736 // d) The 1st argument must be a pointer.
5737 // e) The 2nd argument (if any) must be an unsigned integer.
5738 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5739 CXXMethodDecl::isStaticOverloadedOperator(
5740 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005741 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005742 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005743 return;
5744 }
5745 // Interrupt handler must have void return type.
5746 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5747 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5748 diag::err_anyx86_interrupt_attribute)
5749 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5750 ? 0
5751 : 1)
5752 << 0;
5753 return;
5754 }
5755 // Interrupt handler must have 1 or 2 parameters.
5756 unsigned NumParams = getFunctionOrMethodNumParams(D);
5757 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005758 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005759 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5760 ? 0
5761 : 1)
5762 << 1;
5763 return;
5764 }
5765 // The first argument must be a pointer.
5766 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5767 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5768 diag::err_anyx86_interrupt_attribute)
5769 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5770 ? 0
5771 : 1)
5772 << 2;
5773 return;
5774 }
5775 // The second argument, if present, must be an unsigned integer.
5776 unsigned TypeSize =
5777 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5778 ? 64
5779 : 32;
5780 if (NumParams == 2 &&
5781 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5782 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5783 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5784 diag::err_anyx86_interrupt_attribute)
5785 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5786 ? 0
5787 : 1)
5788 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5789 return;
5790 }
Erich Keane6a24e802019-09-13 17:39:31 +00005791 D->addAttr(::new (S.Context) AnyX86InterruptAttr(S.Context, AL));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005792 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5793}
5794
Erich Keanee891aa92018-07-13 15:07:47 +00005795static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005796 if (!isFunctionOrMethod(D)) {
5797 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5798 << "'interrupt'" << ExpectedFunction;
5799 return;
5800 }
5801
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005802 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005803 return;
5804
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005805 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005806}
5807
Erich Keanee891aa92018-07-13 15:07:47 +00005808static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005809 if (!isFunctionOrMethod(D)) {
5810 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5811 << "'signal'" << ExpectedFunction;
5812 return;
5813 }
5814
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005815 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005816 return;
5817
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005818 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005819}
5820
Yonghong Song4e2ce222019-11-01 22:16:59 -07005821static void handleBPFPreserveAIRecord(Sema &S, RecordDecl *RD) {
5822 // Add preserve_access_index attribute to all fields and inner records.
5823 for (auto D : RD->decls()) {
5824 if (D->hasAttr<BPFPreserveAccessIndexAttr>())
5825 continue;
5826
5827 D->addAttr(BPFPreserveAccessIndexAttr::CreateImplicit(S.Context));
5828 if (auto *Rec = dyn_cast<RecordDecl>(D))
5829 handleBPFPreserveAIRecord(S, Rec);
5830 }
5831}
5832
5833static void handleBPFPreserveAccessIndexAttr(Sema &S, Decl *D,
5834 const ParsedAttr &AL) {
5835 auto *Rec = cast<RecordDecl>(D);
5836 handleBPFPreserveAIRecord(S, Rec);
5837 Rec->addAttr(::new (S.Context) BPFPreserveAccessIndexAttr(S.Context, AL));
5838}
5839
Sam Clegg881d8772019-11-05 10:15:56 -08005840static void handleWebAssemblyExportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5841 if (!isFunctionOrMethod(D)) {
5842 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5843 << "'export_name'" << ExpectedFunction;
5844 return;
5845 }
5846
5847 auto *FD = cast<FunctionDecl>(D);
5848 if (FD->isThisDeclarationADefinition()) {
5849 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5850 return;
5851 }
5852
5853 StringRef Str;
5854 SourceLocation ArgLoc;
5855 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5856 return;
5857
Sam Clegg0a1e3492019-12-13 14:44:06 -08005858 D->addAttr(::new (S.Context) WebAssemblyExportNameAttr(S.Context, AL, Str));
5859 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Sam Clegg881d8772019-11-05 10:15:56 -08005860}
5861
Dan Gohmanb4323692019-01-24 21:08:30 +00005862static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5863 if (!isFunctionOrMethod(D)) {
5864 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5865 << "'import_module'" << ExpectedFunction;
5866 return;
5867 }
5868
5869 auto *FD = cast<FunctionDecl>(D);
5870 if (FD->isThisDeclarationADefinition()) {
5871 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5872 return;
5873 }
5874
5875 StringRef Str;
5876 SourceLocation ArgLoc;
5877 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5878 return;
5879
Erich Keane6a24e802019-09-13 17:39:31 +00005880 FD->addAttr(::new (S.Context)
5881 WebAssemblyImportModuleAttr(S.Context, AL, Str));
Dan Gohmanb4323692019-01-24 21:08:30 +00005882}
Ana Pazos1eee1b72018-07-26 17:37:45 +00005883
Dan Gohmancae84592019-02-01 22:25:23 +00005884static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5885 if (!isFunctionOrMethod(D)) {
5886 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5887 << "'import_name'" << ExpectedFunction;
5888 return;
5889 }
5890
5891 auto *FD = cast<FunctionDecl>(D);
5892 if (FD->isThisDeclarationADefinition()) {
5893 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5894 return;
5895 }
5896
5897 StringRef Str;
5898 SourceLocation ArgLoc;
5899 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5900 return;
5901
Erich Keane6a24e802019-09-13 17:39:31 +00005902 FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr(S.Context, AL, Str));
Dan Gohmancae84592019-02-01 22:25:23 +00005903}
5904
Ana Pazos1eee1b72018-07-26 17:37:45 +00005905static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5906 const ParsedAttr &AL) {
5907 // Warn about repeated attributes.
5908 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5909 S.Diag(AL.getRange().getBegin(),
5910 diag::warn_riscv_repeated_interrupt_attribute);
5911 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5912 return;
5913 }
5914
5915 // Check the attribute argument. Argument is optional.
5916 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5917 return;
5918
5919 StringRef Str;
5920 SourceLocation ArgLoc;
5921
5922 // 'machine'is the default interrupt mode.
5923 if (AL.getNumArgs() == 0)
5924 Str = "machine";
5925 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5926 return;
5927
5928 // Semantic checks for a function with the 'interrupt' attribute:
5929 // - Must be a function.
5930 // - Must have no parameters.
5931 // - Must have the 'void' return type.
5932 // - The attribute itself must either have no argument or one of the
5933 // valid interrupt types, see [RISCVInterruptDocs].
5934
5935 if (D->getFunctionType() == nullptr) {
5936 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5937 << "'interrupt'" << ExpectedFunction;
5938 return;
5939 }
5940
5941 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005942 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5943 << /*RISC-V*/ 2 << 0;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005944 return;
5945 }
5946
5947 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005948 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5949 << /*RISC-V*/ 2 << 1;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005950 return;
5951 }
5952
5953 RISCVInterruptAttr::InterruptType Kind;
5954 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005955 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5956 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005957 return;
5958 }
5959
Erich Keane6a24e802019-09-13 17:39:31 +00005960 D->addAttr(::new (S.Context) RISCVInterruptAttr(S.Context, AL, Kind));
Ana Pazos1eee1b72018-07-26 17:37:45 +00005961}
5962
Erich Keanee891aa92018-07-13 15:07:47 +00005963static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005964 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005965 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5966 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005967 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005968 break;
5969 case llvm::Triple::mipsel:
5970 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005971 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005972 break;
5973 case llvm::Triple::x86:
5974 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005975 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005976 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005977 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005978 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005979 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005980 case llvm::Triple::riscv32:
5981 case llvm::Triple::riscv64:
5982 handleRISCVInterruptAttr(S, D, AL);
5983 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005984 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005985 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005986 break;
5987 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005988}
5989
Michael Liao7557afa2019-02-26 18:49:36 +00005990static bool
5991checkAMDGPUFlatWorkGroupSizeArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr,
5992 const AMDGPUFlatWorkGroupSizeAttr &Attr) {
5993 // Accept template arguments for now as they depend on something else.
5994 // We'll get to check them when they eventually get instantiated.
5995 if (MinExpr->isValueDependent() || MaxExpr->isValueDependent())
5996 return false;
5997
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005998 uint32_t Min = 0;
Michael Liao7557afa2019-02-26 18:49:36 +00005999 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
6000 return true;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006001
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006002 uint32_t Max = 0;
Michael Liao7557afa2019-02-26 18:49:36 +00006003 if (!checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
6004 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006005
6006 if (Min == 0 && Max != 0) {
Michael Liao7557afa2019-02-26 18:49:36 +00006007 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
6008 << &Attr << 0;
6009 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006010 }
6011 if (Min > Max) {
Michael Liao7557afa2019-02-26 18:49:36 +00006012 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
6013 << &Attr << 1;
6014 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006015 }
6016
Michael Liao7557afa2019-02-26 18:49:36 +00006017 return false;
6018}
6019
Erich Keane6a24e802019-09-13 17:39:31 +00006020void Sema::addAMDGPUFlatWorkGroupSizeAttr(Decl *D,
6021 const AttributeCommonInfo &CI,
6022 Expr *MinExpr, Expr *MaxExpr) {
6023 AMDGPUFlatWorkGroupSizeAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00006024
6025 if (checkAMDGPUFlatWorkGroupSizeArguments(*this, MinExpr, MaxExpr, TmpAttr))
6026 return;
6027
Erich Keane6a24e802019-09-13 17:39:31 +00006028 D->addAttr(::new (Context)
6029 AMDGPUFlatWorkGroupSizeAttr(Context, CI, MinExpr, MaxExpr));
Michael Liao7557afa2019-02-26 18:49:36 +00006030}
6031
6032static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
6033 const ParsedAttr &AL) {
6034 Expr *MinExpr = AL.getArgAsExpr(0);
6035 Expr *MaxExpr = AL.getArgAsExpr(1);
6036
Erich Keane6a24e802019-09-13 17:39:31 +00006037 S.addAMDGPUFlatWorkGroupSizeAttr(D, AL, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00006038}
6039
6040static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr,
6041 Expr *MaxExpr,
6042 const AMDGPUWavesPerEUAttr &Attr) {
6043 if (S.DiagnoseUnexpandedParameterPack(MinExpr) ||
6044 (MaxExpr && S.DiagnoseUnexpandedParameterPack(MaxExpr)))
6045 return true;
6046
6047 // Accept template arguments for now as they depend on something else.
6048 // We'll get to check them when they eventually get instantiated.
6049 if (MinExpr->isValueDependent() || (MaxExpr && MaxExpr->isValueDependent()))
6050 return false;
6051
6052 uint32_t Min = 0;
6053 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
6054 return true;
6055
6056 uint32_t Max = 0;
6057 if (MaxExpr && !checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
6058 return true;
6059
6060 if (Min == 0 && Max != 0) {
6061 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
6062 << &Attr << 0;
6063 return true;
6064 }
6065 if (Max != 0 && Min > Max) {
6066 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
6067 << &Attr << 1;
6068 return true;
6069 }
6070
6071 return false;
6072}
6073
Erich Keane6a24e802019-09-13 17:39:31 +00006074void Sema::addAMDGPUWavesPerEUAttr(Decl *D, const AttributeCommonInfo &CI,
6075 Expr *MinExpr, Expr *MaxExpr) {
6076 AMDGPUWavesPerEUAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00006077
6078 if (checkAMDGPUWavesPerEUArguments(*this, MinExpr, MaxExpr, TmpAttr))
6079 return;
6080
Erich Keane6a24e802019-09-13 17:39:31 +00006081 D->addAttr(::new (Context)
6082 AMDGPUWavesPerEUAttr(Context, CI, MinExpr, MaxExpr));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006083}
6084
Erich Keanee891aa92018-07-13 15:07:47 +00006085static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Liao7557afa2019-02-26 18:49:36 +00006086 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
6087 !checkAttributeAtMostNumArgs(S, AL, 2))
6088 return;
6089
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006090 Expr *MinExpr = AL.getArgAsExpr(0);
Michael Liao7557afa2019-02-26 18:49:36 +00006091 Expr *MaxExpr = (AL.getNumArgs() > 1) ? AL.getArgAsExpr(1) : nullptr;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006092
Erich Keane6a24e802019-09-13 17:39:31 +00006093 S.addAMDGPUWavesPerEUAttr(D, AL, MinExpr, MaxExpr);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006094}
6095
Erich Keanee891aa92018-07-13 15:07:47 +00006096static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006097 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006098 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
6099 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006100 return;
6101
Erich Keane6a24e802019-09-13 17:39:31 +00006102 D->addAttr(::new (S.Context) AMDGPUNumSGPRAttr(S.Context, AL, NumSGPR));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006103}
6104
Erich Keanee891aa92018-07-13 15:07:47 +00006105static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006106 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006107 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
6108 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006109 return;
6110
Erich Keane6a24e802019-09-13 17:39:31 +00006111 D->addAttr(::new (S.Context) AMDGPUNumVGPRAttr(S.Context, AL, NumVGPR));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006112}
6113
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006114static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006115 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006116 // If we try to apply it to a function pointer, don't warn, but don't
6117 // do anything, either. It doesn't matter anyway, because there's nothing
6118 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006119 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006120 if (VD && VD->getType()->isFunctionPointerType())
6121 return;
6122 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006123 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006124 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
6125 TD->getUnderlyingType()->isFunctionType()))
6126 return;
6127 // Attribute can only be applied to function types.
6128 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006129 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006130 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006131 return;
6132 }
6133
Erich Keane6a24e802019-09-13 17:39:31 +00006134 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(S.Context, AL));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006135}
6136
Erich Keanee891aa92018-07-13 15:07:47 +00006137static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00006138 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006139 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
6140 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00006141 return;
6142
6143 // TODO: Investigate what happens with the next major version of MSVC.
Reid Kleckner1a94d872018-12-17 23:16:43 +00006144 if (Version != LangOptions::MSVC2015 / 100) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006145 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00006146 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00006147 return;
6148 }
6149
Reid Kleckner1a94d872018-12-17 23:16:43 +00006150 // The attribute expects a "major" version number like 19, but new versions of
6151 // MSVC have moved to updating the "minor", or less significant numbers, so we
6152 // have to multiply by 100 now.
6153 Version *= 100;
6154
Erich Keane6a24e802019-09-13 17:39:31 +00006155 D->addAttr(::new (S.Context) LayoutVersionAttr(S.Context, AL, Version));
David Majnemercd3ebfe2016-05-23 17:16:12 +00006156}
6157
Erich Keane6a24e802019-09-13 17:39:31 +00006158DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D,
6159 const AttributeCommonInfo &CI) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006160 if (D->hasAttr<DLLExportAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006161 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00006162 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006163 }
6164
6165 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00006166 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006167
Erich Keane6a24e802019-09-13 17:39:31 +00006168 return ::new (Context) DLLImportAttr(Context, CI);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006169}
6170
Erich Keane6a24e802019-09-13 17:39:31 +00006171DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D,
6172 const AttributeCommonInfo &CI) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006173 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00006174 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006175 D->dropAttr<DLLImportAttr>();
6176 }
6177
6178 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00006179 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006180
Erich Keane6a24e802019-09-13 17:39:31 +00006181 return ::new (Context) DLLExportAttr(Context, CI);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006182}
6183
Erich Keanee891aa92018-07-13 15:07:47 +00006184static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00006185 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
6186 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006187 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00006188 return;
6189 }
6190
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006191 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00006192 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00006193 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
6194 // MinGW doesn't allow dllimport on inline functions.
6195 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00006196 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00006197 return;
6198 }
6199 }
6200
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006201 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00006202 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
6203 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006204 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00006205 return;
6206 }
6207 }
6208
Erich Keanee891aa92018-07-13 15:07:47 +00006209 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Erich Keane6a24e802019-09-13 17:39:31 +00006210 ? (Attr *)S.mergeDLLExportAttr(D, A)
6211 : (Attr *)S.mergeDLLImportAttr(D, A);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006212 if (NewAttr)
6213 D->addAttr(NewAttr);
6214}
6215
David Majnemer2c4e00a2014-01-29 22:07:36 +00006216MSInheritanceAttr *
Erich Keane6a24e802019-09-13 17:39:31 +00006217Sema::mergeMSInheritanceAttr(Decl *D, const AttributeCommonInfo &CI,
6218 bool BestCase,
Reid Klecknera9cc64e2019-11-15 18:49:32 -08006219 MSInheritanceModel Model) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00006220 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
Reid Klecknera9cc64e2019-11-15 18:49:32 -08006221 if (IA->getInheritanceModel() == Model)
Craig Topperc3ec1492014-05-26 06:22:03 +00006222 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006223 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
6224 << 1 /*previous declaration*/;
Erich Keane6a24e802019-09-13 17:39:31 +00006225 Diag(CI.getLoc(), diag::note_previous_ms_inheritance);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006226 D->dropAttr<MSInheritanceAttr>();
6227 }
6228
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006229 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006230 if (RD->hasDefinition()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006231 if (checkMSInheritanceAttrOnDefinition(RD, CI.getRange(), BestCase,
Reid Klecknera9cc64e2019-11-15 18:49:32 -08006232 Model)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006233 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006234 }
6235 } else {
6236 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
Erich Keane6a24e802019-09-13 17:39:31 +00006237 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
David Majnemer2c4e00a2014-01-29 22:07:36 +00006238 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006239 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006240 }
6241 if (RD->getDescribedClassTemplate()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006242 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
David Majnemer2c4e00a2014-01-29 22:07:36 +00006243 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006244 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006245 }
6246 }
6247
Erich Keane6a24e802019-09-13 17:39:31 +00006248 return ::new (Context) MSInheritanceAttr(Context, CI, BestCase);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006249}
6250
Erich Keanee891aa92018-07-13 15:07:47 +00006251static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006252 // The capability attributes take a single string parameter for the name of
6253 // the capability they represent. The lockable attribute does not take any
6254 // parameters. However, semantically, both attributes represent the same
6255 // concept, and so they use the same semantic attribute. Eventually, the
6256 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00006257 //
Alp Toker958027b2014-07-14 19:42:55 +00006258 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00006259 // literal will be considered a "mutex."
6260 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006261 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00006262 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006263 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006264 return;
6265
Erich Keane6a24e802019-09-13 17:39:31 +00006266 D->addAttr(::new (S.Context) CapabilityAttr(S.Context, AL, N));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006267}
6268
Erich Keanee891aa92018-07-13 15:07:47 +00006269static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00006270 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006271 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00006272 return;
6273
Erich Keane6a24e802019-09-13 17:39:31 +00006274 D->addAttr(::new (S.Context)
6275 AssertCapabilityAttr(S.Context, AL, Args.data(), Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006276}
6277
6278static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006279 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006280 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006281 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006282 return;
6283
Erich Keane6a24e802019-09-13 17:39:31 +00006284 D->addAttr(::new (S.Context) AcquireCapabilityAttr(S.Context, AL, Args.data(),
6285 Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006286}
6287
6288static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006289 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006290 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006291 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006292 return;
6293
Erich Keane6a24e802019-09-13 17:39:31 +00006294 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(
6295 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006296}
6297
6298static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006299 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006300 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00006301 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006302 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006303
Erich Keane6a24e802019-09-13 17:39:31 +00006304 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(S.Context, AL, Args.data(),
6305 Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006306}
6307
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006308static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006309 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006310 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006311 return;
6312
6313 // check that all arguments are lockable objects
6314 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006315 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006316 if (Args.empty())
6317 return;
6318
6319 RequiresCapabilityAttr *RCA = ::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00006320 RequiresCapabilityAttr(S.Context, AL, Args.data(), Args.size());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006321
6322 D->addAttr(RCA);
6323}
6324
Erich Keanee891aa92018-07-13 15:07:47 +00006325static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006326 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00006327 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006328 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00006329 // Do not want to attach the attribute to the namespace because that will
6330 // cause confusing diagnostic reports for uses of declarations within the
6331 // namespace.
6332 return;
6333 }
6334 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00006335
Manman Renc7890fe2016-03-16 18:50:49 +00006336 // Handle the cases where the attribute has a text message.
6337 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006338 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
6339 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00006340 return;
6341
6342 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006343 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
6344 checkAttributeAtMostNumArgs(S, AL, 1);
6345 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
Sander de Smalen44a22532018-11-26 16:38:37 +00006346 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
6347 return;
6348
6349 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
6350 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
6351
Erich Keane6a24e802019-09-13 17:39:31 +00006352 D->addAttr(::new (S.Context) DeprecatedAttr(S.Context, AL, Str, Replacement));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006353}
6354
6355static bool isGlobalVar(const Decl *D) {
6356 if (const auto *S = dyn_cast<VarDecl>(D))
6357 return S->hasGlobalStorage();
6358 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00006359}
6360
Erich Keanee891aa92018-07-13 15:07:47 +00006361static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006362 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00006363 return;
6364
Benjamin Kramer1b582012016-02-13 18:11:49 +00006365 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00006366
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006367 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00006368 StringRef SanitizerName;
6369 SourceLocation LiteralLoc;
6370
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006371 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00006372 return;
6373
Pierre Gousseauae5303d2019-03-01 10:05:15 +00006374 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) ==
6375 SanitizerMask())
Peter Collingbourne915df992015-05-15 18:33:32 +00006376 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006377 else if (isGlobalVar(D) && SanitizerName != "address")
6378 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006379 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00006380 Sanitizers.push_back(SanitizerName);
6381 }
6382
Erich Keane6a24e802019-09-13 17:39:31 +00006383 D->addAttr(::new (S.Context) NoSanitizeAttr(S.Context, AL, Sanitizers.data(),
6384 Sanitizers.size()));
Peter Collingbourne915df992015-05-15 18:33:32 +00006385}
6386
6387static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006388 const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00006389 StringRef AttrName = AL.getAttrName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00006390 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006391 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
6392 .Case("no_address_safety_analysis", "address")
6393 .Case("no_sanitize_address", "address")
6394 .Case("no_sanitize_thread", "thread")
6395 .Case("no_sanitize_memory", "memory");
6396 if (isGlobalVar(D) && SanitizerName != "address")
6397 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006398 << AL << ExpectedFunction;
Aaron Ballman31ca49b2019-05-21 17:24:49 +00006399
6400 // FIXME: Rather than create a NoSanitizeSpecificAttr, this creates a
6401 // NoSanitizeAttr object; but we need to calculate the correct spelling list
6402 // index rather than incorrectly assume the index for NoSanitizeSpecificAttr
6403 // has the same spellings as the index for NoSanitizeAttr. We don't have a
6404 // general way to "translate" between the two, so this hack attempts to work
6405 // around the issue with hard-coded indicies. This is critical for calling
6406 // getSpelling() or prettyPrint() on the resulting semantic attribute object
6407 // without failing assertions.
6408 unsigned TranslatedSpellingIndex = 0;
6409 if (AL.isC2xAttribute() || AL.isCXX11Attribute())
6410 TranslatedSpellingIndex = 1;
6411
Erich Keane6a24e802019-09-13 17:39:31 +00006412 AttributeCommonInfo Info = AL;
6413 Info.setAttributeSpellingListIndex(TranslatedSpellingIndex);
6414 D->addAttr(::new (S.Context)
6415 NoSanitizeAttr(S.Context, Info, &SanitizerName, 1));
Peter Collingbourne915df992015-05-15 18:33:32 +00006416}
6417
Erich Keanee891aa92018-07-13 15:07:47 +00006418static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006419 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006420 D->addAttr(Internal);
6421}
6422
Erich Keanee891aa92018-07-13 15:07:47 +00006423static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006424 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006425 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00006426 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006427 else
Erich Keane44bacdf2018-08-09 13:21:32 +00006428 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
6429 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006430}
6431
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006432/// Handles semantic checking for features that are common to all attributes,
6433/// such as checking whether a parameter was properly specified, or the correct
6434/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006435static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006436 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006437 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00006438 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006439 //
6440 // We also bail on unknown and ignored attributes because those are handled
6441 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00006442 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006443 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006444 // Check whether the attribute requires specific language extensions to be
6445 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006446 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00006447 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00006448 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006449 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00006450 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006451 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00006452 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006453
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006454 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006455 // If there are no optional arguments, then checking for the argument count
6456 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006457 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006458 return true;
6459 } else {
6460 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006461 if (AL.getMinArgs() &&
6462 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006463 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006464 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
6465 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006466 return true;
6467 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00006468
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006469 if (S.CheckAttrTarget(AL))
6470 return true;
6471
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006472 return false;
6473}
6474
Erich Keanee891aa92018-07-13 15:07:47 +00006475static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006476 if (D->isInvalidDecl())
6477 return;
6478
6479 // Check if there is only one access qualifier.
6480 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006481 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
6482 AL.getSemanticSpelling()) {
6483 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
Erich Keane6a24e802019-09-13 17:39:31 +00006484 << AL.getAttrName()->getName() << AL.getRange();
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006485 } else {
6486 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
6487 << D->getSourceRange();
6488 D->setInvalidDecl(true);
6489 return;
6490 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00006491 }
6492
6493 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
6494 // image object can be read and written.
6495 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
6496 // object. Using the read_write (or __read_write) qualifier with the pipe
6497 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006498 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006499 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Erich Keane6a24e802019-09-13 17:39:31 +00006500 if (AL.getAttrName()->getName().find("read_write") != StringRef::npos) {
Anastasia Stulova2c4730d2019-02-15 12:07:57 +00006501 if ((!S.getLangOpts().OpenCLCPlusPlus &&
6502 S.getLangOpts().OpenCLVersion < 200) ||
6503 DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006504 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00006505 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00006506 D->setInvalidDecl(true);
6507 return;
6508 }
6509 }
6510 }
6511
Erich Keane6a24e802019-09-13 17:39:31 +00006512 D->addAttr(::new (S.Context) OpenCLAccessAttr(S.Context, AL));
Xiuli Pan11e13f62016-02-26 03:13:03 +00006513}
6514
Mariya Podchishchaevac094e7d2019-11-06 17:35:50 +03006515static void handleSYCLKernelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6516 // The 'sycl_kernel' attribute applies only to function templates.
6517 const auto *FD = cast<FunctionDecl>(D);
6518 const FunctionTemplateDecl *FT = FD->getDescribedFunctionTemplate();
6519 assert(FT && "Function template is expected");
6520
6521 // Function template must have at least two template parameters.
6522 const TemplateParameterList *TL = FT->getTemplateParameters();
6523 if (TL->size() < 2) {
6524 S.Diag(FT->getLocation(), diag::warn_sycl_kernel_num_of_template_params);
6525 return;
6526 }
6527
6528 // Template parameters must be typenames.
6529 for (unsigned I = 0; I < 2; ++I) {
6530 const NamedDecl *TParam = TL->getParam(I);
6531 if (isa<NonTypeTemplateParmDecl>(TParam)) {
6532 S.Diag(FT->getLocation(),
6533 diag::warn_sycl_kernel_invalid_template_param_type);
6534 return;
6535 }
6536 }
6537
6538 // Function must have at least one argument.
6539 if (getFunctionOrMethodNumParams(D) != 1) {
6540 S.Diag(FT->getLocation(), diag::warn_sycl_kernel_num_of_function_params);
6541 return;
6542 }
6543
6544 // Function must return void.
6545 QualType RetTy = getFunctionOrMethodResultType(D);
6546 if (!RetTy->isVoidType()) {
6547 S.Diag(FT->getLocation(), diag::warn_sycl_kernel_return_type);
6548 return;
6549 }
6550
6551 handleSimpleAttribute<SYCLKernelAttr>(S, D, AL);
6552}
6553
Erik Pilkington5a559e62018-08-21 17:24:06 +00006554static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006555 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00006556 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
6557 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
6558 return;
6559 }
6560
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006561 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00006562 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006563 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00006564 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00006565}
6566
JF Bastien14daa202018-12-18 05:12:21 +00006567static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6568 assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic &&
6569 "uninitialized is only valid on automatic duration variables");
Erich Keane6a24e802019-09-13 17:39:31 +00006570 D->addAttr(::new (S.Context) UninitializedAttr(S.Context, AL));
JF Bastien14daa202018-12-18 05:12:21 +00006571}
6572
Erik Pilkington1e368822019-01-04 18:33:06 +00006573static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD,
6574 bool DiagnoseFailure) {
6575 QualType Ty = VD->getType();
6576 if (!Ty->isObjCRetainableType()) {
6577 if (DiagnoseFailure) {
6578 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6579 << 0;
6580 }
6581 return false;
6582 }
6583
6584 Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime();
6585
6586 // Sema::inferObjCARCLifetime must run after processing decl attributes
6587 // (because __block lowers to an attribute), so if the lifetime hasn't been
6588 // explicitly specified, infer it locally now.
6589 if (LifetimeQual == Qualifiers::OCL_None)
6590 LifetimeQual = Ty->getObjCARCImplicitLifetime();
6591
6592 // The attributes only really makes sense for __strong variables; ignore any
6593 // attempts to annotate a parameter with any other lifetime qualifier.
6594 if (LifetimeQual != Qualifiers::OCL_Strong) {
6595 if (DiagnoseFailure) {
6596 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6597 << 1;
6598 }
6599 return false;
6600 }
6601
6602 // Tampering with the type of a VarDecl here is a bit of a hack, but we need
6603 // to ensure that the variable is 'const' so that we can error on
6604 // modification, which can otherwise over-release.
6605 VD->setType(Ty.withConst());
6606 VD->setARCPseudoStrong(true);
6607 return true;
6608}
6609
6610static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D,
6611 const ParsedAttr &AL) {
6612 if (auto *VD = dyn_cast<VarDecl>(D)) {
6613 assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically");
6614 if (!VD->hasLocalStorage()) {
6615 S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6616 << 0;
6617 return;
6618 }
6619
6620 if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true))
6621 return;
6622
6623 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6624 return;
6625 }
6626
6627 // If D is a function-like declaration (method, block, or function), then we
6628 // make every parameter psuedo-strong.
Erik Pilkington2e4f5e62020-02-28 15:24:23 -08006629 unsigned NumParams =
6630 hasFunctionProto(D) ? getFunctionOrMethodNumParams(D) : 0;
6631 for (unsigned I = 0; I != NumParams; ++I) {
Erik Pilkington1e368822019-01-04 18:33:06 +00006632 auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I));
6633 QualType Ty = PVD->getType();
6634
6635 // If a user wrote a parameter with __strong explicitly, then assume they
6636 // want "real" strong semantics for that parameter. This works because if
6637 // the parameter was written with __strong, then the strong qualifier will
6638 // be non-local.
6639 if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() ==
6640 Qualifiers::OCL_Strong)
6641 continue;
6642
6643 tryMakeVariablePseudoStrong(S, PVD, /*DiagnoseFailure=*/false);
6644 }
6645 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6646}
6647
Artem Dergachevc333d772019-02-21 00:01:02 +00006648static void handleMIGServerRoutineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6649 // Check that the return type is a `typedef int kern_return_t` or a typedef
6650 // around it, because otherwise MIG convention checks make no sense.
6651 // BlockDecl doesn't store a return type, so it's annoying to check,
6652 // so let's skip it for now.
6653 if (!isa<BlockDecl>(D)) {
6654 QualType T = getFunctionOrMethodResultType(D);
6655 bool IsKernReturnT = false;
6656 while (const auto *TT = T->getAs<TypedefType>()) {
6657 IsKernReturnT = (TT->getDecl()->getName() == "kern_return_t");
6658 T = TT->desugar();
6659 }
6660 if (!IsKernReturnT || T.getCanonicalType() != S.getASTContext().IntTy) {
6661 S.Diag(D->getBeginLoc(),
6662 diag::warn_mig_server_routine_does_not_return_kern_return_t);
6663 return;
6664 }
6665 }
6666
6667 handleSimpleAttribute<MIGServerRoutineAttr>(S, D, AL);
6668}
6669
Reid Kleckner1181c9f2019-03-25 23:20:18 +00006670static void handleMSAllocatorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6671 // Warn if the return type is not a pointer or reference type.
6672 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6673 QualType RetTy = FD->getReturnType();
6674 if (!RetTy->isPointerType() && !RetTy->isReferenceType()) {
6675 S.Diag(AL.getLoc(), diag::warn_declspec_allocator_nonpointer)
6676 << AL.getRange() << RetTy;
6677 return;
6678 }
6679 }
6680
6681 handleSimpleAttribute<MSAllocatorAttr>(S, D, AL);
6682}
6683
Gabor Horvathfe17b302019-12-04 16:12:50 -08006684static void handeAcquireHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6685 if (AL.isUsedAsTypeAttr())
6686 return;
6687 // Warn if the parameter is definitely not an output parameter.
6688 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
6689 if (PVD->getType()->isIntegerType()) {
6690 S.Diag(AL.getLoc(), diag::err_attribute_output_parameter)
6691 << AL.getRange();
6692 return;
6693 }
6694 }
6695 StringRef Argument;
6696 if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument))
6697 return;
6698 D->addAttr(AcquireHandleAttr::Create(S.Context, Argument, AL));
6699}
6700
6701template<typename Attr>
6702static void handleHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6703 StringRef Argument;
6704 if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument))
6705 return;
6706 D->addAttr(Attr::Create(S.Context, Argument, AL));
6707}
6708
Andrew Paverdbdd88b72020-01-10 11:08:18 +00006709static void handleCFGuardAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6710 // The guard attribute takes a single identifier argument.
6711
6712 if (!AL.isArgIdent(0)) {
6713 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
6714 << AL << AANT_ArgumentIdentifier;
6715 return;
6716 }
6717
6718 CFGuardAttr::GuardArg Arg;
6719 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
6720 if (!CFGuardAttr::ConvertStrToGuardArg(II->getName(), Arg)) {
6721 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
6722 return;
6723 }
6724
6725 D->addAttr(::new (S.Context) CFGuardAttr(S.Context, AL, Arg));
6726}
6727
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00006728//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006729// Top Level Sema Entry Points
6730//===----------------------------------------------------------------------===//
6731
Richard Smithf8a75c32013-08-29 00:47:48 +00006732/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
6733/// the attribute applies to decls. If the attribute is a type attribute, just
6734/// silently ignore it if a GNU attribute.
6735static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006736 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00006737 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00006738 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00006739 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00006740
Richard Smithf8a75c32013-08-29 00:47:48 +00006741 // Ignore C++11 attributes on declarator chunks: they appertain to the type
6742 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006743 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00006744 return;
6745
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006746 // Unknown attributes are automatically warned on. Target-specific attributes
6747 // which do not apply to the current target architecture are treated as
6748 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006749 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006750 !AL.existsInTarget(S.Context.getTargetInfo())) {
Simon Pilgrimfc0ff612018-12-17 12:17:37 +00006751 S.Diag(AL.getLoc(),
6752 AL.isDeclspecAttribute()
6753 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
6754 : (unsigned)diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006755 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006756 return;
6757 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006758
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006759 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006760 return;
6761
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006762 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006763 default:
John Brawnfa0320d2020-02-28 14:51:30 +00006764 if (AL.getInfo().handleDeclAttribute(S, D, AL) != ParsedAttrInfo::NotHandled)
6765 break;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006766 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00006767 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006768 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00006769 break;
6770 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006771 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00006772 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006773 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006774 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006775 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006776 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006777 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006778 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006779 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006780 case ParsedAttr::AT_DLLExport:
6781 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006782 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006783 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006784 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006785 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006786 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006787 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006788 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006789 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006790 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006791 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006792 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006793 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006794 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006795 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006796 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006797 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006798 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006799 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006800 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006801 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006802 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006803 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006804 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006805 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006806 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006807 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006808 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006809 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006810 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006811 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006812 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006813 break;
Yonghong Song4e2ce222019-11-01 22:16:59 -07006814 case ParsedAttr::AT_BPFPreserveAccessIndex:
6815 handleBPFPreserveAccessIndexAttr(S, D, AL);
6816 break;
Sam Clegg881d8772019-11-05 10:15:56 -08006817 case ParsedAttr::AT_WebAssemblyExportName:
6818 handleWebAssemblyExportNameAttr(S, D, AL);
6819 break;
Dan Gohmanb4323692019-01-24 21:08:30 +00006820 case ParsedAttr::AT_WebAssemblyImportModule:
6821 handleWebAssemblyImportModuleAttr(S, D, AL);
6822 break;
Dan Gohmancae84592019-02-01 22:25:23 +00006823 case ParsedAttr::AT_WebAssemblyImportName:
6824 handleWebAssemblyImportNameAttr(S, D, AL);
6825 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006826 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006827 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006828 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006829 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006830 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006831 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006832 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006833 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006834 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006835 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006836 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006837 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006838 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006839 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006840 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006841 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006842 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006843 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006844 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006845 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006846 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006847 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006848 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006849 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006850 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006851 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006852 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006853 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006854 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006855 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006856 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006857 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006858 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006859 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006860 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006861 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006862 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006863 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006864 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006865 case ParsedAttr::AT_CPUDispatch:
6866 case ParsedAttr::AT_CPUSpecific:
6867 handleCPUSpecificAttr(S, D, AL);
6868 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006869 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006870 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006871 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006872 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006873 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006874 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006875 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006876 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006877 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006878 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006879 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006880 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006881 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006882 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006883 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006884 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006885 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006886 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006887 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006888 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006889 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006890 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006891 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006892 break;
Guillaume Chatelet98f31512019-09-25 11:31:28 +02006893 case ParsedAttr::AT_NoBuiltin:
6894 handleNoBuiltinAttr(S, D, AL);
6895 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006896 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006897 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006898 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006899 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006900 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006901 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006902 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006903 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006904 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006905 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006906 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006907 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006908 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006909 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006910 break;
Mariya Podchishchaevac094e7d2019-11-06 17:35:50 +03006911 case ParsedAttr::AT_SYCLKernel:
6912 handleSYCLKernelAttr(S, D, AL);
6913 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006914 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006915 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006916 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006917 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006918 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006919 break;
Johannes Doerfertac991bb2019-01-19 05:36:54 +00006920 case ParsedAttr::AT_Callback:
6921 handleCallbackAttr(S, D, AL);
6922 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006923 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006924 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006925 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006926 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006927 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006928 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006929 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006930 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006931 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006932 break;
Yaxun Liuc3dfe902019-06-26 03:47:37 +00006933 case ParsedAttr::AT_HIPPinnedShadow:
6934 handleSimpleAttributeWithExclusions<HIPPinnedShadowAttr, CUDADeviceAttr,
6935 CUDAConstantAttr>(S, D, AL);
6936 break;
Michael Liao5be9b8c2020-03-27 15:47:12 -04006937 case ParsedAttr::AT_CUDADeviceBuiltinSurfaceType:
6938 handleSimpleAttributeWithExclusions<CUDADeviceBuiltinSurfaceTypeAttr,
6939 CUDADeviceBuiltinTextureTypeAttr>(S, D,
6940 AL);
6941 break;
6942 case ParsedAttr::AT_CUDADeviceBuiltinTextureType:
6943 handleSimpleAttributeWithExclusions<CUDADeviceBuiltinTextureTypeAttr,
6944 CUDADeviceBuiltinSurfaceTypeAttr>(S, D,
6945 AL);
6946 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006947 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006948 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006949 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006950 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006951 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006952 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006953 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006954 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006955 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006956 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006957 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006958 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006959 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006960 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6961 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006962 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006963 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006964 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006965 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006966 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006967 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006968 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006969 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006970 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006971 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006972 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006973 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006974 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006975 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006976 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006977 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006978 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006979 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006980 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006981 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006982 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006983 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006984 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006985 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006986 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006987 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006988 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006989 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006990 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006991 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006992 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006993 handleNoCfCheckAttr(S, D, AL);
6994 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006995 case ParsedAttr::AT_NoThrow:
Erich Keaned02f4a12019-05-30 17:31:54 +00006996 if (!AL.isUsedAsTypeAttr())
6997 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006998 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006999 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007000 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007001 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007002 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007003 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007004 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007005 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007006 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007007 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007008 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007009 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007010 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007011 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007012 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007013 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007014 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007015 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007016 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007017 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007018 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007019 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007020 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007021 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007022 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007023 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007024 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007025 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007026 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007027 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007028 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007029 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007030 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00007031 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007032 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007033 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00007034 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007035 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00007036 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
7037 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007038 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007039 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00007040 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
7041 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007042 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007043 case ParsedAttr::AT_CFConsumed:
7044 case ParsedAttr::AT_NSConsumed:
George Karpenkov1657f362018-11-30 02:18:37 +00007045 case ParsedAttr::AT_OSConsumed:
Erich Keane6a24e802019-09-13 17:39:31 +00007046 S.AddXConsumedAttr(D, AL, parsedAttrToRetainOwnershipKind(AL),
7047 /*IsTemplateInstantiation=*/false);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007048 break;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00007049 case ParsedAttr::AT_OSReturnsRetainedOnZero:
7050 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnZeroAttr>(
7051 S, D, AL, isValidOSObjectOutParameter(D),
7052 diag::warn_ns_attribute_wrong_parameter_type,
7053 /*Extra Args=*/AL, /*pointer-to-OSObject-pointer*/ 3, AL.getRange());
7054 break;
7055 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
7056 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnNonZeroAttr>(
7057 S, D, AL, isValidOSObjectOutParameter(D),
7058 diag::warn_ns_attribute_wrong_parameter_type,
7059 /*Extra Args=*/AL, /*pointer-to-OSObject-poointer*/ 3, AL.getRange());
7060 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007061 case ParsedAttr::AT_NSReturnsAutoreleased:
7062 case ParsedAttr::AT_NSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00007063 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00007064 case ParsedAttr::AT_CFReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00007065 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00007066 case ParsedAttr::AT_OSReturnsNotRetained:
7067 case ParsedAttr::AT_OSReturnsRetained:
7068 handleXReturnsXRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007069 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007070 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007071 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007072 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007073 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007074 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007075 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007076 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007077 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00007078 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007079 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007080 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007081 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007082 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007083 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007084 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007085 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007086 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007087 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007088 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007089 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007090 break;
Zola Bridgescbac3ad2018-11-27 19:56:46 +00007091 case ParsedAttr::AT_SpeculativeLoadHardening:
Zola Bridges826ef592019-01-18 17:20:46 +00007092 handleSimpleAttributeWithExclusions<SpeculativeLoadHardeningAttr,
7093 NoSpeculativeLoadHardeningAttr>(S, D,
7094 AL);
7095 break;
7096 case ParsedAttr::AT_NoSpeculativeLoadHardening:
7097 handleSimpleAttributeWithExclusions<NoSpeculativeLoadHardeningAttr,
7098 SpeculativeLoadHardeningAttr>(S, D, AL);
Zola Bridgescbac3ad2018-11-27 19:56:46 +00007099 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00007100 case ParsedAttr::AT_CodeSeg:
7101 handleCodeSegAttr(S, D, AL);
7102 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007103 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007104 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00007105 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007106 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00007107 handleMinVectorWidthAttr(S, D, AL);
7108 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007109 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007110 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00007111 break;
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08007112 case ParsedAttr::AT_ObjCDirect:
7113 handleObjCDirectAttr(S, D, AL);
7114 break;
7115 case ParsedAttr::AT_ObjCDirectMembers:
7116 handleObjCDirectMembersAttr(S, D, AL);
7117 handleSimpleAttribute<ObjCDirectMembersAttr>(S, D, AL);
7118 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007119 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007120 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00007121 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007122 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007123 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007124 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007125 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00007126 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
7127 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00007128 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007129 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00007130 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
7131 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00007132 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007133 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007134 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00007135 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007136 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007137 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00007138 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007139 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007140 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00007141 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007142 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007143 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007144 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007145 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007146 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007147 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007148 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007149 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007150 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007151 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007152 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00007153 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007154 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007155 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007156 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007157 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007158 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00007159 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007160 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007161 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007162 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007163 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007164 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007165 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007166 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007167 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007168 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007169 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007170 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007171 break;
Momchil Velikov080d0462020-03-24 09:32:51 +00007172 case ParsedAttr::AT_CmseNSEntry:
7173 handleCmseNSEntryAttr(S, D, AL);
7174 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007175 case ParsedAttr::AT_StdCall:
7176 case ParsedAttr::AT_CDecl:
7177 case ParsedAttr::AT_FastCall:
7178 case ParsedAttr::AT_ThisCall:
7179 case ParsedAttr::AT_Pascal:
7180 case ParsedAttr::AT_RegCall:
7181 case ParsedAttr::AT_SwiftCall:
7182 case ParsedAttr::AT_VectorCall:
7183 case ParsedAttr::AT_MSABI:
7184 case ParsedAttr::AT_SysVABI:
7185 case ParsedAttr::AT_Pcs:
7186 case ParsedAttr::AT_IntelOclBicc:
7187 case ParsedAttr::AT_PreserveMost:
7188 case ParsedAttr::AT_PreserveAll:
Sander de Smalen44a22532018-11-26 16:38:37 +00007189 case ParsedAttr::AT_AArch64VectorPcs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007190 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00007191 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007192 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007193 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00007194 break;
Matthias Gehred293cbd2019-07-25 17:50:51 +00007195 case ParsedAttr::AT_Owner:
7196 case ParsedAttr::AT_Pointer:
7197 handleLifetimeCategoryAttr(S, D, AL);
7198 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007199 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007200 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007201 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007202 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007203 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00007204 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007205 case ParsedAttr::AT_SwiftContext:
Erich Keane6a24e802019-09-13 17:39:31 +00007206 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00007207 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007208 case ParsedAttr::AT_SwiftErrorResult:
Erich Keane6a24e802019-09-13 17:39:31 +00007209 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00007210 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007211 case ParsedAttr::AT_SwiftIndirectResult:
Erich Keane6a24e802019-09-13 17:39:31 +00007212 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00007213 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007214 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007215 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00007216 break;
John McCall8d32c052012-05-22 21:28:12 +00007217
7218 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007219 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007220 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007221 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007222 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007223 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00007224 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007225 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007226 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007227 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007228 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007229 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00007230 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00007231
Erich Keanee891aa92018-07-13 15:07:47 +00007232 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007233 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00007234 break;
Andrew Paverdbdd88b72020-01-10 11:08:18 +00007235 case ParsedAttr::AT_CFGuard:
7236 handleCFGuardAttr(S, D, AL);
7237 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007238
7239 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007240 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007241 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007242 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007243 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007244 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007245 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007246 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007247 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007248 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007249 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007250 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00007251 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007252 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007253 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00007254 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007255 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007256 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007257 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007258 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007259 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007260 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007261 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007262 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007263 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007264 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007265 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007266 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007267 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007268 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007269 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007270 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007271 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007272 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007273 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007274 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007275 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007276 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007277 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007278 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007279
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007280 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007281 case ParsedAttr::AT_Capability:
7282 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007283 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007284 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007285 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007286 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007287 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00007288
Erich Keanee891aa92018-07-13 15:07:47 +00007289 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007290 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007291 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007292 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007293 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007294 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007295 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007296 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007297 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007298 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007299 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007300 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007301
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00007302 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007303 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007304 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00007305 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007306 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007307 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007308 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007309 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007310 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00007311 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007312 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007313 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00007314 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007315 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007316 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007317 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007318 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007319 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007320 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007321
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007322 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007323 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007324 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007325 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007326 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007327 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007328 break;
John Brawnfa0320d2020-02-28 14:51:30 +00007329
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007330 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007331 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007332 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00007333 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00007334
Fangrui Songa44c4342020-01-04 15:39:19 -08007335 case ParsedAttr::AT_PatchableFunctionEntry:
7336 handlePatchableFunctionEntryAttr(S, D, AL);
7337 break;
7338
Erik Pilkington5a559e62018-08-21 17:24:06 +00007339 case ParsedAttr::AT_AlwaysDestroy:
7340 case ParsedAttr::AT_NoDestroy:
7341 handleDestroyAttr(S, D, AL);
7342 break;
JF Bastien14daa202018-12-18 05:12:21 +00007343
7344 case ParsedAttr::AT_Uninitialized:
7345 handleUninitializedAttr(S, D, AL);
7346 break;
Erik Pilkington1e368822019-01-04 18:33:06 +00007347
Jon Chesterfieldc45eaea2020-03-17 21:22:04 +00007348 case ParsedAttr::AT_LoaderUninitialized:
7349 handleSimpleAttribute<LoaderUninitializedAttr>(S, D, AL);
7350 break;
7351
Erik Pilkington1e368822019-01-04 18:33:06 +00007352 case ParsedAttr::AT_ObjCExternallyRetained:
7353 handleObjCExternallyRetainedAttr(S, D, AL);
7354 break;
Erik Pilkingtone3cd7352019-02-11 23:21:39 +00007355
Artem Dergachevc333d772019-02-21 00:01:02 +00007356 case ParsedAttr::AT_MIGServerRoutine:
7357 handleMIGServerRoutineAttr(S, D, AL);
7358 break;
Reid Kleckner1181c9f2019-03-25 23:20:18 +00007359
7360 case ParsedAttr::AT_MSAllocator:
7361 handleMSAllocatorAttr(S, D, AL);
7362 break;
Simon Tatham7c11da02019-09-02 15:35:09 +01007363
Mikhail Maltsev47edf5ba2020-03-10 14:01:42 +00007364 case ParsedAttr::AT_ArmBuiltinAlias:
7365 handleArmBuiltinAliasAttr(S, D, AL);
Simon Tatham7c11da02019-09-02 15:35:09 +01007366 break;
Gabor Horvathfe17b302019-12-04 16:12:50 -08007367
7368 case ParsedAttr::AT_AcquireHandle:
7369 handeAcquireHandleAttr(S, D, AL);
7370 break;
7371
7372 case ParsedAttr::AT_ReleaseHandle:
7373 handleHandleAttr<ReleaseHandleAttr>(S, D, AL);
7374 break;
7375
7376 case ParsedAttr::AT_UseHandle:
7377 handleHandleAttr<UseHandleAttr>(S, D, AL);
7378 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007379 }
7380}
7381
7382/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
7383/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00007384void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00007385 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00007386 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00007387 if (AttrList.empty())
7388 return;
7389
Erich Keanee891aa92018-07-13 15:07:47 +00007390 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00007391 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00007392
Joey Gouly2cd9db12013-12-13 16:15:28 +00007393 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00007394 // GCC accepts
7395 // static int a9 __attribute__((weakref));
7396 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00007397 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00007398 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
7399 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00007400 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00007401 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007402 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007403
Aaron Ballmanbe243a72014-12-04 22:45:31 +00007404 // FIXME: We should be able to handle this in TableGen as well. It would be
7405 // good to have a way to specify "these attributes must appear as a group",
7406 // for these. Additionally, it would be good to have a way to specify "these
7407 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00007408 if (!D->hasAttr<OpenCLKernelAttr>()) {
7409 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007410 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00007411 // FIXME: This emits a different error message than
7412 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00007413 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007414 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007415 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007416 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007417 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007418 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007419 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007420 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007421 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00007422 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
7423 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00007424 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
7425 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
7426 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7427 << A << ExpectedKernelFunction;
7428 D->setInvalidDecl();
7429 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
7430 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7431 << A << ExpectedKernelFunction;
7432 D->setInvalidDecl();
7433 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
7434 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7435 << A << ExpectedKernelFunction;
7436 D->setInvalidDecl();
7437 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
7438 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7439 << A << ExpectedKernelFunction;
7440 D->setInvalidDecl();
7441 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007442 }
7443 }
Erik Pilkington81d3f452019-02-13 20:32:37 +00007444
7445 // Do this check after processing D's attributes because the attribute
7446 // objc_method_family can change whether the given method is in the init
7447 // family, and it can be applied after objc_designated_initializer. This is a
7448 // bit of a hack, but we need it to be compatible with versions of clang that
7449 // processed the attribute list in the wrong order.
7450 if (D->hasAttr<ObjCDesignatedInitializerAttr>() &&
7451 cast<ObjCMethodDecl>(D)->getMethodFamily() != OMF_init) {
7452 Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
7453 D->dropAttr<ObjCDesignatedInitializerAttr>();
7454 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007455}
7456
Yonghong Song4e2ce222019-11-01 22:16:59 -07007457// Helper for delayed processing TransparentUnion or BPFPreserveAccessIndexAttr
7458// attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007459void Sema::ProcessDeclAttributeDelayed(Decl *D,
7460 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007461 for (const ParsedAttr &AL : AttrList)
7462 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00007463 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00007464 break;
7465 }
Yonghong Song4e2ce222019-11-01 22:16:59 -07007466
7467 // For BPFPreserveAccessIndexAttr, we want to populate the attributes
7468 // to fields and inner records as well.
7469 if (D && D->hasAttr<BPFPreserveAccessIndexAttr>())
7470 handleBPFPreserveAIRecord(*this, cast<RecordDecl>(D));
Erich Keane2fe684b2017-02-28 20:44:39 +00007471}
7472
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007473// Annotation attributes are the only attributes allowed after an access
7474// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00007475bool Sema::ProcessAccessDeclAttributeList(
7476 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007477 for (const ParsedAttr &AL : AttrList) {
7478 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00007479 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007480 } else {
Erich Keanec480f302018-07-12 21:09:05 +00007481 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007482 return true;
7483 }
7484 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007485 return false;
7486}
7487
John McCall42856de2011-10-01 05:17:03 +00007488/// checkUnusedDeclAttributes - Check a list of attributes to see if it
7489/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00007490static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00007491 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00007492 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007493 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
7494 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00007495 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00007496 continue;
John McCall42856de2011-10-01 05:17:03 +00007497
Erich Keanee891aa92018-07-13 15:07:47 +00007498 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00007499 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00007500 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007501 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00007502 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
7503 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007504 }
7505 }
7506}
7507
7508/// checkUnusedDeclAttributes - Given a declarator which is not being
7509/// used to build a declaration, complain about any decl attributes
7510/// which might be lying around on it.
7511void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00007512 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00007513 ::checkUnusedDeclAttributes(*this, D.getAttributes());
7514 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
7515 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
7516}
7517
Ryan Flynn7d470f32009-07-30 03:15:39 +00007518/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00007519/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00007520NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
7521 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00007522 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00007523 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007524 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00007525 FunctionDecl *NewFD;
7526 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00007527 // FIXME: Mangling?
7528 // FIXME: Is the qualifier info correct?
7529 // FIXME: Is the DeclContext correct?
Gauthier Harnisch796ed032019-06-14 08:56:20 +00007530 NewFD = FunctionDecl::Create(
7531 FD->getASTContext(), FD->getDeclContext(), Loc, Loc,
7532 DeclarationName(II), FD->getType(), FD->getTypeSourceInfo(), SC_None,
Saar Razb65b1f32020-01-09 15:07:51 +02007533 false /*isInlineSpecified*/, FD->hasPrototype(), CSK_unspecified,
7534 FD->getTrailingRequiresClause());
Eli Friedmance3e2c82011-09-07 04:05:06 +00007535 NewD = NewFD;
7536
7537 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00007538 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00007539
7540 // Fake up parameter variables; they are declared as if this were
7541 // a typedef.
7542 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007543 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00007544 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00007545 for (const auto &AI : FT->param_types()) {
7546 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007547 Param->setScopeInfo(0, Params.size());
7548 Params.push_back(Param);
7549 }
David Blaikie9c70e042011-09-21 18:16:56 +00007550 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00007551 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007552 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00007553 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00007554 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00007555 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00007556 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007557 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00007558 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00007559 }
7560 return NewD;
7561}
7562
James Dennett634962f2012-06-14 21:40:34 +00007563/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00007564/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00007565void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00007566 if (W.getUsed()) return; // only do this once
7567 W.setUsed(true);
7568 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
7569 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00007570 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Erich Keane6a24e802019-09-13 17:39:31 +00007571 NewD->addAttr(
7572 AliasAttr::CreateImplicit(Context, NDId->getName(), W.getLocation()));
7573 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation(),
7574 AttributeCommonInfo::AS_Pragma));
Chris Lattnere6eab982009-09-08 18:10:11 +00007575 WeakTopLevelDecl.push_back(NewD);
7576 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
7577 // to insert Decl at TU scope, sorry.
7578 DeclContext *SavedContext = CurContext;
7579 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00007580 NewD->setDeclContext(CurContext);
7581 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00007582 PushOnScopeChains(NewD, S);
7583 CurContext = SavedContext;
7584 } else { // just add weak to existing
Erich Keane6a24e802019-09-13 17:39:31 +00007585 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation(),
7586 AttributeCommonInfo::AS_Pragma));
Ryan Flynn7d470f32009-07-30 03:15:39 +00007587 }
7588}
7589
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007590void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
7591 // It's valid to "forward-declare" #pragma weak, in which case we
7592 // have to do this.
7593 LoadExternalWeakUndeclaredIdentifiers();
7594 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00007595 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007596 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007597 if (VD->isExternC())
7598 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007599 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007600 if (FD->isExternC())
7601 ND = FD;
7602 if (ND) {
7603 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00007604 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007605 if (I != WeakUndeclaredIdentifiers.end()) {
7606 WeakInfo W = I->second;
7607 DeclApplyPragmaWeak(S, ND, W);
7608 WeakUndeclaredIdentifiers[Id] = W;
7609 }
7610 }
7611 }
7612 }
7613}
7614
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007615/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
7616/// it, apply them to D. This is a bit tricky because PD can have attributes
7617/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00007618void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007619 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00007620 if (!PD.getDeclSpec().getAttributes().empty())
7621 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00007622
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007623 // Walk the declarator structure, applying decl attributes that were in a type
7624 // position to the decl itself. This handles cases like:
7625 // int *__attr__(x)** D;
7626 // when X is a decl attribute.
7627 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00007628 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
7629 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00007630
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007631 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00007632 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00007633
7634 // Apply additional attributes specified by '#pragma clang attribute'.
7635 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007636}
John McCall28a6aea2009-11-04 02:18:39 +00007637
John McCall31168b02011-06-15 23:02:42 +00007638/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00007639/// If so, it'll still be annotated with an attribute that makes it
7640/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007641static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00007642 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00007643 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00007644 // Private ivars are always okay. Unfortunately, people don't
7645 // always properly make their ivars private, even in system headers.
7646 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007647 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
7648 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00007649 return false;
7650
John McCallc6af8c62015-10-28 05:03:19 +00007651 // Silently accept unsupported uses of __weak in both user and system
7652 // declarations when it's been disabled, for ease of integration with
7653 // -fno-objc-arc files. We do have to take some care against attempts
7654 // to define such things; for now, we've only done that for ivars
7655 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007656 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00007657 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
7658 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
7659 reason = UnavailableAttr::IR_ForbiddenWeak;
7660 return true;
7661 }
John McCallb61e14e2015-10-27 04:54:50 +00007662 }
7663
John McCallc6af8c62015-10-28 05:03:19 +00007664 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007665 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00007666 // Currently, all the failures dealt with this way are due to ARC
7667 // restrictions.
7668 reason = UnavailableAttr::IR_ARCForbiddenType;
7669 return true;
John McCallb61e14e2015-10-27 04:54:50 +00007670 }
7671
7672 return false;
John McCall31168b02011-06-15 23:02:42 +00007673}
7674
7675/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007676static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
7677 Decl *D) {
7678 auto Reason = UnavailableAttr::IR_None;
7679 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
7680 assert(Reason && "didn't set reason?");
7681 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00007682 return;
7683 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00007684 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007685 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00007686 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007687 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007688 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007689 DD.getForbiddenTypeDiagnostic() ==
7690 diag::err_arc_array_param_no_ownership) {
7691 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007692 return;
7693 }
7694 }
John McCall31168b02011-06-15 23:02:42 +00007695
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007696 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
7697 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
7698 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00007699}
7700
Aaron Ballmanfb237522014-10-15 15:37:51 +00007701
John McCall2ec85372012-05-07 06:16:41 +00007702void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7703 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007704 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007705 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007706
John McCall2ec85372012-05-07 06:16:41 +00007707 // When delaying diagnostics to run in the context of a parsed
7708 // declaration, we only want to actually emit anything if parsing
7709 // succeeds.
7710 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007711
John McCall2ec85372012-05-07 06:16:41 +00007712 // We emit all the active diagnostics in this pool or any of its
7713 // parents. In general, we'll get one pool for the decl spec
7714 // and a child pool for each declarator; in a decl group like:
7715 // deprecated_typedef foo, *bar, baz();
7716 // only the declarator pops will be passed decls. This is correct;
7717 // we really do need to consider delayed diagnostics from the decl spec
7718 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007719 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007720 do {
Richard Smith5c9b3b72018-09-25 22:12:44 +00007721 bool AnyAccessFailures = false;
John McCall6347b682012-05-07 06:16:58 +00007722 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007723 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7724 // This const_cast is a bit lame. Really, Triggered should be mutable.
7725 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007726 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007727 continue;
7728
John McCallc1465822011-02-14 07:13:47 +00007729 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007730 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007731 // Don't bother giving deprecation/unavailable diagnostics if
7732 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007733 if (!decl->isInvalidDecl())
Reid Klecknerdd8e0a02020-01-24 15:16:22 -08007734 handleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00007735 break;
7736
7737 case DelayedDiagnostic::Access:
Richard Smith5c9b3b72018-09-25 22:12:44 +00007738 // Only produce one access control diagnostic for a structured binding
7739 // declaration: we don't need to tell the user that all the fields are
7740 // inaccessible one at a time.
7741 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
7742 continue;
John McCall2ec85372012-05-07 06:16:41 +00007743 HandleDelayedAccessCheck(diag, decl);
Richard Smith5c9b3b72018-09-25 22:12:44 +00007744 if (diag.Triggered)
7745 AnyAccessFailures = true;
John McCall86121512010-01-27 03:50:35 +00007746 break;
John McCall31168b02011-06-15 23:02:42 +00007747
7748 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007749 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007750 break;
John McCall86121512010-01-27 03:50:35 +00007751 }
7752 }
John McCall2ec85372012-05-07 06:16:41 +00007753 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007754}
7755
John McCall6347b682012-05-07 06:16:58 +00007756/// Given a set of delayed diagnostics, re-emit them as if they had
7757/// been delayed in the current context instead of in the given pool.
7758/// Essentially, this just moves them to the current pool.
7759void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7760 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7761 assert(curPool && "re-emitting in undelayed context not supported");
7762 curPool->steal(pool);
7763}