blob: f8a34573f3c828b2101842e000d8e09fa7f4a30b [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner2c6fcf52008-06-26 18:38:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements decl-related attribute processing.
10//
11//===----------------------------------------------------------------------===//
12
David Majnemer929025d2016-01-26 19:30:26 +000013#include "clang/AST/ASTConsumer.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000014#include "clang/AST/ASTContext.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000015#include "clang/AST/ASTMutationListener.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000021#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Erik Pilkington5cd57172016-08-16 17:44:11 +000023#include "clang/AST/RecursiveASTVisitor.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000024#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000026#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000027#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000028#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000029#include "clang/Sema/DelayedDiagnostic.h"
Artem Belevichbcec9da2016-06-06 22:54:57 +000030#include "clang/Sema/Initialization.h"
John McCallf1e8b342011-09-29 07:17:38 +000031#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000032#include "clang/Sema/Scope.h"
Reid Kleckner04f9bca2018-03-07 22:48:35 +000033#include "clang/Sema/ScopeInfo.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000034#include "clang/Sema/SemaInternal.h"
George Burgess IV177399e2017-01-09 04:12:14 +000035#include "llvm/ADT/STLExtras.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000036#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000037#include "llvm/Support/MathExtras.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000038
Chris Lattner2c6fcf52008-06-26 18:38:35 +000039using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000040using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000041
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000042namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000043 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000044 C,
45 Cpp,
46 ObjC
47 };
Eugene Zelenko1ced5092016-02-12 22:53:10 +000048} // end namespace AttributeLangSupport
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000049
Chris Lattner58418ff2008-06-29 00:16:31 +000050//===----------------------------------------------------------------------===//
51// Helper functions
52//===----------------------------------------------------------------------===//
53
Ted Kremenek527042b2009-08-14 20:49:40 +000054/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000055/// type (function or function-typed variable) or an Objective-C
56/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000058 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000059}
Eugene Zelenko1ced5092016-02-12 22:53:10 +000060
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000061/// Return true if the given decl has function type (function or
David Majnemer06864812015-04-07 06:01:53 +000062/// function-typed variable) or an Objective-C method or a block.
63static bool isFunctionOrMethodOrBlock(const Decl *D) {
64 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
65}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000066
John McCall3882ace2011-01-05 12:14:39 +000067/// Return true if the given decl has a declarator that should have
68/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000069static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000070 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000071 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
72 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000073}
74
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000075/// hasFunctionProto - Return true if the given decl has a argument
76/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000077/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000079 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000080 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000081 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000082}
83
Alp Toker601b22c2014-01-21 23:35:24 +000084/// getFunctionOrMethodNumParams - Return number of function or method
85/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000086/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000087static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000088 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000089 return cast<FunctionProtoType>(FnTy)->getNumParams();
Aaron Ballmana70c6b52018-02-15 16:20:20 +000090 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000091 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000092 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000093}
94
Erik Pilkington1e368822019-01-04 18:33:06 +000095static const ParmVarDecl *getFunctionOrMethodParam(const Decl *D,
96 unsigned Idx) {
97 if (const auto *FD = dyn_cast<FunctionDecl>(D))
98 return FD->getParamDecl(Idx);
99 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
100 return MD->getParamDecl(Idx);
101 if (const auto *BD = dyn_cast<BlockDecl>(D))
102 return BD->getParamDecl(Idx);
103 return nullptr;
104}
105
Alp Toker601b22c2014-01-21 23:35:24 +0000106static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000107 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +0000108 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000109 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000110 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000111
Alp Toker03376dc2014-07-07 09:02:20 +0000112 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000113}
114
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000115static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
Erik Pilkington1e368822019-01-04 18:33:06 +0000116 if (auto *PVD = getFunctionOrMethodParam(D, Idx))
117 return PVD->getSourceRange();
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000118 return SourceRange();
119}
120
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000121static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000122 if (const FunctionType *FnTy = D->getFunctionType())
George Burgess IV00f70bd2018-03-01 05:43:23 +0000123 return FnTy->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000124 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000125}
126
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000127static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
128 if (const auto *FD = dyn_cast<FunctionDecl>(D))
129 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000130 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000131 return MD->getReturnTypeSourceRange();
132 return SourceRange();
133}
134
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000135static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000136 if (const FunctionType *FnTy = D->getFunctionType())
137 return cast<FunctionProtoType>(FnTy)->isVariadic();
138 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballmane13d0092014-08-01 17:02:34 +0000139 return BD->isVariadic();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000140 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000141}
142
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000143static bool isInstanceMethod(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000144 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000145 return MethodDecl->isInstance();
146 return false;
147}
148
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000149static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000150 const auto *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000151 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000152 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000153
John McCall96fa4842010-05-17 21:00:27 +0000154 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
155 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000156 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000157
John McCall96fa4842010-05-17 21:00:27 +0000158 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000159
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000160 // FIXME: Should we walk the chain of classes?
161 return ClsName == &Ctx.Idents.get("NSString") ||
162 ClsName == &Ctx.Idents.get("NSMutableString");
163}
164
Daniel Dunbar980c6692008-09-26 03:32:58 +0000165static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000166 const auto *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000167 if (!PT)
168 return false;
169
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000170 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000171 if (!RT)
172 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000173
Daniel Dunbar980c6692008-09-26 03:32:58 +0000174 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000175 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000176 return false;
177
178 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
179}
180
Erich Keanee891aa92018-07-13 15:07:47 +0000181static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
Richard Smithb87c4652013-10-31 21:23:20 +0000182 // FIXME: Include the type in the argument list.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000183 return AL.getNumArgs() + AL.hasParsedType();
Richard Smithb87c4652013-10-31 21:23:20 +0000184}
185
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000186template <typename Compare>
Erich Keanee891aa92018-07-13 15:07:47 +0000187static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000188 unsigned Num, unsigned Diag,
189 Compare Comp) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000190 if (Comp(getNumAttributeArgs(AL), Num)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000191 S.Diag(AL.getLoc(), Diag) << AL << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000192 return false;
193 }
194
195 return true;
196}
197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000198/// Check if the attribute has exactly as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000199/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000200static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000201 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000202 diag::err_attribute_wrong_number_arguments,
203 std::not_equal_to<unsigned>());
204}
205
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000206/// Check if the attribute has at least as many args as Num. May
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000207/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000208static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL,
Richard Smithb87c4652013-10-31 21:23:20 +0000209 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000210 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000211 diag::err_attribute_too_few_arguments,
212 std::less<unsigned>());
213}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000214
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000215/// Check if the attribute has at most as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000216/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000217static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL,
218 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000219 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000220 diag::err_attribute_too_many_arguments,
221 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000222}
223
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000224/// A helper function to provide Attribute Location for the Attr types
Erich Keanee891aa92018-07-13 15:07:47 +0000225/// AND the ParsedAttr.
Erich Keane623efd82017-03-30 21:48:55 +0000226template <typename AttrInfo>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000227static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value,
Erich Keane623efd82017-03-30 21:48:55 +0000228 SourceLocation>::type
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000229getAttrLoc(const AttrInfo &AL) {
230 return AL.getLocation();
Erich Keane623efd82017-03-30 21:48:55 +0000231}
Erich Keanee891aa92018-07-13 15:07:47 +0000232static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); }
Erich Keane623efd82017-03-30 21:48:55 +0000233
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000234/// If Expr is a valid integer constant, get the value of the integer
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000235/// expression and return success or failure. May output an error.
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000236///
237/// Negative argument is implicitly converted to unsigned, unless
238/// \p StrictlyUnsigned is true.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000239template <typename AttrInfo>
240static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000241 uint32_t &Val, unsigned Idx = UINT_MAX,
242 bool StrictlyUnsigned = false) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000243 llvm::APSInt I(32);
244 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
245 !Expr->isIntegerConstantExpr(I, S.Context)) {
246 if (Idx != UINT_MAX)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000247 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
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>
George Karpenkov1657f362018-11-30 02:18:37 +0000401static void handleSimpleAttribute(Sema &S, Decl *D, SourceRange SR,
402 unsigned SpellingIndex) {
403 D->addAttr(::new (S.Context) AttrType(SR, S.Context, SpellingIndex));
404}
405
406template <typename AttrType>
Erich Keanee891aa92018-07-13 15:07:47 +0000407static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) {
George Karpenkov1657f362018-11-30 02:18:37 +0000408 handleSimpleAttribute<AttrType>(S, D, AL.getRange(),
409 AL.getAttributeSpellingListIndex());
410}
411
412
413template <typename... DiagnosticArgs>
414static const Sema::SemaDiagnosticBuilder&
415appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr) {
416 return Bldr;
417}
418
419template <typename T, typename... DiagnosticArgs>
420static const Sema::SemaDiagnosticBuilder&
421appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr, T &&ExtraArg,
422 DiagnosticArgs &&... ExtraArgs) {
423 return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
424 std::forward<DiagnosticArgs>(ExtraArgs)...);
425}
426
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000427/// Add an attribute {@code AttrType} to declaration {@code D}, provided that
428/// {@code PassesCheck} is true.
429/// Otherwise, emit diagnostic {@code DiagID}, passing in all parameters
430/// specified in {@code ExtraArgs}.
George Karpenkov1657f362018-11-30 02:18:37 +0000431template <typename AttrType, typename... DiagnosticArgs>
432static void
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000433handleSimpleAttributeOrDiagnose(Sema &S, Decl *D, SourceRange SR,
George Karpenkov1657f362018-11-30 02:18:37 +0000434 unsigned SpellingIndex,
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000435 bool PassesCheck,
436 unsigned DiagID, DiagnosticArgs&&... ExtraArgs) {
437 if (!PassesCheck) {
George Karpenkov1657f362018-11-30 02:18:37 +0000438 Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
439 appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
440 return;
441 }
442 handleSimpleAttribute<AttrType>(S, D, SR, SpellingIndex);
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000443}
444
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000445template <typename AttrType, typename... DiagnosticArgs>
446static void
447handleSimpleAttributeOrDiagnose(Sema &S, Decl *D, const ParsedAttr &AL,
448 bool PassesCheck,
449 unsigned DiagID,
450 DiagnosticArgs&&... ExtraArgs) {
451 return handleSimpleAttributeOrDiagnose<AttrType>(
452 S, D, AL.getRange(), AL.getAttributeSpellingListIndex(), PassesCheck,
453 DiagID, std::forward<DiagnosticArgs>(ExtraArgs)...);
454}
455
Justin Lebar3eaaf862016-01-13 01:07:35 +0000456template <typename AttrType>
457static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000458 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000459 handleSimpleAttribute<AttrType>(S, D, AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000460}
461
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000462/// Applies the given attribute to the Decl so long as the Decl doesn't
Justin Lebar3eaaf862016-01-13 01:07:35 +0000463/// already have one of the given incompatible attributes.
464template <typename AttrType, typename IncompatibleAttrType,
465 typename... IncompatibleAttrTypes>
466static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000467 const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000468 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
Justin Lebar3eaaf862016-01-13 01:07:35 +0000469 return;
470 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000471 AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000472}
473
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000474/// Check if the passed-in expression is of type int or bool.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475static bool isIntOrBool(Expr *Exp) {
476 QualType QT = Exp->getType();
477 return QT->isBooleanType() || QT->isIntegerType();
478}
479
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000480
481// Check to see if the type is a smart pointer of some kind. We assume
482// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000483static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Trieu8d3fa392018-09-22 01:50:52 +0000484 auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record,
485 OverloadedOperatorKind Op) {
486 DeclContextLookupResult Result =
487 Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op));
488 return !Result.empty();
489 };
490
491 const RecordDecl *Record = RT->getDecl();
492 bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
493 bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
494 if (foundStarOperator && foundArrowOperator)
495 return true;
496
497 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
498 if (!CXXRecord)
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000499 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000500
Richard Trieu8d3fa392018-09-22 01:50:52 +0000501 for (auto BaseSpecifier : CXXRecord->bases()) {
502 if (!foundStarOperator)
503 foundStarOperator = IsOverloadedOperatorPresent(
504 BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
505 if (!foundArrowOperator)
506 foundArrowOperator = IsOverloadedOperatorPresent(
507 BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
508 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000509
Richard Trieu8d3fa392018-09-22 01:50:52 +0000510 if (foundStarOperator && foundArrowOperator)
511 return true;
512
513 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000514}
515
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000516/// Check if passed in Decl is a pointer type.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000517/// Note that this function may produce an error message.
518/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000519static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000520 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000521 const auto *VD = cast<ValueDecl>(D);
522 QualType QT = VD->getType();
Aaron Ballman553e6812013-12-26 14:54:11 +0000523 if (QT->isAnyPointerType())
524 return true;
525
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000526 if (const auto *RT = QT->getAs<RecordType>()) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000527 // If it's an incomplete type, it could be a smart pointer; skip it.
528 // (We don't want to force template instantiation if we can avoid it,
529 // since that would alter the order in which templates are instantiated.)
530 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000531 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000532
Aaron Ballman553e6812013-12-26 14:54:11 +0000533 if (threadSafetyCheckIsSmartPointer(S, RT))
534 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000535 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000536
Erich Keane44bacdf2018-08-09 13:21:32 +0000537 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000538 return false;
539}
540
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000541/// Checks that the passed in QualType either is of RecordType or points
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000542/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000543static const RecordType *getRecordType(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000544 if (const auto *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000545 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000546
547 // Now check if we point to record type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000548 if (const auto *PT = QT->getAs<PointerType>())
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000549 return PT->getPointeeType()->getAs<RecordType>();
550
Craig Topperc3ec1492014-05-26 06:22:03 +0000551 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000552}
553
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000554template <typename AttrType>
555static bool checkRecordDeclForAttr(const RecordDecl *RD) {
556 // Check if the record itself has the attribute.
557 if (RD->hasAttr<AttrType>())
558 return true;
559
560 // Else check if any base classes have the attribute.
561 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
562 CXXBasePaths BPaths(false, false);
563 if (CRD->lookupInBases(
564 [](const CXXBaseSpecifier *BS, CXXBasePath &) {
565 const auto &Ty = *BS->getType();
566 // If it's type-dependent, we assume it could have the attribute.
567 if (Ty.isDependentType())
568 return true;
569 return Ty.getAs<RecordType>()->getDecl()->hasAttr<AttrType>();
570 },
571 BPaths, true))
572 return true;
573 }
574 return false;
575}
576
Josh Gao55afa752017-08-11 07:54:35 +0000577static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000578 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000579
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000580 if (!RT)
581 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000582
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000583 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000584 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000585 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000586
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000587 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000588 // FIXME -- Check the type that the smart pointer points to.
589 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000590 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000591
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000592 return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl());
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000593}
594
Josh Gao55afa752017-08-11 07:54:35 +0000595static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000596 const auto *TD = Ty->getAs<TypedefType>();
597 if (!TD)
598 return false;
599
600 TypedefNameDecl *TN = TD->getDecl();
601 if (!TN)
602 return false;
603
Josh Gao55afa752017-08-11 07:54:35 +0000604 return TN->hasAttr<CapabilityAttr>();
Aaron Ballman76050722014-04-04 15:13:57 +0000605}
606
Josh Gaob40c1772017-08-08 19:44:35 +0000607static bool typeHasCapability(Sema &S, QualType Ty) {
Josh Gao55afa752017-08-11 07:54:35 +0000608 if (checkTypedefTypeForCapability(Ty))
609 return true;
Josh Gaob40c1772017-08-08 19:44:35 +0000610
Josh Gao55afa752017-08-11 07:54:35 +0000611 if (checkRecordTypeForCapability(S, Ty))
612 return true;
613
614 return false;
Josh Gaob40c1772017-08-08 19:44:35 +0000615}
616
Aaron Ballman76050722014-04-04 15:13:57 +0000617static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
618 // Capability expressions are simple expressions involving the boolean logic
619 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
620 // a DeclRefExpr is found, its type should be checked to determine whether it
621 // is a capability or not.
622
Yi Kong2d58d192017-12-14 22:24:45 +0000623 if (const auto *E = dyn_cast<CastExpr>(Ex))
Aaron Ballman76050722014-04-04 15:13:57 +0000624 return isCapabilityExpr(S, E->getSubExpr());
625 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
626 return isCapabilityExpr(S, E->getSubExpr());
627 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
Yi Kong2d58d192017-12-14 22:24:45 +0000628 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
629 E->getOpcode() == UO_Deref)
Aaron Ballman76050722014-04-04 15:13:57 +0000630 return isCapabilityExpr(S, E->getSubExpr());
631 return false;
632 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
633 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
634 return isCapabilityExpr(S, E->getLHS()) &&
635 isCapabilityExpr(S, E->getRHS());
636 return false;
637 }
638
Yi Kong2d58d192017-12-14 22:24:45 +0000639 return typeHasCapability(S, Ex->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000640}
641
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000642/// Checks that all attribute arguments, starting from Sidx, resolve to
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000643/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000644/// \param Sidx The attribute argument index to start checking with.
645/// \param ParamIdxOk Whether an argument can be indexing into a function
646/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000647static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000648 const ParsedAttr &AL,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000649 SmallVectorImpl<Expr *> &Args,
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000650 unsigned Sidx = 0,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000651 bool ParamIdxOk = false) {
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000652 if (Sidx == AL.getNumArgs()) {
653 // If we don't have any capability arguments, the attribute implicitly
654 // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
655 // a non-static method, and that the class is a (scoped) capability.
656 const auto *MD = dyn_cast<const CXXMethodDecl>(D);
657 if (MD && !MD->isStatic()) {
658 const CXXRecordDecl *RD = MD->getParent();
659 // FIXME -- need to check this again on template instantiation
660 if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
661 !checkRecordDeclForAttr<ScopedLockableAttr>(RD))
662 S.Diag(AL.getLoc(),
663 diag::warn_thread_attribute_not_on_capability_member)
664 << AL << MD->getParent();
665 } else {
666 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
667 << AL;
668 }
669 }
670
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000671 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
672 Expr *ArgExp = AL.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000673
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000674 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000675 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000676 Args.push_back(ArgExp);
677 continue;
678 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000679
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000680 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000681 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000682 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000683 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000684 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000685 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000686 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000687 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000688
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000689 // We allow constant strings to be used as a placeholder for expressions
690 // that are not valid C++ syntax, but warn that they are ignored.
Erich Keane44bacdf2018-08-09 13:21:32 +0000691 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000692 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000693 continue;
694 }
695
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000696 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000697
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000698 // A pointer to member expression of the form &MyClass::mu is treated
699 // specially -- we need to look at the type of the member.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000700 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000701 if (UOp->getOpcode() == UO_AddrOf)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000702 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000703 if (DRE->getDecl()->isCXXInstanceMember())
704 ArgTy = DRE->getDecl()->getType();
705
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000706 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000707 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000708
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000709 // Now check if we index into a record type function param.
Josh Gao55afa752017-08-11 07:54:35 +0000710 if(!RT && ParamIdxOk) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000711 const auto *FD = dyn_cast<FunctionDecl>(D);
712 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
Josh Gao55afa752017-08-11 07:54:35 +0000713 if(FD && IL) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000714 unsigned int NumParams = FD->getNumParams();
715 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000716 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
717 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000718 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Aaron Ballmance667f62019-02-12 13:19:02 +0000719 S.Diag(AL.getLoc(),
720 diag::err_attribute_argument_out_of_bounds_extra_info)
Erich Keane44bacdf2018-08-09 13:21:32 +0000721 << AL << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000722 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000723 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000724 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000725 }
726 }
727
Aaron Ballman76050722014-04-04 15:13:57 +0000728 // If the type does not have a capability, see if the components of the
729 // expression have capabilities. This allows for writing C code where the
730 // capability may be on the type, and the expression is a capability
731 // boolean logic expression. Eg) requires_capability(A || B && !C)
732 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000733 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Erich Keane44bacdf2018-08-09 13:21:32 +0000734 << AL << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000735
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000736 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000737 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000738}
739
Chris Lattner58418ff2008-06-29 00:16:31 +0000740//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000741// Attribute Implementations
742//===----------------------------------------------------------------------===//
743
Erich Keanee891aa92018-07-13 15:07:47 +0000744static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000745 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000746 return;
747
Michael Han99315932013-01-24 16:46:58 +0000748 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000749 PtGuardedVarAttr(AL.getRange(), S.Context,
750 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000751}
752
Erich Keanee891aa92018-07-13 15:07:47 +0000753static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000754 Expr *&Arg) {
755 SmallVector<Expr *, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000756 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000757 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000758 unsigned Size = Args.size();
759 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000760 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000761
Michael Han3be3b442012-07-23 18:48:41 +0000762 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000763
Michael Han3be3b442012-07-23 18:48:41 +0000764 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000765}
766
Erich Keanee891aa92018-07-13 15:07:47 +0000767static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000768 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000769 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000770 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000771
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000772 D->addAttr(::new (S.Context) GuardedByAttr(
773 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000774}
775
Erich Keanee891aa92018-07-13 15:07:47 +0000776static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000777 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000778 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000779 return;
780
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000781 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000782 return;
783
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000784 D->addAttr(::new (S.Context) PtGuardedByAttr(
785 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000786}
787
Erich Keanee891aa92018-07-13 15:07:47 +0000788static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000789 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000790 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000791 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000792
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000793 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000794 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000795 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000796 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000797 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000798 }
799
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000800 // Check that all arguments are lockable objects.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000801 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000802 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000803 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000804
Michael Han3be3b442012-07-23 18:48:41 +0000805 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000806}
807
Erich Keanee891aa92018-07-13 15:07:47 +0000808static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000809 SmallVector<Expr *, 1> Args;
810 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000811 return;
812
813 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000814 D->addAttr(::new (S.Context) AcquiredAfterAttr(
815 AL.getRange(), S.Context, StartArg, Args.size(),
816 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000817}
818
Erich Keanee891aa92018-07-13 15:07:47 +0000819static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000820 SmallVector<Expr *, 1> Args;
821 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000822 return;
823
824 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000825 D->addAttr(::new (S.Context) AcquiredBeforeAttr(
826 AL.getRange(), S.Context, StartArg, Args.size(),
827 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000828}
829
Erich Keanee891aa92018-07-13 15:07:47 +0000830static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000831 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000832 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000833 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000834 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000835
Michael Han3be3b442012-07-23 18:48:41 +0000836 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000837}
838
Erich Keanee891aa92018-07-13 15:07:47 +0000839static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000840 SmallVector<Expr *, 1> Args;
841 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000842 return;
843
844 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000845 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000846 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000847 AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size,
848 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000849}
850
851static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000852 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000853 SmallVector<Expr *, 1> Args;
854 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000855 return;
856
857 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000858 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000859 D->addAttr(::new (S.Context) AssertExclusiveLockAttr(
860 AL.getRange(), S.Context, StartArg, Size,
861 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000862}
863
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000864/// Checks to be sure that the given parameter number is in bounds, and
Aaron Ballman836684a2018-02-25 20:40:06 +0000865/// is an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000866/// false.
867///
Aaron Ballman836684a2018-02-25 20:40:06 +0000868/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000869template <typename AttrInfo>
870static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Joel E. Denny81508102018-03-13 14:51:22 +0000871 const AttrInfo &AI, unsigned AttrArgNo) {
Aaron Ballman836684a2018-02-25 20:40:06 +0000872 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
873 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
Joel E. Denny81508102018-03-13 14:51:22 +0000874 ParamIdx Idx;
Aaron Ballman836684a2018-02-25 20:40:06 +0000875 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000876 Idx))
877 return false;
878
Joel E. Denny81508102018-03-13 14:51:22 +0000879 const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +0000880 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000881 SourceLocation SrcLoc = AttrArg->getBeginLoc();
Erich Keane623efd82017-03-30 21:48:55 +0000882 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +0000883 << AI << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000884 return false;
885 }
886 return true;
887}
888
Erich Keanee891aa92018-07-13 15:07:47 +0000889static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000890 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
891 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000892 return;
893
894 const auto *FD = cast<FunctionDecl>(D);
895 if (!FD->getReturnType()->isPointerType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000896 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
George Burgess IVe3763372016-12-22 02:50:20 +0000897 return;
898 }
899
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000900 const Expr *SizeExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +0000901 int SizeArgNoVal;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000902 // Parameter indices are 1-indexed, hence Index=1
Joel E. Denny81508102018-03-13 14:51:22 +0000903 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Index=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000904 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000905 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0))
George Burgess IVe3763372016-12-22 02:50:20 +0000906 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000907 ParamIdx SizeArgNo(SizeArgNoVal, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000908
Joel E. Denny81508102018-03-13 14:51:22 +0000909 ParamIdx NumberArgNo;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000910 if (AL.getNumArgs() == 2) {
911 const Expr *NumberExpr = AL.getArgAsExpr(1);
Joel E. Denny81508102018-03-13 14:51:22 +0000912 int Val;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000913 // Parameter indices are 1-based, hence Index=2
Joel E. Denny81508102018-03-13 14:51:22 +0000914 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Index=*/2))
George Burgess IVe3763372016-12-22 02:50:20 +0000915 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000916 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000917 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000918 NumberArgNo = ParamIdx(Val, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000919 }
920
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000921 D->addAttr(::new (S.Context)
922 AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo,
923 AL.getAttributeSpellingListIndex()));
George Burgess IVe3763372016-12-22 02:50:20 +0000924}
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000925
Erich Keanee891aa92018-07-13 15:07:47 +0000926static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000927 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000928 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000929 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000930
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000931 if (!isIntOrBool(AL.getArgAsExpr(0))) {
932 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000933 << AL << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000934 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000935 }
936
937 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000938 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000939
Michael Han3be3b442012-07-23 18:48:41 +0000940 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000941}
942
Michael Hana9171bc2012-08-03 17:40:43 +0000943static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000944 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000945 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000946 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000947 return;
948
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000949 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
950 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(),
951 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000952}
953
Michael Hana9171bc2012-08-03 17:40:43 +0000954static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000955 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000956 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000957 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000958 return;
959
Nico Weber462fd1e2015-01-07 23:50:05 +0000960 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000961 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(),
962 Args.size(), AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000963}
964
Erich Keanee891aa92018-07-13 15:07:47 +0000965static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000966 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000967 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000968 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000969 unsigned Size = Args.size();
970 if (Size == 0)
971 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000972
Michael Han99315932013-01-24 16:46:58 +0000973 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000974 LockReturnedAttr(AL.getRange(), S.Context, Args[0],
975 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000976}
977
Erich Keanee891aa92018-07-13 15:07:47 +0000978static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000979 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000980 return;
981
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000982 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000983 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000984 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000985 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000986 if (Size == 0)
987 return;
988 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000989
Michael Han99315932013-01-24 16:46:58 +0000990 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000991 LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size,
992 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000993}
994
Erich Keanee891aa92018-07-13 15:07:47 +0000995static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
George Burgess IV177399e2017-01-09 04:12:14 +0000996 Expr *&Cond, StringRef &Msg) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000997 Cond = AL.getArgAsExpr(0);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000998 if (!Cond->isTypeDependent()) {
999 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
1000 if (Converted.isInvalid())
George Burgess IV177399e2017-01-09 04:12:14 +00001001 return false;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001002 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001003 }
1004
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001005 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001006 return false;
1007
1008 if (Msg.empty())
1009 Msg = "<no message provided>";
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001010
1011 SmallVector<PartialDiagnosticAt, 8> Diags;
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001012 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001013 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
1014 Diags)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001015 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
George Burgess IV0d546532016-11-10 21:47:12 +00001016 for (const PartialDiagnosticAt &PDiag : Diags)
1017 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +00001018 return false;
1019 }
1020 return true;
1021}
1022
Erich Keanee891aa92018-07-13 15:07:47 +00001023static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001024 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001025
1026 Expr *Cond;
1027 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001028 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001029 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001030 EnableIfAttr(AL.getRange(), S.Context, Cond, Msg,
1031 AL.getAttributeSpellingListIndex()));
George Burgess IV177399e2017-01-09 04:12:14 +00001032}
1033
1034namespace {
1035/// Determines if a given Expr references any of the given function's
1036/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
1037class ArgumentDependenceChecker
1038 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
1039#ifndef NDEBUG
1040 const CXXRecordDecl *ClassType;
1041#endif
1042 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
1043 bool Result;
1044
1045public:
1046 ArgumentDependenceChecker(const FunctionDecl *FD) {
1047#ifndef NDEBUG
1048 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1049 ClassType = MD->getParent();
1050 else
1051 ClassType = nullptr;
1052#endif
1053 Parms.insert(FD->param_begin(), FD->param_end());
1054 }
1055
1056 bool referencesArgs(Expr *E) {
1057 Result = false;
1058 TraverseStmt(E);
1059 return Result;
1060 }
1061
1062 bool VisitCXXThisExpr(CXXThisExpr *E) {
1063 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
1064 "`this` doesn't refer to the enclosing class?");
1065 Result = true;
1066 return false;
1067 }
1068
1069 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1070 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1071 if (Parms.count(PVD)) {
1072 Result = true;
1073 return false;
1074 }
1075 return true;
1076 }
1077};
1078}
1079
Erich Keanee891aa92018-07-13 15:07:47 +00001080static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001081 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001082
1083 Expr *Cond;
1084 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001085 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001086 return;
1087
1088 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001089 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +00001090 return;
1091
1092 DiagnoseIfAttr::DiagnosticType DiagType;
1093 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001094 S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
George Burgess IV177399e2017-01-09 04:12:14 +00001095 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001096 return;
1097 }
1098
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001099 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +00001100 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001101 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +00001102 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001103 AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent,
1104 cast<NamedDecl>(D), AL.getAttributeSpellingListIndex()));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001105}
1106
Erich Keanee891aa92018-07-13 15:07:47 +00001107static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001108 if (D->hasAttr<PassObjectSizeAttr>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001109 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001110 return;
1111 }
1112
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001113 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001114 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001115 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001116 return;
1117
1118 // pass_object_size's argument is passed in as the second argument of
1119 // __builtin_object_size. So, it has the same constraints as that second
1120 // argument; namely, it must be in the range [0, 3].
1121 if (Type > 3) {
Aaron Ballman52c9ad22019-02-12 13:04:11 +00001122 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00001123 << AL << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001124 return;
1125 }
1126
1127 // pass_object_size is only supported on constant pointer parameters; as a
1128 // kindness to users, we allow the parameter to be non-const for declarations.
1129 // At this point, we have no clue if `D` belongs to a function declaration or
1130 // definition, so we defer the constness check until later.
1131 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001132 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001133 return;
1134 }
1135
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001136 D->addAttr(::new (S.Context) PassObjectSizeAttr(
1137 AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex()));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001138}
1139
Erich Keanee891aa92018-07-13 15:07:47 +00001140static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001141 ConsumableAttr::ConsumedState DefaultState;
1142
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001143 if (AL.isArgIdent(0)) {
1144 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001145 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1146 DefaultState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001147 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1148 << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001149 return;
1150 }
David Blaikie16f76d22013-09-06 01:28:43 +00001151 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001152 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001153 << AL << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001154 return;
1155 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001156
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001157 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001158 ConsumableAttr(AL.getRange(), S.Context, DefaultState,
1159 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001160}
1161
1162static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Erich Keanee891aa92018-07-13 15:07:47 +00001163 const ParsedAttr &AL) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001164 QualType ThisType = MD->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001165
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001166 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1167 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001168 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001169 RD->getNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001170
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001171 return false;
1172 }
1173 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001174
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001175 return true;
1176}
1177
Erich Keanee891aa92018-07-13 15:07:47 +00001178static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001179 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001180 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001181
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001182 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001183 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001184
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001185 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001186 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001187 CallableWhenAttr::ConsumedState CallableState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001188
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001189 StringRef StateString;
1190 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001191 if (AL.isArgIdent(ArgIndex)) {
1192 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001193 StateString = Ident->Ident->getName();
1194 Loc = Ident->Loc;
1195 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001196 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001197 return;
1198 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001199
1200 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001201 CallableState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001202 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001203 return;
1204 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001205
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001206 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001207 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001208
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001209 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001210 CallableWhenAttr(AL.getRange(), S.Context, States.data(),
1211 States.size(), AL.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001212}
1213
Erich Keanee891aa92018-07-13 15:07:47 +00001214static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001215 ParamTypestateAttr::ConsumedState ParamState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001216
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001217 if (AL.isArgIdent(0)) {
1218 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001219 StringRef StateString = Ident->Ident->getName();
1220
1221 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1222 ParamState)) {
1223 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00001224 << AL << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001225 return;
1226 }
1227 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001228 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1229 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001230 return;
1231 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001232
DeLesley Hutchins69391772013-10-17 23:23:53 +00001233 // FIXME: This check is currently being done in the analysis. It can be
1234 // enabled here only after the parser propagates attributes at
1235 // template specialization definition, not declaration.
1236 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1237 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1238 //
1239 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001240 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001241 // ReturnType.getAsString();
1242 // return;
1243 //}
Fangrui Song6907ce22018-07-30 19:24:48 +00001244
DeLesley Hutchins69391772013-10-17 23:23:53 +00001245 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001246 ParamTypestateAttr(AL.getRange(), S.Context, ParamState,
1247 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001248}
1249
Erich Keanee891aa92018-07-13 15:07:47 +00001250static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001251 ReturnTypestateAttr::ConsumedState ReturnState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001252
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001253 if (AL.isArgIdent(0)) {
1254 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001255 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1256 ReturnState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001257 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1258 << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001259 return;
1260 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001261 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001262 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1263 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001264 return;
1265 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001266
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001267 // FIXME: This check is currently being done in the analysis. It can be
1268 // enabled here only after the parser propagates attributes at
1269 // template specialization definition, not declaration.
1270 //QualType ReturnType;
1271 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001272 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1273 // ReturnType = Param->getType();
1274 //
1275 //} else if (const CXXConstructorDecl *Constructor =
1276 // dyn_cast<CXXConstructorDecl>(D)) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001277 // ReturnType = Constructor->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001278 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001279 //} else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001280 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001281 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1282 //}
1283 //
1284 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1285 //
1286 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1287 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1288 // ReturnType.getAsString();
1289 // return;
1290 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001291
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001292 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001293 ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState,
1294 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001295}
1296
Erich Keanee891aa92018-07-13 15:07:47 +00001297static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001298 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001299 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001300
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001301 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001302 if (AL.isArgIdent(0)) {
1303 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001304 StringRef Param = Ident->Ident->getName();
1305 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001306 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1307 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001308 return;
1309 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001310 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001311 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1312 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001313 return;
1314 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001315
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001316 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001317 SetTypestateAttr(AL.getRange(), S.Context, NewState,
1318 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001319}
1320
Erich Keanee891aa92018-07-13 15:07:47 +00001321static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001322 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001323 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001324
1325 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001326 if (AL.isArgIdent(0)) {
1327 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001328 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001329 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001330 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1331 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001332 return;
1333 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001334 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001335 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1336 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001337 return;
1338 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001339
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001340 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001341 TestTypestateAttr(AL.getRange(), S.Context, TestState,
1342 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001343}
1344
Erich Keanee891aa92018-07-13 15:07:47 +00001345static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001346 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001347 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001348}
1349
Erich Keanee891aa92018-07-13 15:07:47 +00001350static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001351 if (auto *TD = dyn_cast<TagDecl>(D))
1352 TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context,
1353 AL.getAttributeSpellingListIndex()));
1354 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
1366 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001367 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001368 } else {
1369 // Report warning about changed offset in the newer compiler versions.
1370 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001371 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001372
1373 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001374 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001375 }
1376
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001377 } else
Erich Keane44bacdf2018-08-09 13:21:32 +00001378 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001379}
1380
Erich Keanee891aa92018-07-13 15:07:47 +00001381static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001382 // The IBOutlet/IBOutletCollection attributes only apply to instance
1383 // variables or properties of Objective-C classes. The outlet must also
1384 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001385 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001386 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001387 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001388 << AL << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001389 return false;
1390 }
1391 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001392 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001393 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001394 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001395 << AL << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001396 return false;
1397 }
1398 }
1399 else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001400 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001401 return false;
1402 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001403
Ted Kremenek7fd17232011-09-29 07:02:25 +00001404 return true;
1405}
1406
Erich Keanee891aa92018-07-13 15:07:47 +00001407static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001408 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001409 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001410
Michael Han99315932013-01-24 16:46:58 +00001411 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001412 IBOutletAttr(AL.getRange(), S.Context,
1413 AL.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001414}
1415
Erich Keanee891aa92018-07-13 15:07:47 +00001416static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001417
1418 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001419 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001420 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001421 return;
1422 }
1423
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001424 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001425 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001426
Richard Smithb1f9a282013-10-31 01:56:18 +00001427 ParsedType PT;
1428
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001429 if (AL.hasParsedType())
1430 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001431 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001432 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001433 S.getScopeForContext(D->getDeclContext()->getParent()));
1434 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001435 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001436 return;
1437 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001438 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001439
Craig Topperc3ec1492014-05-26 06:22:03 +00001440 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001441 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1442 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001443 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001444
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001445 // Diagnose use of non-object type in iboutletcollection attribute.
1446 // FIXME. Gnu attribute extension ignores use of builtin types in
1447 // attributes. So, __attribute__((iboutletcollection(char))) will be
1448 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001449 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001450 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001451 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1452 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001453 return;
1454 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001455
Michael Han99315932013-01-24 16:46:58 +00001456 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001457 IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc,
1458 AL.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001459}
1460
Hal Finkelee90a222014-09-26 05:04:30 +00001461bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1462 if (RefOkay) {
1463 if (T->isReferenceType())
1464 return true;
1465 } else {
1466 T = T.getNonReferenceType();
1467 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001468
Hal Finkelee90a222014-09-26 05:04:30 +00001469 // The nonnull attribute, and other similar attributes, can be applied to a
1470 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001471 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001472 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1473 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001474 for (const auto *I : UD->fields()) {
1475 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001476 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1477 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001478 }
1479 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001480 }
1481
1482 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001483}
1484
Erich Keanee891aa92018-07-13 15:07:47 +00001485static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001486 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001487 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001488 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001489 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001490 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001491 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001492 << AL << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001493 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001494 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001495 << AL << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001496 return false;
1497 }
1498 return true;
1499}
1500
Erich Keanee891aa92018-07-13 15:07:47 +00001501static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001502 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001503 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1504 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001505 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001506 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001507 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001508
1509 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001510 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1511 !attrNonNullArgCheck(
1512 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1513 Ex->getSourceRange(),
1514 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001515 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001516
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001517 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001518 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001519
1520 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001521 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1522 // check if the attribute came from a macro expansion or a template
1523 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001524 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001525 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001526 bool AnyPointers = isFunctionOrMethodVariadic(D);
1527 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1528 I != E && !AnyPointers; ++I) {
1529 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001530 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001531 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001532 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001533
Richard Smith588bd9b2014-08-27 04:59:42 +00001534 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001535 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001536 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001537
Joel E. Denny81508102018-03-13 14:51:22 +00001538 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001539 unsigned Size = NonNullArgs.size();
1540 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001541 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001542 NonNullAttr(AL.getRange(), S.Context, Start, Size,
1543 AL.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001544}
1545
Jordan Rosec9399072014-02-11 17:27:59 +00001546static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00001547 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001548 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001549 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001550 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001551 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001552 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001553 << D->getSourceRange();
1554 }
1555 return;
1556 }
1557
1558 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001559 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001560 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001561 return;
1562
1563 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001564 NonNullAttr(AL.getRange(), S.Context, nullptr, 0,
1565 AL.getAttributeSpellingListIndex()));
Jordan Rosec9399072014-02-11 17:27:59 +00001566}
1567
Erich Keanee891aa92018-07-13 15:07:47 +00001568static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001569 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001570 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001571 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001572 /* isReturnValue */ true))
1573 return;
1574
1575 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001576 ReturnsNonNullAttr(AL.getRange(), S.Context,
1577 AL.getAttributeSpellingListIndex()));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001578}
1579
Erich Keanee891aa92018-07-13 15:07:47 +00001580static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001581 if (D->isInvalidDecl())
1582 return;
1583
1584 // noescape only applies to pointer types.
1585 QualType T = cast<ParmVarDecl>(D)->getType();
1586 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001587 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001588 << AL << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001589 return;
1590 }
1591
1592 D->addAttr(::new (S.Context) NoEscapeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001593 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001594}
1595
Erich Keanee891aa92018-07-13 15:07:47 +00001596static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001597 Expr *E = AL.getArgAsExpr(0),
1598 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1599 S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE,
1600 AL.getAttributeSpellingListIndex());
Hal Finkelee90a222014-09-26 05:04:30 +00001601}
1602
Erich Keanee891aa92018-07-13 15:07:47 +00001603static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001604 S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0),
1605 AL.getAttributeSpellingListIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001606}
1607
Hal Finkelee90a222014-09-26 05:04:30 +00001608void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1609 Expr *OE, unsigned SpellingListIndex) {
1610 QualType ResultType = getFunctionOrMethodResultType(D);
1611 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1612
1613 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1614 SourceLocation AttrLoc = AttrRange.getBegin();
1615
1616 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1617 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1618 << &TmpAttr << AttrRange << SR;
1619 return;
1620 }
1621
1622 if (!E->isValueDependent()) {
1623 llvm::APSInt I(64);
1624 if (!E->isIntegerConstantExpr(I, Context)) {
1625 if (OE)
1626 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1627 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1628 << E->getSourceRange();
1629 else
1630 Diag(AttrLoc, diag::err_attribute_argument_type)
1631 << &TmpAttr << AANT_ArgumentIntegerConstant
1632 << E->getSourceRange();
1633 return;
1634 }
1635
1636 if (!I.isPowerOf2()) {
1637 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1638 << E->getSourceRange();
1639 return;
1640 }
1641 }
1642
1643 if (OE) {
1644 if (!OE->isValueDependent()) {
1645 llvm::APSInt I(64);
1646 if (!OE->isIntegerConstantExpr(I, Context)) {
1647 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1648 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1649 << OE->getSourceRange();
1650 return;
1651 }
1652 }
1653 }
1654
1655 D->addAttr(::new (Context)
1656 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1657}
1658
Erich Keane623efd82017-03-30 21:48:55 +00001659void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr,
1660 unsigned SpellingListIndex) {
1661 QualType ResultType = getFunctionOrMethodResultType(D);
1662
Joel E. Denny81508102018-03-13 14:51:22 +00001663 AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex);
Erich Keane623efd82017-03-30 21:48:55 +00001664 SourceLocation AttrLoc = AttrRange.getBegin();
1665
1666 if (!ResultType->isDependentType() &&
1667 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1668 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1669 << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D);
1670 return;
1671 }
1672
Joel E. Denny81508102018-03-13 14:51:22 +00001673 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001674 const auto *FuncDecl = cast<FunctionDecl>(D);
1675 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Joel E. Denny81508102018-03-13 14:51:22 +00001676 /*AttrArgNo=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001677 return;
1678
Joel E. Denny81508102018-03-13 14:51:22 +00001679 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001680 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001681 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001682 << &TmpAttr
1683 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001684 return;
1685 }
1686
Joel E. Denny81508102018-03-13 14:51:22 +00001687 D->addAttr(::new (Context)
1688 AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex));
Erich Keane623efd82017-03-30 21:48:55 +00001689}
1690
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001691/// Normalize the attribute, __foo__ becomes foo.
1692/// Returns true if normalization was applied.
1693static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001694 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1695 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001696 AttrName = AttrName.drop_front(2).drop_back(2);
1697 return true;
1698 }
1699 return false;
1700}
1701
Erich Keanee891aa92018-07-13 15:07:47 +00001702static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001703 // This attribute must be applied to a function declaration. The first
1704 // argument to the attribute must be an identifier, the name of the resource,
1705 // for example: malloc. The following arguments must be argument indexes, the
1706 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001707 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001708 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001709 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001710
Aaron Ballman00e99962013-08-31 01:11:41 +00001711 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001712 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001713 << AL << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001714 return;
1715 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001716
Richard Smith852e9ce2013-11-27 01:46:48 +00001717 // Figure out our Kind.
1718 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001719 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001720 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001721
Richard Smith852e9ce2013-11-27 01:46:48 +00001722 // Check arguments.
1723 switch (K) {
1724 case OwnershipAttr::Takes:
1725 case OwnershipAttr::Holds:
1726 if (AL.getNumArgs() < 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001727 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001728 return;
1729 }
1730 break;
1731 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001732 if (AL.getNumArgs() > 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001733 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001734 return;
1735 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001736 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001737 }
1738
Richard Smith852e9ce2013-11-27 01:46:48 +00001739 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001740
Richard Smith852e9ce2013-11-27 01:46:48 +00001741 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001742 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001743 Module = &S.PP.getIdentifierTable().get(ModuleName);
1744 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001745
Joel E. Denny81508102018-03-13 14:51:22 +00001746 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001747 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1748 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001749 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001750 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001751 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001752
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001753 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001754 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001755 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001756 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001757 case OwnershipAttr::Takes:
1758 case OwnershipAttr::Holds:
1759 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1760 Err = 0;
1761 break;
1762 case OwnershipAttr::Returns:
1763 if (!T->isIntegerType())
1764 Err = 1;
1765 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001766 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001767 if (-1 != Err) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001768 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1769 << Ex->getSourceRange();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001770 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001771 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001772
1773 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001774 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001775 // Cannot have two ownership attributes of different kinds for the same
1776 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001777 if (I->getOwnKind() != K && I->args_end() !=
1778 std::find(I->args_begin(), I->args_end(), Idx)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001779 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001780 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001781 } else if (K == OwnershipAttr::Returns &&
1782 I->getOwnKind() == OwnershipAttr::Returns) {
1783 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001784 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001785 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1786 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001787 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001788 if (I->args_size())
1789 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001790 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001791 return;
1792 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001793 }
1794 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001795 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001796 }
1797
Joel E. Denny81508102018-03-13 14:51:22 +00001798 ParamIdx *Start = OwnershipArgs.data();
1799 unsigned Size = OwnershipArgs.size();
1800 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001801 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001802 OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size,
1803 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001804}
1805
Erich Keanee891aa92018-07-13 15:07:47 +00001806static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001807 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001808 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001809 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001810 return;
1811 }
1812
1813 // gcc rejects
1814 // class c {
1815 // static int a __attribute__((weakref ("v2")));
1816 // static int b() __attribute__((weakref ("f3")));
1817 // };
1818 // and ignores the attributes of
1819 // void f(void) {
1820 // static int a __attribute__((weakref ("v2")));
1821 // }
1822 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001823 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001824 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001825 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1826 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001827 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001828 }
1829
1830 // The GCC manual says
1831 //
1832 // At present, a declaration to which `weakref' is attached can only
1833 // be `static'.
1834 //
1835 // It also says
1836 //
1837 // Without a TARGET,
1838 // given as an argument to `weakref' or to `alias', `weakref' is
1839 // equivalent to `weak'.
1840 //
1841 // gcc 4.4.1 will accept
1842 // int a7 __attribute__((weakref));
1843 // as
1844 // int a7 __attribute__((weak));
1845 // This looks like a bug in gcc. We reject that for now. We should revisit
1846 // it if this behaviour is actually used.
1847
Rafael Espindolac18086a2010-02-23 22:00:30 +00001848 // GCC rejects
1849 // static ((alias ("y"), weakref)).
1850 // Should we? How to check that weakref is before or after alias?
1851
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001852 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1853 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1854 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001855 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001856 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001857 // GCC will accept anything as the argument of weakref. Should we
1858 // check for an existing decl?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001859 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1860 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001861
Michael Han99315932013-01-24 16:46:58 +00001862 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001863 WeakRefAttr(AL.getRange(), S.Context,
1864 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001865}
1866
Erich Keanee891aa92018-07-13 15:07:47 +00001867static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001868 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001869 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001870 return;
1871
1872 // Aliases should be on declarations, not definitions.
1873 const auto *FD = cast<FunctionDecl>(D);
1874 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001875 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001876 return;
1877 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001878
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001879 D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str,
1880 AL.getAttributeSpellingListIndex()));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001881}
1882
Erich Keanee891aa92018-07-13 15:07:47 +00001883static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001884 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001885 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001886 return;
1887
Douglas Gregore8bbc122011-09-02 00:18:52 +00001888 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001889 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001890 return;
1891 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001892 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001893 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001894 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001895
David Majnemer2dc81462015-01-19 09:00:28 +00001896 // Aliases should be on declarations, not definitions.
1897 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1898 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001899 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001900 return;
1901 }
1902 } else {
1903 const auto *VD = cast<VarDecl>(D);
1904 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001905 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001906 return;
1907 }
1908 }
1909
Nick Desaulniers9b9fe412019-01-09 23:54:55 +00001910 // Mark target used to prevent unneeded-internal-declaration warnings.
1911 if (!S.LangOpts.CPlusPlus) {
1912 // FIXME: demangle Str for C++, as the attribute refers to the mangled
1913 // linkage name, not the pre-mangled identifier.
1914 const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc());
1915 LookupResult LR(S, target, Sema::LookupOrdinaryName);
1916 if (S.LookupQualifiedName(LR, S.getCurLexicalContext()))
1917 for (NamedDecl *ND : LR)
1918 ND->markUsed(S.Context);
1919 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001920
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001921 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1922 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001923}
1924
Erich Keanee891aa92018-07-13 15:07:47 +00001925static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001926 StringRef Model;
1927 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001928 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001929 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001930 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001931
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001932 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001933 if (Model != "global-dynamic" && Model != "local-dynamic"
1934 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001935 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001936 return;
1937 }
1938
Michael Han99315932013-01-24 16:46:58 +00001939 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001940 TLSModelAttr(AL.getRange(), S.Context, Model,
1941 AL.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001942}
1943
Erich Keanee891aa92018-07-13 15:07:47 +00001944static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001945 QualType ResultType = getFunctionOrMethodResultType(D);
1946 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1947 D->addAttr(::new (S.Context) RestrictAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001948 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
David Majnemer631a90b2015-02-04 07:23:21 +00001949 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001950 }
1951
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001952 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001953 << AL << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001954}
1955
Erich Keane3efe0022018-07-20 14:13:28 +00001956static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1957 FunctionDecl *FD = cast<FunctionDecl>(D);
Erich Keane659c8712018-09-10 14:31:56 +00001958
1959 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1960 if (MD->getParent()->isLambda()) {
1961 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
1962 return;
1963 }
1964 }
1965
Erich Keane3efe0022018-07-20 14:13:28 +00001966 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1967 return;
1968
1969 SmallVector<IdentifierInfo *, 8> CPUs;
1970 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
1971 if (!AL.isArgIdent(ArgNo)) {
1972 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001973 << AL << AANT_ArgumentIdentifier;
Erich Keane3efe0022018-07-20 14:13:28 +00001974 return;
1975 }
1976
1977 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1978 StringRef CPUName = CPUArg->Ident->getName().trim();
1979
1980 if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1981 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1982 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1983 return;
1984 }
1985
1986 const TargetInfo &Target = S.Context.getTargetInfo();
1987 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1988 return Target.CPUSpecificManglingCharacter(CPUName) ==
1989 Target.CPUSpecificManglingCharacter(Cur->getName());
1990 })) {
1991 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1992 return;
1993 }
1994 CPUs.push_back(CPUArg->Ident);
1995 }
1996
1997 FD->setIsMultiVersion(true);
1998 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
1999 D->addAttr(::new (S.Context) CPUSpecificAttr(
2000 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
2001 AL.getAttributeSpellingListIndex()));
2002 else
2003 D->addAttr(::new (S.Context) CPUDispatchAttr(
2004 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
2005 AL.getAttributeSpellingListIndex()));
2006}
2007
Erich Keanee891aa92018-07-13 15:07:47 +00002008static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00002009 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002010 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00002011 << AL << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00002012 return;
2013 }
2014
Erich Keane44bacdf2018-08-09 13:21:32 +00002015 if (CommonAttr *CA = S.mergeCommonAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00002016 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00002017}
2018
Erich Keanee891aa92018-07-13 15:07:47 +00002019static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002020 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
Charles Davis0e379112016-08-08 21:19:08 +00002021 return;
2022
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002023 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002024 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
2025 const auto &Arch = Triple.getArch();
2026 if (Arch != llvm::Triple::x86 &&
2027 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002028 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
Erich Keane44bacdf2018-08-09 13:21:32 +00002029 << AL << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002030 return;
2031 }
2032 }
2033
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002034 D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context,
2035 AL.getAttributeSpellingListIndex()));
Charles Davis0e379112016-08-08 21:19:08 +00002036}
2037
Erich Keanee891aa92018-07-13 15:07:47 +00002038static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002039 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00002040
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002041 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002042 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002043 << Attrs << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002044 return;
2045 }
2046
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002047 D->addAttr(::new (S.Context) NoReturnAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00002048 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002049}
2050
Erich Keanee891aa92018-07-13 15:07:47 +00002051static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00002052 if (!S.getLangOpts().CFProtectionBranch)
2053 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
2054 else
2055 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00002056}
2057
Erich Keanee891aa92018-07-13 15:07:47 +00002058bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002059 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
2060 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002061 return true;
2062 }
2063
2064 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002065}
2066
Erich Keanee891aa92018-07-13 15:07:47 +00002067bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002068 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002069 if (!AL.existsInTarget(Context.getTargetInfo())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002070 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002071 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002072 return true;
2073 }
2074
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002075 return false;
2076}
2077
Erich Keanee891aa92018-07-13 15:07:47 +00002078static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2079
Ted Kremenek5295ce82010-08-19 00:51:58 +00002080 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2081 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00002082 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002083 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00002084 if (!VD || (!VD->getType()->isBlockPointerType() &&
2085 !VD->getType()->isFunctionPointerType())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002086 S.Diag(AL.getLoc(), AL.isCXX11Attribute()
2087 ? diag::err_attribute_wrong_decl_type
2088 : diag::warn_attribute_wrong_decl_type)
2089 << AL << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00002090 return;
2091 }
2092 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002093
Michael Han99315932013-01-24 16:46:58 +00002094 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002095 AnalyzerNoReturnAttr(AL.getRange(), S.Context,
2096 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002097}
2098
John Thompsoncdb847ba2010-08-09 21:53:52 +00002099// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00002100static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2101 /*
2102 Returning a Vector Class in Registers
2103
2104 According to the PPU ABI specifications, a class with a single member of
2105 vector type is returned in memory when used as the return value of a
2106 function.
2107 This results in inefficient code when implementing vector classes. To return
2108 the value in a single vector register, add the vecreturn attribute to the
2109 class definition. This attribute is also applicable to struct types.
2110
2111 Example:
2112
2113 struct Vector
2114 {
2115 __vector float xyzw;
2116 } __attribute__((vecreturn));
2117
2118 Vector Add(Vector lhs, Vector rhs)
2119 {
2120 Vector result;
2121 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2122 return result; // This will be returned in a register
2123 }
2124 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002125 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002126 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002127 return;
2128 }
2129
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002130 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002131 int count = 0;
2132
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002133 if (!isa<CXXRecordDecl>(R)) {
2134 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002135 return;
2136 }
2137
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002138 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2139 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002140 return;
2141 }
2142
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002143 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002144 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002145 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002146 return;
2147 }
2148 count++;
2149 }
2150
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002151 D->addAttr(::new (S.Context) VecReturnAttr(
2152 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002153}
2154
Richard Smithe233fbf2013-01-28 22:42:45 +00002155static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002156 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002157 if (isa<ParmVarDecl>(D)) {
2158 // [[carries_dependency]] can only be applied to a parameter if it is a
2159 // parameter of a function declaration or lambda.
2160 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002161 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002162 diag::err_carries_dependency_param_not_function_decl);
2163 return;
2164 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002165 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002166
2167 D->addAttr(::new (S.Context) CarriesDependencyAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002168 AL.getRange(), S.Context,
2169 AL.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002170}
2171
Erich Keanee891aa92018-07-13 15:07:47 +00002172static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002173 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002174
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002175 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002176 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002177 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Erich Keane44bacdf2018-08-09 13:21:32 +00002178 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002179
2180 D->addAttr(::new (S.Context) UnusedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002181 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002182}
2183
Erich Keanee891aa92018-07-13 15:07:47 +00002184static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002185 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002186 if (AL.getNumArgs() &&
2187 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002188 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002189
Michael Han99315932013-01-24 16:46:58 +00002190 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002191 ConstructorAttr(AL.getRange(), S.Context, priority,
2192 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002193}
2194
Erich Keanee891aa92018-07-13 15:07:47 +00002195static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002196 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002197 if (AL.getNumArgs() &&
2198 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002199 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002200
Michael Han99315932013-01-24 16:46:58 +00002201 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002202 DestructorAttr(AL.getRange(), S.Context, priority,
2203 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002204}
2205
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002206template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002207static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002208 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002209 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002210 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002211 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002212
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002213 D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str,
2214 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002215}
2216
Ted Kremenek438f8db2014-02-22 01:06:05 +00002217static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002218 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002219 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002220 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
Erich Keane44bacdf2018-08-09 13:21:32 +00002221 << AL << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002222 return;
2223 }
2224
Ted Kremenek28eace62013-11-23 01:01:34 +00002225 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002226 ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context,
2227 AL.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00002228}
2229
Jordy Rose740b0c22012-05-08 03:27:22 +00002230static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2231 IdentifierInfo *Platform,
2232 VersionTuple Introduced,
2233 VersionTuple Deprecated,
2234 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002235 StringRef PlatformName
2236 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2237 if (PlatformName.empty())
2238 PlatformName = Platform->getName();
2239
2240 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2241 // of these steps are needed).
2242 if (!Introduced.empty() && !Deprecated.empty() &&
2243 !(Introduced <= Deprecated)) {
2244 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2245 << 1 << PlatformName << Deprecated.getAsString()
2246 << 0 << Introduced.getAsString();
2247 return true;
2248 }
2249
2250 if (!Introduced.empty() && !Obsoleted.empty() &&
2251 !(Introduced <= Obsoleted)) {
2252 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2253 << 2 << PlatformName << Obsoleted.getAsString()
2254 << 0 << Introduced.getAsString();
2255 return true;
2256 }
2257
2258 if (!Deprecated.empty() && !Obsoleted.empty() &&
2259 !(Deprecated <= Obsoleted)) {
2260 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2261 << 2 << PlatformName << Obsoleted.getAsString()
2262 << 1 << Deprecated.getAsString();
2263 return true;
2264 }
2265
2266 return false;
2267}
2268
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002269/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002270///
2271/// If either version tuple is empty, then they are assumed to match. If
2272/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2273static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2274 bool BeforeIsOkay) {
2275 if (X.empty() || Y.empty())
2276 return true;
2277
2278 if (X == Y)
2279 return true;
2280
2281 if (BeforeIsOkay && X < Y)
2282 return true;
2283
2284 return false;
2285}
2286
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002287AvailabilityAttr *Sema::mergeAvailabilityAttr(
2288 NamedDecl *D, SourceRange Range, IdentifierInfo *Platform, bool Implicit,
2289 VersionTuple Introduced, VersionTuple Deprecated, VersionTuple Obsoleted,
2290 bool IsUnavailable, StringRef Message, bool IsStrict, StringRef Replacement,
2291 AvailabilityMergeKind AMK, int Priority, unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002292 VersionTuple MergedIntroduced = Introduced;
2293 VersionTuple MergedDeprecated = Deprecated;
2294 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002295 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002296 bool OverrideOrImpl = false;
2297 switch (AMK) {
2298 case AMK_None:
2299 case AMK_Redeclaration:
2300 OverrideOrImpl = false;
2301 break;
2302
2303 case AMK_Override:
2304 case AMK_ProtocolImplementation:
2305 OverrideOrImpl = true;
2306 break;
2307 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002308
Rafael Espindolac67f2232012-05-10 02:50:16 +00002309 if (D->hasAttrs()) {
2310 AttrVec &Attrs = D->getAttrs();
2311 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002312 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002313 if (!OldAA) {
2314 ++i;
2315 continue;
2316 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002317
Rafael Espindolac67f2232012-05-10 02:50:16 +00002318 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2319 if (OldPlatform != Platform) {
2320 ++i;
2321 continue;
2322 }
2323
Tim Northover7a73cc72015-10-30 16:30:49 +00002324 // If there is an existing availability attribute for this platform that
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002325 // has a lower priority use the existing one and discard the new
2326 // attribute.
2327 if (OldAA->getPriority() < Priority)
Tim Northover7a73cc72015-10-30 16:30:49 +00002328 return nullptr;
Tim Northover7a73cc72015-10-30 16:30:49 +00002329
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002330 // If there is an existing attribute for this platform that has a higher
2331 // priority than the new attribute then erase the old one and continue
2332 // processing the attributes.
2333 if (OldAA->getPriority() > Priority) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002334 Attrs.erase(Attrs.begin() + i);
2335 --e;
2336 continue;
2337 }
2338
Rafael Espindolac67f2232012-05-10 02:50:16 +00002339 FoundAny = true;
2340 VersionTuple OldIntroduced = OldAA->getIntroduced();
2341 VersionTuple OldDeprecated = OldAA->getDeprecated();
2342 VersionTuple OldObsoleted = OldAA->getObsoleted();
2343 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002344
Douglas Gregord2a713e2015-09-30 21:27:42 +00002345 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2346 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2347 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002348 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002349 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2350 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002351 int Which = -1;
2352 VersionTuple FirstVersion;
2353 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002354 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002355 Which = 0;
2356 FirstVersion = OldIntroduced;
2357 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002358 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002359 Which = 1;
2360 FirstVersion = Deprecated;
2361 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002362 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002363 Which = 2;
2364 FirstVersion = Obsoleted;
2365 SecondVersion = OldObsoleted;
2366 }
2367
2368 if (Which == -1) {
2369 Diag(OldAA->getLocation(),
2370 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002371 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2372 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002373 } else {
2374 Diag(OldAA->getLocation(),
2375 diag::warn_mismatched_availability_override)
2376 << Which
2377 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002378 << FirstVersion.getAsString() << SecondVersion.getAsString()
2379 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002380 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002381 if (AMK == AMK_Override)
2382 Diag(Range.getBegin(), diag::note_overridden_method);
2383 else
2384 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002385 } else {
2386 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2387 Diag(Range.getBegin(), diag::note_previous_attribute);
2388 }
2389
Rafael Espindolac67f2232012-05-10 02:50:16 +00002390 Attrs.erase(Attrs.begin() + i);
2391 --e;
2392 continue;
2393 }
2394
2395 VersionTuple MergedIntroduced2 = MergedIntroduced;
2396 VersionTuple MergedDeprecated2 = MergedDeprecated;
2397 VersionTuple MergedObsoleted2 = MergedObsoleted;
2398
2399 if (MergedIntroduced2.empty())
2400 MergedIntroduced2 = OldIntroduced;
2401 if (MergedDeprecated2.empty())
2402 MergedDeprecated2 = OldDeprecated;
2403 if (MergedObsoleted2.empty())
2404 MergedObsoleted2 = OldObsoleted;
2405
2406 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2407 MergedIntroduced2, MergedDeprecated2,
2408 MergedObsoleted2)) {
2409 Attrs.erase(Attrs.begin() + i);
2410 --e;
2411 continue;
2412 }
2413
2414 MergedIntroduced = MergedIntroduced2;
2415 MergedDeprecated = MergedDeprecated2;
2416 MergedObsoleted = MergedObsoleted2;
2417 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002418 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002419 }
2420
2421 if (FoundAny &&
2422 MergedIntroduced == Introduced &&
2423 MergedDeprecated == Deprecated &&
2424 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002425 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002426
Douglas Gregord2a713e2015-09-30 21:27:42 +00002427 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002428 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002429 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002430 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002431 !OverrideOrImpl) {
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002432 auto *Avail = ::new (Context)
2433 AvailabilityAttr(Range, Context, Platform, Introduced, Deprecated,
2434 Obsoleted, IsUnavailable, Message, IsStrict,
2435 Replacement, Priority, AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002436 Avail->setImplicit(Implicit);
2437 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002438 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002439 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002440}
2441
Erich Keanee891aa92018-07-13 15:07:47 +00002442static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002443 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002444 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002445 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2446 unsigned Index = AL.getAttributeSpellingListIndex();
Fangrui Song6907ce22018-07-30 19:24:48 +00002447
Aaron Ballman00e99962013-08-31 01:11:41 +00002448 IdentifierInfo *II = Platform->Ident;
2449 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2450 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2451 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002452
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002453 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002454 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002455 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002456
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002457 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2458 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2459 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2460 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2461 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002462 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002463 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002464 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002465 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002466 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002467 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002468
Michael Wu260e9622018-11-12 02:44:33 +00002469 if (II->isStr("swift")) {
2470 if (Introduced.isValid() || Obsoleted.isValid() ||
2471 (!IsUnavailable && !Deprecated.isValid())) {
2472 S.Diag(AL.getLoc(),
2473 diag::warn_availability_swift_unavailable_deprecated_only);
2474 return;
2475 }
2476 }
2477
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002478 int PriorityModifier = AL.isPragmaClangAttribute()
2479 ? Sema::AP_PragmaClangAttribute
2480 : Sema::AP_Explicit;
2481 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2482 ND, AL.getRange(), II, false /*Implicit*/, Introduced.Version,
2483 Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict,
2484 Replacement, Sema::AMK_None, PriorityModifier, Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002485 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002486 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002487
2488 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2489 // matches before the start of the watchOS platform.
2490 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2491 IdentifierInfo *NewII = nullptr;
2492 if (II->getName() == "ios")
2493 NewII = &S.Context.Idents.get("watchos");
2494 else if (II->getName() == "ios_app_extension")
2495 NewII = &S.Context.Idents.get("watchos_app_extension");
2496
2497 if (NewII) {
2498 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2499 if (Version.empty())
2500 return Version;
2501 auto Major = Version.getMajor();
2502 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2503 if (NewMajor >= 2) {
2504 if (Version.getMinor().hasValue()) {
2505 if (Version.getSubminor().hasValue())
2506 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2507 Version.getSubminor().getValue());
2508 else
2509 return VersionTuple(NewMajor, Version.getMinor().getValue());
2510 }
Alex Lorenz0b436482019-03-20 20:02:00 +00002511 return VersionTuple(NewMajor);
Tim Northover7a73cc72015-10-30 16:30:49 +00002512 }
2513
2514 return VersionTuple(2, 0);
2515 };
2516
2517 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2518 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2519 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2520
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002521 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2522 ND, AL.getRange(), NewII, true /*Implicit*/, NewIntroduced,
2523 NewDeprecated, NewObsoleted, IsUnavailable, Str, IsStrict,
2524 Replacement, Sema::AMK_None,
2525 PriorityModifier + Sema::AP_InferredFromOtherPlatform, Index);
Tim Northover7a73cc72015-10-30 16:30:49 +00002526 if (NewAttr)
2527 D->addAttr(NewAttr);
2528 }
2529 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2530 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2531 // matches before the start of the tvOS platform.
2532 IdentifierInfo *NewII = nullptr;
2533 if (II->getName() == "ios")
2534 NewII = &S.Context.Idents.get("tvos");
2535 else if (II->getName() == "ios_app_extension")
2536 NewII = &S.Context.Idents.get("tvos_app_extension");
2537
2538 if (NewII) {
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002539 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2540 ND, AL.getRange(), NewII, true /*Implicit*/, Introduced.Version,
2541 Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict,
2542 Replacement, Sema::AMK_None,
2543 PriorityModifier + Sema::AP_InferredFromOtherPlatform, Index);
2544 if (NewAttr)
2545 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002546 }
2547 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002548}
2549
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002550static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002551 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002552 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002553 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002554 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002555 "Invalid number of arguments in an external_source_symbol attribute");
2556
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002557 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002558 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002559 Language = SE->getString();
2560 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002561 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002562 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002563 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002564
2565 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002566 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2567 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002568}
2569
John McCalld041a9b2013-02-20 01:54:26 +00002570template <class T>
2571static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2572 typename T::VisibilityType value,
2573 unsigned attrSpellingListIndex) {
2574 T *existingAttr = D->getAttr<T>();
2575 if (existingAttr) {
2576 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2577 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002578 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002579 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2580 S.Diag(range.getBegin(), diag::note_previous_attribute);
2581 D->dropAttr<T>();
2582 }
2583 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2584}
2585
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002586VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002587 VisibilityAttr::VisibilityType Vis,
2588 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002589 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2590 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002591}
2592
John McCalld041a9b2013-02-20 01:54:26 +00002593TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2594 TypeVisibilityAttr::VisibilityType Vis,
2595 unsigned AttrSpellingListIndex) {
2596 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2597 AttrSpellingListIndex);
2598}
2599
Erich Keanee891aa92018-07-13 15:07:47 +00002600static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002601 bool isTypeVisibility) {
2602 // Visibility attributes don't mean anything on a typedef.
2603 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002604 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002605 return;
2606 }
2607
2608 // 'type_visibility' can only go on a type or namespace.
2609 if (isTypeVisibility &&
2610 !(isa<TagDecl>(D) ||
2611 isa<ObjCInterfaceDecl>(D) ||
2612 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002613 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002614 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002615 return;
2616 }
2617
Benjamin Kramer70370212013-09-09 15:08:57 +00002618 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002619 StringRef TypeStr;
2620 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002621 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002622 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002623
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002624 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002625 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002626 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2627 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628 return;
2629 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002630
Aaron Ballman682ee422013-09-11 19:47:58 +00002631 // Complain about attempts to use protected visibility on targets
2632 // (like Darwin) that don't support it.
2633 if (type == VisibilityAttr::Protected &&
2634 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002635 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002636 type = VisibilityAttr::Default;
2637 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002638
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002639 unsigned Index = AL.getAttributeSpellingListIndex();
2640 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002641 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002642 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002643 (TypeVisibilityAttr::VisibilityType) type,
2644 Index);
2645 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002646 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002647 }
2648 if (newAttr)
2649 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002650}
2651
Erich Keanee891aa92018-07-13 15:07:47 +00002652static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002653 const auto *M = cast<ObjCMethodDecl>(D);
2654 if (!AL.isArgIdent(0)) {
2655 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002656 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002657 return;
2658 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002659
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002660 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002661 ObjCMethodFamilyAttr::FamilyKind F;
2662 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002663 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002664 return;
2665 }
2666
Alp Toker314cc812014-01-25 16:55:45 +00002667 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002668 !M->getReturnType()->isObjCObjectPointerType()) {
2669 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2670 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002671 // Ignore the attribute.
2672 return;
2673 }
2674
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002675 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2676 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002677}
2678
Erich Keanee891aa92018-07-13 15:07:47 +00002679static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002680 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002681 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002682 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002683 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2684 return;
2685 }
2686 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002687 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002688 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002689 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002690 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2691 return;
2692 }
2693 }
2694 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002695 // It is okay to include this attribute on properties, e.g.:
2696 //
2697 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2698 //
2699 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002700 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002701 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002702 }
Michael Han99315932013-01-24 16:46:58 +00002703 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002704 ObjCNSObjectAttr(AL.getRange(), S.Context,
2705 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002706}
2707
Erich Keanee891aa92018-07-13 15:07:47 +00002708static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002709 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002710 QualType T = TD->getUnderlyingType();
2711 if (!T->isObjCObjectPointerType()) {
2712 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2713 return;
2714 }
2715 } else {
2716 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2717 return;
2718 }
2719 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002720 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2721 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002722}
2723
Erich Keanee891aa92018-07-13 15:07:47 +00002724static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002725 if (!AL.isArgIdent(0)) {
2726 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002727 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002728 return;
2729 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002730
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002731 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002732 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002733 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002734 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002735 return;
2736 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002737
Michael Han99315932013-01-24 16:46:58 +00002738 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002739 BlocksAttr(AL.getRange(), S.Context, type,
2740 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002741}
2742
Erich Keanee891aa92018-07-13 15:07:47 +00002743static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002744 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002745 if (AL.getNumArgs() > 0) {
2746 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002747 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002748 if (E->isTypeDependent() || E->isValueDependent() ||
2749 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002750 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002751 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002752 return;
2753 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002754
John McCallb46f2872011-09-09 07:56:05 +00002755 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002756 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002757 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002758 return;
2759 }
John McCallb46f2872011-09-09 07:56:05 +00002760
2761 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002762 }
2763
Aaron Ballman18a78382013-11-21 00:28:23 +00002764 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002765 if (AL.getNumArgs() > 1) {
2766 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002767 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002768 if (E->isTypeDependent() || E->isValueDependent() ||
2769 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002770 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002771 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002772 return;
2773 }
2774 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002775
John McCallb46f2872011-09-09 07:56:05 +00002776 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002777 // FIXME: This error message could be improved, it would be nice
2778 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002779 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002780 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002781 return;
2782 }
2783 }
2784
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002785 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002786 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002787 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002788 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002789 return;
2790 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002791
Chris Lattner9363e312009-03-17 23:03:47 +00002792 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002793 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002794 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002795 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002796 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002797 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002798 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002799 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002800 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002801 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002802 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002803 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002804 return;
2805 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002806 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002807 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002808 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002809 const FunctionType *FT = Ty->isFunctionPointerType()
2810 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002811 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002812 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002813 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002814 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002815 return;
2816 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002817 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002818 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002819 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002820 return;
2821 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002822 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002823 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002824 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002825 return;
2826 }
Michael Han99315932013-01-24 16:46:58 +00002827 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002828 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2829 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002830}
2831
Erich Keanee891aa92018-07-13 15:07:47 +00002832static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002833 if (D->getFunctionType() &&
2834 D->getFunctionType()->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002835 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002836 return;
2837 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002838 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002839 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002840 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002841 return;
2842 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002843
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002844 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002845 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002846 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2847 !AL.getScopeName())
Erich Keane44bacdf2018-08-09 13:21:32 +00002848 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballmane7964782016-03-07 22:44:55 +00002849
Fangrui Song6907ce22018-07-30 19:24:48 +00002850 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002851 WarnUnusedResultAttr(AL.getRange(), S.Context,
2852 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002853}
2854
Erich Keanee891aa92018-07-13 15:07:47 +00002855static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002856 // weak_import only applies to variable & function declarations.
2857 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002858 if (!D->canBeWeakImported(isDef)) {
2859 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002860 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002861 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002862 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002863 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002864 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002865 // Nothing to warn about here.
2866 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002867 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002868 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002869
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002870 return;
2871 }
2872
Michael Han99315932013-01-24 16:46:58 +00002873 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002874 WeakImportAttr(AL.getRange(), S.Context,
2875 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002876}
2877
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002878// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002879template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002880static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002881 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002882 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002883 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002884 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2885 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002886 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002887 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002888 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002889 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002890 return;
2891 }
2892 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002893
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002894 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2895 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2896 Existing->getYDim() == WGSize[1] &&
2897 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002898 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002899
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002900 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002901 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002902 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002903}
2904
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002905// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002906static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002907 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002908 const Expr *E = AL.getArgAsExpr(0);
2909 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002910 return;
2911 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002912 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002913 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002914 return;
2915 }
2916
2917 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2918 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2919 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002920 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002921
2922 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002923 AL.getRange(), S.Context, SGSize,
2924 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002925}
2926
Erich Keanee891aa92018-07-13 15:07:47 +00002927static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002928 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002929 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002930 return;
2931 }
2932
Craig Topperc3ec1492014-05-26 06:22:03 +00002933 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002934 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002935 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002936
2937 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2938 (ParmType->isBooleanType() ||
2939 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002940 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002941 << ParmType;
2942 return;
2943 }
2944
Aaron Ballmana9e05402013-12-02 22:16:55 +00002945 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002946 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002947 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002948 return;
2949 }
2950 }
2951
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002952 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002953 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002954 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002955}
2956
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002957SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002958 StringRef Name,
2959 unsigned AttrSpellingListIndex) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002960 // Explicit or partial specializations do not inherit
2961 // the section attribute from the primary template.
2962 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2963 if (AttrSpellingListIndex == SectionAttr::Declspec_allocate &&
2964 FD->isFunctionTemplateSpecialization())
2965 return nullptr;
2966 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002967 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2968 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002969 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002970 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2971 << 1 /*section*/;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002972 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002973 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002974 }
Michael Han99315932013-01-24 16:46:58 +00002975 return ::new (Context) SectionAttr(Range, Context, Name,
2976 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002977}
2978
Reid Kleckner2a133222015-03-04 23:39:17 +00002979bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2980 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2981 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002982 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002983 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002984 return false;
2985 }
2986 return true;
2987}
2988
Erich Keanee891aa92018-07-13 15:07:47 +00002989static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002990 // Make sure that there is a string literal as the sections's single
2991 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002992 StringRef Str;
2993 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002994 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002995 return;
Mike Stump11289f42009-09-09 15:08:12 +00002996
Reid Kleckner2a133222015-03-04 23:39:17 +00002997 if (!S.checkSectionName(LiteralLoc, Str))
2998 return;
2999
Chris Lattner30ba6742009-08-10 19:03:04 +00003000 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003001 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00003002 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003003 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00003004 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00003005 return;
3006 }
Mike Stump11289f42009-09-09 15:08:12 +00003007
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003008 unsigned Index = AL.getAttributeSpellingListIndex();
3009 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003010 if (NewAttr)
3011 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00003012}
3013
Erich Keane7963e8b2018-07-18 20:04:48 +00003014static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) {
3015 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
3016 if (!Error.empty()) {
3017 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
3018 << 0 /*'code-seg'*/;
3019 return false;
3020 }
3021 return true;
3022}
3023
3024CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range,
3025 StringRef Name,
3026 unsigned AttrSpellingListIndex) {
3027 // Explicit or partial specializations do not inherit
3028 // the code_seg attribute from the primary template.
3029 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3030 if (FD->isFunctionTemplateSpecialization())
3031 return nullptr;
3032 }
3033 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3034 if (ExistingAttr->getName() == Name)
3035 return nullptr;
3036 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3037 << 0 /*codeseg*/;
3038 Diag(Range.getBegin(), diag::note_previous_attribute);
3039 return nullptr;
3040 }
3041 return ::new (Context) CodeSegAttr(Range, Context, Name,
3042 AttrSpellingListIndex);
3043}
3044
3045static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3046 StringRef Str;
3047 SourceLocation LiteralLoc;
3048 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3049 return;
3050 if (!checkCodeSegName(S, LiteralLoc, Str))
3051 return;
3052 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3053 if (!ExistingAttr->isImplicit()) {
3054 S.Diag(AL.getLoc(),
3055 ExistingAttr->getName() == Str
3056 ? diag::warn_duplicate_codeseg_attribute
3057 : diag::err_conflicting_codeseg_attribute);
3058 return;
3059 }
3060 D->dropAttr<CodeSegAttr>();
3061 }
3062 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str,
3063 AL.getAttributeSpellingListIndex()))
3064 D->addAttr(CSA);
3065}
3066
Erich Keane57e15cd2017-07-19 22:06:33 +00003067// Check for things we'd like to warn about. Multiversioning issues are
3068// handled later in the process, once we know how many exist.
3069bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3070 enum FirstParam { Unsupported, Duplicate };
3071 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00003072 for (auto Str : {"tune=", "fpmath="})
3073 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00003074 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3075 << Unsupported << None << Str;
3076
3077 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
3078
3079 if (!ParsedAttrs.Architecture.empty() &&
3080 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3081 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3082 << Unsupported << Architecture << ParsedAttrs.Architecture;
3083
3084 if (ParsedAttrs.DuplicateArchitecture)
3085 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3086 << Duplicate << None << "arch=";
3087
3088 for (const auto &Feature : ParsedAttrs.Features) {
3089 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3090 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3091 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3092 << Unsupported << None << CurFeature;
3093 }
3094
Erich Keane29636aa2018-02-16 17:31:59 +00003095 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003096}
3097
Erich Keanee891aa92018-07-13 15:07:47 +00003098static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003099 StringRef Str;
3100 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003101 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003102 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003103 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003104
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003105 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00003106 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003107 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
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
3125 D->addAttr(::new (S.Context)
3126 MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth,
3127 AL.getAttributeSpellingListIndex()));
3128}
3129
Erich Keanee891aa92018-07-13 15:07:47 +00003130static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003131 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003132 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003133 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003134 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003135
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003136 // gcc only allows for simple identifiers. Since we support more than gcc, we
3137 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003138 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003139 if (DRE->hasQualifier())
3140 S.Diag(Loc, diag::warn_cleanup_ext);
3141 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3142 NI = DRE->getNameInfo();
3143 if (!FD) {
3144 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3145 << NI.getName();
3146 return;
3147 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003148 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003149 if (ULE->hasExplicitTemplateArgs())
3150 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003151 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3152 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003153 if (!FD) {
3154 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3155 << NI.getName();
3156 if (ULE->getType() == S.Context.OverloadTy)
3157 S.NoteAllOverloadCandidates(ULE);
3158 return;
3159 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003160 } else {
3161 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003162 return;
3163 }
3164
Anders Carlssond277d792009-01-31 01:16:18 +00003165 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003166 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3167 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003168 return;
3169 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003170
Anders Carlsson723f55d2009-02-07 23:16:50 +00003171 // We're currently more strict than GCC about what function types we accept.
3172 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003173 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003174 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003175 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3176 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003177 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3178 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003179 return;
3180 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003181
Michael Han99315932013-01-24 16:46:58 +00003182 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003183 CleanupAttr(AL.getRange(), S.Context, FD,
3184 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003185}
3186
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003187static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003188 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003189 if (!AL.isArgIdent(0)) {
3190 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003191 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003192 return;
3193 }
3194
3195 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003196 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003197 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3198 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003199 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003200 return;
3201 }
3202
3203 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003204 AL.getRange(), S.Context, ExtensibilityKind,
3205 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003206}
3207
Mike Stumpd3bb5572009-07-24 19:02:52 +00003208/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003209/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003210static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003211 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003212 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003213 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003214 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003215
Eric Christopherb64963e2015-08-13 21:34:35 +00003216 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003217 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003218
Eric Christopherb64963e2015-08-13 21:34:35 +00003219 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3220 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003221 !isCFStringType(Ty, S.Context) &&
3222 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003223 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003224 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003225 << "a string type" << IdxExpr->getSourceRange()
3226 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003227 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003228 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003229 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003230 if (!isNSStringType(Ty, S.Context) &&
3231 !isCFStringType(Ty, S.Context) &&
3232 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003233 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003234 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003235 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003236 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003237 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003238 }
3239
Joel E. Denny81508102018-03-13 14:51:22 +00003240 D->addAttr(::new (S.Context) FormatArgAttr(
3241 AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003242}
3243
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003244enum FormatAttrKind {
3245 CFStringFormat,
3246 NSStringFormat,
3247 StrftimeFormat,
3248 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003249 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003250 InvalidFormat
3251};
3252
3253/// getFormatAttrKind - Map from format attribute names to supported format
3254/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003255static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003256 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003257 // Check for formats that get handled specially.
3258 .Case("NSString", NSStringFormat)
3259 .Case("CFString", CFStringFormat)
3260 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003261
Mehdi Amini06d367c2016-10-24 20:39:34 +00003262 // Otherwise, check for supported formats.
3263 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3264 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3265 .Case("kprintf", SupportedFormat) // OpenBSD.
3266 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3267 .Case("os_trace", SupportedFormat)
3268 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003269
Mehdi Amini06d367c2016-10-24 20:39:34 +00003270 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3271 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003272}
3273
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003274/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003275/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003276static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003277 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003278 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003279 return;
3280 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003281
Aaron Ballman4a611152013-11-27 16:34:09 +00003282 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003283 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3284 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003285 return;
3286 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003287 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003288 if (S.Context.getAsArrayType(T))
3289 T = S.Context.getBaseElementType(T);
3290 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003291 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3292 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003293 return;
3294 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003295
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003296 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003297 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003298 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3299 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003300 return;
3301 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003302
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003303 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballman52c9ad22019-02-12 13:04:11 +00003304 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003305 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003306 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003307 return;
3308 }
Michael Han99315932013-01-24 16:46:58 +00003309 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003310 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3311 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003312}
3313
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003314FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3315 IdentifierInfo *Format, int FormatIdx,
3316 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003317 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003318 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003319 for (auto *F : D->specific_attrs<FormatAttr>()) {
3320 if (F->getType() == Format &&
3321 F->getFormatIdx() == FormatIdx &&
3322 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003323 // If we don't have a valid location for this attribute, adopt the
3324 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003325 if (F->getLocation().isInvalid())
3326 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003327 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003328 }
3329 }
3330
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003331 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3332 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003333}
3334
Mike Stumpd3bb5572009-07-24 19:02:52 +00003335/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003336/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003337static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003338 if (!AL.isArgIdent(0)) {
3339 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003340 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003341 return;
3342 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003343
Chandler Carruth743682b2010-11-16 08:35:43 +00003344 // In C++ the implicit 'this' function parameter also counts, and they are
3345 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003346 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003347 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003348
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003349 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003350 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003351
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003352 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003353 // If we've modified the string name, we need a new identifier for it.
3354 II = &S.Context.Idents.get(Format);
3355 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003356
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003357 // Check for supported formats.
3358 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003359
Chris Lattner12161d32010-03-22 21:08:50 +00003360 if (Kind == IgnoredFormat)
3361 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003362
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003363 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003364 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003365 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003366 return;
3367 }
3368
3369 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003370 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003371 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003372 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003373 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003374
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003375 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003376 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003377 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003378 return;
3379 }
3380
3381 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003382 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003383
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003384 if (HasImplicitThisParam) {
3385 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003386 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003387 diag::err_format_attribute_implicit_this_format_string)
3388 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003389 return;
3390 }
3391 ArgIdx--;
3392 }
Mike Stump11289f42009-09-09 15:08:12 +00003393
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003394 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003395 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003396
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003397 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003398 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003399 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003400 << "a CFString" << IdxExpr->getSourceRange()
3401 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003402 return;
3403 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003404 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003405 // FIXME: do we need to check if the type is NSString*? What are the
3406 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003407 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003408 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003409 << "an NSString" << IdxExpr->getSourceRange()
3410 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003411 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003412 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003413 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003414 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003415 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003416 << "a string type" << IdxExpr->getSourceRange()
3417 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003418 return;
3419 }
3420
3421 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003422 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003423 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003424 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003425 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003426
3427 // check if the function is variadic if the 3rd argument non-zero
3428 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003429 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003430 ++NumArgs; // +1 for ...
3431 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003432 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003433 return;
3434 }
3435 }
3436
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003437 // strftime requires FirstArg to be 0 because it doesn't read from any
3438 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003439 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003440 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003441 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003442 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003443 return;
3444 }
3445 // if 0 it disables parameter checking (to use with e.g. va_list)
3446 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003447 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003448 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003449 return;
3450 }
3451
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003452 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003453 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003454 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003455 if (NewAttr)
3456 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003457}
3458
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003459/// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
3460static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3461 // The index that identifies the callback callee is mandatory.
3462 if (AL.getNumArgs() == 0) {
3463 S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee)
3464 << AL.getRange();
3465 return;
3466 }
3467
3468 bool HasImplicitThisParam = isInstanceMethod(D);
3469 int32_t NumArgs = getFunctionOrMethodNumParams(D);
3470
3471 FunctionDecl *FD = D->getAsFunction();
3472 assert(FD && "Expected a function declaration!");
3473
3474 llvm::StringMap<int> NameIdxMapping;
3475 NameIdxMapping["__"] = -1;
3476
3477 NameIdxMapping["this"] = 0;
3478
3479 int Idx = 1;
3480 for (const ParmVarDecl *PVD : FD->parameters())
3481 NameIdxMapping[PVD->getName()] = Idx++;
3482
3483 auto UnknownName = NameIdxMapping.end();
3484
3485 SmallVector<int, 8> EncodingIndices;
3486 for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) {
3487 SourceRange SR;
3488 int32_t ArgIdx;
3489
3490 if (AL.isArgIdent(I)) {
3491 IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
3492 auto It = NameIdxMapping.find(IdLoc->Ident->getName());
3493 if (It == UnknownName) {
3494 S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown)
3495 << IdLoc->Ident << IdLoc->Loc;
3496 return;
3497 }
3498
3499 SR = SourceRange(IdLoc->Loc);
3500 ArgIdx = It->second;
3501 } else if (AL.isArgExpr(I)) {
3502 Expr *IdxExpr = AL.getArgAsExpr(I);
3503
3504 // If the expression is not parseable as an int32_t we have a problem.
3505 if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1,
3506 false)) {
3507 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3508 << AL << (I + 1) << IdxExpr->getSourceRange();
3509 return;
3510 }
3511
3512 // Check oob, excluding the special values, 0 and -1.
3513 if (ArgIdx < -1 || ArgIdx > NumArgs) {
3514 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3515 << AL << (I + 1) << IdxExpr->getSourceRange();
3516 return;
3517 }
3518
3519 SR = IdxExpr->getSourceRange();
3520 } else {
3521 llvm_unreachable("Unexpected ParsedAttr argument type!");
3522 }
3523
3524 if (ArgIdx == 0 && !HasImplicitThisParam) {
3525 S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
3526 << (I + 1) << SR;
3527 return;
3528 }
3529
3530 // Adjust for the case we do not have an implicit "this" parameter. In this
3531 // case we decrease all positive values by 1 to get LLVM argument indices.
3532 if (!HasImplicitThisParam && ArgIdx > 0)
3533 ArgIdx -= 1;
3534
3535 EncodingIndices.push_back(ArgIdx);
3536 }
3537
3538 int CalleeIdx = EncodingIndices.front();
3539 // Check if the callee index is proper, thus not "this" and not "unknown".
Johannes Doerferte068d052019-01-21 14:23:46 +00003540 // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam"
3541 // is false and positive if "HasImplicitThisParam" is true.
3542 if (CalleeIdx < (int)HasImplicitThisParam) {
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003543 S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee)
3544 << AL.getRange();
3545 return;
3546 }
3547
3548 // Get the callee type, note the index adjustment as the AST doesn't contain
3549 // the this type (which the callee cannot reference anyway!).
3550 const Type *CalleeType =
3551 getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam)
3552 .getTypePtr();
3553 if (!CalleeType || !CalleeType->isFunctionPointerType()) {
3554 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3555 << AL.getRange();
3556 return;
3557 }
3558
3559 const Type *CalleeFnType =
3560 CalleeType->getPointeeType()->getUnqualifiedDesugaredType();
3561
3562 // TODO: Check the type of the callee arguments.
3563
3564 const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
3565 if (!CalleeFnProtoType) {
3566 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3567 << AL.getRange();
3568 return;
3569 }
3570
3571 if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
3572 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3573 << AL << (unsigned)(EncodingIndices.size() - 1);
3574 return;
3575 }
3576
3577 if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
3578 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3579 << AL << (unsigned)(EncodingIndices.size() - 1);
3580 return;
3581 }
3582
3583 if (CalleeFnProtoType->isVariadic()) {
3584 S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
3585 return;
3586 }
3587
3588 // Do not allow multiple callback attributes.
3589 if (D->hasAttr<CallbackAttr>()) {
3590 S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
3591 return;
3592 }
3593
3594 D->addAttr(::new (S.Context) CallbackAttr(
3595 AL.getRange(), S.Context, EncodingIndices.data(), EncodingIndices.size(),
3596 AL.getAttributeSpellingListIndex()));
3597}
3598
Erich Keanee891aa92018-07-13 15:07:47 +00003599static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003600 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003601 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003602 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003603 if (TD && TD->getUnderlyingType()->isUnionType())
3604 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3605 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003606 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003607
3608 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003609 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3610 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003611 return;
3612 }
3613
John McCallf937c022011-10-07 06:10:15 +00003614 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003615 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003616 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003617 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003618 return;
3619 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003620
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003621 RecordDecl::field_iterator Field = RD->field_begin(),
3622 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003623 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003624 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003625 return;
3626 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003627
David Blaikie40ed2972012-06-06 20:45:41 +00003628 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003629 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003630 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003631 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003632 diag::warn_transparent_union_attribute_floating)
3633 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003634 return;
3635 }
3636
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003637 if (FirstType->isIncompleteType())
3638 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003639 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3640 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3641 for (; Field != FieldEnd; ++Field) {
3642 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003643 if (FieldType->isIncompleteType())
3644 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003645 // FIXME: this isn't fully correct; we also need to test whether the
3646 // members of the union would all have the same calling convention as the
3647 // first member of the union. Checking just the size and alignment isn't
3648 // sufficient (consider structs passed on the stack instead of in registers
3649 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003650 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003651 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003652 // Warn if we drop the attribute.
3653 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003654 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003655 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003656 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003657 diag::warn_transparent_union_attribute_field_size_align)
3658 << isSize << Field->getDeclName() << FieldBits;
3659 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003660 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003661 diag::note_transparent_union_first_field_size_align)
3662 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003663 return;
3664 }
3665 }
3666
Michael Han99315932013-01-24 16:46:58 +00003667 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003668 TransparentUnionAttr(AL.getRange(), S.Context,
3669 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003670}
3671
Erich Keanee891aa92018-07-13 15:07:47 +00003672static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003673 // Make sure that there is a string literal as the annotation's single
3674 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003675 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003676 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003677 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003678
3679 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003680 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3681 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003682 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003683 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003684
Michael Han99315932013-01-24 16:46:58 +00003685 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003686 AnnotateAttr(AL.getRange(), S.Context, Str,
3687 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003688}
3689
Erich Keanee891aa92018-07-13 15:07:47 +00003690static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003691 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3692 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003693}
3694
3695void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3696 unsigned SpellingListIndex) {
3697 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3698 SourceLocation AttrLoc = AttrRange.getBegin();
3699
3700 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003701 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003702 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003703 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003704 T = VD->getType();
3705 else
3706 llvm_unreachable("Unknown decl type for align_value");
3707
3708 if (!T->isDependentType() && !T->isAnyPointerType() &&
3709 !T->isReferenceType() && !T->isMemberPointerType()) {
3710 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3711 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3712 return;
3713 }
3714
3715 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003716 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003717 ExprResult ICE
3718 = VerifyIntegerConstantExpression(E, &Alignment,
3719 diag::err_align_value_attribute_argument_not_int,
3720 /*AllowFold*/ false);
3721 if (ICE.isInvalid())
3722 return;
3723
3724 if (!Alignment.isPowerOf2()) {
3725 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3726 << E->getSourceRange();
3727 return;
3728 }
3729
3730 D->addAttr(::new (Context)
3731 AlignValueAttr(AttrRange, Context, ICE.get(),
3732 SpellingListIndex));
3733 return;
3734 }
3735
3736 // Save dependent expressions in the AST to be instantiated.
3737 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003738}
3739
Erich Keanee891aa92018-07-13 15:07:47 +00003740static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003741 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003742 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003743 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003744 return;
3745 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003746
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003747 if (AL.getNumArgs() == 0) {
3748 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3749 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003750 return;
3751 }
3752
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003753 Expr *E = AL.getArgAsExpr(0);
3754 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3755 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003756 diag::err_pack_expansion_without_parameter_packs);
3757 return;
3758 }
3759
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003760 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003761 return;
3762
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003763 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3764 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003765}
3766
3767void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003768 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003769 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3770 SourceLocation AttrLoc = AttrRange.getBegin();
3771
Richard Smith1dba27c2013-01-29 09:02:09 +00003772 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003773 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003774 // C++11 [dcl.align]p1:
3775 // An alignment-specifier may be applied to a variable or to a class
3776 // data member, but it shall not be applied to a bit-field, a function
3777 // parameter, the formal parameter of a catch clause, or a variable
3778 // declared with the register storage class specifier. An
3779 // alignment-specifier may also be applied to the declaration of a class
3780 // or enumeration type.
3781 // C11 6.7.5/2:
3782 // An alignment attribute shall not be specified in a declaration of
3783 // a typedef, or a bit-field, or a function, or a parameter, or an
3784 // object declared with the register storage-class specifier.
3785 int DiagKind = -1;
3786 if (isa<ParmVarDecl>(D)) {
3787 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003788 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003789 if (VD->getStorageClass() == SC_Register)
3790 DiagKind = 1;
3791 if (VD->isExceptionVariable())
3792 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003793 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003794 if (FD->isBitField())
3795 DiagKind = 3;
3796 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003797 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003798 << (TmpAttr.isC11() ? ExpectedVariableOrField
3799 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003800 return;
3801 }
3802 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003803 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003804 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003805 return;
3806 }
3807 }
3808
Richard Smith90ae9672018-06-20 23:36:55 +00003809 if (E->isValueDependent()) {
3810 // We can't support a dependent alignment on a non-dependent type,
3811 // because we have no way to model that a type is "alignment-dependent"
3812 // but not dependent in any other way.
3813 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3814 if (!TND->getUnderlyingType()->isDependentType()) {
3815 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3816 << E->getSourceRange();
3817 return;
3818 }
3819 }
3820
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003821 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003822 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3823 AA->setPackExpansion(IsPackExpansion);
3824 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003825 return;
3826 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003827
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003828 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003829 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003830 ExprResult ICE
3831 = VerifyIntegerConstantExpression(E, &Alignment,
3832 diag::err_aligned_attribute_argument_not_int,
3833 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003834 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003835 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003836
David Majnemer0be6bd02015-07-26 09:02:21 +00003837 uint64_t AlignVal = Alignment.getZExtValue();
3838
Richard Smith848e1f12013-02-01 08:12:08 +00003839 // C++11 [dcl.align]p2:
3840 // -- if the constant expression evaluates to zero, the alignment
3841 // specifier shall have no effect
3842 // C11 6.7.5p6:
3843 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003844 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003845 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003846 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3847 << E->getSourceRange();
3848 return;
3849 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003850 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003851
David Majnemerabecae72014-02-12 20:36:10 +00003852 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003853 unsigned MaxValidAlignment =
3854 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3855 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003856 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003857 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3858 << E->getSourceRange();
3859 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003860 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003861
David Majnemer0be6bd02015-07-26 09:02:21 +00003862 if (Context.getTargetInfo().isTLSSupported()) {
3863 unsigned MaxTLSAlign =
3864 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3865 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003866 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003867 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3868 VD->getTLSKind() != VarDecl::TLS_None) {
3869 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3870 << (unsigned)AlignVal << VD << MaxTLSAlign;
3871 return;
3872 }
3873 }
3874
Richard Smith44c247f2013-02-22 08:32:16 +00003875 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003876 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003877 AA->setPackExpansion(IsPackExpansion);
3878 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003879}
3880
Michael Hanaf02bbe2013-02-01 01:19:17 +00003881void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003882 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003883 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003884 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003885 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3886 SpellingListIndex);
3887 AA->setPackExpansion(IsPackExpansion);
3888 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003889}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003890
Richard Smith848e1f12013-02-01 08:12:08 +00003891void Sema::CheckAlignasUnderalignment(Decl *D) {
3892 assert(D->hasAttrs() && "no attributes on decl");
3893
David Majnemer475b25e2015-01-21 10:54:38 +00003894 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003895 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003896 UnderlyingTy = DiagTy = VD->getType();
3897 } else {
3898 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003899 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003900 UnderlyingTy = ED->getIntegerType();
3901 }
3902 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003903 return;
3904
3905 // C++11 [dcl.align]p5, C11 6.7.5/4:
3906 // The combined effect of all alignment attributes in a declaration shall
3907 // not specify an alignment that is less strict than the alignment that
3908 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003909 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003910 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003911 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003912 if (I->isAlignmentDependent())
3913 return;
3914 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003915 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003916 Align = std::max(Align, I->getAlignment(Context));
3917 }
3918
3919 if (AlignasAttr && Align) {
3920 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003921 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003922 if (NaturalAlign > RequestedAlign)
3923 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003924 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003925 }
3926}
3927
David Majnemer2c4e00a2014-01-29 22:07:36 +00003928bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003929 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003930 MSInheritanceAttr::Spelling SemanticSpelling) {
3931 assert(RD->hasDefinition() && "RD has no definition!");
3932
David Majnemer98c9ee22014-02-07 00:43:07 +00003933 // We may not have seen base specifiers or any virtual methods yet. We will
3934 // have to wait until the record is defined to catch any mismatches.
3935 if (!RD->getDefinition()->isCompleteDefinition())
3936 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003937
David Majnemer98c9ee22014-02-07 00:43:07 +00003938 // The unspecified model never matches what a definition could need.
3939 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3940 return false;
3941
David Majnemer4bb09802014-02-10 19:50:15 +00003942 if (BestCase) {
3943 if (RD->calculateInheritanceModel() == SemanticSpelling)
3944 return false;
3945 } else {
3946 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3947 return false;
3948 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003949
3950 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3951 << 0 /*definition*/;
3952 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3953 << RD->getNameAsString();
3954 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003955}
3956
Alexey Bataevf278eb12015-11-19 10:13:11 +00003957/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3958/// attribute.
3959static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3960 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003961 IntegerMode = true;
3962 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003963 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003964 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003965 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003966 case 'Q':
3967 DestWidth = 8;
3968 break;
3969 case 'H':
3970 DestWidth = 16;
3971 break;
3972 case 'S':
3973 DestWidth = 32;
3974 break;
3975 case 'D':
3976 DestWidth = 64;
3977 break;
3978 case 'X':
3979 DestWidth = 96;
3980 break;
3981 case 'T':
3982 DestWidth = 128;
3983 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003984 }
3985 if (Str[1] == 'F') {
3986 IntegerMode = false;
3987 } else if (Str[1] == 'C') {
3988 IntegerMode = false;
3989 ComplexMode = true;
3990 } else if (Str[1] != 'I') {
3991 DestWidth = 0;
3992 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003993 break;
3994 case 4:
3995 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3996 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003997 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003998 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003999 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00004000 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00004001 break;
4002 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00004003 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00004004 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00004005 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00004006 case 11:
4007 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00004008 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00004009 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004010 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00004011}
4012
4013/// handleModeAttr - This attribute modifies the width of a decl with primitive
4014/// type.
4015///
4016/// Despite what would be logical, the mode attribute is a decl attribute, not a
4017/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
4018/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00004019static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00004020 // This attribute isn't documented, but glibc uses it. It changes
4021 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004022 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004023 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
4024 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004025 return;
4026 }
4027
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004028 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004029
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004030 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004031}
4032
4033void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
4034 unsigned SpellingListIndex, bool InInstantiation) {
4035 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00004036 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004037 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00004038
4039 unsigned DestWidth = 0;
4040 bool IntegerMode = true;
4041 bool ComplexMode = false;
4042 llvm::APInt VectorSize(64, 0);
4043 if (Str.size() >= 4 && Str[0] == 'V') {
4044 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
4045 size_t StrSize = Str.size();
4046 size_t VectorStringLength = 0;
4047 while ((VectorStringLength + 1) < StrSize &&
4048 isdigit(Str[VectorStringLength + 1]))
4049 ++VectorStringLength;
4050 if (VectorStringLength &&
4051 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
4052 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004053 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00004054 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004055 // Avoid duplicate warning from template instantiation.
4056 if (!InInstantiation)
4057 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00004058 } else {
4059 VectorSize = 0;
4060 }
4061 }
4062
4063 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004064 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
4065
4066 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
4067 // and friends, at least with glibc.
4068 // FIXME: Make sure floating-point mappings are accurate
4069 // FIXME: Support XF and TF types
4070 if (!DestWidth) {
4071 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
4072 return;
4073 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00004074
4075 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004076 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00004077 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004078 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004079 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
4080 // Try to get type from enum declaration, default to int.
4081 OldTy = ED->getIntegerType();
4082 if (OldTy.isNull())
4083 OldTy = Context.IntTy;
4084 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004085 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00004086
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004087 if (OldTy->isDependentType()) {
4088 D->addAttr(::new (Context)
4089 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
4090 return;
4091 }
4092
Alexey Bataev326057d2015-06-19 07:46:21 +00004093 // Base type can also be a vector type (see PR17453).
4094 // Distinguish between base type and base element type.
4095 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004096 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00004097 OldElemTy = VT->getElementType();
4098
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004099 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
4100 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
4101 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
4102 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
4103 VectorSize.getBoolValue()) {
4104 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
4105 return;
4106 }
4107 bool IntegralOrAnyEnumType =
4108 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
4109
4110 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
4111 !IntegralOrAnyEnumType)
4112 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00004113 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004114 if (!IntegralOrAnyEnumType)
4115 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004116 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004117 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004118 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004119 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00004120 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004121 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004122 }
4123
Alexey Bataev326057d2015-06-19 07:46:21 +00004124 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004125
4126 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004127 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
4128 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004129 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004130 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004131
Alexey Bataev326057d2015-06-19 07:46:21 +00004132 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004133 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004134 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004135 }
4136
Eli Friedman4735374e2009-03-03 06:41:03 +00004137 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004138 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004139 }
4140
4141 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004142 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004143 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
4144 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004145 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004146 // Complex machine mode does not support base vector types.
4147 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004148 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004149 return;
4150 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004151 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00004152 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004153 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004154 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004155 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00004156 }
4157
4158 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004159 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004160 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004161 }
4162
4163 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004164 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004165 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004166 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004167 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004168 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004169 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004170
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004171 D->addAttr(::new (Context)
4172 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00004173}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004174
Erich Keanee891aa92018-07-13 15:07:47 +00004175static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Han99315932013-01-24 16:46:58 +00004176 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004177 NoDebugAttr(AL.getRange(), S.Context,
4178 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00004179}
4180
Paul Robinson30e41fb2014-12-15 18:57:28 +00004181AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00004182 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00004183 unsigned AttrSpellingListIndex) {
4184 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004185 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00004186 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4187 return nullptr;
4188 }
4189
4190 if (D->hasAttr<AlwaysInlineAttr>())
4191 return nullptr;
4192
4193 return ::new (Context) AlwaysInlineAttr(Range, Context,
4194 AttrSpellingListIndex);
4195}
4196
Erich Keane44bacdf2018-08-09 13:21:32 +00004197CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
4198 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004199 return nullptr;
4200
Erich Keane44bacdf2018-08-09 13:21:32 +00004201 return ::new (Context)
4202 CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004203}
4204
Erich Keane44bacdf2018-08-09 13:21:32 +00004205CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
4206 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
4207 return nullptr;
4208
4209 return ::new (Context)
4210 CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex());
4211}
4212
4213InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4214 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004215 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004216 // Attribute applies to Var but not any subclass of it (like ParmVar,
4217 // ImplicitParm or VarTemplateSpecialization).
4218 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004219 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4220 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4221 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004222 return nullptr;
4223 }
4224 // Attribute does not apply to non-static local variables.
4225 if (VD->hasLocalStorage()) {
4226 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4227 return nullptr;
4228 }
4229 }
4230
Erich Keane44bacdf2018-08-09 13:21:32 +00004231 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
4232 return nullptr;
4233
4234 return ::new (Context) InternalLinkageAttr(
4235 AL.getRange(), Context, AL.getAttributeSpellingListIndex());
4236}
4237InternalLinkageAttr *
4238Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4239 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4240 // Attribute applies to Var but not any subclass of it (like ParmVar,
4241 // ImplicitParm or VarTemplateSpecialization).
4242 if (VD->getKind() != Decl::Var) {
4243 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4244 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4245 : ExpectedVariableOrFunction);
4246 return nullptr;
4247 }
4248 // Attribute does not apply to non-static local variables.
4249 if (VD->hasLocalStorage()) {
4250 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4251 return nullptr;
4252 }
4253 }
4254
4255 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004256 return nullptr;
4257
4258 return ::new (Context)
Erich Keane44bacdf2018-08-09 13:21:32 +00004259 InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004260}
4261
Paul Robinson30e41fb2014-12-15 18:57:28 +00004262MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
4263 unsigned AttrSpellingListIndex) {
4264 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4265 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
4266 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4267 return nullptr;
4268 }
4269
4270 if (D->hasAttr<MinSizeAttr>())
4271 return nullptr;
4272
4273 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
4274}
4275
Zola Bridges826ef592019-01-18 17:20:46 +00004276NoSpeculativeLoadHardeningAttr *Sema::mergeNoSpeculativeLoadHardeningAttr(
4277 Decl *D, const NoSpeculativeLoadHardeningAttr &AL) {
4278 if (checkAttrMutualExclusion<SpeculativeLoadHardeningAttr>(*this, D, AL))
4279 return nullptr;
4280
4281 return ::new (Context) NoSpeculativeLoadHardeningAttr(
4282 AL.getRange(), Context, AL.getSpellingListIndex());
4283}
4284
Paul Robinson30e41fb2014-12-15 18:57:28 +00004285OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
4286 unsigned AttrSpellingListIndex) {
4287 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4288 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4289 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4290 D->dropAttr<AlwaysInlineAttr>();
4291 }
4292 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4293 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4294 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4295 D->dropAttr<MinSizeAttr>();
4296 }
4297
4298 if (D->hasAttr<OptimizeNoneAttr>())
4299 return nullptr;
4300
4301 return ::new (Context) OptimizeNoneAttr(Range, Context,
4302 AttrSpellingListIndex);
4303}
4304
Zola Bridges826ef592019-01-18 17:20:46 +00004305SpeculativeLoadHardeningAttr *Sema::mergeSpeculativeLoadHardeningAttr(
4306 Decl *D, const SpeculativeLoadHardeningAttr &AL) {
4307 if (checkAttrMutualExclusion<NoSpeculativeLoadHardeningAttr>(*this, D, AL))
4308 return nullptr;
4309
4310 return ::new (Context) SpeculativeLoadHardeningAttr(
4311 AL.getRange(), Context, AL.getSpellingListIndex());
4312}
4313
Erich Keanee891aa92018-07-13 15:07:47 +00004314static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004315 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004316 return;
4317
Paul Robinson080b1f32015-01-13 18:34:56 +00004318 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004319 D, AL.getRange(), AL.getName(),
4320 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004321 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004322}
4323
Erich Keanee891aa92018-07-13 15:07:47 +00004324static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004325 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004326 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004327 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004328}
4329
Erich Keanee891aa92018-07-13 15:07:47 +00004330static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004331 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004332 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004333 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004334}
4335
Erich Keanee891aa92018-07-13 15:07:47 +00004336static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004337 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004338 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004339 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004340 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004341 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004342 return;
4343 }
4344 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004345 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004346}
4347
Erich Keanee891aa92018-07-13 15:07:47 +00004348static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004349 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004350 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004351 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004352 // extern __shared__ is only allowed on arrays with no length (e.g.
4353 // "int x[]").
Yaxun Liu97670892018-10-02 17:48:54 +00004354 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004355 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004356 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004357 return;
4358 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004359 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004360 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004361 << S.CurrentCUDATarget())
4362 return;
Justin Lebar10411012016-09-30 23:57:30 +00004363 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004364 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004365}
4366
Erich Keanee891aa92018-07-13 15:07:47 +00004367static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004368 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4369 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004370 return;
4371 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004372 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004373 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004374 SourceRange RTRange = FD->getReturnTypeSourceRange();
4375 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004376 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004377 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4378 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004379 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004380 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004381 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4382 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004383 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004384 << Method;
4385 return;
4386 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004387 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004388 }
4389 // Only warn for "inline" when compiling for host, to cut down on noise.
4390 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004391 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004392
Aaron Ballman3aff6332013-12-02 19:30:36 +00004393 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004394 CUDAGlobalAttr(AL.getRange(), S.Context,
4395 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004396}
4397
Erich Keanee891aa92018-07-13 15:07:47 +00004398static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004399 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004400 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004401 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004402 return;
4403 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004404
Michael Han99315932013-01-24 16:46:58 +00004405 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004406 GNUInlineAttr(AL.getRange(), S.Context,
4407 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004408}
4409
Erich Keanee891aa92018-07-13 15:07:47 +00004410static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004411 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004412
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004413 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004414 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4415 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004416 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004417 return;
4418
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004419 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004420 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004421 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004422 return;
4423 }
4424
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004425 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004426 case ParsedAttr::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004427 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004428 FastCallAttr(AL.getRange(), S.Context,
4429 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004430 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004431 case ParsedAttr::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004432 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004433 StdCallAttr(AL.getRange(), S.Context,
4434 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004435 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004436 case ParsedAttr::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004437 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004438 ThisCallAttr(AL.getRange(), S.Context,
4439 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004440 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004441 case ParsedAttr::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004442 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004443 CDeclAttr(AL.getRange(), S.Context,
4444 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004445 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004446 case ParsedAttr::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004447 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004448 PascalAttr(AL.getRange(), S.Context,
4449 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004450 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004451 case ParsedAttr::AT_SwiftCall:
John McCall477f2bb2016-03-03 06:39:32 +00004452 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004453 SwiftCallAttr(AL.getRange(), S.Context,
4454 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004455 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004456 case ParsedAttr::AT_VectorCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004457 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004458 VectorCallAttr(AL.getRange(), S.Context,
4459 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004460 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004461 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004462 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004463 MSABIAttr(AL.getRange(), S.Context,
4464 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004465 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004466 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004467 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004468 SysVABIAttr(AL.getRange(), S.Context,
4469 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004470 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004471 case ParsedAttr::AT_RegCall:
Erich Keane757d3172016-11-02 18:29:35 +00004472 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004473 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004474 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004475 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004476 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004477 switch (CC) {
4478 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004479 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004480 break;
4481 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004482 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004483 break;
4484 default:
4485 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004486 }
4487
Michael Han99315932013-01-24 16:46:58 +00004488 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004489 PcsAttr(AL.getRange(), S.Context, PCS,
4490 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004491 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004492 }
Sander de Smalen44a22532018-11-26 16:38:37 +00004493 case ParsedAttr::AT_AArch64VectorPcs:
4494 D->addAttr(::new(S.Context)
4495 AArch64VectorPcsAttr(AL.getRange(), S.Context,
4496 AL.getAttributeSpellingListIndex()));
4497 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004498 case ParsedAttr::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004499 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004500 IntelOclBiccAttr(AL.getRange(), S.Context,
4501 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004502 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004503 case ParsedAttr::AT_PreserveMost:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004504 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004505 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004506 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004507 case ParsedAttr::AT_PreserveAll:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004508 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004509 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004510 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004511 default:
4512 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004513 }
4514}
4515
Erich Keanee891aa92018-07-13 15:07:47 +00004516static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004517 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004518 return;
4519
4520 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004521 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004522 StringRef RuleName;
4523
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004524 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004525 return;
4526
4527 // FIXME: Warn if the rule name is unknown. This is tricky because only
4528 // clang-tidy knows about available rules.
4529 DiagnosticIdentifiers.push_back(RuleName);
4530 }
4531 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004532 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4533 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004534}
4535
Erich Keanee891aa92018-07-13 15:07:47 +00004536bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004537 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004538 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004539 return true;
4540
Erich Keaneb11ebc52017-09-27 03:20:13 +00004541 if (Attrs.hasProcessingCache()) {
4542 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004543 return false;
4544 }
4545
Erich Keanee891aa92018-07-13 15:07:47 +00004546 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004547 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4548 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004549 return true;
4550 }
4551
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004552 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004553 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004554 case ParsedAttr::AT_CDecl:
4555 CC = CC_C;
4556 break;
4557 case ParsedAttr::AT_FastCall:
4558 CC = CC_X86FastCall;
4559 break;
4560 case ParsedAttr::AT_StdCall:
4561 CC = CC_X86StdCall;
4562 break;
4563 case ParsedAttr::AT_ThisCall:
4564 CC = CC_X86ThisCall;
4565 break;
4566 case ParsedAttr::AT_Pascal:
4567 CC = CC_X86Pascal;
4568 break;
4569 case ParsedAttr::AT_SwiftCall:
4570 CC = CC_Swift;
4571 break;
4572 case ParsedAttr::AT_VectorCall:
4573 CC = CC_X86VectorCall;
4574 break;
Sander de Smalen44a22532018-11-26 16:38:37 +00004575 case ParsedAttr::AT_AArch64VectorPcs:
4576 CC = CC_AArch64VectorCall;
4577 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004578 case ParsedAttr::AT_RegCall:
4579 CC = CC_X86RegCall;
4580 break;
4581 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004582 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004583 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004584 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004585 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004586 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4587 CC_C;
4588 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004589 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004590 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004591 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4592 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004593 return true;
4594 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004595 if (StrRef == "aapcs") {
4596 CC = CC_AAPCS;
4597 break;
4598 } else if (StrRef == "aapcs-vfp") {
4599 CC = CC_AAPCS_VFP;
4600 break;
4601 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004602
Erich Keaneb11ebc52017-09-27 03:20:13 +00004603 Attrs.setInvalid();
4604 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004605 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004606 }
Erich Keanee891aa92018-07-13 15:07:47 +00004607 case ParsedAttr::AT_IntelOclBicc:
4608 CC = CC_IntelOclBicc;
4609 break;
4610 case ParsedAttr::AT_PreserveMost:
4611 CC = CC_PreserveMost;
4612 break;
4613 case ParsedAttr::AT_PreserveAll:
4614 CC = CC_PreserveAll;
4615 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004616 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004617 }
4618
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004619 TargetInfo::CallingConvCheckResult A = TargetInfo::CCCR_OK;
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004620 const TargetInfo &TI = Context.getTargetInfo();
Yaxun Liu785cbd82019-02-27 15:46:29 +00004621 // CUDA functions may have host and/or device attributes which indicate
4622 // their targeted execution environment, therefore the calling convention
4623 // of functions in CUDA should be checked against the target deduced based
4624 // on their host/device attributes.
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004625 if (LangOpts.CUDA) {
Yaxun Liu785cbd82019-02-27 15:46:29 +00004626 auto *Aux = Context.getAuxTargetInfo();
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004627 auto CudaTarget = IdentifyCUDATarget(FD);
4628 bool CheckHost = false, CheckDevice = false;
4629 switch (CudaTarget) {
4630 case CFT_HostDevice:
4631 CheckHost = true;
4632 CheckDevice = true;
4633 break;
4634 case CFT_Host:
4635 CheckHost = true;
4636 break;
4637 case CFT_Device:
4638 case CFT_Global:
4639 CheckDevice = true;
4640 break;
4641 case CFT_InvalidTarget:
4642 llvm_unreachable("unexpected cuda target");
4643 }
4644 auto *HostTI = LangOpts.CUDAIsDevice ? Aux : &TI;
4645 auto *DeviceTI = LangOpts.CUDAIsDevice ? &TI : Aux;
4646 if (CheckHost && HostTI)
4647 A = HostTI->checkCallingConvention(CC);
4648 if (A == TargetInfo::CCCR_OK && CheckDevice && DeviceTI)
4649 A = DeviceTI->checkCallingConvention(CC);
4650 } else {
4651 A = TI.checkCallingConvention(CC);
4652 }
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004653 if (A != TargetInfo::CCCR_OK) {
4654 if (A == TargetInfo::CCCR_Warning)
Erich Keane505427c2019-03-21 13:30:56 +00004655 Diag(Attrs.getLoc(), diag::warn_cconv_ignored)
4656 << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004657
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004658 // This convention is not valid for the target. Use the default function or
4659 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004660 bool IsCXXMethod = false, IsVariadic = false;
4661 if (FD) {
4662 IsCXXMethod = FD->isCXXInstanceMember();
4663 IsVariadic = FD->isVariadic();
4664 }
4665 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004666 }
4667
Erich Keaneb11ebc52017-09-27 03:20:13 +00004668 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004669 return false;
4670}
4671
John McCall477f2bb2016-03-03 06:39:32 +00004672/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004673static bool isValidSwiftContextType(QualType Ty) {
4674 if (!Ty->hasPointerRepresentation())
4675 return Ty->isDependentType();
4676 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004677}
4678
4679/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004680static bool isValidSwiftIndirectResultType(QualType Ty) {
4681 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4682 Ty = PtrType->getPointeeType();
4683 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4684 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004685 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004686 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004687 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004688 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004689}
4690
4691/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004692static bool isValidSwiftErrorResultType(QualType Ty) {
4693 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4694 Ty = PtrType->getPointeeType();
4695 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4696 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004697 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004698 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004699 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004700 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004701 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004702 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004703}
4704
Erich Keanee891aa92018-07-13 15:07:47 +00004705static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004706 ParameterABI Abi) {
4707 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4708 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004709}
4710
4711void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4712 unsigned spellingIndex) {
4713
4714 QualType type = cast<ParmVarDecl>(D)->getType();
4715
4716 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4717 if (existingAttr->getABI() != abi) {
4718 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4719 << getParameterABISpelling(abi) << existingAttr;
4720 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4721 return;
4722 }
4723 }
4724
4725 switch (abi) {
4726 case ParameterABI::Ordinary:
4727 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4728
4729 case ParameterABI::SwiftContext:
4730 if (!isValidSwiftContextType(type)) {
4731 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4732 << getParameterABISpelling(abi)
4733 << /*pointer to pointer */ 0 << type;
4734 }
4735 D->addAttr(::new (Context)
4736 SwiftContextAttr(range, Context, spellingIndex));
4737 return;
4738
4739 case ParameterABI::SwiftErrorResult:
4740 if (!isValidSwiftErrorResultType(type)) {
4741 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4742 << getParameterABISpelling(abi)
4743 << /*pointer to pointer */ 1 << type;
4744 }
4745 D->addAttr(::new (Context)
4746 SwiftErrorResultAttr(range, Context, spellingIndex));
4747 return;
4748
4749 case ParameterABI::SwiftIndirectResult:
4750 if (!isValidSwiftIndirectResultType(type)) {
4751 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4752 << getParameterABISpelling(abi)
4753 << /*pointer*/ 0 << type;
4754 }
4755 D->addAttr(::new (Context)
4756 SwiftIndirectResultAttr(range, Context, spellingIndex));
4757 return;
4758 }
4759 llvm_unreachable("bad parameter ABI attribute");
4760}
4761
John McCall3882ace2011-01-05 12:14:39 +00004762/// Checks a regparm attribute, returning true if it is ill-formed and
4763/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004764bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004765 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004766 return true;
4767
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004768 if (!checkAttributeNumArgs(*this, AL, 1)) {
4769 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004770 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004771 }
Eli Friedman7044b762009-03-27 21:06:47 +00004772
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004773 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004774 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4775 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4776 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004777 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004778 }
4779
Douglas Gregore8bbc122011-09-02 00:18:52 +00004780 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004781 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004782 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004783 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004784 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004785 }
4786
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004787 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004788 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004789 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004790 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004791 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004792 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004793 }
4794
John McCall3882ace2011-01-05 12:14:39 +00004795 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004796}
4797
Artem Belevichbcec9da2016-06-06 22:54:57 +00004798// Checks whether an argument of launch_bounds attribute is
4799// acceptable, performs implicit conversion to Rvalue, and returns
4800// non-nullptr Expr result on success. Otherwise, it returns nullptr
4801// and may output an error.
4802static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004803 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004804 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004805 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004806 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004807
4808 // Accept template arguments for now as they depend on something else.
4809 // We'll get to check them when they eventually get instantiated.
4810 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004811 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004812
4813 llvm::APSInt I(64);
4814 if (!E->isIntegerConstantExpr(I, S.Context)) {
4815 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004816 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004817 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004818 }
4819 // Make sure we can fit it in 32 bits.
4820 if (!I.isIntN(32)) {
4821 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4822 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004823 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004824 }
4825 if (I < 0)
4826 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004827 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004828
Artem Belevichbcec9da2016-06-06 22:54:57 +00004829 // We may need to perform implicit conversion of the argument.
4830 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4831 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4832 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4833 assert(!ValArg.isInvalid() &&
4834 "Unexpected PerformCopyInitialization() failure.");
4835
4836 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004837}
4838
4839void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4840 Expr *MinBlocks, unsigned SpellingListIndex) {
4841 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4842 SpellingListIndex);
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
4853 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4854 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4855}
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
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004862 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4863 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4864 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004865}
4866
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004867static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004868 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004869 if (!AL.isArgIdent(0)) {
4870 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004871 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004872 return;
4873 }
Joel E. Denny81508102018-03-13 14:51:22 +00004874
4875 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004876 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004877 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004878 return;
4879
Joel E. Denny81508102018-03-13 14:51:22 +00004880 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004881 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004882 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004883 return;
4884
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004885 bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004886 if (IsPointer) {
4887 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004888 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4889 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4890 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004891 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004892 }
4893
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004894 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
4895 AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx,
4896 TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004897}
4898
4899static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004900 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004901 if (!AL.isArgIdent(0)) {
4902 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004903 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004904 return;
4905 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004906
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004907 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004908 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004909
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004910 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004911 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004912 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004913 return;
4914 }
4915
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004916 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004917 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004918 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004919 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004920
Michael Han99315932013-01-24 16:46:58 +00004921 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004922 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004923 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004924 AL.getLayoutCompatible(),
4925 AL.getMustBeNull(),
4926 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004927}
4928
Erich Keanee891aa92018-07-13 15:07:47 +00004929static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004930 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004931
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004932 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004933 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004934 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004935 return;
4936
Joel E. Denny81508102018-03-13 14:51:22 +00004937 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
4938 D->addAttr(::new (S.Context) XRayLogArgsAttr(
4939 AL.getRange(), S.Context, ArgCount.getSourceIndex(),
4940 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004941}
4942
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004943//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004944// Checker-specific attribute handlers.
4945//===----------------------------------------------------------------------===//
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004946static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4947 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004948}
4949
George Karpenkov1657f362018-11-30 02:18:37 +00004950static bool isValidSubjectOfNSAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004951 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004952 QT->isObjCNSObjectType();
John McCalled433932011-01-25 03:31:58 +00004953}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004954
George Karpenkov1657f362018-11-30 02:18:37 +00004955static bool isValidSubjectOfCFAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004956 return QT->isDependentType() || QT->isPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004957 isValidSubjectOfNSAttribute(QT);
John McCalled433932011-01-25 03:31:58 +00004958}
4959
George Karpenkov1657f362018-11-30 02:18:37 +00004960static bool isValidSubjectOfOSAttribute(QualType QT) {
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004961 if (QT->isDependentType())
4962 return true;
4963 QualType PT = QT->getPointeeType();
4964 return !PT.isNull() && PT->getAsCXXRecordDecl() != nullptr;
John McCall3b5a8f52016-03-03 00:10:03 +00004965}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004966
George Karpenkov1657f362018-11-30 02:18:37 +00004967void Sema::AddXConsumedAttr(Decl *D, SourceRange SR, unsigned SpellingIndex,
4968 RetainOwnershipKind K,
4969 bool IsTemplateInstantiation) {
4970 ValueDecl *VD = cast<ValueDecl>(D);
4971 switch (K) {
4972 case RetainOwnershipKind::OS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004973 handleSimpleAttributeOrDiagnose<OSConsumedAttr>(
4974 *this, VD, SR, SpellingIndex, isValidSubjectOfOSAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00004975 diag::warn_ns_attribute_wrong_parameter_type,
4976 /*ExtraArgs=*/SR, "os_consumed", /*pointers*/ 1);
4977 return;
4978 case RetainOwnershipKind::NS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004979 handleSimpleAttributeOrDiagnose<NSConsumedAttr>(
4980 *this, VD, SR, SpellingIndex, isValidSubjectOfNSAttribute(VD->getType()),
John McCall3b5a8f52016-03-03 00:10:03 +00004981
George Karpenkov1657f362018-11-30 02:18:37 +00004982 // These attributes are normally just advisory, but in ARC, ns_consumed
4983 // is significant. Allow non-dependent code to contain inappropriate
4984 // attributes even in ARC, but require template instantiations to be
4985 // set up correctly.
4986 ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount)
4987 ? diag::err_ns_attribute_wrong_parameter_type
4988 : diag::warn_ns_attribute_wrong_parameter_type),
4989 /*ExtraArgs=*/SR, "ns_consumed", /*objc pointers*/ 0);
4990 return;
4991 case RetainOwnershipKind::CF:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004992 handleSimpleAttributeOrDiagnose<CFConsumedAttr>(
George Karpenkov1657f362018-11-30 02:18:37 +00004993 *this, VD, SR, SpellingIndex,
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004994 isValidSubjectOfCFAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00004995 diag::warn_ns_attribute_wrong_parameter_type,
4996 /*ExtraArgs=*/SR, "cf_consumed", /*pointers*/1);
John McCalled433932011-01-25 03:31:58 +00004997 return;
4998 }
George Karpenkov1657f362018-11-30 02:18:37 +00004999}
John McCalled433932011-01-25 03:31:58 +00005000
George Karpenkov1657f362018-11-30 02:18:37 +00005001static Sema::RetainOwnershipKind
5002parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) {
5003 switch (AL.getKind()) {
5004 case ParsedAttr::AT_CFConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005005 case ParsedAttr::AT_CFReturnsRetained:
5006 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005007 return Sema::RetainOwnershipKind::CF;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005008 case ParsedAttr::AT_OSConsumesThis:
George Karpenkov1657f362018-11-30 02:18:37 +00005009 case ParsedAttr::AT_OSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005010 case ParsedAttr::AT_OSReturnsRetained:
5011 case ParsedAttr::AT_OSReturnsNotRetained:
5012 case ParsedAttr::AT_OSReturnsRetainedOnZero:
5013 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
George Karpenkov1657f362018-11-30 02:18:37 +00005014 return Sema::RetainOwnershipKind::OS;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005015 case ParsedAttr::AT_NSConsumesSelf:
George Karpenkov1657f362018-11-30 02:18:37 +00005016 case ParsedAttr::AT_NSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005017 case ParsedAttr::AT_NSReturnsRetained:
5018 case ParsedAttr::AT_NSReturnsNotRetained:
5019 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005020 return Sema::RetainOwnershipKind::NS;
5021 default:
5022 llvm_unreachable("Wrong argument supplied");
5023 }
John McCalled433932011-01-25 03:31:58 +00005024}
5025
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005026bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
5027 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00005028 return false;
5029
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005030 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
5031 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00005032 return true;
5033}
5034
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005035/// \return whether the parameter is a pointer to OSObject pointer.
5036static bool isValidOSObjectOutParameter(const Decl *D) {
5037 const auto *PVD = dyn_cast<ParmVarDecl>(D);
5038 if (!PVD)
5039 return false;
5040 QualType QT = PVD->getType();
5041 QualType PT = QT->getPointeeType();
5042 return !PT.isNull() && isValidSubjectOfOSAttribute(PT);
5043}
5044
George Karpenkov1657f362018-11-30 02:18:37 +00005045static void handleXReturnsXRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005046 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005047 QualType ReturnType;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005048 Sema::RetainOwnershipKind K = parsedAttrToRetainOwnershipKind(AL);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005049
George Karpenkov1657f362018-11-30 02:18:37 +00005050 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005051 ReturnType = MD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005052 } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
5053 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) {
John McCall31168b02011-06-15 23:02:42 +00005054 return; // ignore: was handled as a type attribute
George Karpenkov1657f362018-11-30 02:18:37 +00005055 } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005056 ReturnType = PD->getType();
George Karpenkov1657f362018-11-30 02:18:37 +00005057 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005058 ReturnType = FD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005059 } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
5060 // Attributes on parameters are used for out-parameters,
5061 // passed as pointers-to-pointers.
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005062 unsigned DiagID = K == Sema::RetainOwnershipKind::CF
5063 ? /*pointer-to-CF-pointer*/2
5064 : /*pointer-to-OSObject-pointer*/3;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005065 ReturnType = Param->getType()->getPointeeType();
5066 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005067 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005068 << AL << DiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005069 return;
5070 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005071 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00005072 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005073 } else {
5074 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005075 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005076 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005077 case ParsedAttr::AT_NSReturnsRetained:
5078 case ParsedAttr::AT_NSReturnsAutoreleased:
5079 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005080 ExpectedDeclKind = ExpectedFunctionOrMethod;
5081 break;
5082
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005083 case ParsedAttr::AT_OSReturnsRetained:
5084 case ParsedAttr::AT_OSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00005085 case ParsedAttr::AT_CFReturnsRetained:
5086 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005087 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
5088 break;
5089 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005090 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005091 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005092 return;
5093 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005094
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005095 bool TypeOK;
5096 bool Cf;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005097 unsigned ParmDiagID = 2; // Pointer-to-CF-pointer
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005098 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00005099 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005100 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005101 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
5102 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00005103 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005104
5105 case ParsedAttr::AT_NSReturnsAutoreleased:
5106 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005107 TypeOK = isValidSubjectOfNSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005108 Cf = false;
John McCalled433932011-01-25 03:31:58 +00005109 break;
5110
Erich Keanee891aa92018-07-13 15:07:47 +00005111 case ParsedAttr::AT_CFReturnsRetained:
5112 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005113 TypeOK = isValidSubjectOfCFAttribute(ReturnType);
5114 Cf = true;
5115 break;
5116
5117 case ParsedAttr::AT_OSReturnsRetained:
5118 case ParsedAttr::AT_OSReturnsNotRetained:
5119 TypeOK = isValidSubjectOfOSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005120 Cf = true;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005121 ParmDiagID = 3; // Pointer-to-OSObject-pointer
John McCalled433932011-01-25 03:31:58 +00005122 break;
5123 }
5124
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005125 if (!TypeOK) {
5126 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00005127 return;
5128
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005129 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005130 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005131 << AL << ParmDiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005132 } else {
5133 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
5134 enum : unsigned {
5135 Function,
5136 Method,
5137 Property
5138 } SubjectKind = Function;
5139 if (isa<ObjCMethodDecl>(D))
5140 SubjectKind = Method;
5141 else if (isa<ObjCPropertyDecl>(D))
5142 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005143 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005144 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005145 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005146 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00005147 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005148
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005149 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005150 default:
David Blaikie83d382b2011-09-23 05:06:16 +00005151 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005152 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005153 handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL);
John McCalled433932011-01-25 03:31:58 +00005154 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005155 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005156 handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005157 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005158 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005159 handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005160 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005161 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005162 handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005163 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005164 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005165 handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL);
5166 return;
5167 case ParsedAttr::AT_OSReturnsRetained:
5168 handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL);
5169 return;
5170 case ParsedAttr::AT_OSReturnsNotRetained:
5171 handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005172 return;
5173 };
5174}
5175
John McCallcf166702011-07-22 08:53:00 +00005176static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005177 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00005178 const int EP_ObjCMethod = 1;
5179 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00005180
Erich Keaneb11ebc52017-09-27 03:20:13 +00005181 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005182 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005183 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00005184 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005185 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005186 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00005187
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00005188 if (!resultType->isReferenceType() &&
5189 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005190 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005191 << SourceRange(loc) << Attrs
5192 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
5193 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00005194
5195 // Drop the attribute.
5196 return;
5197 }
5198
Nico Weber462fd1e2015-01-07 23:50:05 +00005199 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00005200 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00005201}
5202
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005203static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005204 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005205 const auto *Method = cast<ObjCMethodDecl>(D);
5206
5207 const DeclContext *DC = Method->getDeclContext();
5208 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005209 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005210 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005211 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
5212 return;
5213 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005214 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005215 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005216 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005217 return;
5218 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005219
5220 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
5221 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005222}
5223
Erich Keanee891aa92018-07-13 15:07:47 +00005224static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005225 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00005226
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005227 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005228 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005229 return;
5230 }
John McCall28592582015-02-01 22:34:06 +00005231
5232 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005233 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00005234 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005235 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00005236 return;
5237 }
5238
5239 // Only allow 'cv void *'.
5240 QualType T = TD->getUnderlyingType();
5241 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005242 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00005243 return;
5244 }
5245 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005246
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005247 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005248 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
5249 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005250}
5251
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005252static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005253 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005254 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00005255
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005256 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005257 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005258 return;
5259 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005260
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005261 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005262 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
5263 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005264}
5265
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005266static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005267 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005268 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005269 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005270 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005271 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005272 return;
5273 }
5274 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005275 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005276 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005277 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005278 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005279 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005280 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005281 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005282}
5283
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005284static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005285 const ParsedAttr &AL) {
Erik Pilkington81d3f452019-02-13 20:32:37 +00005286 DeclContext *Ctx = D->getDeclContext();
5287
5288 // This attribute can only be applied to methods in interfaces or class
5289 // extensions.
5290 if (!isa<ObjCInterfaceDecl>(Ctx) &&
5291 !(isa<ObjCCategoryDecl>(Ctx) &&
5292 cast<ObjCCategoryDecl>(Ctx)->IsClassExtension())) {
5293 S.Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
5294 return;
5295 }
5296
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005297 ObjCInterfaceDecl *IFace;
Erik Pilkington81d3f452019-02-13 20:32:37 +00005298 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(Ctx))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005299 IFace = CatDecl->getClassInterface();
5300 else
Erik Pilkington81d3f452019-02-13 20:32:37 +00005301 IFace = cast<ObjCInterfaceDecl>(Ctx);
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005302
5303 if (!IFace)
5304 return;
5305
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00005306 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00005307 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005308 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
5309 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005310}
5311
Erich Keanee891aa92018-07-13 15:07:47 +00005312static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005313 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005314 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005315 return;
5316 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005317 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005318 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005319 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005320}
5321
Nico Webera6916892016-06-10 18:53:04 +00005322// When a user wants to use objc_boxable with a union or struct
5323// but they don't have access to the declaration (legacy/third-party code)
5324// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00005325// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00005326static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00005327 bool notify = false;
5328
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005329 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00005330 if (RD && RD->getDefinition()) {
5331 RD = RD->getDefinition();
5332 notify = true;
5333 }
5334
5335 if (RD) {
5336 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005337 ObjCBoxableAttr(AL.getRange(), S.Context,
5338 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00005339 RD->addAttr(BoxableAttr);
5340 if (notify) {
5341 // we need to notify ASTReader/ASTWriter about
5342 // modification of existing declaration
5343 if (ASTMutationListener *L = S.getASTMutationListener())
5344 L->AddedAttributeToRecord(BoxableAttr, RD);
5345 }
5346 }
5347}
5348
Erich Keanee891aa92018-07-13 15:07:47 +00005349static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005350 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005351
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005352 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005353 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005354}
5355
Chandler Carruthedc2c642011-07-02 00:01:44 +00005356static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005357 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005358 const auto *VD = cast<ValueDecl>(D);
5359 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005360
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005361 if (!QT->isDependentType() &&
5362 !QT->isObjCLifetimeType()) {
5363 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5364 << QT;
John McCall31168b02011-06-15 23:02:42 +00005365 return;
5366 }
5367
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005368 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005369
5370 // If we have no lifetime yet, check the lifetime we're presumably
5371 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005372 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5373 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005374
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005375 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005376 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005377 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005378 "didn't infer lifetime for non-dependent type?");
5379 break;
5380
5381 case Qualifiers::OCL_Weak: // meaningful
5382 case Qualifiers::OCL_Strong: // meaningful
5383 break;
5384
5385 case Qualifiers::OCL_ExplicitNone:
5386 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005387 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5388 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005389 break;
5390 }
5391
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005392 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005393 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
5394 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00005395}
5396
Francois Picheta83957a2010-12-19 06:50:37 +00005397//===----------------------------------------------------------------------===//
5398// Microsoft specific attribute handlers.
5399//===----------------------------------------------------------------------===//
5400
Nico Weber88f5ed92016-09-13 18:55:26 +00005401UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
5402 unsigned AttrSpellingListIndex, StringRef Uuid) {
5403 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005404 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005405 return nullptr;
5406 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5407 Diag(Range.getBegin(), diag::note_previous_uuid);
5408 D->dropAttr<UuidAttr>();
5409 }
5410
5411 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5412}
5413
Erich Keanee891aa92018-07-13 15:07:47 +00005414static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005415 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005416 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005417 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005418 return;
5419 }
5420
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005421 StringRef StrRef;
5422 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005423 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005424 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005425
David Majnemer89085342013-08-09 08:56:20 +00005426 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5427 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005428 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5429 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005430
Reid Kleckner140c4a72013-05-17 14:04:52 +00005431 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005432 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005433 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005434 return;
5435 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005436
David Majnemer89085342013-08-09 08:56:20 +00005437 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005438 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005439 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005440 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005441 return;
5442 }
David Majnemer89085342013-08-09 08:56:20 +00005443 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005444 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005445 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005446 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005447 }
Francois Picheta83957a2010-12-19 06:50:37 +00005448
Nico Weber469891e2017-05-05 17:05:56 +00005449 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5450 // the only thing in the [] list, the [] too), and add an insertion of
5451 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5452 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005453 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005454 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005455 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5456 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005457
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005458 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5459 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005460 if (UA)
5461 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005462}
5463
Erich Keanee891aa92018-07-13 15:07:47 +00005464static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005465 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005466 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005467 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005468 return;
5469 }
5470 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005471 D, AL.getRange(), /*BestCase=*/true,
5472 AL.getAttributeSpellingListIndex(),
5473 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005474 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005475 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005476 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5477 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005478}
5479
Erich Keanee891aa92018-07-13 15:07:47 +00005480static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005481 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005482 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005483 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005484 return;
5485 }
5486 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005487 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005488 return;
5489 }
5490 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005491 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005492 return;
5493 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005494 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5495 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005496}
5497
Erich Keanee891aa92018-07-13 15:07:47 +00005498static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005499 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005500 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005501 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005502 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005503 return;
5504 Tags.push_back(Tag);
5505 }
5506
5507 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5508 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005509 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005510 return;
5511 }
5512 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005513 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005514 return;
5515 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005516 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005517 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005518 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005519 return;
5520
5521 // Store tags sorted and without duplicates.
Fangrui Song55fab262018-09-26 22:16:28 +00005522 llvm::sort(Tags);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005523 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5524
5525 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005526 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5527 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005528}
5529
Erich Keanee891aa92018-07-13 15:07:47 +00005530static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005531 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005532 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005533 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005534 return;
5535 }
5536
5537 StringRef Str;
5538 SourceLocation ArgLoc;
5539
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005540 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005541 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005542 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005543 return;
5544
5545 ARMInterruptAttr::InterruptType Kind;
5546 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005547 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5548 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005549 return;
5550 }
5551
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005552 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005553 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005554 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005555}
5556
Erich Keanee891aa92018-07-13 15:07:47 +00005557static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005558 // MSP430 'interrupt' attribute is applied to
5559 // a function with no parameters and void return type.
5560 if (!isFunctionOrMethod(D)) {
5561 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5562 << "'interrupt'" << ExpectedFunctionOrMethod;
5563 return;
5564 }
5565
5566 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005567 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5568 << /*MSP430*/ 1 << 0;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005569 return;
5570 }
5571
5572 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005573 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5574 << /*MSP430*/ 1 << 1;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005575 return;
5576 }
5577
5578 // The attribute takes one integer argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005579 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005580 return;
5581
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005582 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005583 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5584 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005585 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005586 }
5587
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005588 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005589 llvm::APSInt NumParams(32);
5590 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005591 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005592 << AL << AANT_ArgumentIntegerConstant
5593 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005594 return;
5595 }
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005596 // The argument should be in range 0..63.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005597 unsigned Num = NumParams.getLimitedValue(255);
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005598 if (Num > 63) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005599 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005600 << AL << (int)NumParams.getSExtValue()
5601 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005602 return;
5603 }
5604
Aaron Ballman36a53502014-01-16 13:03:14 +00005605 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005606 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5607 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005608 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005609}
5610
Erich Keanee891aa92018-07-13 15:07:47 +00005611static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005612 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005613 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005614 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005615 return;
5616 }
5617
5618 StringRef Str;
5619 SourceLocation ArgLoc;
5620
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005621 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005622 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005623 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005624 return;
5625
5626 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5627 // a) Must be a function.
5628 // b) Must have no parameters.
5629 // c) Must have the 'void' return type.
5630 // d) Cannot have the 'mips16' attribute, as that instruction set
5631 // lacks the 'eret' instruction.
5632 // e) The attribute itself must either have no argument or one of the
5633 // valid interrupt types, see [MipsInterruptDocs].
5634
5635 if (!isFunctionOrMethod(D)) {
5636 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5637 << "'interrupt'" << ExpectedFunctionOrMethod;
5638 return;
5639 }
5640
5641 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005642 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5643 << /*MIPS*/ 0 << 0;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005644 return;
5645 }
5646
5647 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005648 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5649 << /*MIPS*/ 0 << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005650 return;
5651 }
5652
Erich Keane44bacdf2018-08-09 13:21:32 +00005653 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005654 return;
5655
5656 MipsInterruptAttr::InterruptType Kind;
5657 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005658 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005659 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005660 return;
5661 }
5662
5663 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005664 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005665}
5666
Erich Keanee891aa92018-07-13 15:07:47 +00005667static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005668 // Semantic checks for a function with the 'interrupt' attribute.
5669 // a) Must be a function.
5670 // b) Must have the 'void' return type.
5671 // c) Must take 1 or 2 arguments.
5672 // d) The 1st argument must be a pointer.
5673 // e) The 2nd argument (if any) must be an unsigned integer.
5674 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5675 CXXMethodDecl::isStaticOverloadedOperator(
5676 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005677 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005678 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005679 return;
5680 }
5681 // Interrupt handler must have void return type.
5682 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5683 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5684 diag::err_anyx86_interrupt_attribute)
5685 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5686 ? 0
5687 : 1)
5688 << 0;
5689 return;
5690 }
5691 // Interrupt handler must have 1 or 2 parameters.
5692 unsigned NumParams = getFunctionOrMethodNumParams(D);
5693 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005694 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005695 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5696 ? 0
5697 : 1)
5698 << 1;
5699 return;
5700 }
5701 // The first argument must be a pointer.
5702 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5703 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5704 diag::err_anyx86_interrupt_attribute)
5705 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5706 ? 0
5707 : 1)
5708 << 2;
5709 return;
5710 }
5711 // The second argument, if present, must be an unsigned integer.
5712 unsigned TypeSize =
5713 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5714 ? 64
5715 : 32;
5716 if (NumParams == 2 &&
5717 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5718 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5719 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5720 diag::err_anyx86_interrupt_attribute)
5721 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5722 ? 0
5723 : 1)
5724 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5725 return;
5726 }
5727 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005728 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005729 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5730}
5731
Erich Keanee891aa92018-07-13 15:07:47 +00005732static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005733 if (!isFunctionOrMethod(D)) {
5734 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5735 << "'interrupt'" << ExpectedFunction;
5736 return;
5737 }
5738
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005739 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005740 return;
5741
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005742 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005743}
5744
Erich Keanee891aa92018-07-13 15:07:47 +00005745static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005746 if (!isFunctionOrMethod(D)) {
5747 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5748 << "'signal'" << ExpectedFunction;
5749 return;
5750 }
5751
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005752 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005753 return;
5754
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005755 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005756}
5757
Dan Gohmanb4323692019-01-24 21:08:30 +00005758static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5759 if (!isFunctionOrMethod(D)) {
5760 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5761 << "'import_module'" << ExpectedFunction;
5762 return;
5763 }
5764
5765 auto *FD = cast<FunctionDecl>(D);
5766 if (FD->isThisDeclarationADefinition()) {
5767 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5768 return;
5769 }
5770
5771 StringRef Str;
5772 SourceLocation ArgLoc;
5773 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5774 return;
5775
5776 FD->addAttr(::new (S.Context) WebAssemblyImportModuleAttr(
5777 AL.getRange(), S.Context, Str,
5778 AL.getAttributeSpellingListIndex()));
5779}
Ana Pazos1eee1b72018-07-26 17:37:45 +00005780
Dan Gohmancae84592019-02-01 22:25:23 +00005781static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5782 if (!isFunctionOrMethod(D)) {
5783 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5784 << "'import_name'" << ExpectedFunction;
5785 return;
5786 }
5787
5788 auto *FD = cast<FunctionDecl>(D);
5789 if (FD->isThisDeclarationADefinition()) {
5790 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5791 return;
5792 }
5793
5794 StringRef Str;
5795 SourceLocation ArgLoc;
5796 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5797 return;
5798
5799 FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr(
5800 AL.getRange(), S.Context, Str,
5801 AL.getAttributeSpellingListIndex()));
5802}
5803
Ana Pazos1eee1b72018-07-26 17:37:45 +00005804static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5805 const ParsedAttr &AL) {
5806 // Warn about repeated attributes.
5807 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5808 S.Diag(AL.getRange().getBegin(),
5809 diag::warn_riscv_repeated_interrupt_attribute);
5810 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5811 return;
5812 }
5813
5814 // Check the attribute argument. Argument is optional.
5815 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5816 return;
5817
5818 StringRef Str;
5819 SourceLocation ArgLoc;
5820
5821 // 'machine'is the default interrupt mode.
5822 if (AL.getNumArgs() == 0)
5823 Str = "machine";
5824 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5825 return;
5826
5827 // Semantic checks for a function with the 'interrupt' attribute:
5828 // - Must be a function.
5829 // - Must have no parameters.
5830 // - Must have the 'void' return type.
5831 // - The attribute itself must either have no argument or one of the
5832 // valid interrupt types, see [RISCVInterruptDocs].
5833
5834 if (D->getFunctionType() == nullptr) {
5835 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5836 << "'interrupt'" << ExpectedFunction;
5837 return;
5838 }
5839
5840 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005841 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5842 << /*RISC-V*/ 2 << 0;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005843 return;
5844 }
5845
5846 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005847 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5848 << /*RISC-V*/ 2 << 1;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005849 return;
5850 }
5851
5852 RISCVInterruptAttr::InterruptType Kind;
5853 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005854 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5855 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005856 return;
5857 }
5858
5859 D->addAttr(::new (S.Context) RISCVInterruptAttr(
5860 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
5861}
5862
Erich Keanee891aa92018-07-13 15:07:47 +00005863static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005864 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005865 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5866 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005867 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005868 break;
5869 case llvm::Triple::mipsel:
5870 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005871 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005872 break;
5873 case llvm::Triple::x86:
5874 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005875 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005876 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005877 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005878 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005879 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005880 case llvm::Triple::riscv32:
5881 case llvm::Triple::riscv64:
5882 handleRISCVInterruptAttr(S, D, AL);
5883 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005884 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005885 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005886 break;
5887 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005888}
5889
Michael Liao7557afa2019-02-26 18:49:36 +00005890static bool
5891checkAMDGPUFlatWorkGroupSizeArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr,
5892 const AMDGPUFlatWorkGroupSizeAttr &Attr) {
5893 // Accept template arguments for now as they depend on something else.
5894 // We'll get to check them when they eventually get instantiated.
5895 if (MinExpr->isValueDependent() || MaxExpr->isValueDependent())
5896 return false;
5897
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005898 uint32_t Min = 0;
Michael Liao7557afa2019-02-26 18:49:36 +00005899 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
5900 return true;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005901
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005902 uint32_t Max = 0;
Michael Liao7557afa2019-02-26 18:49:36 +00005903 if (!checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
5904 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005905
5906 if (Min == 0 && Max != 0) {
Michael Liao7557afa2019-02-26 18:49:36 +00005907 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5908 << &Attr << 0;
5909 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005910 }
5911 if (Min > Max) {
Michael Liao7557afa2019-02-26 18:49:36 +00005912 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5913 << &Attr << 1;
5914 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005915 }
5916
Michael Liao7557afa2019-02-26 18:49:36 +00005917 return false;
5918}
5919
5920void Sema::addAMDGPUFlatWorkGroupSizeAttr(SourceRange AttrRange, Decl *D,
5921 Expr *MinExpr, Expr *MaxExpr,
5922 unsigned SpellingListIndex) {
5923 AMDGPUFlatWorkGroupSizeAttr TmpAttr(AttrRange, Context, MinExpr, MaxExpr,
5924 SpellingListIndex);
5925
5926 if (checkAMDGPUFlatWorkGroupSizeArguments(*this, MinExpr, MaxExpr, TmpAttr))
5927 return;
5928
5929 D->addAttr(::new (Context) AMDGPUFlatWorkGroupSizeAttr(
5930 AttrRange, Context, MinExpr, MaxExpr, SpellingListIndex));
5931}
5932
5933static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
5934 const ParsedAttr &AL) {
5935 Expr *MinExpr = AL.getArgAsExpr(0);
5936 Expr *MaxExpr = AL.getArgAsExpr(1);
5937
5938 S.addAMDGPUFlatWorkGroupSizeAttr(AL.getRange(), D, MinExpr, MaxExpr,
5939 AL.getAttributeSpellingListIndex());
5940}
5941
5942static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr,
5943 Expr *MaxExpr,
5944 const AMDGPUWavesPerEUAttr &Attr) {
5945 if (S.DiagnoseUnexpandedParameterPack(MinExpr) ||
5946 (MaxExpr && S.DiagnoseUnexpandedParameterPack(MaxExpr)))
5947 return true;
5948
5949 // Accept template arguments for now as they depend on something else.
5950 // We'll get to check them when they eventually get instantiated.
5951 if (MinExpr->isValueDependent() || (MaxExpr && MaxExpr->isValueDependent()))
5952 return false;
5953
5954 uint32_t Min = 0;
5955 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
5956 return true;
5957
5958 uint32_t Max = 0;
5959 if (MaxExpr && !checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
5960 return true;
5961
5962 if (Min == 0 && Max != 0) {
5963 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5964 << &Attr << 0;
5965 return true;
5966 }
5967 if (Max != 0 && Min > Max) {
5968 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5969 << &Attr << 1;
5970 return true;
5971 }
5972
5973 return false;
5974}
5975
5976void Sema::addAMDGPUWavesPerEUAttr(SourceRange AttrRange, Decl *D,
5977 Expr *MinExpr, Expr *MaxExpr,
5978 unsigned SpellingListIndex) {
5979 AMDGPUWavesPerEUAttr TmpAttr(AttrRange, Context, MinExpr, MaxExpr,
5980 SpellingListIndex);
5981
5982 if (checkAMDGPUWavesPerEUArguments(*this, MinExpr, MaxExpr, TmpAttr))
5983 return;
5984
5985 D->addAttr(::new (Context) AMDGPUWavesPerEUAttr(AttrRange, Context, MinExpr,
5986 MaxExpr, SpellingListIndex));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005987}
5988
Erich Keanee891aa92018-07-13 15:07:47 +00005989static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Liao7557afa2019-02-26 18:49:36 +00005990 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
5991 !checkAttributeAtMostNumArgs(S, AL, 2))
5992 return;
5993
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005994 Expr *MinExpr = AL.getArgAsExpr(0);
Michael Liao7557afa2019-02-26 18:49:36 +00005995 Expr *MaxExpr = (AL.getNumArgs() > 1) ? AL.getArgAsExpr(1) : nullptr;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005996
Michael Liao7557afa2019-02-26 18:49:36 +00005997 S.addAMDGPUWavesPerEUAttr(AL.getRange(), D, MinExpr, MaxExpr,
5998 AL.getAttributeSpellingListIndex());
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005999}
6000
Erich Keanee891aa92018-07-13 15:07:47 +00006001static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006002 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006003 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
6004 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006005 return;
6006
6007 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006008 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
6009 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006010}
6011
Erich Keanee891aa92018-07-13 15:07:47 +00006012static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006013 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006014 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
6015 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006016 return;
6017
6018 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006019 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
6020 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006021}
6022
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006023static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006024 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006025 // If we try to apply it to a function pointer, don't warn, but don't
6026 // do anything, either. It doesn't matter anyway, because there's nothing
6027 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006028 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006029 if (VD && VD->getType()->isFunctionPointerType())
6030 return;
6031 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006032 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006033 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
6034 TD->getUnderlyingType()->isFunctionType()))
6035 return;
6036 // Attribute can only be applied to function types.
6037 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006038 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006039 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006040 return;
6041 }
6042
Aaron Ballman36a53502014-01-16 13:03:14 +00006043 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006044 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
6045 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006046}
6047
Erich Keanee891aa92018-07-13 15:07:47 +00006048static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00006049 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006050 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
6051 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00006052 return;
6053
6054 // TODO: Investigate what happens with the next major version of MSVC.
Reid Kleckner1a94d872018-12-17 23:16:43 +00006055 if (Version != LangOptions::MSVC2015 / 100) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006056 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00006057 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00006058 return;
6059 }
6060
Reid Kleckner1a94d872018-12-17 23:16:43 +00006061 // The attribute expects a "major" version number like 19, but new versions of
6062 // MSVC have moved to updating the "minor", or less significant numbers, so we
6063 // have to multiply by 100 now.
6064 Version *= 100;
6065
David Majnemercd3ebfe2016-05-23 17:16:12 +00006066 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006067 LayoutVersionAttr(AL.getRange(), S.Context, Version,
6068 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00006069}
6070
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006071DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
6072 unsigned AttrSpellingListIndex) {
6073 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00006074 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00006075 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006076 }
6077
6078 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00006079 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006080
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00006081 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006082}
6083
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006084DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
6085 unsigned AttrSpellingListIndex) {
6086 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00006087 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006088 D->dropAttr<DLLImportAttr>();
6089 }
6090
6091 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00006092 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006093
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00006094 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006095}
6096
Erich Keanee891aa92018-07-13 15:07:47 +00006097static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00006098 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
6099 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006100 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00006101 return;
6102 }
6103
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006104 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00006105 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00006106 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
6107 // MinGW doesn't allow dllimport on inline functions.
6108 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00006109 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00006110 return;
6111 }
6112 }
6113
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006114 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00006115 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
6116 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006117 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00006118 return;
6119 }
6120 }
6121
Hans Wennborge82f19c2014-06-24 23:57:05 +00006122 unsigned Index = A.getAttributeSpellingListIndex();
Erich Keanee891aa92018-07-13 15:07:47 +00006123 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Hans Wennborge82f19c2014-06-24 23:57:05 +00006124 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
6125 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006126 if (NewAttr)
6127 D->addAttr(NewAttr);
6128}
6129
David Majnemer2c4e00a2014-01-29 22:07:36 +00006130MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00006131Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00006132 unsigned AttrSpellingListIndex,
6133 MSInheritanceAttr::Spelling SemanticSpelling) {
6134 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
6135 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00006136 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006137 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
6138 << 1 /*previous declaration*/;
6139 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
6140 D->dropAttr<MSInheritanceAttr>();
6141 }
6142
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006143 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006144 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00006145 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
6146 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006147 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006148 }
6149 } else {
6150 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
6151 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
6152 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006153 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006154 }
6155 if (RD->getDescribedClassTemplate()) {
6156 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
6157 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006158 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006159 }
6160 }
6161
6162 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00006163 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006164}
6165
Erich Keanee891aa92018-07-13 15:07:47 +00006166static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006167 // The capability attributes take a single string parameter for the name of
6168 // the capability they represent. The lockable attribute does not take any
6169 // parameters. However, semantically, both attributes represent the same
6170 // concept, and so they use the same semantic attribute. Eventually, the
6171 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00006172 //
Alp Toker958027b2014-07-14 19:42:55 +00006173 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00006174 // literal will be considered a "mutex."
6175 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006176 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00006177 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006178 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006179 return;
6180
Aaron Ballman6c810072014-03-05 21:47:13 +00006181 // Currently, there are only two names allowed for a capability: role and
6182 // mutex (case insensitive). Diagnose other capability names.
6183 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
6184 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
6185
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006186 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
6187 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006188}
6189
Erich Keanee891aa92018-07-13 15:07:47 +00006190static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00006191 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006192 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00006193 return;
6194
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006195 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00006196 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006197 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006198}
6199
6200static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006201 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006202 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006203 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006204 return;
6205
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006206 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006207 S.Context,
6208 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006209 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006210}
6211
6212static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006213 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006214 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006215 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006216 return;
6217
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006218 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006219 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006220 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006221 Args.data(),
6222 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006223 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006224}
6225
6226static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006227 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006228 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00006229 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006230 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006231
Aaron Ballman18d85ae2014-03-20 16:02:49 +00006232 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006233 AL.getRange(), S.Context, Args.data(), Args.size(),
6234 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006235}
6236
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006237static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006238 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006239 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006240 return;
6241
6242 // check that all arguments are lockable objects
6243 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006244 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006245 if (Args.empty())
6246 return;
6247
6248 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006249 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
6250 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006251
6252 D->addAttr(RCA);
6253}
6254
Erich Keanee891aa92018-07-13 15:07:47 +00006255static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006256 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00006257 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006258 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00006259 // Do not want to attach the attribute to the namespace because that will
6260 // cause confusing diagnostic reports for uses of declarations within the
6261 // namespace.
6262 return;
6263 }
6264 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00006265
Manman Renc7890fe2016-03-16 18:50:49 +00006266 // Handle the cases where the attribute has a text message.
6267 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006268 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
6269 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00006270 return;
6271
6272 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006273 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
6274 checkAttributeAtMostNumArgs(S, AL, 1);
6275 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
Sander de Smalen44a22532018-11-26 16:38:37 +00006276 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
6277 return;
6278
6279 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
6280 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
6281
6282 D->addAttr(::new (S.Context)
6283 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006284 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006285}
6286
6287static bool isGlobalVar(const Decl *D) {
6288 if (const auto *S = dyn_cast<VarDecl>(D))
6289 return S->hasGlobalStorage();
6290 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00006291}
6292
Erich Keanee891aa92018-07-13 15:07:47 +00006293static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006294 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00006295 return;
6296
Benjamin Kramer1b582012016-02-13 18:11:49 +00006297 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00006298
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006299 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00006300 StringRef SanitizerName;
6301 SourceLocation LiteralLoc;
6302
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006303 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00006304 return;
6305
Pierre Gousseauae5303d2019-03-01 10:05:15 +00006306 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) ==
6307 SanitizerMask())
Peter Collingbourne915df992015-05-15 18:33:32 +00006308 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006309 else if (isGlobalVar(D) && SanitizerName != "address")
6310 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006311 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00006312 Sanitizers.push_back(SanitizerName);
6313 }
6314
6315 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006316 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
6317 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00006318}
6319
6320static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006321 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006322 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00006323 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006324 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
6325 .Case("no_address_safety_analysis", "address")
6326 .Case("no_sanitize_address", "address")
6327 .Case("no_sanitize_thread", "thread")
6328 .Case("no_sanitize_memory", "memory");
6329 if (isGlobalVar(D) && SanitizerName != "address")
6330 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006331 << AL << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00006332 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006333 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
6334 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00006335}
6336
Erich Keanee891aa92018-07-13 15:07:47 +00006337static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006338 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006339 D->addAttr(Internal);
6340}
6341
Erich Keanee891aa92018-07-13 15:07:47 +00006342static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006343 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006344 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00006345 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006346 else
Erich Keane44bacdf2018-08-09 13:21:32 +00006347 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
6348 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006349}
6350
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006351/// Handles semantic checking for features that are common to all attributes,
6352/// such as checking whether a parameter was properly specified, or the correct
6353/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006354static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006355 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006356 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00006357 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006358 //
6359 // We also bail on unknown and ignored attributes because those are handled
6360 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00006361 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006362 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006363 // Check whether the attribute requires specific language extensions to be
6364 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006365 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00006366 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00006367 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006368 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00006369 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006370 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00006371 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006372
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006373 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006374 // If there are no optional arguments, then checking for the argument count
6375 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006376 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006377 return true;
6378 } else {
6379 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006380 if (AL.getMinArgs() &&
6381 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006382 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006383 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
6384 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006385 return true;
6386 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00006387
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006388 if (S.CheckAttrTarget(AL))
6389 return true;
6390
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006391 return false;
6392}
6393
Erich Keanee891aa92018-07-13 15:07:47 +00006394static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006395 if (D->isInvalidDecl())
6396 return;
6397
6398 // Check if there is only one access qualifier.
6399 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006400 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
6401 AL.getSemanticSpelling()) {
6402 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
6403 << AL.getName()->getName() << AL.getRange();
6404 } else {
6405 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
6406 << D->getSourceRange();
6407 D->setInvalidDecl(true);
6408 return;
6409 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00006410 }
6411
6412 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
6413 // image object can be read and written.
6414 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
6415 // object. Using the read_write (or __read_write) qualifier with the pipe
6416 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006417 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006418 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006419 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Anastasia Stulova2c4730d2019-02-15 12:07:57 +00006420 if ((!S.getLangOpts().OpenCLCPlusPlus &&
6421 S.getLangOpts().OpenCLVersion < 200) ||
6422 DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006423 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00006424 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00006425 D->setInvalidDecl(true);
6426 return;
6427 }
6428 }
6429 }
6430
6431 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006432 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00006433}
6434
Erik Pilkington5a559e62018-08-21 17:24:06 +00006435static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006436 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00006437 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
6438 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
6439 return;
6440 }
6441
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006442 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00006443 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006444 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00006445 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00006446}
6447
JF Bastien14daa202018-12-18 05:12:21 +00006448static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6449 assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic &&
6450 "uninitialized is only valid on automatic duration variables");
6451 unsigned Index = AL.getAttributeSpellingListIndex();
6452 D->addAttr(::new (S.Context)
6453 UninitializedAttr(AL.getLoc(), S.Context, Index));
6454}
6455
Erik Pilkington1e368822019-01-04 18:33:06 +00006456static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD,
6457 bool DiagnoseFailure) {
6458 QualType Ty = VD->getType();
6459 if (!Ty->isObjCRetainableType()) {
6460 if (DiagnoseFailure) {
6461 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6462 << 0;
6463 }
6464 return false;
6465 }
6466
6467 Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime();
6468
6469 // Sema::inferObjCARCLifetime must run after processing decl attributes
6470 // (because __block lowers to an attribute), so if the lifetime hasn't been
6471 // explicitly specified, infer it locally now.
6472 if (LifetimeQual == Qualifiers::OCL_None)
6473 LifetimeQual = Ty->getObjCARCImplicitLifetime();
6474
6475 // The attributes only really makes sense for __strong variables; ignore any
6476 // attempts to annotate a parameter with any other lifetime qualifier.
6477 if (LifetimeQual != Qualifiers::OCL_Strong) {
6478 if (DiagnoseFailure) {
6479 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6480 << 1;
6481 }
6482 return false;
6483 }
6484
6485 // Tampering with the type of a VarDecl here is a bit of a hack, but we need
6486 // to ensure that the variable is 'const' so that we can error on
6487 // modification, which can otherwise over-release.
6488 VD->setType(Ty.withConst());
6489 VD->setARCPseudoStrong(true);
6490 return true;
6491}
6492
6493static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D,
6494 const ParsedAttr &AL) {
6495 if (auto *VD = dyn_cast<VarDecl>(D)) {
6496 assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically");
6497 if (!VD->hasLocalStorage()) {
6498 S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6499 << 0;
6500 return;
6501 }
6502
6503 if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true))
6504 return;
6505
6506 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6507 return;
6508 }
6509
6510 // If D is a function-like declaration (method, block, or function), then we
6511 // make every parameter psuedo-strong.
6512 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); I != E; ++I) {
6513 auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I));
6514 QualType Ty = PVD->getType();
6515
6516 // If a user wrote a parameter with __strong explicitly, then assume they
6517 // want "real" strong semantics for that parameter. This works because if
6518 // the parameter was written with __strong, then the strong qualifier will
6519 // be non-local.
6520 if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() ==
6521 Qualifiers::OCL_Strong)
6522 continue;
6523
6524 tryMakeVariablePseudoStrong(S, PVD, /*DiagnoseFailure=*/false);
6525 }
6526 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6527}
6528
Artem Dergachevc333d772019-02-21 00:01:02 +00006529static void handleMIGServerRoutineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6530 // Check that the return type is a `typedef int kern_return_t` or a typedef
6531 // around it, because otherwise MIG convention checks make no sense.
6532 // BlockDecl doesn't store a return type, so it's annoying to check,
6533 // so let's skip it for now.
6534 if (!isa<BlockDecl>(D)) {
6535 QualType T = getFunctionOrMethodResultType(D);
6536 bool IsKernReturnT = false;
6537 while (const auto *TT = T->getAs<TypedefType>()) {
6538 IsKernReturnT = (TT->getDecl()->getName() == "kern_return_t");
6539 T = TT->desugar();
6540 }
6541 if (!IsKernReturnT || T.getCanonicalType() != S.getASTContext().IntTy) {
6542 S.Diag(D->getBeginLoc(),
6543 diag::warn_mig_server_routine_does_not_return_kern_return_t);
6544 return;
6545 }
6546 }
6547
6548 handleSimpleAttribute<MIGServerRoutineAttr>(S, D, AL);
6549}
6550
Reid Kleckner1181c9f2019-03-25 23:20:18 +00006551static void handleMSAllocatorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6552 // Warn if the return type is not a pointer or reference type.
6553 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6554 QualType RetTy = FD->getReturnType();
6555 if (!RetTy->isPointerType() && !RetTy->isReferenceType()) {
6556 S.Diag(AL.getLoc(), diag::warn_declspec_allocator_nonpointer)
6557 << AL.getRange() << RetTy;
6558 return;
6559 }
6560 }
6561
6562 handleSimpleAttribute<MSAllocatorAttr>(S, D, AL);
6563}
6564
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00006565//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006566// Top Level Sema Entry Points
6567//===----------------------------------------------------------------------===//
6568
Richard Smithf8a75c32013-08-29 00:47:48 +00006569/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
6570/// the attribute applies to decls. If the attribute is a type attribute, just
6571/// silently ignore it if a GNU attribute.
6572static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006573 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00006574 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00006575 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00006576 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00006577
Richard Smithf8a75c32013-08-29 00:47:48 +00006578 // Ignore C++11 attributes on declarator chunks: they appertain to the type
6579 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006580 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00006581 return;
6582
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006583 // Unknown attributes are automatically warned on. Target-specific attributes
6584 // which do not apply to the current target architecture are treated as
6585 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006586 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006587 !AL.existsInTarget(S.Context.getTargetInfo())) {
Simon Pilgrimfc0ff612018-12-17 12:17:37 +00006588 S.Diag(AL.getLoc(),
6589 AL.isDeclspecAttribute()
6590 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
6591 : (unsigned)diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006592 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006593 return;
6594 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006595
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006596 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006597 return;
6598
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006599 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006600 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006601 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00006602 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006603 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00006604 break;
6605 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006606 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00006607 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006608 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006609 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006610 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006611 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006612 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006613 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006614 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006615 case ParsedAttr::AT_DLLExport:
6616 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006617 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006618 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006619 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006620 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006621 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006622 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006623 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006624 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006625 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006626 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006627 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006628 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006629 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006630 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006631 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006632 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006633 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006634 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006635 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006636 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006637 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006638 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006639 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006640 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006641 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006642 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006643 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006644 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006645 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006646 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006647 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006648 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006649 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006650 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006651 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006652 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006653 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006654 break;
Dan Gohmanb4323692019-01-24 21:08:30 +00006655 case ParsedAttr::AT_WebAssemblyImportModule:
6656 handleWebAssemblyImportModuleAttr(S, D, AL);
6657 break;
Dan Gohmancae84592019-02-01 22:25:23 +00006658 case ParsedAttr::AT_WebAssemblyImportName:
6659 handleWebAssemblyImportNameAttr(S, D, AL);
6660 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006661 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006662 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006663 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006664 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006665 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006666 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006667 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006668 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006669 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006670 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006671 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006672 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006673 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006674 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006675 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006676 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006677 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006678 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006679 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006680 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006681 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006682 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006683 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006684 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006685 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006686 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006687 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006688 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006689 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006690 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006691 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006692 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006693 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006694 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006695 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006696 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006697 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006698 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006699 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006700 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006701 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006702 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006703 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006704 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006705 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006706 case ParsedAttr::AT_CPUDispatch:
6707 case ParsedAttr::AT_CPUSpecific:
6708 handleCPUSpecificAttr(S, D, AL);
6709 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006710 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006711 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006712 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006713 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006714 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006715 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006716 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006717 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006718 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006719 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006720 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006721 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006722 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006723 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006724 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006725 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006726 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006727 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006728 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006729 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006730 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006731 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006732 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006733 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006734 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006735 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006736 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006737 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006738 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006739 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006740 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006741 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006742 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006743 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006744 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006745 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006746 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006747 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006748 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006749 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006750 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006751 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006752 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006753 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006754 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006755 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006756 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006757 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006758 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006759 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006760 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006761 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006762 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006763 break;
Johannes Doerfertac991bb2019-01-19 05:36:54 +00006764 case ParsedAttr::AT_Callback:
6765 handleCallbackAttr(S, D, AL);
6766 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006767 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006768 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006769 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006770 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006771 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006772 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006773 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006774 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006775 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006776 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006777 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006778 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006779 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006780 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006781 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006782 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006783 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006784 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006785 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006786 case ParsedAttr::AT_LifetimeBound:
6787 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6788 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006789 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006790 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006791 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006792 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006793 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006794 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006795 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006796 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006797 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006798 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006799 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006800 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006801 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006802 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006803 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006804 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006805 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6806 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006807 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006808 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006809 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006810 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006811 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006812 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006813 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006814 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006815 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006816 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006817 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006818 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006819 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006820 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006821 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006822 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006823 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006824 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006825 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006826 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006827 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006828 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006829 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006830 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006831 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006832 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006833 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006834 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006835 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006836 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006837 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006838 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006839 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006840 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006841 handleNoCfCheckAttr(S, D, AL);
6842 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006843 case ParsedAttr::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006844 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006845 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006846 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006847 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006848 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006849 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006850 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006851 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006852 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006853 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006854 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006855 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006856 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006857 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006858 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006859 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006860 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006861 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006862 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006863 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006864 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006865 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006866 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006867 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006868 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006869 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006870 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006871 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006872 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006873 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006874 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006875 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006876 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006877 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006878 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006879 case ParsedAttr::AT_ObjCRuntimeVisible:
6880 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6881 break;
6882 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006883 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006884 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006885 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006886 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6887 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006888 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006889 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006890 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6891 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006892 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006893 case ParsedAttr::AT_CFConsumed:
6894 case ParsedAttr::AT_NSConsumed:
George Karpenkov1657f362018-11-30 02:18:37 +00006895 case ParsedAttr::AT_OSConsumed:
6896 S.AddXConsumedAttr(D, AL.getRange(), AL.getAttributeSpellingListIndex(),
6897 parsedAttrToRetainOwnershipKind(AL),
6898 /*IsTemplateInstantiation=*/false);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006899 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006900 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006901 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006902 break;
George Karpenkovda2c77f2018-12-06 22:06:59 +00006903 case ParsedAttr::AT_OSConsumesThis:
6904 handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL);
6905 break;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00006906 case ParsedAttr::AT_OSReturnsRetainedOnZero:
6907 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnZeroAttr>(
6908 S, D, AL, isValidOSObjectOutParameter(D),
6909 diag::warn_ns_attribute_wrong_parameter_type,
6910 /*Extra Args=*/AL, /*pointer-to-OSObject-pointer*/ 3, AL.getRange());
6911 break;
6912 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
6913 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnNonZeroAttr>(
6914 S, D, AL, isValidOSObjectOutParameter(D),
6915 diag::warn_ns_attribute_wrong_parameter_type,
6916 /*Extra Args=*/AL, /*pointer-to-OSObject-poointer*/ 3, AL.getRange());
6917 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006918 case ParsedAttr::AT_NSReturnsAutoreleased:
6919 case ParsedAttr::AT_NSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006920 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006921 case ParsedAttr::AT_CFReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006922 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006923 case ParsedAttr::AT_OSReturnsNotRetained:
6924 case ParsedAttr::AT_OSReturnsRetained:
6925 handleXReturnsXRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006926 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006927 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006928 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006929 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006930 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006931 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006932 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006933 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006934 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006935 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006936 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006937 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006938 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006939 case ParsedAttr::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006940 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006941 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006942 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006943 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006944 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006945 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006946 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006947 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006948 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006949 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006950 break;
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006951 case ParsedAttr::AT_SpeculativeLoadHardening:
Zola Bridges826ef592019-01-18 17:20:46 +00006952 handleSimpleAttributeWithExclusions<SpeculativeLoadHardeningAttr,
6953 NoSpeculativeLoadHardeningAttr>(S, D,
6954 AL);
6955 break;
6956 case ParsedAttr::AT_NoSpeculativeLoadHardening:
6957 handleSimpleAttributeWithExclusions<NoSpeculativeLoadHardeningAttr,
6958 SpeculativeLoadHardeningAttr>(S, D, AL);
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006959 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006960 case ParsedAttr::AT_CodeSeg:
6961 handleCodeSegAttr(S, D, AL);
6962 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006963 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006964 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006965 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006966 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006967 handleMinVectorWidthAttr(S, D, AL);
6968 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006969 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006970 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006971 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006972 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006973 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006974 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006975 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006976 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006977 break;
Joe Danielsf7393d22019-02-04 23:32:55 +00006978 case ParsedAttr::AT_ObjCNonLazyClass:
6979 handleSimpleAttribute<ObjCNonLazyClassAttr>(S, D, AL);
6980 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006981 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006982 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006983 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006984 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006985 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006986 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006987 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006988 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006989 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006990 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006991 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006992 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006993 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006994 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006995 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006996 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006997 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6998 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006999 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007000 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00007001 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
7002 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00007003 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007004 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00007005 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007006 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007007 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007008 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00007009 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007010 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007011 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00007012 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007013 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007014 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007015 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007016 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007017 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00007018 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007019 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007020 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007021 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007022 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007023 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007024 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007025 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007026 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007027 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007028 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007029 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007030 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007031 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007032 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007033 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007034 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007035 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00007036 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007037 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007038 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007039 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007040 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007041 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00007042 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007043 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007044 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007045 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007046 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007047 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007048 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007049 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007050 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007051 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007052 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007053 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007054 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007055 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007056 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007057 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007058 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007059 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007060 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007061 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007062 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007063 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007064 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007065 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00007066 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007067 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007068 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007069 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007070 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007071 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007072 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007073 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00007074 // Interacts with -fstack-protector options.
7075 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
7076 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007077 case ParsedAttr::AT_StdCall:
7078 case ParsedAttr::AT_CDecl:
7079 case ParsedAttr::AT_FastCall:
7080 case ParsedAttr::AT_ThisCall:
7081 case ParsedAttr::AT_Pascal:
7082 case ParsedAttr::AT_RegCall:
7083 case ParsedAttr::AT_SwiftCall:
7084 case ParsedAttr::AT_VectorCall:
7085 case ParsedAttr::AT_MSABI:
7086 case ParsedAttr::AT_SysVABI:
7087 case ParsedAttr::AT_Pcs:
7088 case ParsedAttr::AT_IntelOclBicc:
7089 case ParsedAttr::AT_PreserveMost:
7090 case ParsedAttr::AT_PreserveAll:
Sander de Smalen44a22532018-11-26 16:38:37 +00007091 case ParsedAttr::AT_AArch64VectorPcs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007092 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00007093 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007094 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007095 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00007096 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007097 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007098 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007099 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007100 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007101 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007102 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007103 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007104 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00007105 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007106 case ParsedAttr::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007107 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00007108 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007109 case ParsedAttr::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007110 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00007111 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007112 case ParsedAttr::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007113 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00007114 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007115 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007116 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00007117 break;
Louis Dionned2695792018-10-04 15:49:42 +00007118 case ParsedAttr::AT_ExcludeFromExplicitInstantiation:
7119 handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL);
7120 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007121 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007122 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00007123 break;
John McCall8d32c052012-05-22 21:28:12 +00007124
7125 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007126 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007127 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007128 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007129 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007130 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007131 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007132 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007133 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00007134 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007135 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007136 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00007137 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007138 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007139 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00007140 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007141 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007142 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00007143 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007144 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007145 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007146 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007147 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007148 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007149 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007150 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007151 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00007152 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00007153
Erich Keanee891aa92018-07-13 15:07:47 +00007154 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007155 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00007156 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007157
7158 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007159 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007160 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007161 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007162 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007163 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007164 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007165 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007166 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007167 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007168 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007169 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007170 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007171 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007172 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007173 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007174 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007175 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00007176 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007177 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007178 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00007179 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007180 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007181 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00007182 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007183 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007184 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007185 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007186 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007187 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007188 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007189 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007190 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007191 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007192 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007193 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007194 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007195 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007196 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007197 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007198 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007199 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007200 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007201 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007202 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007203 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007204 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007205 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007206 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007207
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007208 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007209 case ParsedAttr::AT_Capability:
7210 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007211 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007212 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007213 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007214 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007215 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00007216
Erich Keanee891aa92018-07-13 15:07:47 +00007217 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007218 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007219 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007220 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007221 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007222 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007223 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007224 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007225 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007226 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007227 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007228 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007229
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00007230 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007231 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007232 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00007233 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007234 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007235 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007236 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007237 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007238 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007239 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007240 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007241 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007242 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007243 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007244 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00007245 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007246 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007247 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00007248 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007249 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007250 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007251 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007252 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007253 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007254 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007255
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007256 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007257 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007258 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007259 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007260 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007261 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007262 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007263 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00007264 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00007265 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007266 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007267 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00007268 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007269 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007270 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007271 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007272 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007273 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007274 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00007275 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00007276
7277 // Move semantics attribute.
7278 case ParsedAttr::AT_Reinitializes:
7279 handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
7280 break;
Erik Pilkington5a559e62018-08-21 17:24:06 +00007281
7282 case ParsedAttr::AT_AlwaysDestroy:
7283 case ParsedAttr::AT_NoDestroy:
7284 handleDestroyAttr(S, D, AL);
7285 break;
JF Bastien14daa202018-12-18 05:12:21 +00007286
7287 case ParsedAttr::AT_Uninitialized:
7288 handleUninitializedAttr(S, D, AL);
7289 break;
Erik Pilkington1e368822019-01-04 18:33:06 +00007290
7291 case ParsedAttr::AT_ObjCExternallyRetained:
7292 handleObjCExternallyRetainedAttr(S, D, AL);
7293 break;
Erik Pilkingtone3cd7352019-02-11 23:21:39 +00007294
Artem Dergachevc333d772019-02-21 00:01:02 +00007295 case ParsedAttr::AT_MIGServerRoutine:
7296 handleMIGServerRoutineAttr(S, D, AL);
7297 break;
Reid Kleckner1181c9f2019-03-25 23:20:18 +00007298
7299 case ParsedAttr::AT_MSAllocator:
7300 handleMSAllocatorAttr(S, D, AL);
7301 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007302 }
7303}
7304
7305/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
7306/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00007307void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00007308 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00007309 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00007310 if (AttrList.empty())
7311 return;
7312
Erich Keanee891aa92018-07-13 15:07:47 +00007313 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00007314 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00007315
Joey Gouly2cd9db12013-12-13 16:15:28 +00007316 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00007317 // GCC accepts
7318 // static int a9 __attribute__((weakref));
7319 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00007320 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00007321 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
7322 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00007323 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00007324 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007325 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007326
Aaron Ballmanbe243a72014-12-04 22:45:31 +00007327 // FIXME: We should be able to handle this in TableGen as well. It would be
7328 // good to have a way to specify "these attributes must appear as a group",
7329 // for these. Additionally, it would be good to have a way to specify "these
7330 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00007331 if (!D->hasAttr<OpenCLKernelAttr>()) {
7332 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007333 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00007334 // FIXME: This emits a different error message than
7335 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00007336 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007337 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007338 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007339 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007340 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007341 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007342 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007343 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007344 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00007345 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
7346 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00007347 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
7348 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
7349 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7350 << A << ExpectedKernelFunction;
7351 D->setInvalidDecl();
7352 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
7353 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7354 << A << ExpectedKernelFunction;
7355 D->setInvalidDecl();
7356 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
7357 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7358 << A << ExpectedKernelFunction;
7359 D->setInvalidDecl();
7360 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
7361 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7362 << A << ExpectedKernelFunction;
7363 D->setInvalidDecl();
7364 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007365 }
7366 }
Erik Pilkington81d3f452019-02-13 20:32:37 +00007367
7368 // Do this check after processing D's attributes because the attribute
7369 // objc_method_family can change whether the given method is in the init
7370 // family, and it can be applied after objc_designated_initializer. This is a
7371 // bit of a hack, but we need it to be compatible with versions of clang that
7372 // processed the attribute list in the wrong order.
7373 if (D->hasAttr<ObjCDesignatedInitializerAttr>() &&
7374 cast<ObjCMethodDecl>(D)->getMethodFamily() != OMF_init) {
7375 Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
7376 D->dropAttr<ObjCDesignatedInitializerAttr>();
7377 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007378}
7379
Hiroshi Inoue939d9322017-06-30 05:40:31 +00007380// Helper for delayed processing TransparentUnion attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007381void Sema::ProcessDeclAttributeDelayed(Decl *D,
7382 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007383 for (const ParsedAttr &AL : AttrList)
7384 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00007385 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00007386 break;
7387 }
7388}
7389
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007390// Annotation attributes are the only attributes allowed after an access
7391// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00007392bool Sema::ProcessAccessDeclAttributeList(
7393 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007394 for (const ParsedAttr &AL : AttrList) {
7395 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00007396 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007397 } else {
Erich Keanec480f302018-07-12 21:09:05 +00007398 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007399 return true;
7400 }
7401 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007402 return false;
7403}
7404
John McCall42856de2011-10-01 05:17:03 +00007405/// checkUnusedDeclAttributes - Check a list of attributes to see if it
7406/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00007407static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00007408 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00007409 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007410 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
7411 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00007412 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00007413 continue;
John McCall42856de2011-10-01 05:17:03 +00007414
Erich Keanee891aa92018-07-13 15:07:47 +00007415 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00007416 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00007417 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007418 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00007419 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
7420 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007421 }
7422 }
7423}
7424
7425/// checkUnusedDeclAttributes - Given a declarator which is not being
7426/// used to build a declaration, complain about any decl attributes
7427/// which might be lying around on it.
7428void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00007429 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00007430 ::checkUnusedDeclAttributes(*this, D.getAttributes());
7431 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
7432 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
7433}
7434
Ryan Flynn7d470f32009-07-30 03:15:39 +00007435/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00007436/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00007437NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
7438 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00007439 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00007440 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007441 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00007442 FunctionDecl *NewFD;
7443 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00007444 // FIXME: Mangling?
7445 // FIXME: Is the qualifier info correct?
7446 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00007447 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
7448 Loc, Loc, DeclarationName(II),
7449 FD->getType(), FD->getTypeSourceInfo(),
7450 SC_None, false/*isInlineSpecified*/,
7451 FD->hasPrototype(),
7452 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007453 NewD = NewFD;
7454
7455 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00007456 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00007457
7458 // Fake up parameter variables; they are declared as if this were
7459 // a typedef.
7460 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007461 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00007462 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00007463 for (const auto &AI : FT->param_types()) {
7464 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007465 Param->setScopeInfo(0, Params.size());
7466 Params.push_back(Param);
7467 }
David Blaikie9c70e042011-09-21 18:16:56 +00007468 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00007469 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007470 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00007471 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00007472 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00007473 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00007474 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007475 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00007476 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00007477 }
7478 return NewD;
7479}
7480
James Dennett634962f2012-06-14 21:40:34 +00007481/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00007482/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00007483void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00007484 if (W.getUsed()) return; // only do this once
7485 W.setUsed(true);
7486 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
7487 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00007488 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00007489 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
7490 W.getLocation()));
7491 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00007492 WeakTopLevelDecl.push_back(NewD);
7493 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
7494 // to insert Decl at TU scope, sorry.
7495 DeclContext *SavedContext = CurContext;
7496 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00007497 NewD->setDeclContext(CurContext);
7498 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00007499 PushOnScopeChains(NewD, S);
7500 CurContext = SavedContext;
7501 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00007502 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00007503 }
7504}
7505
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007506void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
7507 // It's valid to "forward-declare" #pragma weak, in which case we
7508 // have to do this.
7509 LoadExternalWeakUndeclaredIdentifiers();
7510 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00007511 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007512 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007513 if (VD->isExternC())
7514 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007515 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007516 if (FD->isExternC())
7517 ND = FD;
7518 if (ND) {
7519 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00007520 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007521 if (I != WeakUndeclaredIdentifiers.end()) {
7522 WeakInfo W = I->second;
7523 DeclApplyPragmaWeak(S, ND, W);
7524 WeakUndeclaredIdentifiers[Id] = W;
7525 }
7526 }
7527 }
7528 }
7529}
7530
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007531/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
7532/// it, apply them to D. This is a bit tricky because PD can have attributes
7533/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00007534void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007535 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00007536 if (!PD.getDeclSpec().getAttributes().empty())
7537 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00007538
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007539 // Walk the declarator structure, applying decl attributes that were in a type
7540 // position to the decl itself. This handles cases like:
7541 // int *__attr__(x)** D;
7542 // when X is a decl attribute.
7543 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00007544 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
7545 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00007546
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007547 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00007548 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00007549
7550 // Apply additional attributes specified by '#pragma clang attribute'.
7551 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007552}
John McCall28a6aea2009-11-04 02:18:39 +00007553
John McCall31168b02011-06-15 23:02:42 +00007554/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00007555/// If so, it'll still be annotated with an attribute that makes it
7556/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007557static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00007558 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00007559 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00007560 // Private ivars are always okay. Unfortunately, people don't
7561 // always properly make their ivars private, even in system headers.
7562 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007563 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
7564 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00007565 return false;
7566
John McCallc6af8c62015-10-28 05:03:19 +00007567 // Silently accept unsupported uses of __weak in both user and system
7568 // declarations when it's been disabled, for ease of integration with
7569 // -fno-objc-arc files. We do have to take some care against attempts
7570 // to define such things; for now, we've only done that for ivars
7571 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007572 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00007573 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
7574 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
7575 reason = UnavailableAttr::IR_ForbiddenWeak;
7576 return true;
7577 }
John McCallb61e14e2015-10-27 04:54:50 +00007578 }
7579
John McCallc6af8c62015-10-28 05:03:19 +00007580 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007581 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00007582 // Currently, all the failures dealt with this way are due to ARC
7583 // restrictions.
7584 reason = UnavailableAttr::IR_ARCForbiddenType;
7585 return true;
John McCallb61e14e2015-10-27 04:54:50 +00007586 }
7587
7588 return false;
John McCall31168b02011-06-15 23:02:42 +00007589}
7590
7591/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007592static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
7593 Decl *D) {
7594 auto Reason = UnavailableAttr::IR_None;
7595 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
7596 assert(Reason && "didn't set reason?");
7597 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00007598 return;
7599 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00007600 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007601 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00007602 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007603 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007604 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007605 DD.getForbiddenTypeDiagnostic() ==
7606 diag::err_arc_array_param_no_ownership) {
7607 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007608 return;
7609 }
7610 }
John McCall31168b02011-06-15 23:02:42 +00007611
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007612 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
7613 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
7614 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00007615}
7616
Manman Ren45b1ab12016-05-06 19:57:16 +00007617static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
7618 const Decl *D) {
7619 // Check each AvailabilityAttr to find the one for this platform.
7620 for (const auto *A : D->attrs()) {
7621 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
7622 // FIXME: this is copied from CheckAvailability. We should try to
7623 // de-duplicate.
7624
7625 // Check if this is an App Extension "platform", and if so chop off
7626 // the suffix for matching with the actual platform.
7627 StringRef ActualPlatform = Avail->getPlatform()->getName();
7628 StringRef RealizedPlatform = ActualPlatform;
7629 if (Context.getLangOpts().AppExt) {
7630 size_t suffix = RealizedPlatform.rfind("_app_extension");
7631 if (suffix != StringRef::npos)
7632 RealizedPlatform = RealizedPlatform.slice(0, suffix);
7633 }
7634
7635 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
7636
7637 // Match the platform name.
7638 if (RealizedPlatform == TargetPlatform)
7639 return Avail;
7640 }
7641 }
7642 return nullptr;
7643}
7644
Erik Pilkington9f866a72017-07-18 20:32:07 +00007645/// The diagnostic we should emit for \c D, and the declaration that
7646/// originated it, or \c AR_Available.
7647///
7648/// \param D The declaration to check.
7649/// \param Message If non-null, this will be populated with the message from
7650/// the availability attribute that is selected.
Erik Pilkington42578572018-09-10 22:20:09 +00007651/// \param ClassReceiver If we're checking the the method of a class message
7652/// send, the class. Otherwise nullptr.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007653static std::pair<AvailabilityResult, const NamedDecl *>
Erik Pilkington42578572018-09-10 22:20:09 +00007654ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D,
7655 std::string *Message,
7656 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007657 AvailabilityResult Result = D->getAvailability(Message);
7658
7659 // For typedefs, if the typedef declaration appears available look
7660 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007661 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007662 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007663 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007664 D = TT->getDecl();
7665 Result = D->getAvailability(Message);
7666 continue;
7667 }
7668 }
7669 break;
7670 }
7671
7672 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007673 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007674 if (IDecl->getDefinition()) {
7675 D = IDecl->getDefinition();
7676 Result = D->getAvailability(Message);
7677 }
7678 }
7679
7680 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7681 if (Result == AR_Available) {
7682 const DeclContext *DC = ECD->getDeclContext();
7683 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
7684 Result = TheEnumDecl->getAvailability(Message);
7685 D = TheEnumDecl;
7686 }
7687 }
7688
Erik Pilkington42578572018-09-10 22:20:09 +00007689 // For +new, infer availability from -init.
7690 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7691 if (S.NSAPIObj && ClassReceiver) {
7692 ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod(
7693 S.NSAPIObj->getInitSelector());
7694 if (Init && Result == AR_Available && MD->isClassMethod() &&
7695 MD->getSelector() == S.NSAPIObj->getNewSelector() &&
7696 MD->definedInNSObject(S.getASTContext())) {
7697 Result = Init->getAvailability(Message);
7698 D = Init;
7699 }
7700 }
7701 }
7702
Erik Pilkington9f866a72017-07-18 20:32:07 +00007703 return {Result, D};
7704}
7705
7706
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007707/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007708/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
7709/// in a deprecated context, but not the other way around.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007710static bool
7711ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
7712 VersionTuple DeclVersion, Decl *Ctx,
7713 const NamedDecl *OffendingDecl) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007714 assert(K != AR_Available && "Expected an unavailable declaration here!");
7715
7716 // Checks if we should emit the availability diagnostic in the context of C.
7717 auto CheckContext = [&](const Decl *C) {
7718 if (K == AR_NotYetIntroduced) {
7719 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
7720 if (AA->getIntroduced() >= DeclVersion)
7721 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007722 } else if (K == AR_Deprecated) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007723 if (C->isDeprecated())
7724 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007725 } else if (K == AR_Unavailable) {
7726 // It is perfectly fine to refer to an 'unavailable' Objective-C method
Alex Lorenz194d00e2019-01-17 18:12:45 +00007727 // when it is referenced from within the @implementation itself. In this
7728 // context, we interpret unavailable as a form of access control.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007729 if (const auto *MD = dyn_cast<ObjCMethodDecl>(OffendingDecl)) {
7730 if (const auto *Impl = dyn_cast<ObjCImplDecl>(C)) {
Alex Lorenz194d00e2019-01-17 18:12:45 +00007731 if (MD->getClassInterface() == Impl->getClassInterface())
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007732 return true;
7733 }
7734 }
7735 }
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007736
7737 if (C->isUnavailable())
7738 return true;
7739 return false;
7740 };
7741
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007742 do {
7743 if (CheckContext(Ctx))
7744 return false;
7745
7746 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00007747 // Unless it is "+load" method.
7748 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
7749 if (MethodD->isClassMethod() &&
7750 MethodD->getSelector().getAsString() == "load")
7751 return true;
7752
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007753 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007754 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
7755 if (CheckContext(Interface))
7756 return false;
7757 }
7758 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007759 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007760 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
7761 if (CheckContext(Interface))
7762 return false;
7763 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
7764
7765 return true;
7766}
7767
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007768static bool
7769shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
7770 const VersionTuple &DeploymentVersion,
7771 const VersionTuple &DeclVersion) {
7772 const auto &Triple = Context.getTargetInfo().getTriple();
7773 VersionTuple ForceAvailabilityFromVersion;
7774 switch (Triple.getOS()) {
7775 case llvm::Triple::IOS:
7776 case llvm::Triple::TvOS:
7777 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7778 break;
7779 case llvm::Triple::WatchOS:
7780 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7781 break;
7782 case llvm::Triple::Darwin:
7783 case llvm::Triple::MacOSX:
7784 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7785 break;
7786 default:
7787 // New targets should always warn about availability.
7788 return Triple.getVendor() == llvm::Triple::Apple;
7789 }
7790 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7791 DeclVersion >= ForceAvailabilityFromVersion;
7792}
7793
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007794static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7795 for (Decl *Ctx = OrigCtx; Ctx;
7796 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7797 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7798 return cast<NamedDecl>(Ctx);
7799 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7800 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7801 return Imp->getClassInterface();
7802 return CD;
7803 }
7804 }
7805
7806 return dyn_cast<NamedDecl>(OrigCtx);
7807}
7808
Alex Lorenz727c21e2017-07-26 13:58:02 +00007809namespace {
7810
7811struct AttributeInsertion {
7812 StringRef Prefix;
7813 SourceLocation Loc;
7814 StringRef Suffix;
7815
7816 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007817 return {" ", D->getEndLoc(), ""};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007818 }
7819 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7820 return {" ", Loc, ""};
7821 }
7822 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007823 return {"", D->getBeginLoc(), "\n"};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007824 }
7825};
7826
7827} // end anonymous namespace
7828
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007829/// Tries to parse a string as ObjC method name.
7830///
7831/// \param Name The string to parse. Expected to originate from availability
7832/// attribute argument.
7833/// \param SlotNames The vector that will be populated with slot names. In case
7834/// of unsuccessful parsing can contain invalid data.
7835/// \returns A number of method parameters if parsing was successful, None
7836/// otherwise.
7837static Optional<unsigned>
7838tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
7839 const LangOptions &LangOpts) {
7840 // Accept replacements starting with - or + as valid ObjC method names.
7841 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
7842 Name = Name.drop_front(1);
7843 if (Name.empty())
7844 return None;
7845 Name.split(SlotNames, ':');
7846 unsigned NumParams;
7847 if (Name.back() == ':') {
7848 // Remove an empty string at the end that doesn't represent any slot.
7849 SlotNames.pop_back();
7850 NumParams = SlotNames.size();
7851 } else {
7852 if (SlotNames.size() != 1)
7853 // Not a valid method name, just a colon-separated string.
7854 return None;
7855 NumParams = 0;
7856 }
7857 // Verify all slot names are valid.
7858 bool AllowDollar = LangOpts.DollarIdents;
7859 for (StringRef S : SlotNames) {
7860 if (S.empty())
7861 continue;
7862 if (!isValidIdentifier(S, AllowDollar))
7863 return None;
7864 }
7865 return NumParams;
7866}
7867
Alex Lorenz727c21e2017-07-26 13:58:02 +00007868/// Returns a source location in which it's appropriate to insert a new
7869/// attribute for the given declaration \D.
7870static Optional<AttributeInsertion>
7871createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7872 const LangOptions &LangOpts) {
7873 if (isa<ObjCPropertyDecl>(D))
7874 return AttributeInsertion::createInsertionAfter(D);
7875 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7876 if (MD->hasBody())
7877 return None;
7878 return AttributeInsertion::createInsertionAfter(D);
7879 }
7880 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7881 SourceLocation Loc =
7882 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7883 if (Loc.isInvalid())
7884 return None;
7885 // Insert after the 'struct'/whatever keyword.
7886 return AttributeInsertion::createInsertionAfter(Loc);
7887 }
7888 return AttributeInsertion::createInsertionBefore(D);
7889}
7890
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007891/// Actually emit an availability diagnostic for a reference to an unavailable
7892/// decl.
7893///
7894/// \param Ctx The context that the reference occurred in
7895/// \param ReferringDecl The exact declaration that was referenced.
7896/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007897/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007898/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7899/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7900/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007901static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007902 Decl *Ctx, const NamedDecl *ReferringDecl,
7903 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007904 StringRef Message,
7905 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007906 const ObjCInterfaceDecl *UnknownObjCClass,
7907 const ObjCPropertyDecl *ObjCProperty,
7908 bool ObjCPropertyAccess) {
7909 // Diagnostics for deprecated or unavailable.
7910 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007911 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007912 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007913
7914 // Matches 'diag::note_property_attribute' options.
7915 unsigned property_note_select;
7916
7917 // Matches diag::note_availability_specified_here.
7918 unsigned available_here_select_kind;
7919
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007920 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007921 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007922 DeclVersion = AA->getIntroduced();
7923
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007924 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx,
7925 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007926 return;
7927
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007928 SourceLocation Loc = Locs.front();
7929
Erik Pilkington8b352c42017-08-14 19:49:12 +00007930 // The declaration can have multiple availability attributes, we are looking
7931 // at one of them.
7932 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7933 if (A && A->isInherited()) {
7934 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7935 Redecl = Redecl->getPreviousDecl()) {
7936 const AvailabilityAttr *AForRedecl =
7937 getAttrForPlatform(S.Context, Redecl);
7938 if (AForRedecl && !AForRedecl->isInherited()) {
7939 // If D is a declaration with inherited attributes, the note should
7940 // point to the declaration with actual attributes.
7941 NoteLocation = Redecl->getLocation();
7942 break;
7943 }
7944 }
7945 }
7946
Aaron Ballmanfb237522014-10-15 15:37:51 +00007947 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007948 case AR_NotYetIntroduced: {
7949 // We would like to emit the diagnostic even if -Wunguarded-availability is
7950 // not specified for deployment targets >= to iOS 11 or equivalent or
7951 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7952 // later.
7953 const AvailabilityAttr *AA =
7954 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7955 VersionTuple Introduced = AA->getIntroduced();
7956
7957 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7958 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7959 Introduced);
7960 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7961 : diag::warn_unguarded_availability;
7962
Erik Pilkington0535b0f2019-01-14 19:17:31 +00007963 std::string PlatformName = AvailabilityAttr::getPrettyPlatformName(
7964 S.getASTContext().getTargetInfo().getPlatformName());
Erik Pilkington8b352c42017-08-14 19:49:12 +00007965
Erik Pilkington0535b0f2019-01-14 19:17:31 +00007966 S.Diag(Loc, Warning) << OffendingDecl << PlatformName
7967 << Introduced.getAsString();
7968
7969 S.Diag(OffendingDecl->getLocation(),
7970 diag::note_partial_availability_specified_here)
7971 << OffendingDecl << PlatformName << Introduced.getAsString()
7972 << S.Context.getTargetInfo().getPlatformMinVersion().getAsString();
Erik Pilkington8b352c42017-08-14 19:49:12 +00007973
7974 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007975 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007976 if (TD->getDeclName().isEmpty()) {
7977 S.Diag(TD->getLocation(),
7978 diag::note_decl_unguarded_availability_silence)
7979 << /*Anonymous*/ 1 << TD->getKindName();
7980 return;
7981 }
7982 auto FixitNoteDiag =
7983 S.Diag(Enclosing->getLocation(),
7984 diag::note_decl_unguarded_availability_silence)
7985 << /*Named*/ 0 << Enclosing;
7986 // Don't offer a fixit for declarations with availability attributes.
7987 if (Enclosing->hasAttr<AvailabilityAttr>())
7988 return;
7989 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7990 return;
7991 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7992 Enclosing, S.getSourceManager(), S.getLangOpts());
7993 if (!Insertion)
7994 return;
7995 std::string PlatformName =
7996 AvailabilityAttr::getPlatformNameSourceSpelling(
7997 S.getASTContext().getTargetInfo().getPlatformName())
7998 .lower();
7999 std::string Introduced =
8000 OffendingDecl->getVersionIntroduced().getAsString();
8001 FixitNoteDiag << FixItHint::CreateInsertion(
8002 Insertion->Loc,
8003 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
8004 "(" + Introduced + "))" + Insertion->Suffix)
8005 .str());
8006 }
8007 return;
8008 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00008009 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00008010 diag = !ObjCPropertyAccess ? diag::warn_deprecated
8011 : diag::warn_property_method_deprecated;
8012 diag_message = diag::warn_deprecated_message;
8013 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
8014 property_note_select = /* deprecated */ 0;
8015 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008016 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
8017 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00008018 break;
8019
Erik Pilkington796a3e22016-08-05 22:59:03 +00008020 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00008021 diag = !ObjCPropertyAccess ? diag::err_unavailable
8022 : diag::err_property_method_unavailable;
8023 diag_message = diag::err_unavailable_message;
8024 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
8025 property_note_select = /* unavailable */ 1;
8026 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00008027
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008028 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
8029 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00008030 // Most of these failures are due to extra restrictions in ARC;
8031 // reflect that in the primary diagnostic when applicable.
8032 auto flagARCError = [&] {
8033 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008034 S.getSourceManager().isInSystemHeader(
8035 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00008036 diag = diag::err_unavailable_in_arc;
8037 };
8038
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008039 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00008040 case UnavailableAttr::IR_None: break;
8041
8042 case UnavailableAttr::IR_ARCForbiddenType:
8043 flagARCError();
8044 diag_available_here = diag::note_arc_forbidden_type;
8045 break;
8046
8047 case UnavailableAttr::IR_ForbiddenWeak:
8048 if (S.getLangOpts().ObjCWeakRuntime)
8049 diag_available_here = diag::note_arc_weak_disabled;
8050 else
8051 diag_available_here = diag::note_arc_weak_no_runtime;
8052 break;
8053
8054 case UnavailableAttr::IR_ARCForbiddenConversion:
8055 flagARCError();
8056 diag_available_here = diag::note_performs_forbidden_arc_conversion;
8057 break;
8058
8059 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
8060 flagARCError();
8061 diag_available_here = diag::note_arc_init_returns_unrelated;
8062 break;
8063
8064 case UnavailableAttr::IR_ARCFieldWithOwnership:
8065 flagARCError();
8066 diag_available_here = diag::note_arc_field_with_ownership;
8067 break;
8068 }
8069 }
John McCallb61e14e2015-10-27 04:54:50 +00008070 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00008071 break;
8072
Erik Pilkington796a3e22016-08-05 22:59:03 +00008073 case AR_Available:
8074 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00008075 }
8076
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008077 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00008078 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008079 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008080 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
8081 Replacement = AL->getReplacement();
8082 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
8083 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00008084
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008085 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00008086 if (!Replacement.empty())
8087 UseRange =
8088 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008089 if (UseRange.isValid()) {
8090 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
8091 Selector Sel = MethodDecl->getSelector();
8092 SmallVector<StringRef, 12> SelectorSlotNames;
8093 Optional<unsigned> NumParams = tryParseObjCMethodName(
8094 Replacement, SelectorSlotNames, S.getLangOpts());
8095 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
8096 assert(SelectorSlotNames.size() == Locs.size());
8097 for (unsigned I = 0; I < Locs.size(); ++I) {
8098 if (!Sel.getNameForSlot(I).empty()) {
8099 CharSourceRange NameRange = CharSourceRange::getCharRange(
8100 Locs[I], S.getLocForEndOfToken(Locs[I]));
8101 FixIts.push_back(FixItHint::CreateReplacement(
8102 NameRange, SelectorSlotNames[I]));
8103 } else
8104 FixIts.push_back(
8105 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
8106 }
8107 } else
8108 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
8109 } else
8110 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
8111 }
Manman Renc7890fe2016-03-16 18:50:49 +00008112 }
8113
Aaron Ballmanfb237522014-10-15 15:37:51 +00008114 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008115 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008116 if (ObjCProperty)
8117 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
8118 << ObjCProperty->getDeclName() << property_note_select;
8119 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008120 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008121 if (ObjCProperty)
8122 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
8123 << ObjCProperty->getDeclName() << property_note_select;
8124 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008125 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008126 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
8127 }
8128
Erik Pilkington8b352c42017-08-14 19:49:12 +00008129 S.Diag(NoteLocation, diag_available_here)
8130 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008131}
8132
8133static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
8134 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00008135 assert(DD.Kind == DelayedDiagnostic::Availability &&
8136 "Expected an availability diagnostic here");
8137
Aaron Ballmanfb237522014-10-15 15:37:51 +00008138 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00008139 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008140 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008141 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
8142 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
8143 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00008144}
8145
John McCall2ec85372012-05-07 06:16:41 +00008146void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
8147 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00008148 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00008149 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00008150
John McCall2ec85372012-05-07 06:16:41 +00008151 // When delaying diagnostics to run in the context of a parsed
8152 // declaration, we only want to actually emit anything if parsing
8153 // succeeds.
8154 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00008155
John McCall2ec85372012-05-07 06:16:41 +00008156 // We emit all the active diagnostics in this pool or any of its
8157 // parents. In general, we'll get one pool for the decl spec
8158 // and a child pool for each declarator; in a decl group like:
8159 // deprecated_typedef foo, *bar, baz();
8160 // only the declarator pops will be passed decls. This is correct;
8161 // we really do need to consider delayed diagnostics from the decl spec
8162 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00008163 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00008164 do {
Richard Smith5c9b3b72018-09-25 22:12:44 +00008165 bool AnyAccessFailures = false;
John McCall6347b682012-05-07 06:16:58 +00008166 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00008167 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
8168 // This const_cast is a bit lame. Really, Triggered should be mutable.
8169 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00008170 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00008171 continue;
8172
John McCallc1465822011-02-14 07:13:47 +00008173 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00008174 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00008175 // Don't bother giving deprecation/unavailable diagnostics if
8176 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00008177 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00008178 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00008179 break;
8180
8181 case DelayedDiagnostic::Access:
Richard Smith5c9b3b72018-09-25 22:12:44 +00008182 // Only produce one access control diagnostic for a structured binding
8183 // declaration: we don't need to tell the user that all the fields are
8184 // inaccessible one at a time.
8185 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
8186 continue;
John McCall2ec85372012-05-07 06:16:41 +00008187 HandleDelayedAccessCheck(diag, decl);
Richard Smith5c9b3b72018-09-25 22:12:44 +00008188 if (diag.Triggered)
8189 AnyAccessFailures = true;
John McCall86121512010-01-27 03:50:35 +00008190 break;
John McCall31168b02011-06-15 23:02:42 +00008191
8192 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00008193 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00008194 break;
John McCall86121512010-01-27 03:50:35 +00008195 }
8196 }
John McCall2ec85372012-05-07 06:16:41 +00008197 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00008198}
8199
John McCall6347b682012-05-07 06:16:58 +00008200/// Given a set of delayed diagnostics, re-emit them as if they had
8201/// been delayed in the current context instead of in the given pool.
8202/// Essentially, this just moves them to the current pool.
8203void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
8204 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
8205 assert(curPool && "re-emitting in undelayed context not supported");
8206 curPool->steal(pool);
8207}
8208
Erik Pilkington9f866a72017-07-18 20:32:07 +00008209static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
8210 const NamedDecl *ReferringDecl,
8211 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008212 StringRef Message,
8213 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008214 const ObjCInterfaceDecl *UnknownObjCClass,
8215 const ObjCPropertyDecl *ObjCProperty,
8216 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00008217 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00008218 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
8219 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008220 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008221 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008222 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00008223 return;
8224 }
8225
Erik Pilkington9f866a72017-07-18 20:32:07 +00008226 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
8227 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008228 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008229 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00008230}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00008231
Erik Pilkington5cd57172016-08-16 17:44:11 +00008232namespace {
8233
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008234/// Returns true if the given statement can be a body-like child of \p Parent.
8235bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
8236 switch (Parent->getStmtClass()) {
8237 case Stmt::IfStmtClass:
8238 return cast<IfStmt>(Parent)->getThen() == S ||
8239 cast<IfStmt>(Parent)->getElse() == S;
8240 case Stmt::WhileStmtClass:
8241 return cast<WhileStmt>(Parent)->getBody() == S;
8242 case Stmt::DoStmtClass:
8243 return cast<DoStmt>(Parent)->getBody() == S;
8244 case Stmt::ForStmtClass:
8245 return cast<ForStmt>(Parent)->getBody() == S;
8246 case Stmt::CXXForRangeStmtClass:
8247 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
8248 case Stmt::ObjCForCollectionStmtClass:
8249 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
8250 case Stmt::CaseStmtClass:
8251 case Stmt::DefaultStmtClass:
8252 return cast<SwitchCase>(Parent)->getSubStmt() == S;
8253 default:
8254 return false;
8255 }
8256}
8257
8258class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
8259 const Stmt *Target;
8260
8261public:
8262 bool VisitStmt(Stmt *S) { return S != Target; }
8263
8264 /// Returns true if the given statement is present in the given declaration.
8265 static bool isContained(const Stmt *Target, const Decl *D) {
8266 StmtUSEFinder Visitor;
8267 Visitor.Target = Target;
8268 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
8269 }
8270};
8271
8272/// Traverses the AST and finds the last statement that used a given
8273/// declaration.
8274class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
8275 const Decl *D;
8276
8277public:
8278 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
8279 if (DRE->getDecl() == D)
8280 return false;
8281 return true;
8282 }
8283
8284 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
8285 const CompoundStmt *Scope) {
8286 LastDeclUSEFinder Visitor;
8287 Visitor.D = D;
8288 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
8289 const Stmt *S = *I;
8290 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
8291 return S;
8292 }
8293 return nullptr;
8294 }
8295};
8296
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008297/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00008298///
8299/// This is done with a traversal of the AST of a function that makes reference
8300/// to a partially available declaration. Whenever we encounter an \c if of the
8301/// form: \c if(@available(...)), we use the version from the condition to visit
8302/// the then statement.
8303class DiagnoseUnguardedAvailability
8304 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
8305 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
8306
8307 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008308 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00008309
8310 /// Stack of potentially nested 'if (@available(...))'s.
8311 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008312 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00008313
Erik Pilkington42578572018-09-10 22:20:09 +00008314 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range,
8315 ObjCInterfaceDecl *ClassReceiver = nullptr);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008316
8317public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008318 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
8319 : SemaRef(SemaRef), Ctx(Ctx) {
8320 AvailabilityStack.push_back(
8321 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008322 }
8323
Alex Lorenz6f911122017-05-16 13:58:53 +00008324 bool TraverseDecl(Decl *D) {
8325 // Avoid visiting nested functions to prevent duplicate warnings.
8326 if (!D || isa<FunctionDecl>(D))
8327 return true;
8328 return Base::TraverseDecl(D);
8329 }
8330
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008331 bool TraverseStmt(Stmt *S) {
8332 if (!S)
8333 return true;
8334 StmtStack.push_back(S);
8335 bool Result = Base::TraverseStmt(S);
8336 StmtStack.pop_back();
8337 return Result;
8338 }
8339
Erik Pilkington5cd57172016-08-16 17:44:11 +00008340 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
8341
8342 bool TraverseIfStmt(IfStmt *If);
8343
Alex Lorenz6f911122017-05-16 13:58:53 +00008344 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
8345
Erik Pilkingtonba87c622017-08-18 20:20:56 +00008346 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
8347 // to any useful diagnostics.
8348 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
8349
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008350 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
8351 if (PRE->isClassReceiver())
8352 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
8353 return true;
8354 }
8355
Erik Pilkington5cd57172016-08-16 17:44:11 +00008356 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
Erik Pilkington42578572018-09-10 22:20:09 +00008357 if (ObjCMethodDecl *D = Msg->getMethodDecl()) {
8358 ObjCInterfaceDecl *ID = nullptr;
8359 QualType ReceiverTy = Msg->getClassReceiver();
8360 if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType())
8361 ID = ReceiverTy->getAsObjCInterfaceType()->getInterface();
8362
Erik Pilkington5cd57172016-08-16 17:44:11 +00008363 DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00008364 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID);
8365 }
Erik Pilkington5cd57172016-08-16 17:44:11 +00008366 return true;
8367 }
8368
8369 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
8370 DiagnoseDeclAvailability(DRE->getDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008371 SourceRange(DRE->getBeginLoc(), DRE->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00008372 return true;
8373 }
8374
8375 bool VisitMemberExpr(MemberExpr *ME) {
8376 DiagnoseDeclAvailability(ME->getMemberDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008377 SourceRange(ME->getBeginLoc(), ME->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00008378 return true;
8379 }
8380
Alex Lorenz0a484ba2017-05-24 15:15:29 +00008381 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008382 SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use)
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008383 << (!SemaRef.getLangOpts().ObjC);
Alex Lorenz0a484ba2017-05-24 15:15:29 +00008384 return true;
8385 }
8386
Erik Pilkington5cd57172016-08-16 17:44:11 +00008387 bool VisitTypeLoc(TypeLoc Ty);
8388};
8389
8390void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00008391 NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008392 AvailabilityResult Result;
8393 const NamedDecl *OffendingDecl;
8394 std::tie(Result, OffendingDecl) =
Erik Pilkington42578572018-09-10 22:20:09 +00008395 ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008396 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008397 // All other diagnostic kinds have already been handled in
8398 // DiagnoseAvailabilityOfDecl.
8399 if (Result != AR_NotYetIntroduced)
8400 return;
8401
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008402 const AvailabilityAttr *AA =
8403 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008404 VersionTuple Introduced = AA->getIntroduced();
8405
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008406 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008407 return;
8408
8409 // If the context of this function is less available than D, we should not
8410 // emit a diagnostic.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00008411 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx,
8412 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008413 return;
8414
Alex Lorenzc9a369f2017-06-22 17:02:24 +00008415 // We would like to emit the diagnostic even if -Wunguarded-availability is
8416 // not specified for deployment targets >= to iOS 11 or equivalent or
8417 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
8418 // later.
8419 unsigned DiagKind =
8420 shouldDiagnoseAvailabilityByDefault(
8421 SemaRef.Context,
8422 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
8423 ? diag::warn_unguarded_availability_new
8424 : diag::warn_unguarded_availability;
8425
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008426 std::string PlatformName = AvailabilityAttr::getPrettyPlatformName(
8427 SemaRef.getASTContext().getTargetInfo().getPlatformName());
8428
Alex Lorenzc9a369f2017-06-22 17:02:24 +00008429 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008430 << Range << D << PlatformName << Introduced.getAsString();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008431
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008432 SemaRef.Diag(OffendingDecl->getLocation(),
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008433 diag::note_partial_availability_specified_here)
8434 << OffendingDecl << PlatformName << Introduced.getAsString()
8435 << SemaRef.Context.getTargetInfo()
8436 .getPlatformMinVersion()
8437 .getAsString();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008438
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008439 auto FixitDiag =
8440 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
8441 << Range << D
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008442 << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0
8443 : /*__builtin_available*/ 1);
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008444
8445 // Find the statement which should be enclosed in the if @available check.
8446 if (StmtStack.empty())
8447 return;
8448 const Stmt *StmtOfUse = StmtStack.back();
8449 const CompoundStmt *Scope = nullptr;
8450 for (const Stmt *S : llvm::reverse(StmtStack)) {
8451 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
8452 Scope = CS;
8453 break;
8454 }
8455 if (isBodyLikeChildStmt(StmtOfUse, S)) {
8456 // The declaration won't be seen outside of the statement, so we don't
8457 // have to wrap the uses of any declared variables in if (@available).
8458 // Therefore we can avoid setting Scope here.
8459 break;
8460 }
8461 StmtOfUse = S;
8462 }
8463 const Stmt *LastStmtOfUse = nullptr;
8464 if (isa<DeclStmt>(StmtOfUse) && Scope) {
8465 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
8466 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
8467 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
8468 break;
8469 }
8470 }
8471 }
8472
8473 const SourceManager &SM = SemaRef.getSourceManager();
8474 SourceLocation IfInsertionLoc =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008475 SM.getExpansionLoc(StmtOfUse->getBeginLoc());
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008476 SourceLocation StmtEndLoc =
8477 SM.getExpansionRange(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008478 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc())
Richard Smithb5f81712018-04-30 05:25:48 +00008479 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008480 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
8481 return;
8482
8483 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
8484 const char *ExtraIndentation = " ";
8485 std::string FixItString;
8486 llvm::raw_string_ostream FixItOS(FixItString);
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008487 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available"
8488 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00008489 << "("
8490 << AvailabilityAttr::getPlatformNameSourceSpelling(
8491 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008492 << " " << Introduced.getAsString() << ", *)) {\n"
8493 << Indentation << ExtraIndentation;
8494 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
8495 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
8496 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
8497 /*SkipTrailingWhitespaceAndNewLine=*/false);
8498 if (ElseInsertionLoc.isInvalid())
8499 ElseInsertionLoc =
8500 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
8501 FixItOS.str().clear();
8502 FixItOS << "\n"
8503 << Indentation << "} else {\n"
8504 << Indentation << ExtraIndentation
8505 << "// Fallback on earlier versions\n"
8506 << Indentation << "}";
8507 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008508 }
8509}
8510
8511bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
8512 const Type *TyPtr = Ty.getTypePtr();
8513 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
8514
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008515 if (Range.isInvalid())
8516 return true;
8517
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008518 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008519 TagDecl *TD = TT->getDecl();
8520 DiagnoseDeclAvailability(TD, Range);
8521
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008522 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008523 TypedefNameDecl *D = TD->getDecl();
8524 DiagnoseDeclAvailability(D, Range);
8525
8526 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
8527 if (NamedDecl *D = ObjCO->getInterface())
8528 DiagnoseDeclAvailability(D, Range);
8529 }
8530
8531 return true;
8532}
8533
8534bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
8535 VersionTuple CondVersion;
8536 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
8537 CondVersion = E->getVersion();
8538
8539 // If we're using the '*' case here or if this check is redundant, then we
8540 // use the enclosing version to check both branches.
8541 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00008542 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008543 } else {
8544 // This isn't an availability checking 'if', we can just continue.
8545 return Base::TraverseIfStmt(If);
8546 }
8547
8548 AvailabilityStack.push_back(CondVersion);
8549 bool ShouldContinue = TraverseStmt(If->getThen());
8550 AvailabilityStack.pop_back();
8551
8552 return ShouldContinue && TraverseStmt(If->getElse());
8553}
8554
8555} // end anonymous namespace
8556
8557void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
8558 Stmt *Body = nullptr;
8559
8560 if (auto *FD = D->getAsFunction()) {
8561 // FIXME: We only examine the pattern decl for availability violations now,
8562 // but we should also examine instantiated templates.
8563 if (FD->isTemplateInstantiation())
8564 return;
8565
8566 Body = FD->getBody();
8567 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
8568 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00008569 else if (auto *BD = dyn_cast<BlockDecl>(D))
8570 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008571
8572 assert(Body && "Need a body here!");
8573
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008574 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008575}
Erik Pilkington9f866a72017-07-18 20:32:07 +00008576
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008577void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
8578 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008579 const ObjCInterfaceDecl *UnknownObjCClass,
8580 bool ObjCPropertyAccess,
Erik Pilkington42578572018-09-10 22:20:09 +00008581 bool AvoidPartialAvailabilityChecks,
8582 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008583 std::string Message;
8584 AvailabilityResult Result;
8585 const NamedDecl* OffendingDecl;
8586 // See if this declaration is unavailable, deprecated, or partial.
Erik Pilkington42578572018-09-10 22:20:09 +00008587 std::tie(Result, OffendingDecl) =
8588 ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver);
Erik Pilkington9f866a72017-07-18 20:32:07 +00008589 if (Result == AR_Available)
8590 return;
8591
8592 if (Result == AR_NotYetIntroduced) {
8593 if (AvoidPartialAvailabilityChecks)
8594 return;
8595
8596 // We need to know the @available context in the current function to
8597 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
8598 // when we're done parsing the current function.
8599 if (getCurFunctionOrMethodDecl()) {
8600 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
8601 return;
8602 } else if (getCurBlock() || getCurLambda()) {
8603 getCurFunction()->HasPotentialAvailabilityViolations = true;
8604 return;
8605 }
8606 }
8607
8608 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008609 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008610 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
8611 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
8612 if (PDeclResult == Result)
8613 ObjCPDecl = PD;
8614 }
8615 }
8616
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008617 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008618 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
8619}