blob: cebb1c642b91a7655803c692f0c8f2107e186a80 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner2c6fcf52008-06-26 18:38:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements decl-related attribute processing.
10//
11//===----------------------------------------------------------------------===//
12
David Majnemer929025d2016-01-26 19:30:26 +000013#include "clang/AST/ASTConsumer.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000014#include "clang/AST/ASTContext.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000015#include "clang/AST/ASTMutationListener.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000021#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Erik Pilkington5cd57172016-08-16 17:44:11 +000023#include "clang/AST/RecursiveASTVisitor.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000024#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
Simon Tatham7c11da02019-09-02 15:35:09 +010026#include "clang/Basic/TargetBuiltins.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000028#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000029#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000030#include "clang/Sema/DelayedDiagnostic.h"
Artem Belevichbcec9da2016-06-06 22:54:57 +000031#include "clang/Sema/Initialization.h"
John McCallf1e8b342011-09-29 07:17:38 +000032#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000033#include "clang/Sema/Scope.h"
Reid Kleckner04f9bca2018-03-07 22:48:35 +000034#include "clang/Sema/ScopeInfo.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000035#include "clang/Sema/SemaInternal.h"
George Burgess IV177399e2017-01-09 04:12:14 +000036#include "llvm/ADT/STLExtras.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000037#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000038#include "llvm/Support/MathExtras.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000039
Chris Lattner2c6fcf52008-06-26 18:38:35 +000040using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000041using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000042
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000043namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000044 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000045 C,
46 Cpp,
47 ObjC
48 };
Eugene Zelenko1ced5092016-02-12 22:53:10 +000049} // end namespace AttributeLangSupport
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000050
Chris Lattner58418ff2008-06-29 00:16:31 +000051//===----------------------------------------------------------------------===//
52// Helper functions
53//===----------------------------------------------------------------------===//
54
Ted Kremenek527042b2009-08-14 20:49:40 +000055/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000056/// type (function or function-typed variable) or an Objective-C
57/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000058static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000059 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000060}
Eugene Zelenko1ced5092016-02-12 22:53:10 +000061
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000062/// Return true if the given decl has function type (function or
David Majnemer06864812015-04-07 06:01:53 +000063/// function-typed variable) or an Objective-C method or a block.
64static bool isFunctionOrMethodOrBlock(const Decl *D) {
65 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
66}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000067
John McCall3882ace2011-01-05 12:14:39 +000068/// Return true if the given decl has a declarator that should have
69/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000070static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000071 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000072 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
73 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000074}
75
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000076/// hasFunctionProto - Return true if the given decl has a argument
77/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000078/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000080 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000081 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000082 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000083}
84
Alp Toker601b22c2014-01-21 23:35:24 +000085/// getFunctionOrMethodNumParams - Return number of function or method
86/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000087/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000088static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000089 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000090 return cast<FunctionProtoType>(FnTy)->getNumParams();
Aaron Ballmana70c6b52018-02-15 16:20:20 +000091 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000092 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000094}
95
Erik Pilkington1e368822019-01-04 18:33:06 +000096static const ParmVarDecl *getFunctionOrMethodParam(const Decl *D,
97 unsigned Idx) {
98 if (const auto *FD = dyn_cast<FunctionDecl>(D))
99 return FD->getParamDecl(Idx);
100 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
101 return MD->getParamDecl(Idx);
102 if (const auto *BD = dyn_cast<BlockDecl>(D))
103 return BD->getParamDecl(Idx);
104 return nullptr;
105}
106
Alp Toker601b22c2014-01-21 23:35:24 +0000107static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000108 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +0000109 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000110 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000111 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000112
Alp Toker03376dc2014-07-07 09:02:20 +0000113 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000114}
115
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000116static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
Erik Pilkington1e368822019-01-04 18:33:06 +0000117 if (auto *PVD = getFunctionOrMethodParam(D, Idx))
118 return PVD->getSourceRange();
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000119 return SourceRange();
120}
121
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000123 if (const FunctionType *FnTy = D->getFunctionType())
George Burgess IV00f70bd2018-03-01 05:43:23 +0000124 return FnTy->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000125 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000126}
127
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000128static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
129 if (const auto *FD = dyn_cast<FunctionDecl>(D))
130 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000131 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000132 return MD->getReturnTypeSourceRange();
133 return SourceRange();
134}
135
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000137 if (const FunctionType *FnTy = D->getFunctionType())
138 return cast<FunctionProtoType>(FnTy)->isVariadic();
139 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballmane13d0092014-08-01 17:02:34 +0000140 return BD->isVariadic();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000141 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000142}
143
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144static bool isInstanceMethod(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000145 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000146 return MethodDecl->isInstance();
147 return false;
148}
149
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000150static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000151 const auto *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000152 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000153 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000154
John McCall96fa4842010-05-17 21:00:27 +0000155 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
156 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000157 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000158
John McCall96fa4842010-05-17 21:00:27 +0000159 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000160
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000161 // FIXME: Should we walk the chain of classes?
162 return ClsName == &Ctx.Idents.get("NSString") ||
163 ClsName == &Ctx.Idents.get("NSMutableString");
164}
165
Daniel Dunbar980c6692008-09-26 03:32:58 +0000166static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000167 const auto *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000168 if (!PT)
169 return false;
170
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000171 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000172 if (!RT)
173 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000174
Daniel Dunbar980c6692008-09-26 03:32:58 +0000175 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000176 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000177 return false;
178
179 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
180}
181
Erich Keanee891aa92018-07-13 15:07:47 +0000182static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
Richard Smithb87c4652013-10-31 21:23:20 +0000183 // FIXME: Include the type in the argument list.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000184 return AL.getNumArgs() + AL.hasParsedType();
Richard Smithb87c4652013-10-31 21:23:20 +0000185}
186
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000187template <typename Compare>
Erich Keanee891aa92018-07-13 15:07:47 +0000188static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000189 unsigned Num, unsigned Diag,
190 Compare Comp) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000191 if (Comp(getNumAttributeArgs(AL), Num)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000192 S.Diag(AL.getLoc(), Diag) << AL << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000193 return false;
194 }
195
196 return true;
197}
198
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000199/// Check if the attribute has exactly as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000200/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000201static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000202 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000203 diag::err_attribute_wrong_number_arguments,
204 std::not_equal_to<unsigned>());
205}
206
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000207/// Check if the attribute has at least as many args as Num. May
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000208/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000209static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL,
Richard Smithb87c4652013-10-31 21:23:20 +0000210 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000211 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000212 diag::err_attribute_too_few_arguments,
213 std::less<unsigned>());
214}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000215
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000216/// Check if the attribute has at most as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000217/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000218static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL,
219 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000220 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000221 diag::err_attribute_too_many_arguments,
222 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000223}
224
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000225/// A helper function to provide Attribute Location for the Attr types
Erich Keanee891aa92018-07-13 15:07:47 +0000226/// AND the ParsedAttr.
Erich Keane623efd82017-03-30 21:48:55 +0000227template <typename AttrInfo>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000228static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value,
Erich Keane623efd82017-03-30 21:48:55 +0000229 SourceLocation>::type
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000230getAttrLoc(const AttrInfo &AL) {
231 return AL.getLocation();
Erich Keane623efd82017-03-30 21:48:55 +0000232}
Erich Keanee891aa92018-07-13 15:07:47 +0000233static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); }
Erich Keane623efd82017-03-30 21:48:55 +0000234
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000235/// If Expr is a valid integer constant, get the value of the integer
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000236/// expression and return success or failure. May output an error.
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000237///
238/// Negative argument is implicitly converted to unsigned, unless
239/// \p StrictlyUnsigned is true.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000240template <typename AttrInfo>
241static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000242 uint32_t &Val, unsigned Idx = UINT_MAX,
243 bool StrictlyUnsigned = false) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000244 llvm::APSInt I(32);
245 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
246 !Expr->isIntegerConstantExpr(I, S.Context)) {
247 if (Idx != UINT_MAX)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000248 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
Michael Liao7557afa2019-02-26 18:49:36 +0000249 << &AI << Idx << AANT_ArgumentIntegerConstant
Erich Keane44bacdf2018-08-09 13:21:32 +0000250 << Expr->getSourceRange();
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000251 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000252 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
Michael Liao7557afa2019-02-26 18:49:36 +0000253 << &AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange();
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000254 return false;
255 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000256
257 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000258 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
259 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000260 return false;
261 }
262
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000263 if (StrictlyUnsigned && I.isSigned() && I.isNegative()) {
Andrew Savonichev1a5623482018-09-17 10:39:46 +0000264 S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
Michael Liao7557afa2019-02-26 18:49:36 +0000265 << &AI << /*non-negative*/ 1;
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000266 return false;
267 }
268
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000269 Val = (uint32_t)I.getZExtValue();
270 return true;
271}
272
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000273/// Wrapper around checkUInt32Argument, with an extra check to be sure
George Burgess IVe3763372016-12-22 02:50:20 +0000274/// that the result will fit into a regular (signed) int. All args have the same
275/// purpose as they do in checkUInt32Argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000276template <typename AttrInfo>
277static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000278 int &Val, unsigned Idx = UINT_MAX) {
George Burgess IVe3763372016-12-22 02:50:20 +0000279 uint32_t UVal;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000280 if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
George Burgess IVe3763372016-12-22 02:50:20 +0000281 return false;
282
George Burgess IVa8049572016-12-22 19:00:31 +0000283 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
George Burgess IVe3763372016-12-22 02:50:20 +0000284 llvm::APSInt I(32); // for toString
285 I = UVal;
286 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
287 << I.toString(10, false) << 32 << /* Unsigned */ 0;
288 return false;
289 }
290
291 Val = UVal;
292 return true;
293}
294
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000295/// Diagnose mutually exclusive attributes when present on a given
Aaron Ballmanfb763042013-12-02 18:05:46 +0000296/// declaration. Returns true if diagnosed.
297template <typename AttrTy>
Erich Keane44bacdf2018-08-09 13:21:32 +0000298static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000299 if (const auto *A = D->getAttr<AttrTy>()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000300 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
301 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
302 return true;
303 }
304 return false;
305}
306
307template <typename AttrTy>
308static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
309 if (const auto *A = D->getAttr<AttrTy>()) {
310 S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000311 << A;
312 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000313 return true;
314 }
315 return false;
316}
317
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000318/// Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000319/// instance method D. May output an error.
320///
321/// \returns true if IdxExpr is a valid index.
Erich Keane623efd82017-03-30 21:48:55 +0000322template <typename AttrInfo>
323static bool checkFunctionOrMethodParameterIndex(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000324 Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
Joel E. Denny81508102018-03-13 14:51:22 +0000325 const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
David Majnemer06864812015-04-07 06:01:53 +0000326 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000327
328 // In C++ the implicit 'this' function parameter also counts.
329 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000330 bool HP = hasFunctionProto(D);
331 bool HasImplicitThisParam = isInstanceMethod(D);
332 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000333 unsigned NumParams =
334 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000335
336 llvm::APSInt IdxInt;
337 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
338 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000339 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000340 << &AI << AttrArgNum << AANT_ArgumentIntegerConstant
341 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000342 return false;
343 }
344
Joel E. Denny81508102018-03-13 14:51:22 +0000345 unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX);
346 if (IdxSource < 1 || (!IV && IdxSource > NumParams)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000347 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +0000348 << &AI << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000349 return false;
350 }
Joel E. Denny81508102018-03-13 14:51:22 +0000351 if (HasImplicitThisParam && !CanIndexImplicitThis) {
352 if (IdxSource == 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000353 S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
354 << &AI << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000355 return false;
356 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000357 }
358
Joel E. Denny81508102018-03-13 14:51:22 +0000359 Idx = ParamIdx(IdxSource, D);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000360 return true;
361}
362
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000363/// Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000364/// If not emit an error and return false. If the argument is an identifier it
365/// will emit an error with a fixit hint and treat it as if it was a string
366/// literal.
Erich Keanee891aa92018-07-13 15:07:47 +0000367bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
368 StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000369 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000370 // Look for identifiers. If we have one emit a hint to fix it to a literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000371 if (AL.isArgIdent(ArgNum)) {
372 IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000373 Diag(Loc->Loc, diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000374 << AL << AANT_ArgumentString
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000375 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000376 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000377 Str = Loc->Ident->getName();
378 if (ArgLocation)
379 *ArgLocation = Loc->Loc;
380 return true;
381 }
382
383 // Now check for an actual string literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000384 Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
385 const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000386 if (ArgLocation)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000387 *ArgLocation = ArgExpr->getBeginLoc();
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000388
389 if (!Literal || !Literal->isAscii()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000390 Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000391 << AL << AANT_ArgumentString;
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000392 return false;
393 }
394
395 Str = Literal->getString();
396 return true;
397}
398
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000399/// Applies the given attribute to the Decl without performing any
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000400/// additional semantic checking.
401template <typename AttrType>
Erich Keane6a24e802019-09-13 17:39:31 +0000402static void handleSimpleAttribute(Sema &S, Decl *D,
403 const AttributeCommonInfo &CI) {
404 D->addAttr(::new (S.Context) AttrType(S.Context, CI));
George Karpenkov1657f362018-11-30 02:18:37 +0000405}
406
George Karpenkov1657f362018-11-30 02:18:37 +0000407template <typename... DiagnosticArgs>
408static const Sema::SemaDiagnosticBuilder&
409appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr) {
410 return Bldr;
411}
412
413template <typename T, typename... DiagnosticArgs>
414static const Sema::SemaDiagnosticBuilder&
415appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr, T &&ExtraArg,
416 DiagnosticArgs &&... ExtraArgs) {
417 return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
418 std::forward<DiagnosticArgs>(ExtraArgs)...);
419}
420
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000421/// Add an attribute {@code AttrType} to declaration {@code D}, provided that
422/// {@code PassesCheck} is true.
423/// Otherwise, emit diagnostic {@code DiagID}, passing in all parameters
424/// specified in {@code ExtraArgs}.
George Karpenkov1657f362018-11-30 02:18:37 +0000425template <typename AttrType, typename... DiagnosticArgs>
Erich Keane6a24e802019-09-13 17:39:31 +0000426static void handleSimpleAttributeOrDiagnose(Sema &S, Decl *D,
427 const AttributeCommonInfo &CI,
428 bool PassesCheck, unsigned DiagID,
429 DiagnosticArgs &&... ExtraArgs) {
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000430 if (!PassesCheck) {
George Karpenkov1657f362018-11-30 02:18:37 +0000431 Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
432 appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
433 return;
434 }
Erich Keane6a24e802019-09-13 17:39:31 +0000435 handleSimpleAttribute<AttrType>(S, D, CI);
George Karpenkov3a50a9f2019-01-11 18:02:08 +0000436}
437
Justin Lebar3eaaf862016-01-13 01:07:35 +0000438template <typename AttrType>
439static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000440 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000441 handleSimpleAttribute<AttrType>(S, D, AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000442}
443
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000444/// Applies the given attribute to the Decl so long as the Decl doesn't
Justin Lebar3eaaf862016-01-13 01:07:35 +0000445/// already have one of the given incompatible attributes.
446template <typename AttrType, typename IncompatibleAttrType,
447 typename... IncompatibleAttrTypes>
448static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000449 const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000450 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
Justin Lebar3eaaf862016-01-13 01:07:35 +0000451 return;
452 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000453 AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000454}
455
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000456/// Check if the passed-in expression is of type int or bool.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000457static bool isIntOrBool(Expr *Exp) {
458 QualType QT = Exp->getType();
459 return QT->isBooleanType() || QT->isIntegerType();
460}
461
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000462
463// Check to see if the type is a smart pointer of some kind. We assume
464// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000465static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Trieu8d3fa392018-09-22 01:50:52 +0000466 auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record,
467 OverloadedOperatorKind Op) {
468 DeclContextLookupResult Result =
469 Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op));
470 return !Result.empty();
471 };
472
473 const RecordDecl *Record = RT->getDecl();
474 bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
475 bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
476 if (foundStarOperator && foundArrowOperator)
477 return true;
478
479 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
480 if (!CXXRecord)
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000481 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000482
Richard Trieu8d3fa392018-09-22 01:50:52 +0000483 for (auto BaseSpecifier : CXXRecord->bases()) {
484 if (!foundStarOperator)
485 foundStarOperator = IsOverloadedOperatorPresent(
486 BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
487 if (!foundArrowOperator)
488 foundArrowOperator = IsOverloadedOperatorPresent(
489 BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
490 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000491
Richard Trieu8d3fa392018-09-22 01:50:52 +0000492 if (foundStarOperator && foundArrowOperator)
493 return true;
494
495 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000496}
497
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000498/// Check if passed in Decl is a pointer type.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000499/// Note that this function may produce an error message.
500/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000501static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000502 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000503 const auto *VD = cast<ValueDecl>(D);
504 QualType QT = VD->getType();
Aaron Ballman553e6812013-12-26 14:54:11 +0000505 if (QT->isAnyPointerType())
506 return true;
507
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000508 if (const auto *RT = QT->getAs<RecordType>()) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000509 // If it's an incomplete type, it could be a smart pointer; skip it.
510 // (We don't want to force template instantiation if we can avoid it,
511 // since that would alter the order in which templates are instantiated.)
512 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000513 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000514
Aaron Ballman553e6812013-12-26 14:54:11 +0000515 if (threadSafetyCheckIsSmartPointer(S, RT))
516 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000517 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000518
Erich Keane44bacdf2018-08-09 13:21:32 +0000519 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000520 return false;
521}
522
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000523/// Checks that the passed in QualType either is of RecordType or points
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000524/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000525static const RecordType *getRecordType(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000526 if (const auto *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000528
529 // Now check if we point to record type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000530 if (const auto *PT = QT->getAs<PointerType>())
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000531 return PT->getPointeeType()->getAs<RecordType>();
532
Craig Topperc3ec1492014-05-26 06:22:03 +0000533 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000534}
535
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000536template <typename AttrType>
537static bool checkRecordDeclForAttr(const RecordDecl *RD) {
538 // Check if the record itself has the attribute.
539 if (RD->hasAttr<AttrType>())
540 return true;
541
542 // Else check if any base classes have the attribute.
543 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
544 CXXBasePaths BPaths(false, false);
545 if (CRD->lookupInBases(
546 [](const CXXBaseSpecifier *BS, CXXBasePath &) {
547 const auto &Ty = *BS->getType();
548 // If it's type-dependent, we assume it could have the attribute.
549 if (Ty.isDependentType())
550 return true;
Simon Pilgrim1cd399c2019-10-03 11:22:48 +0000551 return Ty.castAs<RecordType>()->getDecl()->hasAttr<AttrType>();
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000552 },
553 BPaths, true))
554 return true;
555 }
556 return false;
557}
558
Josh Gao55afa752017-08-11 07:54:35 +0000559static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000560 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000561
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000562 if (!RT)
563 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000564
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000565 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000566 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000567 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000568
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000569 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000570 // FIXME -- Check the type that the smart pointer points to.
571 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000572 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000573
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000574 return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl());
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000575}
576
Josh Gao55afa752017-08-11 07:54:35 +0000577static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000578 const auto *TD = Ty->getAs<TypedefType>();
579 if (!TD)
580 return false;
581
582 TypedefNameDecl *TN = TD->getDecl();
583 if (!TN)
584 return false;
585
Josh Gao55afa752017-08-11 07:54:35 +0000586 return TN->hasAttr<CapabilityAttr>();
Aaron Ballman76050722014-04-04 15:13:57 +0000587}
588
Josh Gaob40c1772017-08-08 19:44:35 +0000589static bool typeHasCapability(Sema &S, QualType Ty) {
Josh Gao55afa752017-08-11 07:54:35 +0000590 if (checkTypedefTypeForCapability(Ty))
591 return true;
Josh Gaob40c1772017-08-08 19:44:35 +0000592
Josh Gao55afa752017-08-11 07:54:35 +0000593 if (checkRecordTypeForCapability(S, Ty))
594 return true;
595
596 return false;
Josh Gaob40c1772017-08-08 19:44:35 +0000597}
598
Aaron Ballman76050722014-04-04 15:13:57 +0000599static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
600 // Capability expressions are simple expressions involving the boolean logic
601 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
602 // a DeclRefExpr is found, its type should be checked to determine whether it
603 // is a capability or not.
604
Yi Kong2d58d192017-12-14 22:24:45 +0000605 if (const auto *E = dyn_cast<CastExpr>(Ex))
Aaron Ballman76050722014-04-04 15:13:57 +0000606 return isCapabilityExpr(S, E->getSubExpr());
607 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
608 return isCapabilityExpr(S, E->getSubExpr());
609 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
Yi Kong2d58d192017-12-14 22:24:45 +0000610 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
611 E->getOpcode() == UO_Deref)
Aaron Ballman76050722014-04-04 15:13:57 +0000612 return isCapabilityExpr(S, E->getSubExpr());
613 return false;
614 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
615 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
616 return isCapabilityExpr(S, E->getLHS()) &&
617 isCapabilityExpr(S, E->getRHS());
618 return false;
619 }
620
Yi Kong2d58d192017-12-14 22:24:45 +0000621 return typeHasCapability(S, Ex->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000622}
623
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000624/// Checks that all attribute arguments, starting from Sidx, resolve to
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000625/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000626/// \param Sidx The attribute argument index to start checking with.
627/// \param ParamIdxOk Whether an argument can be indexing into a function
628/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000629static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000630 const ParsedAttr &AL,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000631 SmallVectorImpl<Expr *> &Args,
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000632 unsigned Sidx = 0,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000633 bool ParamIdxOk = false) {
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000634 if (Sidx == AL.getNumArgs()) {
635 // If we don't have any capability arguments, the attribute implicitly
636 // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
637 // a non-static method, and that the class is a (scoped) capability.
638 const auto *MD = dyn_cast<const CXXMethodDecl>(D);
639 if (MD && !MD->isStatic()) {
640 const CXXRecordDecl *RD = MD->getParent();
641 // FIXME -- need to check this again on template instantiation
642 if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
643 !checkRecordDeclForAttr<ScopedLockableAttr>(RD))
644 S.Diag(AL.getLoc(),
645 diag::warn_thread_attribute_not_on_capability_member)
646 << AL << MD->getParent();
647 } else {
648 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
649 << AL;
650 }
651 }
652
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000653 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
654 Expr *ArgExp = AL.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000655
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000656 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000657 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000658 Args.push_back(ArgExp);
659 continue;
660 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000661
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000662 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000663 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000664 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000665 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000666 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000667 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000668 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000669 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000670
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000671 // We allow constant strings to be used as a placeholder for expressions
672 // that are not valid C++ syntax, but warn that they are ignored.
Erich Keane44bacdf2018-08-09 13:21:32 +0000673 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000674 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000675 continue;
676 }
677
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000678 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000679
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000680 // A pointer to member expression of the form &MyClass::mu is treated
681 // specially -- we need to look at the type of the member.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000682 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000683 if (UOp->getOpcode() == UO_AddrOf)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000684 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000685 if (DRE->getDecl()->isCXXInstanceMember())
686 ArgTy = DRE->getDecl()->getType();
687
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000688 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000689 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000690
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000691 // Now check if we index into a record type function param.
Josh Gao55afa752017-08-11 07:54:35 +0000692 if(!RT && ParamIdxOk) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000693 const auto *FD = dyn_cast<FunctionDecl>(D);
694 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
Josh Gao55afa752017-08-11 07:54:35 +0000695 if(FD && IL) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000696 unsigned int NumParams = FD->getNumParams();
697 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000698 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
699 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000700 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Aaron Ballmance667f62019-02-12 13:19:02 +0000701 S.Diag(AL.getLoc(),
702 diag::err_attribute_argument_out_of_bounds_extra_info)
Erich Keane44bacdf2018-08-09 13:21:32 +0000703 << AL << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000704 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000705 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000706 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000707 }
708 }
709
Aaron Ballman76050722014-04-04 15:13:57 +0000710 // If the type does not have a capability, see if the components of the
711 // expression have capabilities. This allows for writing C code where the
712 // capability may be on the type, and the expression is a capability
713 // boolean logic expression. Eg) requires_capability(A || B && !C)
714 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000715 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Erich Keane44bacdf2018-08-09 13:21:32 +0000716 << AL << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000717
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000718 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000719 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000720}
721
Chris Lattner58418ff2008-06-29 00:16:31 +0000722//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000723// Attribute Implementations
724//===----------------------------------------------------------------------===//
725
Erich Keanee891aa92018-07-13 15:07:47 +0000726static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000727 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000728 return;
729
Erich Keane6a24e802019-09-13 17:39:31 +0000730 D->addAttr(::new (S.Context) PtGuardedVarAttr(S.Context, AL));
Michael Han3be3b442012-07-23 18:48:41 +0000731}
732
Erich Keanee891aa92018-07-13 15:07:47 +0000733static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000734 Expr *&Arg) {
735 SmallVector<Expr *, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000736 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000737 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000738 unsigned Size = Args.size();
739 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000740 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000741
Michael Han3be3b442012-07-23 18:48:41 +0000742 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000743
Michael Han3be3b442012-07-23 18:48:41 +0000744 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000745}
746
Erich Keanee891aa92018-07-13 15:07:47 +0000747static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000748 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000749 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000750 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000751
Erich Keane6a24e802019-09-13 17:39:31 +0000752 D->addAttr(::new (S.Context) GuardedByAttr(S.Context, AL, Arg));
Michael Han3be3b442012-07-23 18:48:41 +0000753}
754
Erich Keanee891aa92018-07-13 15:07:47 +0000755static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000756 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000757 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000758 return;
759
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000760 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000761 return;
762
Erich Keane6a24e802019-09-13 17:39:31 +0000763 D->addAttr(::new (S.Context) PtGuardedByAttr(S.Context, AL, Arg));
Michael Han3be3b442012-07-23 18:48:41 +0000764}
765
Erich Keanee891aa92018-07-13 15:07:47 +0000766static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000767 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000768 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000769 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000770
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000771 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000772 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000773 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000774 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000775 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000776 }
777
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000778 // Check that all arguments are lockable objects.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000779 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000780 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000781 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000782
Michael Han3be3b442012-07-23 18:48:41 +0000783 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784}
785
Erich Keanee891aa92018-07-13 15:07:47 +0000786static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000787 SmallVector<Expr *, 1> Args;
788 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000789 return;
790
791 Expr **StartArg = &Args[0];
Erich Keane6a24e802019-09-13 17:39:31 +0000792 D->addAttr(::new (S.Context)
793 AcquiredAfterAttr(S.Context, AL, StartArg, Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000794}
795
Erich Keanee891aa92018-07-13 15:07:47 +0000796static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000797 SmallVector<Expr *, 1> Args;
798 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000799 return;
800
801 Expr **StartArg = &Args[0];
Erich Keane6a24e802019-09-13 17:39:31 +0000802 D->addAttr(::new (S.Context)
803 AcquiredBeforeAttr(S.Context, AL, StartArg, Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000804}
805
Erich Keanee891aa92018-07-13 15:07:47 +0000806static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000807 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000808 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000809 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000810 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000811
Michael Han3be3b442012-07-23 18:48:41 +0000812 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000813}
814
Erich Keanee891aa92018-07-13 15:07:47 +0000815static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000816 SmallVector<Expr *, 1> Args;
817 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000818 return;
819
820 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000821 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000822 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +0000823 AssertSharedLockAttr(S.Context, AL, StartArg, Size));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000824}
825
826static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000827 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000828 SmallVector<Expr *, 1> Args;
829 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000830 return;
831
832 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000833 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
Erich Keane6a24e802019-09-13 17:39:31 +0000834 D->addAttr(::new (S.Context)
835 AssertExclusiveLockAttr(S.Context, AL, StartArg, Size));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000836}
837
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000838/// Checks to be sure that the given parameter number is in bounds, and
Aaron Ballman836684a2018-02-25 20:40:06 +0000839/// is an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000840/// false.
841///
Aaron Ballman836684a2018-02-25 20:40:06 +0000842/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000843template <typename AttrInfo>
844static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Joel E. Denny81508102018-03-13 14:51:22 +0000845 const AttrInfo &AI, unsigned AttrArgNo) {
Aaron Ballman836684a2018-02-25 20:40:06 +0000846 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
847 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
Joel E. Denny81508102018-03-13 14:51:22 +0000848 ParamIdx Idx;
Aaron Ballman836684a2018-02-25 20:40:06 +0000849 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000850 Idx))
851 return false;
852
Joel E. Denny81508102018-03-13 14:51:22 +0000853 const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +0000854 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000855 SourceLocation SrcLoc = AttrArg->getBeginLoc();
Erich Keane623efd82017-03-30 21:48:55 +0000856 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +0000857 << AI << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000858 return false;
859 }
860 return true;
861}
862
Erich Keanee891aa92018-07-13 15:07:47 +0000863static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000864 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
865 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000866 return;
867
868 const auto *FD = cast<FunctionDecl>(D);
869 if (!FD->getReturnType()->isPointerType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000870 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
George Burgess IVe3763372016-12-22 02:50:20 +0000871 return;
872 }
873
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000874 const Expr *SizeExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +0000875 int SizeArgNoVal;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000876 // Parameter indices are 1-indexed, hence Index=1
Rui Ueyama49a3ad22019-07-16 04:46:31 +0000877 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Idx=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000878 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000879 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0))
George Burgess IVe3763372016-12-22 02:50:20 +0000880 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000881 ParamIdx SizeArgNo(SizeArgNoVal, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000882
Joel E. Denny81508102018-03-13 14:51:22 +0000883 ParamIdx NumberArgNo;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000884 if (AL.getNumArgs() == 2) {
885 const Expr *NumberExpr = AL.getArgAsExpr(1);
Joel E. Denny81508102018-03-13 14:51:22 +0000886 int Val;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000887 // Parameter indices are 1-based, hence Index=2
Rui Ueyama49a3ad22019-07-16 04:46:31 +0000888 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Idx=*/2))
George Burgess IVe3763372016-12-22 02:50:20 +0000889 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000890 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000891 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000892 NumberArgNo = ParamIdx(Val, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000893 }
894
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000895 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +0000896 AllocSizeAttr(S.Context, AL, SizeArgNo, NumberArgNo));
George Burgess IVe3763372016-12-22 02:50:20 +0000897}
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000898
Erich Keanee891aa92018-07-13 15:07:47 +0000899static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000900 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000901 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000902 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000903
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000904 if (!isIntOrBool(AL.getArgAsExpr(0))) {
905 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000906 << AL << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000907 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000908 }
909
910 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000911 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000912
Michael Han3be3b442012-07-23 18:48:41 +0000913 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000914}
915
Michael Hana9171bc2012-08-03 17:40:43 +0000916static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000917 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000918 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000919 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000920 return;
921
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000922 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
Erich Keane6a24e802019-09-13 17:39:31 +0000923 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000924}
925
Michael Hana9171bc2012-08-03 17:40:43 +0000926static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000927 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000928 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000929 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000930 return;
931
Nico Weber462fd1e2015-01-07 23:50:05 +0000932 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
Erich Keane6a24e802019-09-13 17:39:31 +0000933 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000934}
935
Erich Keanee891aa92018-07-13 15:07:47 +0000936static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000937 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000938 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000939 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000940 unsigned Size = Args.size();
941 if (Size == 0)
942 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000943
Erich Keane6a24e802019-09-13 17:39:31 +0000944 D->addAttr(::new (S.Context) LockReturnedAttr(S.Context, AL, Args[0]));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000945}
946
Erich Keanee891aa92018-07-13 15:07:47 +0000947static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000948 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000949 return;
950
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000951 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000952 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000953 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000954 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000955 if (Size == 0)
956 return;
957 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000958
Michael Han99315932013-01-24 16:46:58 +0000959 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +0000960 LocksExcludedAttr(S.Context, AL, StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000961}
962
Erich Keanee891aa92018-07-13 15:07:47 +0000963static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
George Burgess IV177399e2017-01-09 04:12:14 +0000964 Expr *&Cond, StringRef &Msg) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000965 Cond = AL.getArgAsExpr(0);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000966 if (!Cond->isTypeDependent()) {
967 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
968 if (Converted.isInvalid())
George Burgess IV177399e2017-01-09 04:12:14 +0000969 return false;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000970 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000971 }
972
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000973 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000974 return false;
975
976 if (Msg.empty())
977 Msg = "<no message provided>";
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000978
979 SmallVector<PartialDiagnosticAt, 8> Diags;
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000980 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000981 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
982 Diags)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000983 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
George Burgess IV0d546532016-11-10 21:47:12 +0000984 for (const PartialDiagnosticAt &PDiag : Diags)
985 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +0000986 return false;
987 }
988 return true;
989}
990
Erich Keanee891aa92018-07-13 15:07:47 +0000991static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000992 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +0000993
994 Expr *Cond;
995 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000996 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
Erich Keane6a24e802019-09-13 17:39:31 +0000997 D->addAttr(::new (S.Context) EnableIfAttr(S.Context, AL, Cond, Msg));
George Burgess IV177399e2017-01-09 04:12:14 +0000998}
999
1000namespace {
1001/// Determines if a given Expr references any of the given function's
1002/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
1003class ArgumentDependenceChecker
1004 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
1005#ifndef NDEBUG
1006 const CXXRecordDecl *ClassType;
1007#endif
1008 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
1009 bool Result;
1010
1011public:
1012 ArgumentDependenceChecker(const FunctionDecl *FD) {
1013#ifndef NDEBUG
1014 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1015 ClassType = MD->getParent();
1016 else
1017 ClassType = nullptr;
1018#endif
1019 Parms.insert(FD->param_begin(), FD->param_end());
1020 }
1021
1022 bool referencesArgs(Expr *E) {
1023 Result = false;
1024 TraverseStmt(E);
1025 return Result;
1026 }
1027
1028 bool VisitCXXThisExpr(CXXThisExpr *E) {
1029 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
1030 "`this` doesn't refer to the enclosing class?");
1031 Result = true;
1032 return false;
1033 }
1034
1035 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1036 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1037 if (Parms.count(PVD)) {
1038 Result = true;
1039 return false;
1040 }
1041 return true;
1042 }
1043};
1044}
1045
Erich Keanee891aa92018-07-13 15:07:47 +00001046static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001047 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001048
1049 Expr *Cond;
1050 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001051 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001052 return;
1053
1054 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001055 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +00001056 return;
1057
1058 DiagnoseIfAttr::DiagnosticType DiagType;
1059 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001060 S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
George Burgess IV177399e2017-01-09 04:12:14 +00001061 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001062 return;
1063 }
1064
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001065 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +00001066 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001067 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +00001068 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00001069 S.Context, AL, Cond, Msg, DiagType, ArgDependent, cast<NamedDecl>(D)));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001070}
1071
Erich Keanee891aa92018-07-13 15:07:47 +00001072static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001073 if (D->hasAttr<PassObjectSizeAttr>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001074 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001075 return;
1076 }
1077
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001078 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001079 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001080 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001081 return;
1082
1083 // pass_object_size's argument is passed in as the second argument of
1084 // __builtin_object_size. So, it has the same constraints as that second
1085 // argument; namely, it must be in the range [0, 3].
1086 if (Type > 3) {
Aaron Ballman52c9ad22019-02-12 13:04:11 +00001087 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00001088 << AL << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001089 return;
1090 }
1091
1092 // pass_object_size is only supported on constant pointer parameters; as a
1093 // kindness to users, we allow the parameter to be non-const for declarations.
1094 // At this point, we have no clue if `D` belongs to a function declaration or
1095 // definition, so we defer the constness check until later.
1096 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001097 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001098 return;
1099 }
1100
Erich Keane6a24e802019-09-13 17:39:31 +00001101 D->addAttr(::new (S.Context) PassObjectSizeAttr(S.Context, AL, (int)Type));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001102}
1103
Erich Keanee891aa92018-07-13 15:07:47 +00001104static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001105 ConsumableAttr::ConsumedState DefaultState;
1106
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001107 if (AL.isArgIdent(0)) {
1108 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001109 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1110 DefaultState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001111 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1112 << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001113 return;
1114 }
David Blaikie16f76d22013-09-06 01:28:43 +00001115 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001116 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001117 << AL << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001118 return;
1119 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001120
Erich Keane6a24e802019-09-13 17:39:31 +00001121 D->addAttr(::new (S.Context) ConsumableAttr(S.Context, AL, DefaultState));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001122}
1123
1124static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Erich Keanee891aa92018-07-13 15:07:47 +00001125 const ParsedAttr &AL) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001126 QualType ThisType = MD->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001127
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001128 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1129 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001130 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001131 RD->getNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001132
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001133 return false;
1134 }
1135 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001136
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001137 return true;
1138}
1139
Erich Keanee891aa92018-07-13 15:07:47 +00001140static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001141 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001142 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001143
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001144 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001145 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001146
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001147 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001148 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001149 CallableWhenAttr::ConsumedState CallableState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001150
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001151 StringRef StateString;
1152 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001153 if (AL.isArgIdent(ArgIndex)) {
1154 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001155 StateString = Ident->Ident->getName();
1156 Loc = Ident->Loc;
1157 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001158 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001159 return;
1160 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001161
1162 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001163 CallableState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001164 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001165 return;
1166 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001167
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001168 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001169 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001170
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001171 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00001172 CallableWhenAttr(S.Context, AL, States.data(), States.size()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001173}
1174
Erich Keanee891aa92018-07-13 15:07:47 +00001175static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001176 ParamTypestateAttr::ConsumedState ParamState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001177
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001178 if (AL.isArgIdent(0)) {
1179 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001180 StringRef StateString = Ident->Ident->getName();
1181
1182 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1183 ParamState)) {
1184 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00001185 << AL << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001186 return;
1187 }
1188 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001189 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1190 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001191 return;
1192 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001193
DeLesley Hutchins69391772013-10-17 23:23:53 +00001194 // FIXME: This check is currently being done in the analysis. It can be
1195 // enabled here only after the parser propagates attributes at
1196 // template specialization definition, not declaration.
1197 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1198 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1199 //
1200 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001201 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001202 // ReturnType.getAsString();
1203 // return;
1204 //}
Fangrui Song6907ce22018-07-30 19:24:48 +00001205
Erich Keane6a24e802019-09-13 17:39:31 +00001206 D->addAttr(::new (S.Context) ParamTypestateAttr(S.Context, AL, ParamState));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001207}
1208
Erich Keanee891aa92018-07-13 15:07:47 +00001209static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001210 ReturnTypestateAttr::ConsumedState ReturnState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001211
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001212 if (AL.isArgIdent(0)) {
1213 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001214 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1215 ReturnState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001216 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1217 << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001218 return;
1219 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001220 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001221 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1222 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001223 return;
1224 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001225
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001226 // FIXME: This check is currently being done in the analysis. It can be
1227 // enabled here only after the parser propagates attributes at
1228 // template specialization definition, not declaration.
1229 //QualType ReturnType;
1230 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001231 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1232 // ReturnType = Param->getType();
1233 //
1234 //} else if (const CXXConstructorDecl *Constructor =
1235 // dyn_cast<CXXConstructorDecl>(D)) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001236 // ReturnType = Constructor->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001237 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001238 //} else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001239 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001240 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1241 //}
1242 //
1243 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1244 //
1245 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1246 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1247 // ReturnType.getAsString();
1248 // return;
1249 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001250
Erich Keane6a24e802019-09-13 17:39:31 +00001251 D->addAttr(::new (S.Context) ReturnTypestateAttr(S.Context, AL, ReturnState));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001252}
1253
Erich Keanee891aa92018-07-13 15:07:47 +00001254static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001255 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001256 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001257
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001258 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001259 if (AL.isArgIdent(0)) {
1260 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001261 StringRef Param = Ident->Ident->getName();
1262 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001263 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1264 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001265 return;
1266 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001267 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001268 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1269 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001270 return;
1271 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001272
Erich Keane6a24e802019-09-13 17:39:31 +00001273 D->addAttr(::new (S.Context) SetTypestateAttr(S.Context, AL, NewState));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001274}
1275
Erich Keanee891aa92018-07-13 15:07:47 +00001276static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001277 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001278 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001279
1280 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001281 if (AL.isArgIdent(0)) {
1282 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001283 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001284 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001285 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1286 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001287 return;
1288 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001289 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001290 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1291 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001292 return;
1293 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001294
Erich Keane6a24e802019-09-13 17:39:31 +00001295 D->addAttr(::new (S.Context) TestTypestateAttr(S.Context, AL, TestState));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001296}
1297
Erich Keanee891aa92018-07-13 15:07:47 +00001298static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001299 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001300 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001301}
1302
Erich Keanee891aa92018-07-13 15:07:47 +00001303static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001304 if (auto *TD = dyn_cast<TagDecl>(D))
Erich Keane6a24e802019-09-13 17:39:31 +00001305 TD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001306 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001307 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1308 !FD->getType()->isIncompleteType() &&
1309 FD->isBitField() &&
1310 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001311
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001312 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1313 if (BitfieldByteAligned)
1314 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001315 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001316 << AL << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001317 else
Erich Keane6a24e802019-09-13 17:39:31 +00001318 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001319 } else {
1320 // Report warning about changed offset in the newer compiler versions.
1321 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001322 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001323
Erich Keane6a24e802019-09-13 17:39:31 +00001324 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001325 }
1326
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001327 } else
Erich Keane44bacdf2018-08-09 13:21:32 +00001328 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001329}
1330
Erich Keanee891aa92018-07-13 15:07:47 +00001331static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001332 // The IBOutlet/IBOutletCollection attributes only apply to instance
1333 // variables or properties of Objective-C classes. The outlet must also
1334 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001335 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001336 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001337 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001338 << AL << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001339 return false;
1340 }
1341 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001342 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001343 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001344 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001345 << AL << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001346 return false;
1347 }
1348 }
1349 else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001350 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001351 return false;
1352 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001353
Ted Kremenek7fd17232011-09-29 07:02:25 +00001354 return true;
1355}
1356
Erich Keanee891aa92018-07-13 15:07:47 +00001357static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001358 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001359 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001360
Erich Keane6a24e802019-09-13 17:39:31 +00001361 D->addAttr(::new (S.Context) IBOutletAttr(S.Context, AL));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001362}
1363
Erich Keanee891aa92018-07-13 15:07:47 +00001364static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001365
1366 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001367 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001368 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001369 return;
1370 }
1371
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001372 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001373 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001374
Richard Smithb1f9a282013-10-31 01:56:18 +00001375 ParsedType PT;
1376
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001377 if (AL.hasParsedType())
1378 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001379 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001380 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001381 S.getScopeForContext(D->getDeclContext()->getParent()));
1382 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001383 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001384 return;
1385 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001386 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001387
Craig Topperc3ec1492014-05-26 06:22:03 +00001388 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001389 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1390 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001391 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001392
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001393 // Diagnose use of non-object type in iboutletcollection attribute.
1394 // FIXME. Gnu attribute extension ignores use of builtin types in
1395 // attributes. So, __attribute__((iboutletcollection(char))) will be
1396 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001397 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001398 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001399 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1400 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001401 return;
1402 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001403
Erich Keane6a24e802019-09-13 17:39:31 +00001404 D->addAttr(::new (S.Context) IBOutletCollectionAttr(S.Context, AL, QTLoc));
Ted Kremenek26bde772010-05-19 17:38:06 +00001405}
1406
Hal Finkelee90a222014-09-26 05:04:30 +00001407bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1408 if (RefOkay) {
1409 if (T->isReferenceType())
1410 return true;
1411 } else {
1412 T = T.getNonReferenceType();
1413 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001414
Hal Finkelee90a222014-09-26 05:04:30 +00001415 // The nonnull attribute, and other similar attributes, can be applied to a
1416 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001417 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001418 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1419 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001420 for (const auto *I : UD->fields()) {
1421 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001422 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1423 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001424 }
1425 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001426 }
1427
1428 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001429}
1430
Erich Keanee891aa92018-07-13 15:07:47 +00001431static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001432 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001433 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001434 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001435 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001436 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001437 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001438 << AL << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001439 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001440 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001441 << AL << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001442 return false;
1443 }
1444 return true;
1445}
1446
Erich Keanee891aa92018-07-13 15:07:47 +00001447static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001448 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001449 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1450 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001451 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001452 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001453 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001454
1455 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001456 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1457 !attrNonNullArgCheck(
1458 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1459 Ex->getSourceRange(),
1460 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001461 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001462
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001463 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001464 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001465
1466 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001467 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1468 // check if the attribute came from a macro expansion or a template
1469 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001470 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001471 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001472 bool AnyPointers = isFunctionOrMethodVariadic(D);
1473 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1474 I != E && !AnyPointers; ++I) {
1475 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001476 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001477 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001478 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001479
Richard Smith588bd9b2014-08-27 04:59:42 +00001480 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001481 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001482 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001483
Joel E. Denny81508102018-03-13 14:51:22 +00001484 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001485 unsigned Size = NonNullArgs.size();
1486 llvm::array_pod_sort(Start, Start + Size);
Erich Keane6a24e802019-09-13 17:39:31 +00001487 D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, Start, Size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001488}
1489
Jordan Rosec9399072014-02-11 17:27:59 +00001490static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00001491 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001492 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001493 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001494 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001495 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001496 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001497 << D->getSourceRange();
1498 }
1499 return;
1500 }
1501
1502 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001503 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001504 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001505 return;
1506
Erich Keane6a24e802019-09-13 17:39:31 +00001507 D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, nullptr, 0));
Jordan Rosec9399072014-02-11 17:27:59 +00001508}
1509
Erich Keanee891aa92018-07-13 15:07:47 +00001510static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001511 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001512 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001513 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001514 /* isReturnValue */ true))
1515 return;
1516
Erich Keane6a24e802019-09-13 17:39:31 +00001517 D->addAttr(::new (S.Context) ReturnsNonNullAttr(S.Context, AL));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001518}
1519
Erich Keanee891aa92018-07-13 15:07:47 +00001520static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001521 if (D->isInvalidDecl())
1522 return;
1523
1524 // noescape only applies to pointer types.
1525 QualType T = cast<ParmVarDecl>(D)->getType();
1526 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001527 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001528 << AL << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001529 return;
1530 }
1531
Erich Keane6a24e802019-09-13 17:39:31 +00001532 D->addAttr(::new (S.Context) NoEscapeAttr(S.Context, AL));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001533}
1534
Erich Keanee891aa92018-07-13 15:07:47 +00001535static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001536 Expr *E = AL.getArgAsExpr(0),
1537 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
Erich Keane6a24e802019-09-13 17:39:31 +00001538 S.AddAssumeAlignedAttr(D, AL, E, OE);
Hal Finkelee90a222014-09-26 05:04:30 +00001539}
1540
Erich Keanee891aa92018-07-13 15:07:47 +00001541static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00001542 S.AddAllocAlignAttr(D, AL, AL.getArgAsExpr(0));
Erich Keane623efd82017-03-30 21:48:55 +00001543}
1544
Erich Keane6a24e802019-09-13 17:39:31 +00001545void Sema::AddAssumeAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
1546 Expr *OE) {
Hal Finkelee90a222014-09-26 05:04:30 +00001547 QualType ResultType = getFunctionOrMethodResultType(D);
1548 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1549
Erich Keane6a24e802019-09-13 17:39:31 +00001550 AssumeAlignedAttr TmpAttr(Context, CI, E, OE);
1551 SourceLocation AttrLoc = TmpAttr.getLocation();
Hal Finkelee90a222014-09-26 05:04:30 +00001552
1553 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1554 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
Erich Keane6a24e802019-09-13 17:39:31 +00001555 << &TmpAttr << TmpAttr.getRange() << SR;
Hal Finkelee90a222014-09-26 05:04:30 +00001556 return;
1557 }
1558
1559 if (!E->isValueDependent()) {
1560 llvm::APSInt I(64);
1561 if (!E->isIntegerConstantExpr(I, Context)) {
1562 if (OE)
1563 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1564 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1565 << E->getSourceRange();
1566 else
1567 Diag(AttrLoc, diag::err_attribute_argument_type)
1568 << &TmpAttr << AANT_ArgumentIntegerConstant
1569 << E->getSourceRange();
1570 return;
1571 }
1572
1573 if (!I.isPowerOf2()) {
1574 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1575 << E->getSourceRange();
1576 return;
1577 }
1578 }
1579
1580 if (OE) {
1581 if (!OE->isValueDependent()) {
1582 llvm::APSInt I(64);
1583 if (!OE->isIntegerConstantExpr(I, Context)) {
1584 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1585 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1586 << OE->getSourceRange();
1587 return;
1588 }
1589 }
1590 }
1591
Erich Keane6a24e802019-09-13 17:39:31 +00001592 D->addAttr(::new (Context) AssumeAlignedAttr(Context, CI, E, OE));
Hal Finkelee90a222014-09-26 05:04:30 +00001593}
1594
Erich Keane6a24e802019-09-13 17:39:31 +00001595void Sema::AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI,
1596 Expr *ParamExpr) {
Erich Keane623efd82017-03-30 21:48:55 +00001597 QualType ResultType = getFunctionOrMethodResultType(D);
1598
Erich Keane6a24e802019-09-13 17:39:31 +00001599 AllocAlignAttr TmpAttr(Context, CI, ParamIdx());
1600 SourceLocation AttrLoc = CI.getLoc();
Erich Keane623efd82017-03-30 21:48:55 +00001601
1602 if (!ResultType->isDependentType() &&
1603 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1604 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
Erich Keane6a24e802019-09-13 17:39:31 +00001605 << &TmpAttr << CI.getRange() << getFunctionOrMethodResultSourceRange(D);
Erich Keane623efd82017-03-30 21:48:55 +00001606 return;
1607 }
1608
Joel E. Denny81508102018-03-13 14:51:22 +00001609 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001610 const auto *FuncDecl = cast<FunctionDecl>(D);
1611 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001612 /*AttrArgNum=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001613 return;
1614
Joel E. Denny81508102018-03-13 14:51:22 +00001615 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001616 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001617 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001618 << &TmpAttr
1619 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001620 return;
1621 }
1622
Erich Keane6a24e802019-09-13 17:39:31 +00001623 D->addAttr(::new (Context) AllocAlignAttr(Context, CI, Idx));
Erich Keane623efd82017-03-30 21:48:55 +00001624}
1625
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001626/// Normalize the attribute, __foo__ becomes foo.
1627/// Returns true if normalization was applied.
1628static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001629 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1630 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001631 AttrName = AttrName.drop_front(2).drop_back(2);
1632 return true;
1633 }
1634 return false;
1635}
1636
Erich Keanee891aa92018-07-13 15:07:47 +00001637static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001638 // This attribute must be applied to a function declaration. The first
1639 // argument to the attribute must be an identifier, the name of the resource,
1640 // for example: malloc. The following arguments must be argument indexes, the
1641 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001642 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001643 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001644 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001645
Aaron Ballman00e99962013-08-31 01:11:41 +00001646 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001647 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001648 << AL << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001649 return;
1650 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001651
Richard Smith852e9ce2013-11-27 01:46:48 +00001652 // Figure out our Kind.
1653 OwnershipAttr::OwnershipKind K =
Erich Keane6a24e802019-09-13 17:39:31 +00001654 OwnershipAttr(S.Context, AL, nullptr, nullptr, 0).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001655
Richard Smith852e9ce2013-11-27 01:46:48 +00001656 // Check arguments.
1657 switch (K) {
1658 case OwnershipAttr::Takes:
1659 case OwnershipAttr::Holds:
1660 if (AL.getNumArgs() < 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001661 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001662 return;
1663 }
1664 break;
1665 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001666 if (AL.getNumArgs() > 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001667 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001668 return;
1669 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001670 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001671 }
1672
Richard Smith852e9ce2013-11-27 01:46:48 +00001673 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001674
Richard Smith852e9ce2013-11-27 01:46:48 +00001675 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001676 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001677 Module = &S.PP.getIdentifierTable().get(ModuleName);
1678 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001679
Joel E. Denny81508102018-03-13 14:51:22 +00001680 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001681 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1682 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001683 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001684 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001685 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001686
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001687 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001688 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001689 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001690 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001691 case OwnershipAttr::Takes:
1692 case OwnershipAttr::Holds:
1693 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1694 Err = 0;
1695 break;
1696 case OwnershipAttr::Returns:
1697 if (!T->isIntegerType())
1698 Err = 1;
1699 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001700 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001701 if (-1 != Err) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001702 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1703 << Ex->getSourceRange();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001704 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001705 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001706
1707 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001708 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001709 // Cannot have two ownership attributes of different kinds for the same
1710 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001711 if (I->getOwnKind() != K && I->args_end() !=
1712 std::find(I->args_begin(), I->args_end(), Idx)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001713 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001714 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001715 } else if (K == OwnershipAttr::Returns &&
1716 I->getOwnKind() == OwnershipAttr::Returns) {
1717 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001718 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001719 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1720 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001721 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001722 if (I->args_size())
1723 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001724 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001725 return;
1726 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001727 }
1728 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001729 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001730 }
1731
Joel E. Denny81508102018-03-13 14:51:22 +00001732 ParamIdx *Start = OwnershipArgs.data();
1733 unsigned Size = OwnershipArgs.size();
1734 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001735 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00001736 OwnershipAttr(S.Context, AL, Module, Start, Size));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001737}
1738
Erich Keanee891aa92018-07-13 15:07:47 +00001739static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001740 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001741 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001742 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001743 return;
1744 }
1745
1746 // gcc rejects
1747 // class c {
1748 // static int a __attribute__((weakref ("v2")));
1749 // static int b() __attribute__((weakref ("f3")));
1750 // };
1751 // and ignores the attributes of
1752 // void f(void) {
1753 // static int a __attribute__((weakref ("v2")));
1754 // }
1755 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001756 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001757 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001758 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1759 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001760 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001761 }
1762
1763 // The GCC manual says
1764 //
1765 // At present, a declaration to which `weakref' is attached can only
1766 // be `static'.
1767 //
1768 // It also says
1769 //
1770 // Without a TARGET,
1771 // given as an argument to `weakref' or to `alias', `weakref' is
1772 // equivalent to `weak'.
1773 //
1774 // gcc 4.4.1 will accept
1775 // int a7 __attribute__((weakref));
1776 // as
1777 // int a7 __attribute__((weak));
1778 // This looks like a bug in gcc. We reject that for now. We should revisit
1779 // it if this behaviour is actually used.
1780
Rafael Espindolac18086a2010-02-23 22:00:30 +00001781 // GCC rejects
1782 // static ((alias ("y"), weakref)).
1783 // Should we? How to check that weakref is before or after alias?
1784
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001785 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1786 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1787 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001788 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001789 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001790 // GCC will accept anything as the argument of weakref. Should we
1791 // check for an existing decl?
Erich Keane6a24e802019-09-13 17:39:31 +00001792 D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001793
Erich Keane6a24e802019-09-13 17:39:31 +00001794 D->addAttr(::new (S.Context) WeakRefAttr(S.Context, AL));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001795}
1796
Erich Keanee891aa92018-07-13 15:07:47 +00001797static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001798 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001799 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001800 return;
1801
1802 // Aliases should be on declarations, not definitions.
1803 const auto *FD = cast<FunctionDecl>(D);
1804 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001805 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001806 return;
1807 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001808
Erich Keane6a24e802019-09-13 17:39:31 +00001809 D->addAttr(::new (S.Context) IFuncAttr(S.Context, AL, Str));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001810}
1811
Erich Keanee891aa92018-07-13 15:07:47 +00001812static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001813 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001814 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001815 return;
1816
Douglas Gregore8bbc122011-09-02 00:18:52 +00001817 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001818 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001819 return;
1820 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001821 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001822 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001823 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001824
David Majnemer2dc81462015-01-19 09:00:28 +00001825 // Aliases should be on declarations, not definitions.
1826 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1827 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001828 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001829 return;
1830 }
1831 } else {
1832 const auto *VD = cast<VarDecl>(D);
1833 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001834 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001835 return;
1836 }
1837 }
1838
Nick Desaulniers9b9fe412019-01-09 23:54:55 +00001839 // Mark target used to prevent unneeded-internal-declaration warnings.
1840 if (!S.LangOpts.CPlusPlus) {
1841 // FIXME: demangle Str for C++, as the attribute refers to the mangled
1842 // linkage name, not the pre-mangled identifier.
1843 const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc());
1844 LookupResult LR(S, target, Sema::LookupOrdinaryName);
1845 if (S.LookupQualifiedName(LR, S.getCurLexicalContext()))
1846 for (NamedDecl *ND : LR)
1847 ND->markUsed(S.Context);
1848 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001849
Erich Keane6a24e802019-09-13 17:39:31 +00001850 D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001851}
1852
Erich Keanee891aa92018-07-13 15:07:47 +00001853static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001854 StringRef Model;
1855 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001856 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001857 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001858 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001859
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001860 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001861 if (Model != "global-dynamic" && Model != "local-dynamic"
1862 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001863 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001864 return;
1865 }
1866
Erich Keane6a24e802019-09-13 17:39:31 +00001867 D->addAttr(::new (S.Context) TLSModelAttr(S.Context, AL, Model));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001868}
1869
Erich Keanee891aa92018-07-13 15:07:47 +00001870static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001871 QualType ResultType = getFunctionOrMethodResultType(D);
1872 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
Erich Keane6a24e802019-09-13 17:39:31 +00001873 D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL));
David Majnemer631a90b2015-02-04 07:23:21 +00001874 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001875 }
1876
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001877 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001878 << AL << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001879}
1880
Erich Keane3efe0022018-07-20 14:13:28 +00001881static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1882 FunctionDecl *FD = cast<FunctionDecl>(D);
Erich Keane659c8712018-09-10 14:31:56 +00001883
1884 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1885 if (MD->getParent()->isLambda()) {
1886 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
1887 return;
1888 }
1889 }
1890
Erich Keane3efe0022018-07-20 14:13:28 +00001891 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1892 return;
1893
1894 SmallVector<IdentifierInfo *, 8> CPUs;
1895 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
1896 if (!AL.isArgIdent(ArgNo)) {
1897 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001898 << AL << AANT_ArgumentIdentifier;
Erich Keane3efe0022018-07-20 14:13:28 +00001899 return;
1900 }
1901
1902 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1903 StringRef CPUName = CPUArg->Ident->getName().trim();
1904
1905 if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1906 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1907 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1908 return;
1909 }
1910
1911 const TargetInfo &Target = S.Context.getTargetInfo();
1912 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1913 return Target.CPUSpecificManglingCharacter(CPUName) ==
1914 Target.CPUSpecificManglingCharacter(Cur->getName());
1915 })) {
1916 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1917 return;
1918 }
1919 CPUs.push_back(CPUArg->Ident);
1920 }
1921
1922 FD->setIsMultiVersion(true);
1923 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
Erich Keane6a24e802019-09-13 17:39:31 +00001924 D->addAttr(::new (S.Context)
1925 CPUSpecificAttr(S.Context, AL, CPUs.data(), CPUs.size()));
Erich Keane3efe0022018-07-20 14:13:28 +00001926 else
Erich Keane6a24e802019-09-13 17:39:31 +00001927 D->addAttr(::new (S.Context)
1928 CPUDispatchAttr(S.Context, AL, CPUs.data(), CPUs.size()));
Erich Keane3efe0022018-07-20 14:13:28 +00001929}
1930
Erich Keanee891aa92018-07-13 15:07:47 +00001931static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001932 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001933 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00001934 << AL << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001935 return;
1936 }
1937
Erich Keane44bacdf2018-08-09 13:21:32 +00001938 if (CommonAttr *CA = S.mergeCommonAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001939 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001940}
1941
Erich Keanee891aa92018-07-13 15:07:47 +00001942static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001943 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
Charles Davis0e379112016-08-08 21:19:08 +00001944 return;
1945
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001946 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001947 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
1948 const auto &Arch = Triple.getArch();
1949 if (Arch != llvm::Triple::x86 &&
1950 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001951 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
Erich Keane44bacdf2018-08-09 13:21:32 +00001952 << AL << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001953 return;
1954 }
1955 }
1956
Erich Keane6a24e802019-09-13 17:39:31 +00001957 D->addAttr(::new (S.Context) NakedAttr(S.Context, AL));
Charles Davis0e379112016-08-08 21:19:08 +00001958}
1959
Erich Keanee891aa92018-07-13 15:07:47 +00001960static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001961 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001962
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001963 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001964 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001965 << Attrs << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001966 return;
1967 }
1968
Erich Keane6a24e802019-09-13 17:39:31 +00001969 D->addAttr(::new (S.Context) NoReturnAttr(S.Context, Attrs));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001970}
1971
Erich Keanee891aa92018-07-13 15:07:47 +00001972static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00001973 if (!S.getLangOpts().CFProtectionBranch)
1974 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
1975 else
1976 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00001977}
1978
Erich Keanee891aa92018-07-13 15:07:47 +00001979bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001980 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
1981 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00001982 return true;
1983 }
1984
1985 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001986}
1987
Erich Keanee891aa92018-07-13 15:07:47 +00001988bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001989 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001990 if (!AL.existsInTarget(Context.getTargetInfo())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001991 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001992 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001993 return true;
1994 }
1995
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001996 return false;
1997}
1998
Erich Keanee891aa92018-07-13 15:07:47 +00001999static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2000
Ted Kremenek5295ce82010-08-19 00:51:58 +00002001 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2002 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00002003 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002004 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00002005 if (!VD || (!VD->getType()->isBlockPointerType() &&
2006 !VD->getType()->isFunctionPointerType())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002007 S.Diag(AL.getLoc(), AL.isCXX11Attribute()
2008 ? diag::err_attribute_wrong_decl_type
2009 : diag::warn_attribute_wrong_decl_type)
2010 << AL << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00002011 return;
2012 }
2013 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002014
Erich Keane6a24e802019-09-13 17:39:31 +00002015 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(S.Context, AL));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002016}
2017
John Thompsoncdb847ba2010-08-09 21:53:52 +00002018// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00002019static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2020 /*
2021 Returning a Vector Class in Registers
2022
2023 According to the PPU ABI specifications, a class with a single member of
2024 vector type is returned in memory when used as the return value of a
2025 function.
2026 This results in inefficient code when implementing vector classes. To return
2027 the value in a single vector register, add the vecreturn attribute to the
2028 class definition. This attribute is also applicable to struct types.
2029
2030 Example:
2031
2032 struct Vector
2033 {
2034 __vector float xyzw;
2035 } __attribute__((vecreturn));
2036
2037 Vector Add(Vector lhs, Vector rhs)
2038 {
2039 Vector result;
2040 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2041 return result; // This will be returned in a register
2042 }
2043 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002044 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002045 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002046 return;
2047 }
2048
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002049 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002050 int count = 0;
2051
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002052 if (!isa<CXXRecordDecl>(R)) {
2053 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002054 return;
2055 }
2056
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002057 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2058 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002059 return;
2060 }
2061
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002062 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002063 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002064 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002065 return;
2066 }
2067 count++;
2068 }
2069
Erich Keane6a24e802019-09-13 17:39:31 +00002070 D->addAttr(::new (S.Context) VecReturnAttr(S.Context, AL));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002071}
2072
Richard Smithe233fbf2013-01-28 22:42:45 +00002073static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002074 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002075 if (isa<ParmVarDecl>(D)) {
2076 // [[carries_dependency]] can only be applied to a parameter if it is a
2077 // parameter of a function declaration or lambda.
2078 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002079 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002080 diag::err_carries_dependency_param_not_function_decl);
2081 return;
2082 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002083 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002084
Erich Keane6a24e802019-09-13 17:39:31 +00002085 D->addAttr(::new (S.Context) CarriesDependencyAttr(S.Context, AL));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002086}
2087
Erich Keanee891aa92018-07-13 15:07:47 +00002088static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002089 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002090
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002091 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002092 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002093 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Erich Keane44bacdf2018-08-09 13:21:32 +00002094 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002095
Erich Keane6a24e802019-09-13 17:39:31 +00002096 D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002097}
2098
Erich Keanee891aa92018-07-13 15:07:47 +00002099static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002100 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002101 if (AL.getNumArgs() &&
2102 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002103 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002104
Erich Keane6a24e802019-09-13 17:39:31 +00002105 D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00002106}
2107
Erich Keanee891aa92018-07-13 15:07:47 +00002108static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002109 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002110 if (AL.getNumArgs() &&
2111 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002112 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002113
Erich Keane6a24e802019-09-13 17:39:31 +00002114 D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00002115}
2116
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002117template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002118static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002119 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002120 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002121 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002122 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002123
Erich Keane6a24e802019-09-13 17:39:31 +00002124 D->addAttr(::new (S.Context) AttrTy(S.Context, AL, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002125}
2126
Ted Kremenek438f8db2014-02-22 01:06:05 +00002127static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002128 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002129 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002130 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
Erich Keane44bacdf2018-08-09 13:21:32 +00002131 << AL << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002132 return;
2133 }
2134
Erich Keane6a24e802019-09-13 17:39:31 +00002135 D->addAttr(::new (S.Context) ObjCExplicitProtocolImplAttr(S.Context, AL));
Ted Kremenek28eace62013-11-23 01:01:34 +00002136}
2137
Jordy Rose740b0c22012-05-08 03:27:22 +00002138static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2139 IdentifierInfo *Platform,
2140 VersionTuple Introduced,
2141 VersionTuple Deprecated,
2142 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002143 StringRef PlatformName
2144 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2145 if (PlatformName.empty())
2146 PlatformName = Platform->getName();
2147
2148 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2149 // of these steps are needed).
2150 if (!Introduced.empty() && !Deprecated.empty() &&
2151 !(Introduced <= Deprecated)) {
2152 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2153 << 1 << PlatformName << Deprecated.getAsString()
2154 << 0 << Introduced.getAsString();
2155 return true;
2156 }
2157
2158 if (!Introduced.empty() && !Obsoleted.empty() &&
2159 !(Introduced <= Obsoleted)) {
2160 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2161 << 2 << PlatformName << Obsoleted.getAsString()
2162 << 0 << Introduced.getAsString();
2163 return true;
2164 }
2165
2166 if (!Deprecated.empty() && !Obsoleted.empty() &&
2167 !(Deprecated <= Obsoleted)) {
2168 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2169 << 2 << PlatformName << Obsoleted.getAsString()
2170 << 1 << Deprecated.getAsString();
2171 return true;
2172 }
2173
2174 return false;
2175}
2176
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002177/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002178///
2179/// If either version tuple is empty, then they are assumed to match. If
2180/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2181static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2182 bool BeforeIsOkay) {
2183 if (X.empty() || Y.empty())
2184 return true;
2185
2186 if (X == Y)
2187 return true;
2188
2189 if (BeforeIsOkay && X < Y)
2190 return true;
2191
2192 return false;
2193}
2194
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002195AvailabilityAttr *Sema::mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002196 NamedDecl *D, const AttributeCommonInfo &CI, IdentifierInfo *Platform,
2197 bool Implicit, VersionTuple Introduced, VersionTuple Deprecated,
2198 VersionTuple Obsoleted, bool IsUnavailable, StringRef Message,
2199 bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK,
2200 int Priority) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002201 VersionTuple MergedIntroduced = Introduced;
2202 VersionTuple MergedDeprecated = Deprecated;
2203 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002204 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002205 bool OverrideOrImpl = false;
2206 switch (AMK) {
2207 case AMK_None:
2208 case AMK_Redeclaration:
2209 OverrideOrImpl = false;
2210 break;
2211
2212 case AMK_Override:
2213 case AMK_ProtocolImplementation:
2214 OverrideOrImpl = true;
2215 break;
2216 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002217
Rafael Espindolac67f2232012-05-10 02:50:16 +00002218 if (D->hasAttrs()) {
2219 AttrVec &Attrs = D->getAttrs();
2220 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002221 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002222 if (!OldAA) {
2223 ++i;
2224 continue;
2225 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002226
Rafael Espindolac67f2232012-05-10 02:50:16 +00002227 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2228 if (OldPlatform != Platform) {
2229 ++i;
2230 continue;
2231 }
2232
Tim Northover7a73cc72015-10-30 16:30:49 +00002233 // If there is an existing availability attribute for this platform that
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002234 // has a lower priority use the existing one and discard the new
2235 // attribute.
2236 if (OldAA->getPriority() < Priority)
Tim Northover7a73cc72015-10-30 16:30:49 +00002237 return nullptr;
Tim Northover7a73cc72015-10-30 16:30:49 +00002238
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002239 // If there is an existing attribute for this platform that has a higher
2240 // priority than the new attribute then erase the old one and continue
2241 // processing the attributes.
2242 if (OldAA->getPriority() > Priority) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002243 Attrs.erase(Attrs.begin() + i);
2244 --e;
2245 continue;
2246 }
2247
Rafael Espindolac67f2232012-05-10 02:50:16 +00002248 FoundAny = true;
2249 VersionTuple OldIntroduced = OldAA->getIntroduced();
2250 VersionTuple OldDeprecated = OldAA->getDeprecated();
2251 VersionTuple OldObsoleted = OldAA->getObsoleted();
2252 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002253
Douglas Gregord2a713e2015-09-30 21:27:42 +00002254 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2255 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2256 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002257 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002258 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2259 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002260 int Which = -1;
2261 VersionTuple FirstVersion;
2262 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002263 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002264 Which = 0;
2265 FirstVersion = OldIntroduced;
2266 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002267 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002268 Which = 1;
2269 FirstVersion = Deprecated;
2270 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002271 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002272 Which = 2;
2273 FirstVersion = Obsoleted;
2274 SecondVersion = OldObsoleted;
2275 }
2276
2277 if (Which == -1) {
2278 Diag(OldAA->getLocation(),
2279 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002280 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2281 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002282 } else {
2283 Diag(OldAA->getLocation(),
2284 diag::warn_mismatched_availability_override)
2285 << Which
2286 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002287 << FirstVersion.getAsString() << SecondVersion.getAsString()
2288 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002289 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002290 if (AMK == AMK_Override)
Erich Keane6a24e802019-09-13 17:39:31 +00002291 Diag(CI.getLoc(), diag::note_overridden_method);
Douglas Gregord2a713e2015-09-30 21:27:42 +00002292 else
Erich Keane6a24e802019-09-13 17:39:31 +00002293 Diag(CI.getLoc(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002294 } else {
2295 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
Erich Keane6a24e802019-09-13 17:39:31 +00002296 Diag(CI.getLoc(), diag::note_previous_attribute);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002297 }
2298
Rafael Espindolac67f2232012-05-10 02:50:16 +00002299 Attrs.erase(Attrs.begin() + i);
2300 --e;
2301 continue;
2302 }
2303
2304 VersionTuple MergedIntroduced2 = MergedIntroduced;
2305 VersionTuple MergedDeprecated2 = MergedDeprecated;
2306 VersionTuple MergedObsoleted2 = MergedObsoleted;
2307
2308 if (MergedIntroduced2.empty())
2309 MergedIntroduced2 = OldIntroduced;
2310 if (MergedDeprecated2.empty())
2311 MergedDeprecated2 = OldDeprecated;
2312 if (MergedObsoleted2.empty())
2313 MergedObsoleted2 = OldObsoleted;
2314
2315 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2316 MergedIntroduced2, MergedDeprecated2,
2317 MergedObsoleted2)) {
2318 Attrs.erase(Attrs.begin() + i);
2319 --e;
2320 continue;
2321 }
2322
2323 MergedIntroduced = MergedIntroduced2;
2324 MergedDeprecated = MergedDeprecated2;
2325 MergedObsoleted = MergedObsoleted2;
2326 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002327 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002328 }
2329
2330 if (FoundAny &&
2331 MergedIntroduced == Introduced &&
2332 MergedDeprecated == Deprecated &&
2333 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002334 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002335
Douglas Gregord2a713e2015-09-30 21:27:42 +00002336 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002337 // the checking.
Erich Keane6a24e802019-09-13 17:39:31 +00002338 if (!checkAvailabilityAttr(*this, CI.getRange(), Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002339 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002340 !OverrideOrImpl) {
Erich Keane6a24e802019-09-13 17:39:31 +00002341 auto *Avail = ::new (Context) AvailabilityAttr(
2342 Context, CI, Platform, Introduced, Deprecated, Obsoleted, IsUnavailable,
2343 Message, IsStrict, Replacement, Priority);
Manman Ren719a8642016-05-06 21:04:01 +00002344 Avail->setImplicit(Implicit);
2345 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002346 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002347 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002348}
2349
Erich Keanee891aa92018-07-13 15:07:47 +00002350static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002351 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002352 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002353 IdentifierLoc *Platform = AL.getArgAsIdent(0);
Fangrui Song6907ce22018-07-30 19:24:48 +00002354
Aaron Ballman00e99962013-08-31 01:11:41 +00002355 IdentifierInfo *II = Platform->Ident;
2356 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2357 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2358 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002359
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002360 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002361 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002362 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002363
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002364 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2365 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2366 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2367 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2368 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002369 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002370 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002371 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002372 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002373 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002374 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002375
Michael Wu260e9622018-11-12 02:44:33 +00002376 if (II->isStr("swift")) {
2377 if (Introduced.isValid() || Obsoleted.isValid() ||
2378 (!IsUnavailable && !Deprecated.isValid())) {
2379 S.Diag(AL.getLoc(),
2380 diag::warn_availability_swift_unavailable_deprecated_only);
2381 return;
2382 }
2383 }
2384
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002385 int PriorityModifier = AL.isPragmaClangAttribute()
2386 ? Sema::AP_PragmaClangAttribute
2387 : Sema::AP_Explicit;
2388 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002389 ND, AL, II, false /*Implicit*/, Introduced.Version, Deprecated.Version,
2390 Obsoleted.Version, IsUnavailable, Str, IsStrict, Replacement,
2391 Sema::AMK_None, PriorityModifier);
Rafael Espindola19de5612013-01-12 06:42:30 +00002392 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002393 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002394
2395 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2396 // matches before the start of the watchOS platform.
2397 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2398 IdentifierInfo *NewII = nullptr;
2399 if (II->getName() == "ios")
2400 NewII = &S.Context.Idents.get("watchos");
2401 else if (II->getName() == "ios_app_extension")
2402 NewII = &S.Context.Idents.get("watchos_app_extension");
2403
2404 if (NewII) {
2405 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2406 if (Version.empty())
2407 return Version;
2408 auto Major = Version.getMajor();
2409 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2410 if (NewMajor >= 2) {
2411 if (Version.getMinor().hasValue()) {
2412 if (Version.getSubminor().hasValue())
2413 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2414 Version.getSubminor().getValue());
2415 else
2416 return VersionTuple(NewMajor, Version.getMinor().getValue());
2417 }
Alex Lorenz0b436482019-03-20 20:02:00 +00002418 return VersionTuple(NewMajor);
Tim Northover7a73cc72015-10-30 16:30:49 +00002419 }
2420
2421 return VersionTuple(2, 0);
2422 };
2423
2424 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2425 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2426 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2427
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002428 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002429 ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated,
2430 NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
2431 Sema::AMK_None,
2432 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
Tim Northover7a73cc72015-10-30 16:30:49 +00002433 if (NewAttr)
2434 D->addAttr(NewAttr);
2435 }
2436 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2437 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2438 // matches before the start of the tvOS platform.
2439 IdentifierInfo *NewII = nullptr;
2440 if (II->getName() == "ios")
2441 NewII = &S.Context.Idents.get("tvos");
2442 else if (II->getName() == "ios_app_extension")
2443 NewII = &S.Context.Idents.get("tvos_app_extension");
2444
2445 if (NewII) {
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002446 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002447 ND, AL, NewII, true /*Implicit*/, Introduced.Version,
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002448 Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict,
2449 Replacement, Sema::AMK_None,
Erich Keane6a24e802019-09-13 17:39:31 +00002450 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002451 if (NewAttr)
2452 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002453 }
2454 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002455}
2456
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002457static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002458 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002459 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002460 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002461 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002462 "Invalid number of arguments in an external_source_symbol attribute");
2463
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002464 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002465 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002466 Language = SE->getString();
2467 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002468 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002469 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002470 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002471
2472 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002473 S.Context, AL, Language, DefinedIn, IsGeneratedDeclaration));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002474}
2475
John McCalld041a9b2013-02-20 01:54:26 +00002476template <class T>
Erich Keane6a24e802019-09-13 17:39:31 +00002477static T *mergeVisibilityAttr(Sema &S, Decl *D, const AttributeCommonInfo &CI,
2478 typename T::VisibilityType value) {
John McCalld041a9b2013-02-20 01:54:26 +00002479 T *existingAttr = D->getAttr<T>();
2480 if (existingAttr) {
2481 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2482 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002483 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002484 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
Erich Keane6a24e802019-09-13 17:39:31 +00002485 S.Diag(CI.getLoc(), diag::note_previous_attribute);
John McCalld041a9b2013-02-20 01:54:26 +00002486 D->dropAttr<T>();
2487 }
Erich Keane6a24e802019-09-13 17:39:31 +00002488 return ::new (S.Context) T(S.Context, CI, value);
John McCalld041a9b2013-02-20 01:54:26 +00002489}
2490
Erich Keane6a24e802019-09-13 17:39:31 +00002491VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D,
2492 const AttributeCommonInfo &CI,
2493 VisibilityAttr::VisibilityType Vis) {
2494 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, CI, Vis);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002495}
2496
Erich Keane6a24e802019-09-13 17:39:31 +00002497TypeVisibilityAttr *
2498Sema::mergeTypeVisibilityAttr(Decl *D, const AttributeCommonInfo &CI,
2499 TypeVisibilityAttr::VisibilityType Vis) {
2500 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, CI, Vis);
John McCalld041a9b2013-02-20 01:54:26 +00002501}
2502
Erich Keanee891aa92018-07-13 15:07:47 +00002503static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002504 bool isTypeVisibility) {
2505 // Visibility attributes don't mean anything on a typedef.
2506 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002507 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002508 return;
2509 }
2510
2511 // 'type_visibility' can only go on a type or namespace.
2512 if (isTypeVisibility &&
2513 !(isa<TagDecl>(D) ||
2514 isa<ObjCInterfaceDecl>(D) ||
2515 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002516 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002517 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002518 return;
2519 }
2520
Benjamin Kramer70370212013-09-09 15:08:57 +00002521 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002522 StringRef TypeStr;
2523 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002524 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002525 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002526
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002527 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002528 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002529 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2530 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002531 return;
2532 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002533
Aaron Ballman682ee422013-09-11 19:47:58 +00002534 // Complain about attempts to use protected visibility on targets
2535 // (like Darwin) that don't support it.
2536 if (type == VisibilityAttr::Protected &&
2537 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002538 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002539 type = VisibilityAttr::Default;
2540 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002541
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002542 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002543 if (isTypeVisibility) {
Erich Keane6a24e802019-09-13 17:39:31 +00002544 newAttr = S.mergeTypeVisibilityAttr(
2545 D, AL, (TypeVisibilityAttr::VisibilityType)type);
John McCalld041a9b2013-02-20 01:54:26 +00002546 } else {
Erich Keane6a24e802019-09-13 17:39:31 +00002547 newAttr = S.mergeVisibilityAttr(D, AL, type);
John McCalld041a9b2013-02-20 01:54:26 +00002548 }
2549 if (newAttr)
2550 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002551}
2552
Erich Keanee891aa92018-07-13 15:07:47 +00002553static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002554 const auto *M = cast<ObjCMethodDecl>(D);
2555 if (!AL.isArgIdent(0)) {
2556 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002557 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002558 return;
2559 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002560
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002561 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002562 ObjCMethodFamilyAttr::FamilyKind F;
2563 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002564 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002565 return;
2566 }
2567
Alp Toker314cc812014-01-25 16:55:45 +00002568 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002569 !M->getReturnType()->isObjCObjectPointerType()) {
2570 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2571 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002572 // Ignore the attribute.
2573 return;
2574 }
2575
Erich Keane6a24e802019-09-13 17:39:31 +00002576 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(S.Context, AL, F));
John McCall86bc21f2011-03-02 11:33:24 +00002577}
2578
Erich Keanee891aa92018-07-13 15:07:47 +00002579static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002580 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002581 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002582 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002583 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2584 return;
2585 }
2586 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002587 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002588 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002589 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002590 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2591 return;
2592 }
2593 }
2594 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002595 // It is okay to include this attribute on properties, e.g.:
2596 //
2597 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2598 //
2599 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002600 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002601 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002602 }
Erich Keane6a24e802019-09-13 17:39:31 +00002603 D->addAttr(::new (S.Context) ObjCNSObjectAttr(S.Context, AL));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002604}
2605
Erich Keanee891aa92018-07-13 15:07:47 +00002606static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002607 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002608 QualType T = TD->getUnderlyingType();
2609 if (!T->isObjCObjectPointerType()) {
2610 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2611 return;
2612 }
2613 } else {
2614 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2615 return;
2616 }
Erich Keane6a24e802019-09-13 17:39:31 +00002617 D->addAttr(::new (S.Context) ObjCIndependentClassAttr(S.Context, AL));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002618}
2619
Erich Keanee891aa92018-07-13 15:07:47 +00002620static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002621 if (!AL.isArgIdent(0)) {
2622 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002623 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002624 return;
2625 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002626
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002627 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002628 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002629 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002630 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002631 return;
2632 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002633
Erich Keane6a24e802019-09-13 17:39:31 +00002634 D->addAttr(::new (S.Context) BlocksAttr(S.Context, AL, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002635}
2636
Erich Keanee891aa92018-07-13 15:07:47 +00002637static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002638 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002639 if (AL.getNumArgs() > 0) {
2640 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002641 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002642 if (E->isTypeDependent() || E->isValueDependent() ||
2643 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002644 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002645 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002646 return;
2647 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002648
John McCallb46f2872011-09-09 07:56:05 +00002649 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002650 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002651 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002652 return;
2653 }
John McCallb46f2872011-09-09 07:56:05 +00002654
2655 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002656 }
2657
Aaron Ballman18a78382013-11-21 00:28:23 +00002658 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002659 if (AL.getNumArgs() > 1) {
2660 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002661 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002662 if (E->isTypeDependent() || E->isValueDependent() ||
2663 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002664 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002665 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002666 return;
2667 }
2668 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002669
John McCallb46f2872011-09-09 07:56:05 +00002670 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002671 // FIXME: This error message could be improved, it would be nice
2672 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002673 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002674 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002675 return;
2676 }
2677 }
2678
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002679 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002680 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002681 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002682 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002683 return;
2684 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002685
Chris Lattner9363e312009-03-17 23:03:47 +00002686 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002687 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002688 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002689 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002690 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002691 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002692 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002693 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002694 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002695 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002696 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002697 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002698 return;
2699 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002700 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002701 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002702 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002703 const FunctionType *FT = Ty->isFunctionPointerType()
2704 ? D->getFunctionType()
Simon Pilgrim237d0af2019-10-04 15:02:46 +00002705 : Ty->castAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002706 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002707 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002708 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002709 return;
2710 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002711 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002712 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002713 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002714 return;
2715 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002716 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002717 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002718 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002719 return;
2720 }
Erich Keane6a24e802019-09-13 17:39:31 +00002721 D->addAttr(::new (S.Context) SentinelAttr(S.Context, AL, sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002722}
2723
Erich Keanee891aa92018-07-13 15:07:47 +00002724static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002725 if (D->getFunctionType() &&
Erich Keane46441fd2019-07-25 15:10:56 +00002726 D->getFunctionType()->getReturnType()->isVoidType() &&
2727 !isa<CXXConstructorDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002728 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002729 return;
2730 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002731 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002732 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002733 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002734 return;
2735 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002736
Aaron Ballman3bef0142019-07-20 07:56:34 +00002737 StringRef Str;
2738 if ((AL.isCXX11Attribute() || AL.isC2xAttribute()) && !AL.getScopeName()) {
2739 // If this is spelled as the standard C++17 attribute, but not in C++17,
2740 // warn about using it as an extension. If there are attribute arguments,
2741 // then claim it's a C++2a extension instead.
2742 // FIXME: If WG14 does not seem likely to adopt the same feature, add an
2743 // extension warning for C2x mode.
2744 const LangOptions &LO = S.getLangOpts();
2745 if (AL.getNumArgs() == 1) {
2746 if (LO.CPlusPlus && !LO.CPlusPlus2a)
2747 S.Diag(AL.getLoc(), diag::ext_cxx2a_attr) << AL;
2748
2749 // Since this this is spelled [[nodiscard]], get the optional string
2750 // literal. If in C++ mode, but not in C++2a mode, diagnose as an
2751 // extension.
2752 // FIXME: C2x should support this feature as well, even as an extension.
2753 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
2754 return;
2755 } else if (LO.CPlusPlus && !LO.CPlusPlus17)
2756 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
2757 }
Aaron Ballmane7964782016-03-07 22:44:55 +00002758
Erich Keane6a24e802019-09-13 17:39:31 +00002759 D->addAttr(::new (S.Context) WarnUnusedResultAttr(S.Context, AL, Str));
Chris Lattner237f2752009-02-14 07:37:35 +00002760}
2761
Erich Keanee891aa92018-07-13 15:07:47 +00002762static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002763 // weak_import only applies to variable & function declarations.
2764 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002765 if (!D->canBeWeakImported(isDef)) {
2766 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002767 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002768 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002769 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002770 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002771 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002772 // Nothing to warn about here.
2773 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002774 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002775 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002776
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002777 return;
2778 }
2779
Erich Keane6a24e802019-09-13 17:39:31 +00002780 D->addAttr(::new (S.Context) WeakImportAttr(S.Context, AL));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002781}
2782
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002783// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002784template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002785static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002786 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002787 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002788 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002789 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2790 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002791 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002792 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002793 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002794 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002795 return;
2796 }
2797 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002798
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002799 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2800 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2801 Existing->getYDim() == WGSize[1] &&
2802 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002803 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002804
Erich Keane6a24e802019-09-13 17:39:31 +00002805 D->addAttr(::new (S.Context)
2806 WorkGroupAttr(S.Context, AL, WGSize[0], WGSize[1], WGSize[2]));
Nate Begemanf2758702009-06-26 06:32:41 +00002807}
2808
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002809// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002810static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002811 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002812 const Expr *E = AL.getArgAsExpr(0);
2813 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002814 return;
2815 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002816 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002817 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002818 return;
2819 }
2820
2821 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2822 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2823 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002824 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002825
Erich Keane6a24e802019-09-13 17:39:31 +00002826 D->addAttr(::new (S.Context)
2827 OpenCLIntelReqdSubGroupSizeAttr(S.Context, AL, SGSize));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002828}
2829
Erich Keanee891aa92018-07-13 15:07:47 +00002830static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002831 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002832 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002833 return;
2834 }
2835
Craig Topperc3ec1492014-05-26 06:22:03 +00002836 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002837 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002838 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002839
2840 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2841 (ParmType->isBooleanType() ||
2842 !ParmType->isIntegralType(S.getASTContext()))) {
Matthias Gehred293cbd2019-07-25 17:50:51 +00002843 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument) << 3 << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002844 return;
2845 }
2846
Aaron Ballmana9e05402013-12-02 22:16:55 +00002847 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002848 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002849 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002850 return;
2851 }
2852 }
2853
Erich Keane6a24e802019-09-13 17:39:31 +00002854 D->addAttr(::new (S.Context) VecTypeHintAttr(S.Context, AL, ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002855}
2856
Erich Keane6a24e802019-09-13 17:39:31 +00002857SectionAttr *Sema::mergeSectionAttr(Decl *D, const AttributeCommonInfo &CI,
2858 StringRef Name) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002859 // Explicit or partial specializations do not inherit
2860 // the section attribute from the primary template.
2861 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keane6a24e802019-09-13 17:39:31 +00002862 if (CI.getAttributeSpellingListIndex() == SectionAttr::Declspec_allocate &&
Erich Keane7963e8b2018-07-18 20:04:48 +00002863 FD->isFunctionTemplateSpecialization())
2864 return nullptr;
2865 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002866 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2867 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002868 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002869 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2870 << 1 /*section*/;
Erich Keane6a24e802019-09-13 17:39:31 +00002871 Diag(CI.getLoc(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002872 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002873 }
Erich Keane6a24e802019-09-13 17:39:31 +00002874 return ::new (Context) SectionAttr(Context, CI, Name);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002875}
2876
Reid Kleckner2a133222015-03-04 23:39:17 +00002877bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2878 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2879 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002880 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002881 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002882 return false;
2883 }
2884 return true;
2885}
2886
Erich Keanee891aa92018-07-13 15:07:47 +00002887static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002888 // Make sure that there is a string literal as the sections's single
2889 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002890 StringRef Str;
2891 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002892 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002893 return;
Mike Stump11289f42009-09-09 15:08:12 +00002894
Reid Kleckner2a133222015-03-04 23:39:17 +00002895 if (!S.checkSectionName(LiteralLoc, Str))
2896 return;
2897
Chris Lattner30ba6742009-08-10 19:03:04 +00002898 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002899 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002900 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002901 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002902 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002903 return;
2904 }
Mike Stump11289f42009-09-09 15:08:12 +00002905
Erich Keane6a24e802019-09-13 17:39:31 +00002906 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002907 if (NewAttr)
2908 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002909}
2910
Nico Weber98016212019-07-09 00:02:23 +00002911// This is used for `__declspec(code_seg("segname"))` on a decl.
2912// `#pragma code_seg("segname")` uses checkSectionName() instead.
2913static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc,
2914 StringRef CodeSegName) {
2915 std::string Error =
2916 S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
Erich Keane7963e8b2018-07-18 20:04:48 +00002917 if (!Error.empty()) {
Nico Weber98016212019-07-09 00:02:23 +00002918 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
2919 << Error << 0 /*'code-seg'*/;
Erich Keane7963e8b2018-07-18 20:04:48 +00002920 return false;
2921 }
Nico Weber98016212019-07-09 00:02:23 +00002922
Erich Keane7963e8b2018-07-18 20:04:48 +00002923 return true;
2924}
2925
Erich Keane6a24e802019-09-13 17:39:31 +00002926CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, const AttributeCommonInfo &CI,
2927 StringRef Name) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002928 // Explicit or partial specializations do not inherit
2929 // the code_seg attribute from the primary template.
2930 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2931 if (FD->isFunctionTemplateSpecialization())
2932 return nullptr;
2933 }
2934 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
2935 if (ExistingAttr->getName() == Name)
2936 return nullptr;
2937 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2938 << 0 /*codeseg*/;
Erich Keane6a24e802019-09-13 17:39:31 +00002939 Diag(CI.getLoc(), diag::note_previous_attribute);
Erich Keane7963e8b2018-07-18 20:04:48 +00002940 return nullptr;
2941 }
Erich Keane6a24e802019-09-13 17:39:31 +00002942 return ::new (Context) CodeSegAttr(Context, CI, Name);
Erich Keane7963e8b2018-07-18 20:04:48 +00002943}
2944
2945static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2946 StringRef Str;
2947 SourceLocation LiteralLoc;
2948 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
2949 return;
2950 if (!checkCodeSegName(S, LiteralLoc, Str))
2951 return;
2952 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
2953 if (!ExistingAttr->isImplicit()) {
2954 S.Diag(AL.getLoc(),
2955 ExistingAttr->getName() == Str
2956 ? diag::warn_duplicate_codeseg_attribute
2957 : diag::err_conflicting_codeseg_attribute);
2958 return;
2959 }
2960 D->dropAttr<CodeSegAttr>();
2961 }
Erich Keane6a24e802019-09-13 17:39:31 +00002962 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL, Str))
Erich Keane7963e8b2018-07-18 20:04:48 +00002963 D->addAttr(CSA);
2964}
2965
Erich Keane57e15cd2017-07-19 22:06:33 +00002966// Check for things we'd like to warn about. Multiversioning issues are
2967// handled later in the process, once we know how many exist.
2968bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2969 enum FirstParam { Unsupported, Duplicate };
2970 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00002971 for (auto Str : {"tune=", "fpmath="})
2972 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00002973 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2974 << Unsupported << None << Str;
2975
2976 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
2977
2978 if (!ParsedAttrs.Architecture.empty() &&
2979 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
2980 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2981 << Unsupported << Architecture << ParsedAttrs.Architecture;
2982
2983 if (ParsedAttrs.DuplicateArchitecture)
2984 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2985 << Duplicate << None << "arch=";
2986
2987 for (const auto &Feature : ParsedAttrs.Features) {
2988 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
2989 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
2990 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2991 << Unsupported << None << CurFeature;
2992 }
2993
Erich Keane29636aa2018-02-16 17:31:59 +00002994 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002995}
2996
Erich Keanee891aa92018-07-13 15:07:47 +00002997static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00002998 StringRef Str;
2999 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003000 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003001 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003002 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003003
Erich Keane6a24e802019-09-13 17:39:31 +00003004 TargetAttr *NewAttr = ::new (S.Context) TargetAttr(S.Context, AL, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00003005 D->addAttr(NewAttr);
3006}
3007
Erich Keanee891aa92018-07-13 15:07:47 +00003008static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003009 Expr *E = AL.getArgAsExpr(0);
3010 uint32_t VecWidth;
3011 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3012 AL.setInvalid();
3013 return;
3014 }
3015
3016 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3017 if (Existing && Existing->getVectorWidth() != VecWidth) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003018 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Craig Topper74c10e32018-07-09 19:00:16 +00003019 return;
3020 }
3021
Erich Keane6a24e802019-09-13 17:39:31 +00003022 D->addAttr(::new (S.Context) MinVectorWidthAttr(S.Context, AL, VecWidth));
Craig Topper74c10e32018-07-09 19:00:16 +00003023}
3024
Erich Keanee891aa92018-07-13 15:07:47 +00003025static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003026 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003027 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003028 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003029 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003030
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003031 // gcc only allows for simple identifiers. Since we support more than gcc, we
3032 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003033 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003034 if (DRE->hasQualifier())
3035 S.Diag(Loc, diag::warn_cleanup_ext);
3036 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3037 NI = DRE->getNameInfo();
3038 if (!FD) {
3039 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3040 << NI.getName();
3041 return;
3042 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003043 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003044 if (ULE->hasExplicitTemplateArgs())
3045 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003046 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3047 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003048 if (!FD) {
3049 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3050 << NI.getName();
3051 if (ULE->getType() == S.Context.OverloadTy)
3052 S.NoteAllOverloadCandidates(ULE);
3053 return;
3054 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003055 } else {
3056 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003057 return;
3058 }
3059
Anders Carlssond277d792009-01-31 01:16:18 +00003060 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003061 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3062 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003063 return;
3064 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003065
Anders Carlsson723f55d2009-02-07 23:16:50 +00003066 // We're currently more strict than GCC about what function types we accept.
3067 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003068 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003069 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003070 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3071 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003072 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3073 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003074 return;
3075 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003076
Erich Keane6a24e802019-09-13 17:39:31 +00003077 D->addAttr(::new (S.Context) CleanupAttr(S.Context, AL, FD));
Anders Carlssond277d792009-01-31 01:16:18 +00003078}
3079
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003080static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003081 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003082 if (!AL.isArgIdent(0)) {
3083 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003084 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003085 return;
3086 }
3087
3088 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003089 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003090 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3091 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003092 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003093 return;
3094 }
3095
Erich Keane6a24e802019-09-13 17:39:31 +00003096 D->addAttr(::new (S.Context)
3097 EnumExtensibilityAttr(S.Context, AL, ExtensibilityKind));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003098}
3099
Mike Stumpd3bb5572009-07-24 19:02:52 +00003100/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003101/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003102static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003103 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003104 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003105 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003106 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003107
Eric Christopherb64963e2015-08-13 21:34:35 +00003108 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003109 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003110
Eric Christopherb64963e2015-08-13 21:34:35 +00003111 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3112 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003113 !isCFStringType(Ty, S.Context) &&
3114 (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003115 !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003116 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003117 << "a string type" << IdxExpr->getSourceRange()
3118 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003119 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003120 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003121 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003122 if (!isNSStringType(Ty, S.Context) &&
3123 !isCFStringType(Ty, S.Context) &&
3124 (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003125 !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003126 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003127 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003128 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003129 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003130 }
3131
Erich Keane6a24e802019-09-13 17:39:31 +00003132 D->addAttr(::new (S.Context) FormatArgAttr(S.Context, AL, Idx));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003133}
3134
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003135enum FormatAttrKind {
3136 CFStringFormat,
3137 NSStringFormat,
3138 StrftimeFormat,
3139 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003140 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003141 InvalidFormat
3142};
3143
3144/// getFormatAttrKind - Map from format attribute names to supported format
3145/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003146static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003147 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003148 // Check for formats that get handled specially.
3149 .Case("NSString", NSStringFormat)
3150 .Case("CFString", CFStringFormat)
3151 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003152
Mehdi Amini06d367c2016-10-24 20:39:34 +00003153 // Otherwise, check for supported formats.
3154 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3155 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3156 .Case("kprintf", SupportedFormat) // OpenBSD.
3157 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3158 .Case("os_trace", SupportedFormat)
3159 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003160
Mehdi Amini06d367c2016-10-24 20:39:34 +00003161 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3162 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003163}
3164
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003165/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003166/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003167static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003168 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003169 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003170 return;
3171 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003172
Aaron Ballman4a611152013-11-27 16:34:09 +00003173 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003174 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3175 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003176 return;
3177 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003178 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003179 if (S.Context.getAsArrayType(T))
3180 T = S.Context.getBaseElementType(T);
3181 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003182 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3183 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003184 return;
3185 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003186
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003187 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003188 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003189 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3190 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003191 return;
3192 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003193
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003194 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballman52c9ad22019-02-12 13:04:11 +00003195 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003196 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003197 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003198 return;
3199 }
Erich Keane6a24e802019-09-13 17:39:31 +00003200 D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003201}
3202
Erich Keane6a24e802019-09-13 17:39:31 +00003203FormatAttr *Sema::mergeFormatAttr(Decl *D, const AttributeCommonInfo &CI,
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003204 IdentifierInfo *Format, int FormatIdx,
Erich Keane6a24e802019-09-13 17:39:31 +00003205 int FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003206 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003207 for (auto *F : D->specific_attrs<FormatAttr>()) {
3208 if (F->getType() == Format &&
3209 F->getFormatIdx() == FormatIdx &&
3210 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003211 // If we don't have a valid location for this attribute, adopt the
3212 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003213 if (F->getLocation().isInvalid())
Erich Keane6a24e802019-09-13 17:39:31 +00003214 F->setRange(CI.getRange());
Craig Topperc3ec1492014-05-26 06:22:03 +00003215 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003216 }
3217 }
3218
Erich Keane6a24e802019-09-13 17:39:31 +00003219 return ::new (Context) FormatAttr(Context, CI, Format, FormatIdx, FirstArg);
Rafael Espindola92d49452012-05-11 00:36:07 +00003220}
3221
Mike Stumpd3bb5572009-07-24 19:02:52 +00003222/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003223/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003224static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003225 if (!AL.isArgIdent(0)) {
3226 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003227 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003228 return;
3229 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003230
Chandler Carruth743682b2010-11-16 08:35:43 +00003231 // In C++ the implicit 'this' function parameter also counts, and they are
3232 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003233 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003234 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003235
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003236 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003237 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003238
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003239 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003240 // If we've modified the string name, we need a new identifier for it.
3241 II = &S.Context.Idents.get(Format);
3242 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003243
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003244 // Check for supported formats.
3245 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003246
Chris Lattner12161d32010-03-22 21:08:50 +00003247 if (Kind == IgnoredFormat)
3248 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003249
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003250 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003251 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003252 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003253 return;
3254 }
3255
3256 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003257 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003258 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003259 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003260 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003261
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003262 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003263 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003264 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003265 return;
3266 }
3267
3268 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003269 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003270
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003271 if (HasImplicitThisParam) {
3272 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003273 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003274 diag::err_format_attribute_implicit_this_format_string)
3275 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003276 return;
3277 }
3278 ArgIdx--;
3279 }
Mike Stump11289f42009-09-09 15:08:12 +00003280
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003281 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003282 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003283
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003284 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003285 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003286 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003287 << "a CFString" << IdxExpr->getSourceRange()
3288 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003289 return;
3290 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003291 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003292 // FIXME: do we need to check if the type is NSString*? What are the
3293 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003294 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003295 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003296 << "an NSString" << IdxExpr->getSourceRange()
3297 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003298 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003299 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003300 } else if (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003301 !Ty->castAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003302 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003303 << "a string type" << IdxExpr->getSourceRange()
3304 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003305 return;
3306 }
3307
3308 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003309 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003310 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003311 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003312 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003313
3314 // check if the function is variadic if the 3rd argument non-zero
3315 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003316 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003317 ++NumArgs; // +1 for ...
3318 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003319 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003320 return;
3321 }
3322 }
3323
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003324 // strftime requires FirstArg to be 0 because it doesn't read from any
3325 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003326 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003327 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003328 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003329 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003330 return;
3331 }
3332 // if 0 it disables parameter checking (to use with e.g. va_list)
3333 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003334 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003335 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003336 return;
3337 }
3338
Erich Keane6a24e802019-09-13 17:39:31 +00003339 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL, II, Idx, FirstArg);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003340 if (NewAttr)
3341 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003342}
3343
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003344/// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
3345static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3346 // The index that identifies the callback callee is mandatory.
3347 if (AL.getNumArgs() == 0) {
3348 S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee)
3349 << AL.getRange();
3350 return;
3351 }
3352
3353 bool HasImplicitThisParam = isInstanceMethod(D);
3354 int32_t NumArgs = getFunctionOrMethodNumParams(D);
3355
3356 FunctionDecl *FD = D->getAsFunction();
3357 assert(FD && "Expected a function declaration!");
3358
3359 llvm::StringMap<int> NameIdxMapping;
3360 NameIdxMapping["__"] = -1;
3361
3362 NameIdxMapping["this"] = 0;
3363
3364 int Idx = 1;
3365 for (const ParmVarDecl *PVD : FD->parameters())
3366 NameIdxMapping[PVD->getName()] = Idx++;
3367
3368 auto UnknownName = NameIdxMapping.end();
3369
3370 SmallVector<int, 8> EncodingIndices;
3371 for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) {
3372 SourceRange SR;
3373 int32_t ArgIdx;
3374
3375 if (AL.isArgIdent(I)) {
3376 IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
3377 auto It = NameIdxMapping.find(IdLoc->Ident->getName());
3378 if (It == UnknownName) {
3379 S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown)
3380 << IdLoc->Ident << IdLoc->Loc;
3381 return;
3382 }
3383
3384 SR = SourceRange(IdLoc->Loc);
3385 ArgIdx = It->second;
3386 } else if (AL.isArgExpr(I)) {
3387 Expr *IdxExpr = AL.getArgAsExpr(I);
3388
3389 // If the expression is not parseable as an int32_t we have a problem.
3390 if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1,
3391 false)) {
3392 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3393 << AL << (I + 1) << IdxExpr->getSourceRange();
3394 return;
3395 }
3396
3397 // Check oob, excluding the special values, 0 and -1.
3398 if (ArgIdx < -1 || ArgIdx > NumArgs) {
3399 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3400 << AL << (I + 1) << IdxExpr->getSourceRange();
3401 return;
3402 }
3403
3404 SR = IdxExpr->getSourceRange();
3405 } else {
3406 llvm_unreachable("Unexpected ParsedAttr argument type!");
3407 }
3408
3409 if (ArgIdx == 0 && !HasImplicitThisParam) {
3410 S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
3411 << (I + 1) << SR;
3412 return;
3413 }
3414
3415 // Adjust for the case we do not have an implicit "this" parameter. In this
3416 // case we decrease all positive values by 1 to get LLVM argument indices.
3417 if (!HasImplicitThisParam && ArgIdx > 0)
3418 ArgIdx -= 1;
3419
3420 EncodingIndices.push_back(ArgIdx);
3421 }
3422
3423 int CalleeIdx = EncodingIndices.front();
3424 // Check if the callee index is proper, thus not "this" and not "unknown".
Johannes Doerferte068d052019-01-21 14:23:46 +00003425 // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam"
3426 // is false and positive if "HasImplicitThisParam" is true.
3427 if (CalleeIdx < (int)HasImplicitThisParam) {
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003428 S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee)
3429 << AL.getRange();
3430 return;
3431 }
3432
3433 // Get the callee type, note the index adjustment as the AST doesn't contain
3434 // the this type (which the callee cannot reference anyway!).
3435 const Type *CalleeType =
3436 getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam)
3437 .getTypePtr();
3438 if (!CalleeType || !CalleeType->isFunctionPointerType()) {
3439 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3440 << AL.getRange();
3441 return;
3442 }
3443
3444 const Type *CalleeFnType =
3445 CalleeType->getPointeeType()->getUnqualifiedDesugaredType();
3446
3447 // TODO: Check the type of the callee arguments.
3448
3449 const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
3450 if (!CalleeFnProtoType) {
3451 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3452 << AL.getRange();
3453 return;
3454 }
3455
3456 if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
3457 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3458 << AL << (unsigned)(EncodingIndices.size() - 1);
3459 return;
3460 }
3461
3462 if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
3463 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3464 << AL << (unsigned)(EncodingIndices.size() - 1);
3465 return;
3466 }
3467
3468 if (CalleeFnProtoType->isVariadic()) {
3469 S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
3470 return;
3471 }
3472
3473 // Do not allow multiple callback attributes.
3474 if (D->hasAttr<CallbackAttr>()) {
3475 S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
3476 return;
3477 }
3478
3479 D->addAttr(::new (S.Context) CallbackAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00003480 S.Context, AL, EncodingIndices.data(), EncodingIndices.size()));
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003481}
3482
Erich Keanee891aa92018-07-13 15:07:47 +00003483static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003484 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003485 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003486 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003487 if (TD && TD->getUnderlyingType()->isUnionType())
3488 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3489 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003490 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003491
3492 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003493 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3494 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003495 return;
3496 }
3497
John McCallf937c022011-10-07 06:10:15 +00003498 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003499 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003500 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003501 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003502 return;
3503 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003504
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003505 RecordDecl::field_iterator Field = RD->field_begin(),
3506 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003507 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003508 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003509 return;
3510 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003511
David Blaikie40ed2972012-06-06 20:45:41 +00003512 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003513 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003514 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003515 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003516 diag::warn_transparent_union_attribute_floating)
3517 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003518 return;
3519 }
3520
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003521 if (FirstType->isIncompleteType())
3522 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003523 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3524 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3525 for (; Field != FieldEnd; ++Field) {
3526 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003527 if (FieldType->isIncompleteType())
3528 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003529 // FIXME: this isn't fully correct; we also need to test whether the
3530 // members of the union would all have the same calling convention as the
3531 // first member of the union. Checking just the size and alignment isn't
3532 // sufficient (consider structs passed on the stack instead of in registers
3533 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003534 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003535 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003536 // Warn if we drop the attribute.
3537 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003538 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003539 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003540 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003541 diag::warn_transparent_union_attribute_field_size_align)
3542 << isSize << Field->getDeclName() << FieldBits;
3543 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003544 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003545 diag::note_transparent_union_first_field_size_align)
3546 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003547 return;
3548 }
3549 }
3550
Erich Keane6a24e802019-09-13 17:39:31 +00003551 RD->addAttr(::new (S.Context) TransparentUnionAttr(S.Context, AL));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003552}
3553
Erich Keanee891aa92018-07-13 15:07:47 +00003554static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003555 // Make sure that there is a string literal as the annotation's single
3556 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003557 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003558 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003559 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003560
3561 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003562 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3563 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003564 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003565 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003566
Erich Keane6a24e802019-09-13 17:39:31 +00003567 D->addAttr(::new (S.Context) AnnotateAttr(S.Context, AL, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003568}
3569
Erich Keanee891aa92018-07-13 15:07:47 +00003570static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00003571 S.AddAlignValueAttr(D, AL, AL.getArgAsExpr(0));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003572}
3573
Erich Keane6a24e802019-09-13 17:39:31 +00003574void Sema::AddAlignValueAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E) {
3575 AlignValueAttr TmpAttr(Context, CI, E);
3576 SourceLocation AttrLoc = CI.getLoc();
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003577
3578 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003579 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003580 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003581 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003582 T = VD->getType();
3583 else
3584 llvm_unreachable("Unknown decl type for align_value");
3585
3586 if (!T->isDependentType() && !T->isAnyPointerType() &&
3587 !T->isReferenceType() && !T->isMemberPointerType()) {
3588 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3589 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3590 return;
3591 }
3592
3593 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003594 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003595 ExprResult ICE
3596 = VerifyIntegerConstantExpression(E, &Alignment,
3597 diag::err_align_value_attribute_argument_not_int,
3598 /*AllowFold*/ false);
3599 if (ICE.isInvalid())
3600 return;
3601
3602 if (!Alignment.isPowerOf2()) {
3603 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3604 << E->getSourceRange();
3605 return;
3606 }
3607
Erich Keane6a24e802019-09-13 17:39:31 +00003608 D->addAttr(::new (Context) AlignValueAttr(Context, CI, ICE.get()));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003609 return;
3610 }
3611
3612 // Save dependent expressions in the AST to be instantiated.
Erich Keane6a24e802019-09-13 17:39:31 +00003613 D->addAttr(::new (Context) AlignValueAttr(Context, CI, E));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003614}
3615
Erich Keanee891aa92018-07-13 15:07:47 +00003616static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003617 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003618 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003619 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003620 return;
3621 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003622
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003623 if (AL.getNumArgs() == 0) {
Erich Keane6a24e802019-09-13 17:39:31 +00003624 D->addAttr(::new (S.Context) AlignedAttr(S.Context, AL, true, nullptr));
Richard Smith848e1f12013-02-01 08:12:08 +00003625 return;
3626 }
3627
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003628 Expr *E = AL.getArgAsExpr(0);
3629 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3630 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003631 diag::err_pack_expansion_without_parameter_packs);
3632 return;
3633 }
3634
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003635 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003636 return;
3637
Erich Keane6a24e802019-09-13 17:39:31 +00003638 S.AddAlignedAttr(D, AL, E, AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003639}
3640
Erich Keane6a24e802019-09-13 17:39:31 +00003641void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
3642 bool IsPackExpansion) {
3643 AlignedAttr TmpAttr(Context, CI, true, E);
3644 SourceLocation AttrLoc = CI.getLoc();
Richard Smith848e1f12013-02-01 08:12:08 +00003645
Richard Smith1dba27c2013-01-29 09:02:09 +00003646 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003647 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003648 // C++11 [dcl.align]p1:
3649 // An alignment-specifier may be applied to a variable or to a class
3650 // data member, but it shall not be applied to a bit-field, a function
3651 // parameter, the formal parameter of a catch clause, or a variable
3652 // declared with the register storage class specifier. An
3653 // alignment-specifier may also be applied to the declaration of a class
3654 // or enumeration type.
3655 // C11 6.7.5/2:
3656 // An alignment attribute shall not be specified in a declaration of
3657 // a typedef, or a bit-field, or a function, or a parameter, or an
3658 // object declared with the register storage-class specifier.
3659 int DiagKind = -1;
3660 if (isa<ParmVarDecl>(D)) {
3661 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003662 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003663 if (VD->getStorageClass() == SC_Register)
3664 DiagKind = 1;
3665 if (VD->isExceptionVariable())
3666 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003667 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003668 if (FD->isBitField())
3669 DiagKind = 3;
3670 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003671 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003672 << (TmpAttr.isC11() ? ExpectedVariableOrField
3673 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003674 return;
3675 }
3676 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003677 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003678 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003679 return;
3680 }
3681 }
3682
Richard Smith90ae9672018-06-20 23:36:55 +00003683 if (E->isValueDependent()) {
3684 // We can't support a dependent alignment on a non-dependent type,
3685 // because we have no way to model that a type is "alignment-dependent"
3686 // but not dependent in any other way.
3687 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3688 if (!TND->getUnderlyingType()->isDependentType()) {
3689 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3690 << E->getSourceRange();
3691 return;
3692 }
3693 }
3694
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003695 // Save dependent expressions in the AST to be instantiated.
Erich Keane6a24e802019-09-13 17:39:31 +00003696 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, E);
Richard Smith44c247f2013-02-22 08:32:16 +00003697 AA->setPackExpansion(IsPackExpansion);
3698 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003699 return;
3700 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003701
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003702 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003703 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003704 ExprResult ICE
3705 = VerifyIntegerConstantExpression(E, &Alignment,
3706 diag::err_aligned_attribute_argument_not_int,
3707 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003708 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003709 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003710
David Majnemer0be6bd02015-07-26 09:02:21 +00003711 uint64_t AlignVal = Alignment.getZExtValue();
3712
Richard Smith848e1f12013-02-01 08:12:08 +00003713 // C++11 [dcl.align]p2:
3714 // -- if the constant expression evaluates to zero, the alignment
3715 // specifier shall have no effect
3716 // C11 6.7.5p6:
3717 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003718 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003719 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003720 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3721 << E->getSourceRange();
3722 return;
3723 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003724 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003725
David Majnemerabecae72014-02-12 20:36:10 +00003726 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003727 unsigned MaxValidAlignment =
3728 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3729 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003730 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003731 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3732 << E->getSourceRange();
3733 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003734 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003735
David Majnemer0be6bd02015-07-26 09:02:21 +00003736 if (Context.getTargetInfo().isTLSSupported()) {
3737 unsigned MaxTLSAlign =
3738 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3739 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003740 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003741 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3742 VD->getTLSKind() != VarDecl::TLS_None) {
3743 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3744 << (unsigned)AlignVal << VD << MaxTLSAlign;
3745 return;
3746 }
3747 }
3748
Erich Keane6a24e802019-09-13 17:39:31 +00003749 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, ICE.get());
Richard Smith44c247f2013-02-22 08:32:16 +00003750 AA->setPackExpansion(IsPackExpansion);
3751 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003752}
3753
Erich Keane6a24e802019-09-13 17:39:31 +00003754void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI,
3755 TypeSourceInfo *TS, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003756 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003757 // FIXME: Perform checking of type validity
Erich Keane6a24e802019-09-13 17:39:31 +00003758 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, false, TS);
Richard Smith44c247f2013-02-22 08:32:16 +00003759 AA->setPackExpansion(IsPackExpansion);
3760 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003761}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003762
Richard Smith848e1f12013-02-01 08:12:08 +00003763void Sema::CheckAlignasUnderalignment(Decl *D) {
3764 assert(D->hasAttrs() && "no attributes on decl");
3765
David Majnemer475b25e2015-01-21 10:54:38 +00003766 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003767 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003768 UnderlyingTy = DiagTy = VD->getType();
3769 } else {
3770 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003771 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003772 UnderlyingTy = ED->getIntegerType();
3773 }
3774 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003775 return;
3776
3777 // C++11 [dcl.align]p5, C11 6.7.5/4:
3778 // The combined effect of all alignment attributes in a declaration shall
3779 // not specify an alignment that is less strict than the alignment that
3780 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003781 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003782 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003783 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003784 if (I->isAlignmentDependent())
3785 return;
3786 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003787 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003788 Align = std::max(Align, I->getAlignment(Context));
3789 }
3790
3791 if (AlignasAttr && Align) {
3792 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003793 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003794 if (NaturalAlign > RequestedAlign)
3795 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003796 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003797 }
3798}
3799
David Majnemer2c4e00a2014-01-29 22:07:36 +00003800bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003801 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003802 MSInheritanceAttr::Spelling SemanticSpelling) {
3803 assert(RD->hasDefinition() && "RD has no definition!");
3804
David Majnemer98c9ee22014-02-07 00:43:07 +00003805 // We may not have seen base specifiers or any virtual methods yet. We will
3806 // have to wait until the record is defined to catch any mismatches.
3807 if (!RD->getDefinition()->isCompleteDefinition())
3808 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003809
David Majnemer98c9ee22014-02-07 00:43:07 +00003810 // The unspecified model never matches what a definition could need.
3811 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3812 return false;
3813
David Majnemer4bb09802014-02-10 19:50:15 +00003814 if (BestCase) {
3815 if (RD->calculateInheritanceModel() == SemanticSpelling)
3816 return false;
3817 } else {
3818 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3819 return false;
3820 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003821
3822 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3823 << 0 /*definition*/;
3824 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3825 << RD->getNameAsString();
3826 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003827}
3828
Alexey Bataevf278eb12015-11-19 10:13:11 +00003829/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3830/// attribute.
3831static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3832 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003833 IntegerMode = true;
3834 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003835 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003836 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003837 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003838 case 'Q':
3839 DestWidth = 8;
3840 break;
3841 case 'H':
3842 DestWidth = 16;
3843 break;
3844 case 'S':
3845 DestWidth = 32;
3846 break;
3847 case 'D':
3848 DestWidth = 64;
3849 break;
3850 case 'X':
3851 DestWidth = 96;
3852 break;
3853 case 'T':
3854 DestWidth = 128;
3855 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003856 }
3857 if (Str[1] == 'F') {
3858 IntegerMode = false;
3859 } else if (Str[1] == 'C') {
3860 IntegerMode = false;
3861 ComplexMode = true;
3862 } else if (Str[1] != 'I') {
3863 DestWidth = 0;
3864 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003865 break;
3866 case 4:
3867 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3868 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003869 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003870 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003871 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003872 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003873 break;
3874 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003875 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003876 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003877 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003878 case 11:
3879 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003880 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003881 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003882 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003883}
3884
3885/// handleModeAttr - This attribute modifies the width of a decl with primitive
3886/// type.
3887///
3888/// Despite what would be logical, the mode attribute is a decl attribute, not a
3889/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3890/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00003891static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003892 // This attribute isn't documented, but glibc uses it. It changes
3893 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003894 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003895 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
3896 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003897 return;
3898 }
3899
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003900 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003901
Erich Keane6a24e802019-09-13 17:39:31 +00003902 S.AddModeAttr(D, AL, Name);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003903}
3904
Erich Keane6a24e802019-09-13 17:39:31 +00003905void Sema::AddModeAttr(Decl *D, const AttributeCommonInfo &CI,
3906 IdentifierInfo *Name, bool InInstantiation) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003907 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003908 normalizeName(Str);
Erich Keane6a24e802019-09-13 17:39:31 +00003909 SourceLocation AttrLoc = CI.getLoc();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003910
3911 unsigned DestWidth = 0;
3912 bool IntegerMode = true;
3913 bool ComplexMode = false;
3914 llvm::APInt VectorSize(64, 0);
3915 if (Str.size() >= 4 && Str[0] == 'V') {
3916 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3917 size_t StrSize = Str.size();
3918 size_t VectorStringLength = 0;
3919 while ((VectorStringLength + 1) < StrSize &&
3920 isdigit(Str[VectorStringLength + 1]))
3921 ++VectorStringLength;
3922 if (VectorStringLength &&
3923 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3924 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003925 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003926 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003927 // Avoid duplicate warning from template instantiation.
3928 if (!InInstantiation)
3929 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003930 } else {
3931 VectorSize = 0;
3932 }
3933 }
3934
3935 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003936 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3937
3938 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3939 // and friends, at least with glibc.
3940 // FIXME: Make sure floating-point mappings are accurate
3941 // FIXME: Support XF and TF types
3942 if (!DestWidth) {
3943 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3944 return;
3945 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003946
3947 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003948 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003949 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003950 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003951 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3952 // Try to get type from enum declaration, default to int.
3953 OldTy = ED->getIntegerType();
3954 if (OldTy.isNull())
3955 OldTy = Context.IntTy;
3956 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003957 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003958
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003959 if (OldTy->isDependentType()) {
Erich Keane6a24e802019-09-13 17:39:31 +00003960 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003961 return;
3962 }
3963
Alexey Bataev326057d2015-06-19 07:46:21 +00003964 // Base type can also be a vector type (see PR17453).
3965 // Distinguish between base type and base element type.
3966 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003967 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00003968 OldElemTy = VT->getElementType();
3969
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003970 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3971 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3972 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3973 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3974 VectorSize.getBoolValue()) {
Erich Keane6a24e802019-09-13 17:39:31 +00003975 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << CI.getRange();
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003976 return;
3977 }
3978 bool IntegralOrAnyEnumType =
3979 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3980
3981 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3982 !IntegralOrAnyEnumType)
3983 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003984 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003985 if (!IntegralOrAnyEnumType)
3986 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003987 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003988 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003989 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003990 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003991 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003992 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003993 }
3994
Alexey Bataev326057d2015-06-19 07:46:21 +00003995 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003996
3997 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003998 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3999 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004000 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004001 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004002
Alexey Bataev326057d2015-06-19 07:46:21 +00004003 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004004 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004005 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004006 }
4007
Eli Friedman4735374e2009-03-03 06:41:03 +00004008 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004009 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004010 }
4011
4012 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004013 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004014 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
4015 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004016 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004017 // Complex machine mode does not support base vector types.
4018 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004019 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004020 return;
4021 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004022 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00004023 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004024 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004025 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004026 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00004027 }
4028
4029 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004030 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004031 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004032 }
4033
4034 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004035 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004036 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004037 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004038 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004039 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004040 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004041
Erich Keane6a24e802019-09-13 17:39:31 +00004042 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
Chris Lattneracbc2d22008-06-27 22:18:37 +00004043}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004044
Erich Keanee891aa92018-07-13 15:07:47 +00004045static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004046 D->addAttr(::new (S.Context) NoDebugAttr(S.Context, AL));
Anders Carlsson76187b42009-02-13 06:46:13 +00004047}
4048
Erich Keane6a24e802019-09-13 17:39:31 +00004049AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D,
4050 const AttributeCommonInfo &CI,
4051 const IdentifierInfo *Ident) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004052 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004053 Diag(CI.getLoc(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00004054 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4055 return nullptr;
4056 }
4057
4058 if (D->hasAttr<AlwaysInlineAttr>())
4059 return nullptr;
4060
Erich Keane6a24e802019-09-13 17:39:31 +00004061 return ::new (Context) AlwaysInlineAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004062}
4063
Erich Keane44bacdf2018-08-09 13:21:32 +00004064CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
4065 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004066 return nullptr;
4067
Erich Keane6a24e802019-09-13 17:39:31 +00004068 return ::new (Context) CommonAttr(Context, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004069}
4070
Erich Keane44bacdf2018-08-09 13:21:32 +00004071CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
4072 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
4073 return nullptr;
4074
Erich Keane6a24e802019-09-13 17:39:31 +00004075 return ::new (Context) CommonAttr(Context, AL);
Erich Keane44bacdf2018-08-09 13:21:32 +00004076}
4077
4078InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4079 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004080 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004081 // Attribute applies to Var but not any subclass of it (like ParmVar,
4082 // ImplicitParm or VarTemplateSpecialization).
4083 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004084 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4085 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4086 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004087 return nullptr;
4088 }
4089 // Attribute does not apply to non-static local variables.
4090 if (VD->hasLocalStorage()) {
4091 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4092 return nullptr;
4093 }
4094 }
4095
Erich Keane44bacdf2018-08-09 13:21:32 +00004096 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
4097 return nullptr;
4098
Erich Keane6a24e802019-09-13 17:39:31 +00004099 return ::new (Context) InternalLinkageAttr(Context, AL);
Erich Keane44bacdf2018-08-09 13:21:32 +00004100}
4101InternalLinkageAttr *
4102Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4103 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4104 // Attribute applies to Var but not any subclass of it (like ParmVar,
4105 // ImplicitParm or VarTemplateSpecialization).
4106 if (VD->getKind() != Decl::Var) {
4107 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4108 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4109 : ExpectedVariableOrFunction);
4110 return nullptr;
4111 }
4112 // Attribute does not apply to non-static local variables.
4113 if (VD->hasLocalStorage()) {
4114 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4115 return nullptr;
4116 }
4117 }
4118
4119 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004120 return nullptr;
4121
Erich Keane6a24e802019-09-13 17:39:31 +00004122 return ::new (Context) InternalLinkageAttr(Context, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004123}
4124
Erich Keane6a24e802019-09-13 17:39:31 +00004125MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, const AttributeCommonInfo &CI) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004126 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004127 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'minsize'";
Paul Robinson30e41fb2014-12-15 18:57:28 +00004128 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4129 return nullptr;
4130 }
4131
4132 if (D->hasAttr<MinSizeAttr>())
4133 return nullptr;
4134
Erich Keane6a24e802019-09-13 17:39:31 +00004135 return ::new (Context) MinSizeAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004136}
4137
Zola Bridges826ef592019-01-18 17:20:46 +00004138NoSpeculativeLoadHardeningAttr *Sema::mergeNoSpeculativeLoadHardeningAttr(
4139 Decl *D, const NoSpeculativeLoadHardeningAttr &AL) {
4140 if (checkAttrMutualExclusion<SpeculativeLoadHardeningAttr>(*this, D, AL))
4141 return nullptr;
4142
Erich Keane6a24e802019-09-13 17:39:31 +00004143 return ::new (Context) NoSpeculativeLoadHardeningAttr(Context, AL);
Zola Bridges826ef592019-01-18 17:20:46 +00004144}
4145
Erich Keane6a24e802019-09-13 17:39:31 +00004146OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D,
4147 const AttributeCommonInfo &CI) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004148 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4149 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
Erich Keane6a24e802019-09-13 17:39:31 +00004150 Diag(CI.getLoc(), diag::note_conflicting_attribute);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004151 D->dropAttr<AlwaysInlineAttr>();
4152 }
4153 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4154 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
Erich Keane6a24e802019-09-13 17:39:31 +00004155 Diag(CI.getLoc(), diag::note_conflicting_attribute);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004156 D->dropAttr<MinSizeAttr>();
4157 }
4158
4159 if (D->hasAttr<OptimizeNoneAttr>())
4160 return nullptr;
4161
Erich Keane6a24e802019-09-13 17:39:31 +00004162 return ::new (Context) OptimizeNoneAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004163}
4164
Zola Bridges826ef592019-01-18 17:20:46 +00004165SpeculativeLoadHardeningAttr *Sema::mergeSpeculativeLoadHardeningAttr(
4166 Decl *D, const SpeculativeLoadHardeningAttr &AL) {
4167 if (checkAttrMutualExclusion<NoSpeculativeLoadHardeningAttr>(*this, D, AL))
4168 return nullptr;
4169
Erich Keane6a24e802019-09-13 17:39:31 +00004170 return ::new (Context) SpeculativeLoadHardeningAttr(Context, AL);
Zola Bridges826ef592019-01-18 17:20:46 +00004171}
4172
Erich Keanee891aa92018-07-13 15:07:47 +00004173static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004174 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004175 return;
4176
Erich Keane6a24e802019-09-13 17:39:31 +00004177 if (AlwaysInlineAttr *Inline =
4178 S.mergeAlwaysInlineAttr(D, AL, AL.getAttrName()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004179 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004180}
4181
Erich Keanee891aa92018-07-13 15:07:47 +00004182static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004183 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(D, AL))
Paul Robinson080b1f32015-01-13 18:34:56 +00004184 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004185}
4186
Erich Keanee891aa92018-07-13 15:07:47 +00004187static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004188 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(D, AL))
Paul Robinson080b1f32015-01-13 18:34:56 +00004189 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004190}
4191
Erich Keanee891aa92018-07-13 15:07:47 +00004192static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004193 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004194 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004195 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004196 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004197 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004198 return;
4199 }
Erich Keane6a24e802019-09-13 17:39:31 +00004200 D->addAttr(::new (S.Context) CUDAConstantAttr(S.Context, AL));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004201}
4202
Erich Keanee891aa92018-07-13 15:07:47 +00004203static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004204 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004205 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004206 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004207 // extern __shared__ is only allowed on arrays with no length (e.g.
4208 // "int x[]").
Yaxun Liu97670892018-10-02 17:48:54 +00004209 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004210 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004211 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004212 return;
4213 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004214 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004215 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004216 << S.CurrentCUDATarget())
4217 return;
Erich Keane6a24e802019-09-13 17:39:31 +00004218 D->addAttr(::new (S.Context) CUDASharedAttr(S.Context, AL));
Justin Lebar10411012016-09-30 23:57:30 +00004219}
4220
Erich Keanee891aa92018-07-13 15:07:47 +00004221static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004222 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4223 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004224 return;
4225 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004226 const auto *FD = cast<FunctionDecl>(D);
Michael Liao24337db2019-09-25 16:51:45 +00004227 if (!FD->getReturnType()->isVoidType() &&
4228 !FD->getReturnType()->getAs<AutoType>() &&
4229 !FD->getReturnType()->isInstantiationDependentType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004230 SourceRange RTRange = FD->getReturnTypeSourceRange();
4231 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004232 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004233 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4234 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004235 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004236 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004237 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4238 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004239 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004240 << Method;
4241 return;
4242 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004243 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004244 }
4245 // Only warn for "inline" when compiling for host, to cut down on noise.
4246 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004247 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004248
Erich Keane6a24e802019-09-13 17:39:31 +00004249 D->addAttr(::new (S.Context) CUDAGlobalAttr(S.Context, AL));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004250}
4251
Erich Keanee891aa92018-07-13 15:07:47 +00004252static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004253 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004254 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004255 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004256 return;
4257 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004258
Martin Storsjo71decf82019-09-27 12:25:19 +00004259 if (S.LangOpts.CPlusPlus && Fn->getStorageClass() != SC_Extern)
4260 S.Diag(AL.getLoc(), diag::warn_gnu_inline_cplusplus_without_extern);
4261
Erich Keane6a24e802019-09-13 17:39:31 +00004262 D->addAttr(::new (S.Context) GNUInlineAttr(S.Context, AL));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004263}
4264
Erich Keanee891aa92018-07-13 15:07:47 +00004265static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004266 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004267
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004268 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004269 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4270 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004271 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004272 return;
4273
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004274 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004275 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004276 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004277 return;
4278 }
4279
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004280 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004281 case ParsedAttr::AT_FastCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004282 D->addAttr(::new (S.Context) FastCallAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004283 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004284 case ParsedAttr::AT_StdCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004285 D->addAttr(::new (S.Context) StdCallAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004286 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004287 case ParsedAttr::AT_ThisCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004288 D->addAttr(::new (S.Context) ThisCallAttr(S.Context, AL));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004289 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004290 case ParsedAttr::AT_CDecl:
Erich Keane6a24e802019-09-13 17:39:31 +00004291 D->addAttr(::new (S.Context) CDeclAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004292 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004293 case ParsedAttr::AT_Pascal:
Erich Keane6a24e802019-09-13 17:39:31 +00004294 D->addAttr(::new (S.Context) PascalAttr(S.Context, AL));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004295 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004296 case ParsedAttr::AT_SwiftCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004297 D->addAttr(::new (S.Context) SwiftCallAttr(S.Context, AL));
John McCall477f2bb2016-03-03 06:39:32 +00004298 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004299 case ParsedAttr::AT_VectorCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004300 D->addAttr(::new (S.Context) VectorCallAttr(S.Context, AL));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004301 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004302 case ParsedAttr::AT_MSABI:
Erich Keane6a24e802019-09-13 17:39:31 +00004303 D->addAttr(::new (S.Context) MSABIAttr(S.Context, AL));
Charles Davisb5a214e2013-08-30 04:39:01 +00004304 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004305 case ParsedAttr::AT_SysVABI:
Erich Keane6a24e802019-09-13 17:39:31 +00004306 D->addAttr(::new (S.Context) SysVABIAttr(S.Context, AL));
Charles Davisb5a214e2013-08-30 04:39:01 +00004307 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004308 case ParsedAttr::AT_RegCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004309 D->addAttr(::new (S.Context) RegCallAttr(S.Context, AL));
Erich Keane757d3172016-11-02 18:29:35 +00004310 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004311 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004312 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004313 switch (CC) {
4314 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004315 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004316 break;
4317 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004318 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004319 break;
4320 default:
4321 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004322 }
4323
Erich Keane6a24e802019-09-13 17:39:31 +00004324 D->addAttr(::new (S.Context) PcsAttr(S.Context, AL, PCS));
Derek Schuffa2020962012-10-16 22:30:41 +00004325 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004326 }
Sander de Smalen44a22532018-11-26 16:38:37 +00004327 case ParsedAttr::AT_AArch64VectorPcs:
Erich Keane6a24e802019-09-13 17:39:31 +00004328 D->addAttr(::new (S.Context) AArch64VectorPcsAttr(S.Context, AL));
Sander de Smalen44a22532018-11-26 16:38:37 +00004329 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004330 case ParsedAttr::AT_IntelOclBicc:
Erich Keane6a24e802019-09-13 17:39:31 +00004331 D->addAttr(::new (S.Context) IntelOclBiccAttr(S.Context, AL));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004332 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004333 case ParsedAttr::AT_PreserveMost:
Erich Keane6a24e802019-09-13 17:39:31 +00004334 D->addAttr(::new (S.Context) PreserveMostAttr(S.Context, AL));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004335 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004336 case ParsedAttr::AT_PreserveAll:
Erich Keane6a24e802019-09-13 17:39:31 +00004337 D->addAttr(::new (S.Context) PreserveAllAttr(S.Context, AL));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004338 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004339 default:
4340 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004341 }
4342}
4343
Erich Keanee891aa92018-07-13 15:07:47 +00004344static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004345 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004346 return;
4347
4348 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004349 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004350 StringRef RuleName;
4351
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004352 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004353 return;
4354
4355 // FIXME: Warn if the rule name is unknown. This is tricky because only
4356 // clang-tidy knows about available rules.
4357 DiagnosticIdentifiers.push_back(RuleName);
4358 }
Erich Keane6a24e802019-09-13 17:39:31 +00004359 D->addAttr(::new (S.Context)
4360 SuppressAttr(S.Context, AL, DiagnosticIdentifiers.data(),
4361 DiagnosticIdentifiers.size()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004362}
4363
Matthias Gehred293cbd2019-07-25 17:50:51 +00004364static void handleLifetimeCategoryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4365 TypeSourceInfo *DerefTypeLoc = nullptr;
4366 QualType ParmType;
4367 if (AL.hasParsedType()) {
4368 ParmType = S.GetTypeFromParser(AL.getTypeArg(), &DerefTypeLoc);
4369
4370 unsigned SelectIdx = ~0U;
4371 if (ParmType->isVoidType())
4372 SelectIdx = 0;
4373 else if (ParmType->isReferenceType())
4374 SelectIdx = 1;
4375 else if (ParmType->isArrayType())
4376 SelectIdx = 2;
4377
4378 if (SelectIdx != ~0U) {
4379 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument)
4380 << SelectIdx << AL;
4381 return;
4382 }
4383 }
4384
4385 // To check if earlier decl attributes do not conflict the newly parsed ones
4386 // we always add (and check) the attribute to the cannonical decl.
4387 D = D->getCanonicalDecl();
4388 if (AL.getKind() == ParsedAttr::AT_Owner) {
4389 if (checkAttrMutualExclusion<PointerAttr>(S, D, AL))
4390 return;
4391 if (const auto *OAttr = D->getAttr<OwnerAttr>()) {
4392 const Type *ExistingDerefType = OAttr->getDerefTypeLoc()
4393 ? OAttr->getDerefType().getTypePtr()
4394 : nullptr;
4395 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
4396 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
4397 << AL << OAttr;
4398 S.Diag(OAttr->getLocation(), diag::note_conflicting_attribute);
4399 }
4400 return;
4401 }
Matthias Gehref64f4882019-09-06 08:56:30 +00004402 for (Decl *Redecl : D->redecls()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004403 Redecl->addAttr(::new (S.Context) OwnerAttr(S.Context, AL, DerefTypeLoc));
Matthias Gehref64f4882019-09-06 08:56:30 +00004404 }
Matthias Gehred293cbd2019-07-25 17:50:51 +00004405 } else {
4406 if (checkAttrMutualExclusion<OwnerAttr>(S, D, AL))
4407 return;
4408 if (const auto *PAttr = D->getAttr<PointerAttr>()) {
4409 const Type *ExistingDerefType = PAttr->getDerefTypeLoc()
4410 ? PAttr->getDerefType().getTypePtr()
4411 : nullptr;
4412 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
4413 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
4414 << AL << PAttr;
4415 S.Diag(PAttr->getLocation(), diag::note_conflicting_attribute);
4416 }
4417 return;
4418 }
Matthias Gehref64f4882019-09-06 08:56:30 +00004419 for (Decl *Redecl : D->redecls()) {
4420 Redecl->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00004421 PointerAttr(S.Context, AL, DerefTypeLoc));
Matthias Gehref64f4882019-09-06 08:56:30 +00004422 }
Matthias Gehred293cbd2019-07-25 17:50:51 +00004423 }
4424}
4425
Erich Keanee891aa92018-07-13 15:07:47 +00004426bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004427 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004428 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004429 return true;
4430
Erich Keaneb11ebc52017-09-27 03:20:13 +00004431 if (Attrs.hasProcessingCache()) {
4432 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004433 return false;
4434 }
4435
Erich Keanee891aa92018-07-13 15:07:47 +00004436 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004437 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4438 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004439 return true;
4440 }
4441
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004442 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004443 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004444 case ParsedAttr::AT_CDecl:
4445 CC = CC_C;
4446 break;
4447 case ParsedAttr::AT_FastCall:
4448 CC = CC_X86FastCall;
4449 break;
4450 case ParsedAttr::AT_StdCall:
4451 CC = CC_X86StdCall;
4452 break;
4453 case ParsedAttr::AT_ThisCall:
4454 CC = CC_X86ThisCall;
4455 break;
4456 case ParsedAttr::AT_Pascal:
4457 CC = CC_X86Pascal;
4458 break;
4459 case ParsedAttr::AT_SwiftCall:
4460 CC = CC_Swift;
4461 break;
4462 case ParsedAttr::AT_VectorCall:
4463 CC = CC_X86VectorCall;
4464 break;
Sander de Smalen44a22532018-11-26 16:38:37 +00004465 case ParsedAttr::AT_AArch64VectorPcs:
4466 CC = CC_AArch64VectorCall;
4467 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004468 case ParsedAttr::AT_RegCall:
4469 CC = CC_X86RegCall;
4470 break;
4471 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004472 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004473 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004474 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004475 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004476 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4477 CC_C;
4478 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004479 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004480 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004481 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4482 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004483 return true;
4484 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004485 if (StrRef == "aapcs") {
4486 CC = CC_AAPCS;
4487 break;
4488 } else if (StrRef == "aapcs-vfp") {
4489 CC = CC_AAPCS_VFP;
4490 break;
4491 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004492
Erich Keaneb11ebc52017-09-27 03:20:13 +00004493 Attrs.setInvalid();
4494 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004495 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004496 }
Erich Keanee891aa92018-07-13 15:07:47 +00004497 case ParsedAttr::AT_IntelOclBicc:
4498 CC = CC_IntelOclBicc;
4499 break;
4500 case ParsedAttr::AT_PreserveMost:
4501 CC = CC_PreserveMost;
4502 break;
4503 case ParsedAttr::AT_PreserveAll:
4504 CC = CC_PreserveAll;
4505 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004506 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004507 }
4508
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004509 TargetInfo::CallingConvCheckResult A = TargetInfo::CCCR_OK;
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004510 const TargetInfo &TI = Context.getTargetInfo();
Yaxun Liu785cbd82019-02-27 15:46:29 +00004511 // CUDA functions may have host and/or device attributes which indicate
4512 // their targeted execution environment, therefore the calling convention
4513 // of functions in CUDA should be checked against the target deduced based
4514 // on their host/device attributes.
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004515 if (LangOpts.CUDA) {
Yaxun Liu785cbd82019-02-27 15:46:29 +00004516 auto *Aux = Context.getAuxTargetInfo();
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004517 auto CudaTarget = IdentifyCUDATarget(FD);
4518 bool CheckHost = false, CheckDevice = false;
4519 switch (CudaTarget) {
4520 case CFT_HostDevice:
4521 CheckHost = true;
4522 CheckDevice = true;
4523 break;
4524 case CFT_Host:
4525 CheckHost = true;
4526 break;
4527 case CFT_Device:
4528 case CFT_Global:
4529 CheckDevice = true;
4530 break;
4531 case CFT_InvalidTarget:
4532 llvm_unreachable("unexpected cuda target");
4533 }
4534 auto *HostTI = LangOpts.CUDAIsDevice ? Aux : &TI;
4535 auto *DeviceTI = LangOpts.CUDAIsDevice ? &TI : Aux;
4536 if (CheckHost && HostTI)
4537 A = HostTI->checkCallingConvention(CC);
4538 if (A == TargetInfo::CCCR_OK && CheckDevice && DeviceTI)
4539 A = DeviceTI->checkCallingConvention(CC);
4540 } else {
4541 A = TI.checkCallingConvention(CC);
4542 }
Reid Kleckner4586a192019-07-09 23:17:43 +00004543
4544 switch (A) {
4545 case TargetInfo::CCCR_OK:
4546 break;
4547
4548 case TargetInfo::CCCR_Ignore:
4549 // Treat an ignored convention as if it was an explicit C calling convention
4550 // attribute. For example, __stdcall on Win x64 functions as __cdecl, so
4551 // that command line flags that change the default convention to
4552 // __vectorcall don't affect declarations marked __stdcall.
4553 CC = CC_C;
4554 break;
4555
Sunil Srivastavaf4038e72019-07-19 21:38:34 +00004556 case TargetInfo::CCCR_Error:
4557 Diag(Attrs.getLoc(), diag::error_cconv_unsupported)
4558 << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget;
4559 break;
4560
Reid Kleckner4586a192019-07-09 23:17:43 +00004561 case TargetInfo::CCCR_Warning: {
Sunil Srivastava85d667f2019-07-17 20:41:26 +00004562 Diag(Attrs.getLoc(), diag::warn_cconv_unsupported)
Reid Kleckner4586a192019-07-09 23:17:43 +00004563 << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004564
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004565 // This convention is not valid for the target. Use the default function or
4566 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004567 bool IsCXXMethod = false, IsVariadic = false;
4568 if (FD) {
4569 IsCXXMethod = FD->isCXXInstanceMember();
4570 IsVariadic = FD->isVariadic();
4571 }
4572 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Reid Kleckner4586a192019-07-09 23:17:43 +00004573 break;
4574 }
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004575 }
4576
Erich Keaneb11ebc52017-09-27 03:20:13 +00004577 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004578 return false;
4579}
4580
John McCall477f2bb2016-03-03 06:39:32 +00004581/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004582static bool isValidSwiftContextType(QualType Ty) {
4583 if (!Ty->hasPointerRepresentation())
4584 return Ty->isDependentType();
4585 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004586}
4587
4588/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004589static bool isValidSwiftIndirectResultType(QualType Ty) {
4590 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4591 Ty = PtrType->getPointeeType();
4592 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4593 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004594 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004595 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004596 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004597 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004598}
4599
4600/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004601static bool isValidSwiftErrorResultType(QualType Ty) {
4602 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4603 Ty = PtrType->getPointeeType();
4604 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4605 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004606 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004607 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004608 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004609 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004610 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004611 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004612}
4613
Erich Keane6a24e802019-09-13 17:39:31 +00004614void Sema::AddParameterABIAttr(Decl *D, const AttributeCommonInfo &CI,
4615 ParameterABI abi) {
John McCall477f2bb2016-03-03 06:39:32 +00004616
4617 QualType type = cast<ParmVarDecl>(D)->getType();
4618
4619 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4620 if (existingAttr->getABI() != abi) {
Erich Keane6a24e802019-09-13 17:39:31 +00004621 Diag(CI.getLoc(), diag::err_attributes_are_not_compatible)
4622 << getParameterABISpelling(abi) << existingAttr;
John McCall477f2bb2016-03-03 06:39:32 +00004623 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4624 return;
4625 }
4626 }
4627
4628 switch (abi) {
4629 case ParameterABI::Ordinary:
4630 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4631
4632 case ParameterABI::SwiftContext:
4633 if (!isValidSwiftContextType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004634 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4635 << getParameterABISpelling(abi) << /*pointer to pointer */ 0 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004636 }
Erich Keane6a24e802019-09-13 17:39:31 +00004637 D->addAttr(::new (Context) SwiftContextAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004638 return;
4639
4640 case ParameterABI::SwiftErrorResult:
4641 if (!isValidSwiftErrorResultType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004642 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4643 << getParameterABISpelling(abi) << /*pointer to pointer */ 1 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004644 }
Erich Keane6a24e802019-09-13 17:39:31 +00004645 D->addAttr(::new (Context) SwiftErrorResultAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004646 return;
4647
4648 case ParameterABI::SwiftIndirectResult:
4649 if (!isValidSwiftIndirectResultType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004650 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4651 << getParameterABISpelling(abi) << /*pointer*/ 0 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004652 }
Erich Keane6a24e802019-09-13 17:39:31 +00004653 D->addAttr(::new (Context) SwiftIndirectResultAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004654 return;
4655 }
4656 llvm_unreachable("bad parameter ABI attribute");
4657}
4658
John McCall3882ace2011-01-05 12:14:39 +00004659/// Checks a regparm attribute, returning true if it is ill-formed and
4660/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004661bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004662 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004663 return true;
4664
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004665 if (!checkAttributeNumArgs(*this, AL, 1)) {
4666 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004667 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004668 }
Eli Friedman7044b762009-03-27 21:06:47 +00004669
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004670 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004671 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4672 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4673 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004674 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004675 }
4676
Douglas Gregore8bbc122011-09-02 00:18:52 +00004677 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004678 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004679 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004680 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004681 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004682 }
4683
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004684 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004685 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004686 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004687 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004688 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004689 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004690 }
4691
John McCall3882ace2011-01-05 12:14:39 +00004692 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004693}
4694
Artem Belevichbcec9da2016-06-06 22:54:57 +00004695// Checks whether an argument of launch_bounds attribute is
4696// acceptable, performs implicit conversion to Rvalue, and returns
4697// non-nullptr Expr result on success. Otherwise, it returns nullptr
4698// and may output an error.
4699static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004700 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004701 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004702 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004703 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004704
4705 // Accept template arguments for now as they depend on something else.
4706 // We'll get to check them when they eventually get instantiated.
4707 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004708 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004709
4710 llvm::APSInt I(64);
4711 if (!E->isIntegerConstantExpr(I, S.Context)) {
4712 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004713 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004714 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004715 }
4716 // Make sure we can fit it in 32 bits.
4717 if (!I.isIntN(32)) {
4718 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4719 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004720 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004721 }
4722 if (I < 0)
4723 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004724 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004725
Artem Belevichbcec9da2016-06-06 22:54:57 +00004726 // We may need to perform implicit conversion of the argument.
4727 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4728 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4729 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4730 assert(!ValArg.isInvalid() &&
4731 "Unexpected PerformCopyInitialization() failure.");
4732
4733 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004734}
4735
Erich Keane6a24e802019-09-13 17:39:31 +00004736void Sema::AddLaunchBoundsAttr(Decl *D, const AttributeCommonInfo &CI,
4737 Expr *MaxThreads, Expr *MinBlocks) {
4738 CUDALaunchBoundsAttr TmpAttr(Context, CI, MaxThreads, MinBlocks);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004739 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4740 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004741 return;
4742
Artem Belevichbcec9da2016-06-06 22:54:57 +00004743 if (MinBlocks) {
4744 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4745 if (MinBlocks == nullptr)
4746 return;
4747 }
Artem Belevich7093e402015-04-21 22:55:54 +00004748
Erich Keane6a24e802019-09-13 17:39:31 +00004749 D->addAttr(::new (Context)
4750 CUDALaunchBoundsAttr(Context, CI, MaxThreads, MinBlocks));
Artem Belevich7093e402015-04-21 22:55:54 +00004751}
4752
Erich Keanee891aa92018-07-13 15:07:47 +00004753static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004754 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4755 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004756 return;
4757
Erich Keane6a24e802019-09-13 17:39:31 +00004758 S.AddLaunchBoundsAttr(D, AL, AL.getArgAsExpr(0),
4759 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004760}
4761
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004762static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004763 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004764 if (!AL.isArgIdent(0)) {
4765 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004766 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004767 return;
4768 }
Joel E. Denny81508102018-03-13 14:51:22 +00004769
4770 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004771 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004772 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004773 return;
4774
Joel E. Denny81508102018-03-13 14:51:22 +00004775 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004776 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004777 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004778 return;
4779
Erich Keane6a24e802019-09-13 17:39:31 +00004780 bool IsPointer = AL.getAttrName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004781 if (IsPointer) {
4782 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004783 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4784 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4785 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004786 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004787 }
4788
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004789 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00004790 S.Context, AL, AL.getArgAsIdent(0)->Ident, ArgumentIdx, TypeTagIdx,
4791 IsPointer));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004792}
4793
4794static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004795 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004796 if (!AL.isArgIdent(0)) {
4797 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004798 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004799 return;
4800 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004801
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004802 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004803 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004804
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004805 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004806 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004807 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004808 return;
4809 }
4810
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004811 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004812 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004813 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004814 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004815
Erich Keane6a24e802019-09-13 17:39:31 +00004816 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
4817 S.Context, AL, PointerKind, MatchingCTypeLoc, AL.getLayoutCompatible(),
4818 AL.getMustBeNull()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004819}
4820
Erich Keanee891aa92018-07-13 15:07:47 +00004821static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004822 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004823
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004824 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004825 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004826 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004827 return;
4828
Joel E. Denny81508102018-03-13 14:51:22 +00004829 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
Erich Keane6a24e802019-09-13 17:39:31 +00004830 D->addAttr(::new (S.Context)
4831 XRayLogArgsAttr(S.Context, AL, ArgCount.getSourceIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004832}
4833
Simon Tatham7c11da02019-09-02 15:35:09 +01004834static bool ArmMveAliasValid(unsigned BuiltinID, StringRef AliasName) {
4835 // FIXME: this will be filled in by Tablegen which isn't written yet
4836 return false;
4837}
4838
4839static void handleArmMveAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4840 if (!AL.isArgIdent(0)) {
4841 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4842 << AL << 1 << AANT_ArgumentIdentifier;
4843 return;
4844 }
4845
4846 IdentifierInfo *Ident = AL.getArgAsIdent(0)->Ident;
4847 unsigned BuiltinID = Ident->getBuiltinID();
4848
4849 if (!ArmMveAliasValid(BuiltinID,
4850 cast<FunctionDecl>(D)->getIdentifier()->getName())) {
4851 S.Diag(AL.getLoc(), diag::err_attribute_arm_mve_alias);
4852 return;
4853 }
4854
4855 D->addAttr(::new (S.Context) ArmMveAliasAttr(S.Context, AL, Ident));
4856}
4857
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004858//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004859// Checker-specific attribute handlers.
4860//===----------------------------------------------------------------------===//
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004861static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4862 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004863}
4864
George Karpenkov1657f362018-11-30 02:18:37 +00004865static bool isValidSubjectOfNSAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004866 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004867 QT->isObjCNSObjectType();
John McCalled433932011-01-25 03:31:58 +00004868}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004869
George Karpenkov1657f362018-11-30 02:18:37 +00004870static bool isValidSubjectOfCFAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004871 return QT->isDependentType() || QT->isPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004872 isValidSubjectOfNSAttribute(QT);
John McCalled433932011-01-25 03:31:58 +00004873}
4874
George Karpenkov1657f362018-11-30 02:18:37 +00004875static bool isValidSubjectOfOSAttribute(QualType QT) {
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004876 if (QT->isDependentType())
4877 return true;
4878 QualType PT = QT->getPointeeType();
4879 return !PT.isNull() && PT->getAsCXXRecordDecl() != nullptr;
John McCall3b5a8f52016-03-03 00:10:03 +00004880}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004881
Erich Keane6a24e802019-09-13 17:39:31 +00004882void Sema::AddXConsumedAttr(Decl *D, const AttributeCommonInfo &CI,
George Karpenkov1657f362018-11-30 02:18:37 +00004883 RetainOwnershipKind K,
4884 bool IsTemplateInstantiation) {
4885 ValueDecl *VD = cast<ValueDecl>(D);
4886 switch (K) {
4887 case RetainOwnershipKind::OS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004888 handleSimpleAttributeOrDiagnose<OSConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00004889 *this, VD, CI, isValidSubjectOfOSAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00004890 diag::warn_ns_attribute_wrong_parameter_type,
Erich Keane6a24e802019-09-13 17:39:31 +00004891 /*ExtraArgs=*/CI.getRange(), "os_consumed", /*pointers*/ 1);
George Karpenkov1657f362018-11-30 02:18:37 +00004892 return;
4893 case RetainOwnershipKind::NS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004894 handleSimpleAttributeOrDiagnose<NSConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00004895 *this, VD, CI, isValidSubjectOfNSAttribute(VD->getType()),
John McCall3b5a8f52016-03-03 00:10:03 +00004896
George Karpenkov1657f362018-11-30 02:18:37 +00004897 // These attributes are normally just advisory, but in ARC, ns_consumed
4898 // is significant. Allow non-dependent code to contain inappropriate
4899 // attributes even in ARC, but require template instantiations to be
4900 // set up correctly.
4901 ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount)
4902 ? diag::err_ns_attribute_wrong_parameter_type
4903 : diag::warn_ns_attribute_wrong_parameter_type),
Erich Keane6a24e802019-09-13 17:39:31 +00004904 /*ExtraArgs=*/CI.getRange(), "ns_consumed", /*objc pointers*/ 0);
George Karpenkov1657f362018-11-30 02:18:37 +00004905 return;
4906 case RetainOwnershipKind::CF:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004907 handleSimpleAttributeOrDiagnose<CFConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00004908 *this, VD, CI, isValidSubjectOfCFAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00004909 diag::warn_ns_attribute_wrong_parameter_type,
Erich Keane6a24e802019-09-13 17:39:31 +00004910 /*ExtraArgs=*/CI.getRange(), "cf_consumed", /*pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00004911 return;
4912 }
George Karpenkov1657f362018-11-30 02:18:37 +00004913}
John McCalled433932011-01-25 03:31:58 +00004914
George Karpenkov1657f362018-11-30 02:18:37 +00004915static Sema::RetainOwnershipKind
4916parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) {
4917 switch (AL.getKind()) {
4918 case ParsedAttr::AT_CFConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004919 case ParsedAttr::AT_CFReturnsRetained:
4920 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004921 return Sema::RetainOwnershipKind::CF;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004922 case ParsedAttr::AT_OSConsumesThis:
George Karpenkov1657f362018-11-30 02:18:37 +00004923 case ParsedAttr::AT_OSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004924 case ParsedAttr::AT_OSReturnsRetained:
4925 case ParsedAttr::AT_OSReturnsNotRetained:
4926 case ParsedAttr::AT_OSReturnsRetainedOnZero:
4927 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
George Karpenkov1657f362018-11-30 02:18:37 +00004928 return Sema::RetainOwnershipKind::OS;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004929 case ParsedAttr::AT_NSConsumesSelf:
George Karpenkov1657f362018-11-30 02:18:37 +00004930 case ParsedAttr::AT_NSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004931 case ParsedAttr::AT_NSReturnsRetained:
4932 case ParsedAttr::AT_NSReturnsNotRetained:
4933 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00004934 return Sema::RetainOwnershipKind::NS;
4935 default:
4936 llvm_unreachable("Wrong argument supplied");
4937 }
John McCalled433932011-01-25 03:31:58 +00004938}
4939
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004940bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4941 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004942 return false;
4943
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004944 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4945 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004946 return true;
4947}
4948
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004949/// \return whether the parameter is a pointer to OSObject pointer.
4950static bool isValidOSObjectOutParameter(const Decl *D) {
4951 const auto *PVD = dyn_cast<ParmVarDecl>(D);
4952 if (!PVD)
4953 return false;
4954 QualType QT = PVD->getType();
4955 QualType PT = QT->getPointeeType();
4956 return !PT.isNull() && isValidSubjectOfOSAttribute(PT);
4957}
4958
George Karpenkov1657f362018-11-30 02:18:37 +00004959static void handleXReturnsXRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004960 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004961 QualType ReturnType;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004962 Sema::RetainOwnershipKind K = parsedAttrToRetainOwnershipKind(AL);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004963
George Karpenkov1657f362018-11-30 02:18:37 +00004964 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004965 ReturnType = MD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00004966 } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
4967 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) {
John McCall31168b02011-06-15 23:02:42 +00004968 return; // ignore: was handled as a type attribute
George Karpenkov1657f362018-11-30 02:18:37 +00004969 } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004970 ReturnType = PD->getType();
George Karpenkov1657f362018-11-30 02:18:37 +00004971 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004972 ReturnType = FD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00004973 } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
4974 // Attributes on parameters are used for out-parameters,
4975 // passed as pointers-to-pointers.
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004976 unsigned DiagID = K == Sema::RetainOwnershipKind::CF
4977 ? /*pointer-to-CF-pointer*/2
4978 : /*pointer-to-OSObject-pointer*/3;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004979 ReturnType = Param->getType()->getPointeeType();
4980 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004981 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004982 << AL << DiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004983 return;
4984 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004985 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00004986 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004987 } else {
4988 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004989 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004990 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004991 case ParsedAttr::AT_NSReturnsRetained:
4992 case ParsedAttr::AT_NSReturnsAutoreleased:
4993 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004994 ExpectedDeclKind = ExpectedFunctionOrMethod;
4995 break;
4996
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004997 case ParsedAttr::AT_OSReturnsRetained:
4998 case ParsedAttr::AT_OSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00004999 case ParsedAttr::AT_CFReturnsRetained:
5000 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005001 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
5002 break;
5003 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005004 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005005 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005006 return;
5007 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005008
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005009 bool TypeOK;
5010 bool Cf;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005011 unsigned ParmDiagID = 2; // Pointer-to-CF-pointer
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005012 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00005013 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005014 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005015 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
5016 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00005017 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005018
5019 case ParsedAttr::AT_NSReturnsAutoreleased:
5020 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005021 TypeOK = isValidSubjectOfNSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005022 Cf = false;
John McCalled433932011-01-25 03:31:58 +00005023 break;
5024
Erich Keanee891aa92018-07-13 15:07:47 +00005025 case ParsedAttr::AT_CFReturnsRetained:
5026 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005027 TypeOK = isValidSubjectOfCFAttribute(ReturnType);
5028 Cf = true;
5029 break;
5030
5031 case ParsedAttr::AT_OSReturnsRetained:
5032 case ParsedAttr::AT_OSReturnsNotRetained:
5033 TypeOK = isValidSubjectOfOSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005034 Cf = true;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005035 ParmDiagID = 3; // Pointer-to-OSObject-pointer
John McCalled433932011-01-25 03:31:58 +00005036 break;
5037 }
5038
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005039 if (!TypeOK) {
5040 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00005041 return;
5042
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005043 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005044 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005045 << AL << ParmDiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005046 } else {
5047 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
5048 enum : unsigned {
5049 Function,
5050 Method,
5051 Property
5052 } SubjectKind = Function;
5053 if (isa<ObjCMethodDecl>(D))
5054 SubjectKind = Method;
5055 else if (isa<ObjCPropertyDecl>(D))
5056 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005057 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005058 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005059 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005060 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00005061 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005062
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005063 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005064 default:
David Blaikie83d382b2011-09-23 05:06:16 +00005065 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005066 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005067 handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL);
John McCalled433932011-01-25 03:31:58 +00005068 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005069 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005070 handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005071 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005072 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005073 handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005074 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005075 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005076 handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005077 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005078 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005079 handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL);
5080 return;
5081 case ParsedAttr::AT_OSReturnsRetained:
5082 handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL);
5083 return;
5084 case ParsedAttr::AT_OSReturnsNotRetained:
5085 handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005086 return;
5087 };
5088}
5089
John McCallcf166702011-07-22 08:53:00 +00005090static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005091 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00005092 const int EP_ObjCMethod = 1;
5093 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00005094
Erich Keaneb11ebc52017-09-27 03:20:13 +00005095 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005096 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005097 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00005098 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005099 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005100 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00005101
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00005102 if (!resultType->isReferenceType() &&
5103 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005104 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005105 << SourceRange(loc) << Attrs
5106 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
5107 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00005108
5109 // Drop the attribute.
5110 return;
5111 }
5112
Erich Keane6a24e802019-09-13 17:39:31 +00005113 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(S.Context, Attrs));
John McCallcf166702011-07-22 08:53:00 +00005114}
5115
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005116static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005117 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005118 const auto *Method = cast<ObjCMethodDecl>(D);
5119
5120 const DeclContext *DC = Method->getDeclContext();
5121 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005122 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005123 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005124 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
5125 return;
5126 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005127 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005128 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005129 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005130 return;
5131 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005132
Erich Keane6a24e802019-09-13 17:39:31 +00005133 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(S.Context, Attrs));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005134}
5135
Erich Keanee891aa92018-07-13 15:07:47 +00005136static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005137 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00005138
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005139 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005140 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005141 return;
5142 }
John McCall28592582015-02-01 22:34:06 +00005143
5144 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005145 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00005146 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005147 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00005148 return;
5149 }
5150
5151 // Only allow 'cv void *'.
5152 QualType T = TD->getUnderlyingType();
5153 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005154 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00005155 return;
5156 }
5157 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005158
Erich Keane6a24e802019-09-13 17:39:31 +00005159 D->addAttr(::new (S.Context) ObjCBridgeAttr(S.Context, AL, Parm->Ident));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005160}
5161
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005162static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005163 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005164 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00005165
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005166 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005167 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005168 return;
5169 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005170
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005171 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005172 ObjCBridgeMutableAttr(S.Context, AL, Parm->Ident));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005173}
5174
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005175static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005176 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005177 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005178 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005179 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005180 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005181 return;
5182 }
5183 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005184 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005185 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005186 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Erich Keane6a24e802019-09-13 17:39:31 +00005187 D->addAttr(::new (S.Context) ObjCBridgeRelatedAttr(
5188 S.Context, AL, RelatedClass, ClassMethod, InstanceMethod));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005189}
5190
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005191static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005192 const ParsedAttr &AL) {
Erik Pilkington81d3f452019-02-13 20:32:37 +00005193 DeclContext *Ctx = D->getDeclContext();
5194
5195 // This attribute can only be applied to methods in interfaces or class
5196 // extensions.
5197 if (!isa<ObjCInterfaceDecl>(Ctx) &&
5198 !(isa<ObjCCategoryDecl>(Ctx) &&
5199 cast<ObjCCategoryDecl>(Ctx)->IsClassExtension())) {
5200 S.Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
5201 return;
5202 }
5203
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005204 ObjCInterfaceDecl *IFace;
Erik Pilkington81d3f452019-02-13 20:32:37 +00005205 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(Ctx))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005206 IFace = CatDecl->getClassInterface();
5207 else
Erik Pilkington81d3f452019-02-13 20:32:37 +00005208 IFace = cast<ObjCInterfaceDecl>(Ctx);
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005209
5210 if (!IFace)
5211 return;
5212
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00005213 IFace->setHasDesignatedInitializers();
Erich Keane6a24e802019-09-13 17:39:31 +00005214 D->addAttr(::new (S.Context) ObjCDesignatedInitializerAttr(S.Context, AL));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005215}
5216
Erich Keanee891aa92018-07-13 15:07:47 +00005217static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005218 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005219 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005220 return;
5221 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005222 ObjCRuntimeNameAttr(S.Context, AL, MetaDataName));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005223}
5224
Nico Webera6916892016-06-10 18:53:04 +00005225// When a user wants to use objc_boxable with a union or struct
5226// but they don't have access to the declaration (legacy/third-party code)
5227// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00005228// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00005229static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00005230 bool notify = false;
5231
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005232 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00005233 if (RD && RD->getDefinition()) {
5234 RD = RD->getDefinition();
5235 notify = true;
5236 }
5237
5238 if (RD) {
Erich Keane6a24e802019-09-13 17:39:31 +00005239 ObjCBoxableAttr *BoxableAttr =
5240 ::new (S.Context) ObjCBoxableAttr(S.Context, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00005241 RD->addAttr(BoxableAttr);
5242 if (notify) {
5243 // we need to notify ASTReader/ASTWriter about
5244 // modification of existing declaration
5245 if (ASTMutationListener *L = S.getASTMutationListener())
5246 L->AddedAttributeToRecord(BoxableAttr, RD);
5247 }
5248 }
5249}
5250
Erich Keanee891aa92018-07-13 15:07:47 +00005251static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005252 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005253
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005254 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005255 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005256}
5257
Chandler Carruthedc2c642011-07-02 00:01:44 +00005258static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005259 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005260 const auto *VD = cast<ValueDecl>(D);
5261 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005262
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005263 if (!QT->isDependentType() &&
5264 !QT->isObjCLifetimeType()) {
5265 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5266 << QT;
John McCall31168b02011-06-15 23:02:42 +00005267 return;
5268 }
5269
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005270 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005271
5272 // If we have no lifetime yet, check the lifetime we're presumably
5273 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005274 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5275 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005276
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005277 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005278 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005279 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005280 "didn't infer lifetime for non-dependent type?");
5281 break;
5282
5283 case Qualifiers::OCL_Weak: // meaningful
5284 case Qualifiers::OCL_Strong: // meaningful
5285 break;
5286
5287 case Qualifiers::OCL_ExplicitNone:
5288 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005289 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5290 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005291 break;
5292 }
5293
Erich Keane6a24e802019-09-13 17:39:31 +00005294 D->addAttr(::new (S.Context) ObjCPreciseLifetimeAttr(S.Context, AL));
John McCall31168b02011-06-15 23:02:42 +00005295}
5296
Francois Picheta83957a2010-12-19 06:50:37 +00005297//===----------------------------------------------------------------------===//
5298// Microsoft specific attribute handlers.
5299//===----------------------------------------------------------------------===//
5300
Erich Keane6a24e802019-09-13 17:39:31 +00005301UuidAttr *Sema::mergeUuidAttr(Decl *D, const AttributeCommonInfo &CI,
5302 StringRef Uuid) {
Nico Weber88f5ed92016-09-13 18:55:26 +00005303 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005304 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005305 return nullptr;
5306 Diag(UA->getLocation(), diag::err_mismatched_uuid);
Erich Keane6a24e802019-09-13 17:39:31 +00005307 Diag(CI.getLoc(), diag::note_previous_uuid);
Nico Weber88f5ed92016-09-13 18:55:26 +00005308 D->dropAttr<UuidAttr>();
5309 }
5310
Erich Keane6a24e802019-09-13 17:39:31 +00005311 return ::new (Context) UuidAttr(Context, CI, Uuid);
Nico Weber88f5ed92016-09-13 18:55:26 +00005312}
5313
Erich Keanee891aa92018-07-13 15:07:47 +00005314static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005315 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005316 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005317 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005318 return;
5319 }
5320
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005321 StringRef StrRef;
5322 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005323 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005324 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005325
David Majnemer89085342013-08-09 08:56:20 +00005326 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5327 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005328 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5329 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005330
Reid Kleckner140c4a72013-05-17 14:04:52 +00005331 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005332 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005333 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005334 return;
5335 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005336
David Majnemer89085342013-08-09 08:56:20 +00005337 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005338 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005339 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005340 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005341 return;
5342 }
David Majnemer89085342013-08-09 08:56:20 +00005343 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005344 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005345 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005346 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005347 }
Francois Picheta83957a2010-12-19 06:50:37 +00005348
Nico Weber469891e2017-05-05 17:05:56 +00005349 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5350 // the only thing in the [] list, the [] too), and add an insertion of
5351 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5352 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005353 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005354 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005355 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5356 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005357
Erich Keane6a24e802019-09-13 17:39:31 +00005358 UuidAttr *UA = S.mergeUuidAttr(D, AL, StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005359 if (UA)
5360 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005361}
5362
Erich Keanee891aa92018-07-13 15:07:47 +00005363static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005364 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005365 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005366 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005367 return;
5368 }
5369 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00005370 D, AL, /*BestCase=*/true,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005371 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005372 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005373 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005374 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5375 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005376}
5377
Erich Keanee891aa92018-07-13 15:07:47 +00005378static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005379 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005380 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005381 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005382 return;
5383 }
5384 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005385 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005386 return;
5387 }
5388 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005389 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005390 return;
5391 }
Erich Keane6a24e802019-09-13 17:39:31 +00005392 D->addAttr(::new (S.Context) ThreadAttr(S.Context, AL));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005393}
5394
Erich Keanee891aa92018-07-13 15:07:47 +00005395static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005396 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005397 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005398 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005399 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005400 return;
5401 Tags.push_back(Tag);
5402 }
5403
5404 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5405 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005406 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005407 return;
5408 }
5409 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005410 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005411 return;
5412 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005413 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005414 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005415 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005416 return;
5417
5418 // Store tags sorted and without duplicates.
Fangrui Song55fab262018-09-26 22:16:28 +00005419 llvm::sort(Tags);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005420 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5421
5422 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005423 AbiTagAttr(S.Context, AL, Tags.data(), Tags.size()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005424}
5425
Erich Keanee891aa92018-07-13 15:07:47 +00005426static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005427 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005428 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005429 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005430 return;
5431 }
5432
5433 StringRef Str;
5434 SourceLocation ArgLoc;
5435
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005436 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005437 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005438 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005439 return;
5440
5441 ARMInterruptAttr::InterruptType Kind;
5442 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005443 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5444 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005445 return;
5446 }
5447
Erich Keane6a24e802019-09-13 17:39:31 +00005448 D->addAttr(::new (S.Context) ARMInterruptAttr(S.Context, AL, Kind));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005449}
5450
Erich Keanee891aa92018-07-13 15:07:47 +00005451static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005452 // MSP430 'interrupt' attribute is applied to
5453 // a function with no parameters and void return type.
5454 if (!isFunctionOrMethod(D)) {
5455 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5456 << "'interrupt'" << ExpectedFunctionOrMethod;
5457 return;
5458 }
5459
5460 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005461 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5462 << /*MSP430*/ 1 << 0;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005463 return;
5464 }
5465
5466 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005467 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5468 << /*MSP430*/ 1 << 1;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005469 return;
5470 }
5471
5472 // The attribute takes one integer argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005473 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005474 return;
5475
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005476 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005477 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5478 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005479 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005480 }
5481
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005482 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005483 llvm::APSInt NumParams(32);
5484 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005485 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005486 << AL << AANT_ArgumentIntegerConstant
5487 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005488 return;
5489 }
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005490 // The argument should be in range 0..63.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005491 unsigned Num = NumParams.getLimitedValue(255);
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005492 if (Num > 63) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005493 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005494 << AL << (int)NumParams.getSExtValue()
5495 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005496 return;
5497 }
5498
Erich Keane6a24e802019-09-13 17:39:31 +00005499 D->addAttr(::new (S.Context) MSP430InterruptAttr(S.Context, AL, Num));
Aaron Ballman36a53502014-01-16 13:03:14 +00005500 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005501}
5502
Erich Keanee891aa92018-07-13 15:07:47 +00005503static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005504 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005505 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005506 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005507 return;
5508 }
5509
5510 StringRef Str;
5511 SourceLocation ArgLoc;
5512
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005513 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005514 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005515 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005516 return;
5517
5518 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5519 // a) Must be a function.
5520 // b) Must have no parameters.
5521 // c) Must have the 'void' return type.
5522 // d) Cannot have the 'mips16' attribute, as that instruction set
5523 // lacks the 'eret' instruction.
5524 // e) The attribute itself must either have no argument or one of the
5525 // valid interrupt types, see [MipsInterruptDocs].
5526
5527 if (!isFunctionOrMethod(D)) {
5528 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5529 << "'interrupt'" << ExpectedFunctionOrMethod;
5530 return;
5531 }
5532
5533 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005534 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5535 << /*MIPS*/ 0 << 0;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005536 return;
5537 }
5538
5539 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005540 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5541 << /*MIPS*/ 0 << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005542 return;
5543 }
5544
Erich Keane44bacdf2018-08-09 13:21:32 +00005545 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005546 return;
5547
5548 MipsInterruptAttr::InterruptType Kind;
5549 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005550 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005551 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005552 return;
5553 }
5554
Erich Keane6a24e802019-09-13 17:39:31 +00005555 D->addAttr(::new (S.Context) MipsInterruptAttr(S.Context, AL, Kind));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005556}
5557
Erich Keanee891aa92018-07-13 15:07:47 +00005558static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005559 // Semantic checks for a function with the 'interrupt' attribute.
5560 // a) Must be a function.
5561 // b) Must have the 'void' return type.
5562 // c) Must take 1 or 2 arguments.
5563 // d) The 1st argument must be a pointer.
5564 // e) The 2nd argument (if any) must be an unsigned integer.
5565 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5566 CXXMethodDecl::isStaticOverloadedOperator(
5567 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005568 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005569 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005570 return;
5571 }
5572 // Interrupt handler must have void return type.
5573 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5574 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5575 diag::err_anyx86_interrupt_attribute)
5576 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5577 ? 0
5578 : 1)
5579 << 0;
5580 return;
5581 }
5582 // Interrupt handler must have 1 or 2 parameters.
5583 unsigned NumParams = getFunctionOrMethodNumParams(D);
5584 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005585 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005586 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5587 ? 0
5588 : 1)
5589 << 1;
5590 return;
5591 }
5592 // The first argument must be a pointer.
5593 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5594 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5595 diag::err_anyx86_interrupt_attribute)
5596 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5597 ? 0
5598 : 1)
5599 << 2;
5600 return;
5601 }
5602 // The second argument, if present, must be an unsigned integer.
5603 unsigned TypeSize =
5604 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5605 ? 64
5606 : 32;
5607 if (NumParams == 2 &&
5608 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5609 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5610 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5611 diag::err_anyx86_interrupt_attribute)
5612 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5613 ? 0
5614 : 1)
5615 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5616 return;
5617 }
Erich Keane6a24e802019-09-13 17:39:31 +00005618 D->addAttr(::new (S.Context) AnyX86InterruptAttr(S.Context, AL));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005619 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5620}
5621
Erich Keanee891aa92018-07-13 15:07:47 +00005622static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005623 if (!isFunctionOrMethod(D)) {
5624 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5625 << "'interrupt'" << ExpectedFunction;
5626 return;
5627 }
5628
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005629 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005630 return;
5631
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005632 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005633}
5634
Erich Keanee891aa92018-07-13 15:07:47 +00005635static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005636 if (!isFunctionOrMethod(D)) {
5637 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5638 << "'signal'" << ExpectedFunction;
5639 return;
5640 }
5641
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005642 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005643 return;
5644
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005645 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005646}
5647
Dan Gohmanb4323692019-01-24 21:08:30 +00005648static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5649 if (!isFunctionOrMethod(D)) {
5650 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5651 << "'import_module'" << ExpectedFunction;
5652 return;
5653 }
5654
5655 auto *FD = cast<FunctionDecl>(D);
5656 if (FD->isThisDeclarationADefinition()) {
5657 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5658 return;
5659 }
5660
5661 StringRef Str;
5662 SourceLocation ArgLoc;
5663 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5664 return;
5665
Erich Keane6a24e802019-09-13 17:39:31 +00005666 FD->addAttr(::new (S.Context)
5667 WebAssemblyImportModuleAttr(S.Context, AL, Str));
Dan Gohmanb4323692019-01-24 21:08:30 +00005668}
Ana Pazos1eee1b72018-07-26 17:37:45 +00005669
Dan Gohmancae84592019-02-01 22:25:23 +00005670static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5671 if (!isFunctionOrMethod(D)) {
5672 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5673 << "'import_name'" << ExpectedFunction;
5674 return;
5675 }
5676
5677 auto *FD = cast<FunctionDecl>(D);
5678 if (FD->isThisDeclarationADefinition()) {
5679 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5680 return;
5681 }
5682
5683 StringRef Str;
5684 SourceLocation ArgLoc;
5685 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5686 return;
5687
Erich Keane6a24e802019-09-13 17:39:31 +00005688 FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr(S.Context, AL, Str));
Dan Gohmancae84592019-02-01 22:25:23 +00005689}
5690
Ana Pazos1eee1b72018-07-26 17:37:45 +00005691static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5692 const ParsedAttr &AL) {
5693 // Warn about repeated attributes.
5694 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5695 S.Diag(AL.getRange().getBegin(),
5696 diag::warn_riscv_repeated_interrupt_attribute);
5697 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5698 return;
5699 }
5700
5701 // Check the attribute argument. Argument is optional.
5702 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5703 return;
5704
5705 StringRef Str;
5706 SourceLocation ArgLoc;
5707
5708 // 'machine'is the default interrupt mode.
5709 if (AL.getNumArgs() == 0)
5710 Str = "machine";
5711 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5712 return;
5713
5714 // Semantic checks for a function with the 'interrupt' attribute:
5715 // - Must be a function.
5716 // - Must have no parameters.
5717 // - Must have the 'void' return type.
5718 // - The attribute itself must either have no argument or one of the
5719 // valid interrupt types, see [RISCVInterruptDocs].
5720
5721 if (D->getFunctionType() == nullptr) {
5722 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5723 << "'interrupt'" << ExpectedFunction;
5724 return;
5725 }
5726
5727 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005728 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5729 << /*RISC-V*/ 2 << 0;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005730 return;
5731 }
5732
5733 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005734 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5735 << /*RISC-V*/ 2 << 1;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005736 return;
5737 }
5738
5739 RISCVInterruptAttr::InterruptType Kind;
5740 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005741 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5742 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005743 return;
5744 }
5745
Erich Keane6a24e802019-09-13 17:39:31 +00005746 D->addAttr(::new (S.Context) RISCVInterruptAttr(S.Context, AL, Kind));
Ana Pazos1eee1b72018-07-26 17:37:45 +00005747}
5748
Erich Keanee891aa92018-07-13 15:07:47 +00005749static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005750 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005751 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5752 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005753 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005754 break;
5755 case llvm::Triple::mipsel:
5756 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005757 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005758 break;
5759 case llvm::Triple::x86:
5760 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005761 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005762 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005763 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005764 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005765 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005766 case llvm::Triple::riscv32:
5767 case llvm::Triple::riscv64:
5768 handleRISCVInterruptAttr(S, D, AL);
5769 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005770 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005771 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005772 break;
5773 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005774}
5775
Michael Liao7557afa2019-02-26 18:49:36 +00005776static bool
5777checkAMDGPUFlatWorkGroupSizeArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr,
5778 const AMDGPUFlatWorkGroupSizeAttr &Attr) {
5779 // Accept template arguments for now as they depend on something else.
5780 // We'll get to check them when they eventually get instantiated.
5781 if (MinExpr->isValueDependent() || MaxExpr->isValueDependent())
5782 return false;
5783
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005784 uint32_t Min = 0;
Michael Liao7557afa2019-02-26 18:49:36 +00005785 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
5786 return true;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005787
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005788 uint32_t Max = 0;
Michael Liao7557afa2019-02-26 18:49:36 +00005789 if (!checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
5790 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005791
5792 if (Min == 0 && Max != 0) {
Michael Liao7557afa2019-02-26 18:49:36 +00005793 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5794 << &Attr << 0;
5795 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005796 }
5797 if (Min > Max) {
Michael Liao7557afa2019-02-26 18:49:36 +00005798 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5799 << &Attr << 1;
5800 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005801 }
5802
Michael Liao7557afa2019-02-26 18:49:36 +00005803 return false;
5804}
5805
Erich Keane6a24e802019-09-13 17:39:31 +00005806void Sema::addAMDGPUFlatWorkGroupSizeAttr(Decl *D,
5807 const AttributeCommonInfo &CI,
5808 Expr *MinExpr, Expr *MaxExpr) {
5809 AMDGPUFlatWorkGroupSizeAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00005810
5811 if (checkAMDGPUFlatWorkGroupSizeArguments(*this, MinExpr, MaxExpr, TmpAttr))
5812 return;
5813
Erich Keane6a24e802019-09-13 17:39:31 +00005814 D->addAttr(::new (Context)
5815 AMDGPUFlatWorkGroupSizeAttr(Context, CI, MinExpr, MaxExpr));
Michael Liao7557afa2019-02-26 18:49:36 +00005816}
5817
5818static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
5819 const ParsedAttr &AL) {
5820 Expr *MinExpr = AL.getArgAsExpr(0);
5821 Expr *MaxExpr = AL.getArgAsExpr(1);
5822
Erich Keane6a24e802019-09-13 17:39:31 +00005823 S.addAMDGPUFlatWorkGroupSizeAttr(D, AL, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00005824}
5825
5826static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr,
5827 Expr *MaxExpr,
5828 const AMDGPUWavesPerEUAttr &Attr) {
5829 if (S.DiagnoseUnexpandedParameterPack(MinExpr) ||
5830 (MaxExpr && S.DiagnoseUnexpandedParameterPack(MaxExpr)))
5831 return true;
5832
5833 // Accept template arguments for now as they depend on something else.
5834 // We'll get to check them when they eventually get instantiated.
5835 if (MinExpr->isValueDependent() || (MaxExpr && MaxExpr->isValueDependent()))
5836 return false;
5837
5838 uint32_t Min = 0;
5839 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
5840 return true;
5841
5842 uint32_t Max = 0;
5843 if (MaxExpr && !checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
5844 return true;
5845
5846 if (Min == 0 && Max != 0) {
5847 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5848 << &Attr << 0;
5849 return true;
5850 }
5851 if (Max != 0 && Min > Max) {
5852 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5853 << &Attr << 1;
5854 return true;
5855 }
5856
5857 return false;
5858}
5859
Erich Keane6a24e802019-09-13 17:39:31 +00005860void Sema::addAMDGPUWavesPerEUAttr(Decl *D, const AttributeCommonInfo &CI,
5861 Expr *MinExpr, Expr *MaxExpr) {
5862 AMDGPUWavesPerEUAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00005863
5864 if (checkAMDGPUWavesPerEUArguments(*this, MinExpr, MaxExpr, TmpAttr))
5865 return;
5866
Erich Keane6a24e802019-09-13 17:39:31 +00005867 D->addAttr(::new (Context)
5868 AMDGPUWavesPerEUAttr(Context, CI, MinExpr, MaxExpr));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005869}
5870
Erich Keanee891aa92018-07-13 15:07:47 +00005871static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Liao7557afa2019-02-26 18:49:36 +00005872 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
5873 !checkAttributeAtMostNumArgs(S, AL, 2))
5874 return;
5875
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005876 Expr *MinExpr = AL.getArgAsExpr(0);
Michael Liao7557afa2019-02-26 18:49:36 +00005877 Expr *MaxExpr = (AL.getNumArgs() > 1) ? AL.getArgAsExpr(1) : nullptr;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005878
Erich Keane6a24e802019-09-13 17:39:31 +00005879 S.addAMDGPUWavesPerEUAttr(D, AL, MinExpr, MaxExpr);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005880}
5881
Erich Keanee891aa92018-07-13 15:07:47 +00005882static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005883 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005884 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5885 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005886 return;
5887
Erich Keane6a24e802019-09-13 17:39:31 +00005888 D->addAttr(::new (S.Context) AMDGPUNumSGPRAttr(S.Context, AL, NumSGPR));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005889}
5890
Erich Keanee891aa92018-07-13 15:07:47 +00005891static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005892 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005893 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5894 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005895 return;
5896
Erich Keane6a24e802019-09-13 17:39:31 +00005897 D->addAttr(::new (S.Context) AMDGPUNumVGPRAttr(S.Context, AL, NumVGPR));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005898}
5899
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005900static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005901 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005902 // If we try to apply it to a function pointer, don't warn, but don't
5903 // do anything, either. It doesn't matter anyway, because there's nothing
5904 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005905 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005906 if (VD && VD->getType()->isFunctionPointerType())
5907 return;
5908 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005909 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005910 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5911 TD->getUnderlyingType()->isFunctionType()))
5912 return;
5913 // Attribute can only be applied to function types.
5914 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005915 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005916 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005917 return;
5918 }
5919
Erich Keane6a24e802019-09-13 17:39:31 +00005920 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(S.Context, AL));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005921}
5922
Erich Keanee891aa92018-07-13 15:07:47 +00005923static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005924 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005925 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5926 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005927 return;
5928
5929 // TODO: Investigate what happens with the next major version of MSVC.
Reid Kleckner1a94d872018-12-17 23:16:43 +00005930 if (Version != LangOptions::MSVC2015 / 100) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005931 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005932 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005933 return;
5934 }
5935
Reid Kleckner1a94d872018-12-17 23:16:43 +00005936 // The attribute expects a "major" version number like 19, but new versions of
5937 // MSVC have moved to updating the "minor", or less significant numbers, so we
5938 // have to multiply by 100 now.
5939 Version *= 100;
5940
Erich Keane6a24e802019-09-13 17:39:31 +00005941 D->addAttr(::new (S.Context) LayoutVersionAttr(S.Context, AL, Version));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005942}
5943
Erich Keane6a24e802019-09-13 17:39:31 +00005944DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D,
5945 const AttributeCommonInfo &CI) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005946 if (D->hasAttr<DLLExportAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00005947 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005948 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005949 }
5950
5951 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005952 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005953
Erich Keane6a24e802019-09-13 17:39:31 +00005954 return ::new (Context) DLLImportAttr(Context, CI);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005955}
5956
Erich Keane6a24e802019-09-13 17:39:31 +00005957DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D,
5958 const AttributeCommonInfo &CI) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005959 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005960 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005961 D->dropAttr<DLLImportAttr>();
5962 }
5963
5964 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005965 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005966
Erich Keane6a24e802019-09-13 17:39:31 +00005967 return ::new (Context) DLLExportAttr(Context, CI);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005968}
5969
Erich Keanee891aa92018-07-13 15:07:47 +00005970static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005971 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5972 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005973 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00005974 return;
5975 }
5976
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005977 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00005978 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005979 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5980 // MinGW doesn't allow dllimport on inline functions.
5981 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00005982 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005983 return;
5984 }
5985 }
5986
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005987 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005988 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5989 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005990 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00005991 return;
5992 }
5993 }
5994
Erich Keanee891aa92018-07-13 15:07:47 +00005995 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Erich Keane6a24e802019-09-13 17:39:31 +00005996 ? (Attr *)S.mergeDLLExportAttr(D, A)
5997 : (Attr *)S.mergeDLLImportAttr(D, A);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005998 if (NewAttr)
5999 D->addAttr(NewAttr);
6000}
6001
David Majnemer2c4e00a2014-01-29 22:07:36 +00006002MSInheritanceAttr *
Erich Keane6a24e802019-09-13 17:39:31 +00006003Sema::mergeMSInheritanceAttr(Decl *D, const AttributeCommonInfo &CI,
6004 bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00006005 MSInheritanceAttr::Spelling SemanticSpelling) {
6006 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
6007 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00006008 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006009 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
6010 << 1 /*previous declaration*/;
Erich Keane6a24e802019-09-13 17:39:31 +00006011 Diag(CI.getLoc(), diag::note_previous_ms_inheritance);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006012 D->dropAttr<MSInheritanceAttr>();
6013 }
6014
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006015 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006016 if (RD->hasDefinition()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006017 if (checkMSInheritanceAttrOnDefinition(RD, CI.getRange(), BestCase,
David Majnemer4bb09802014-02-10 19:50:15 +00006018 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006019 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006020 }
6021 } else {
6022 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
Erich Keane6a24e802019-09-13 17:39:31 +00006023 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
David Majnemer2c4e00a2014-01-29 22:07:36 +00006024 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006025 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006026 }
6027 if (RD->getDescribedClassTemplate()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006028 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
David Majnemer2c4e00a2014-01-29 22:07:36 +00006029 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006030 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006031 }
6032 }
6033
Erich Keane6a24e802019-09-13 17:39:31 +00006034 return ::new (Context) MSInheritanceAttr(Context, CI, BestCase);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006035}
6036
Erich Keanee891aa92018-07-13 15:07:47 +00006037static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006038 // The capability attributes take a single string parameter for the name of
6039 // the capability they represent. The lockable attribute does not take any
6040 // parameters. However, semantically, both attributes represent the same
6041 // concept, and so they use the same semantic attribute. Eventually, the
6042 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00006043 //
Alp Toker958027b2014-07-14 19:42:55 +00006044 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00006045 // literal will be considered a "mutex."
6046 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006047 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00006048 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006049 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006050 return;
6051
Aaron Ballman6c810072014-03-05 21:47:13 +00006052 // Currently, there are only two names allowed for a capability: role and
6053 // mutex (case insensitive). Diagnose other capability names.
6054 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
6055 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
6056
Erich Keane6a24e802019-09-13 17:39:31 +00006057 D->addAttr(::new (S.Context) CapabilityAttr(S.Context, AL, N));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006058}
6059
Erich Keanee891aa92018-07-13 15:07:47 +00006060static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00006061 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006062 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00006063 return;
6064
Erich Keane6a24e802019-09-13 17:39:31 +00006065 D->addAttr(::new (S.Context)
6066 AssertCapabilityAttr(S.Context, AL, Args.data(), Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006067}
6068
6069static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006070 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006071 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006072 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006073 return;
6074
Erich Keane6a24e802019-09-13 17:39:31 +00006075 D->addAttr(::new (S.Context) AcquireCapabilityAttr(S.Context, AL, Args.data(),
6076 Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006077}
6078
6079static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006080 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006081 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006082 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006083 return;
6084
Erich Keane6a24e802019-09-13 17:39:31 +00006085 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(
6086 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006087}
6088
6089static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006090 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006091 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00006092 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006093 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006094
Erich Keane6a24e802019-09-13 17:39:31 +00006095 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(S.Context, AL, Args.data(),
6096 Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006097}
6098
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006099static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006100 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006101 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006102 return;
6103
6104 // check that all arguments are lockable objects
6105 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006106 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006107 if (Args.empty())
6108 return;
6109
6110 RequiresCapabilityAttr *RCA = ::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00006111 RequiresCapabilityAttr(S.Context, AL, Args.data(), Args.size());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006112
6113 D->addAttr(RCA);
6114}
6115
Erich Keanee891aa92018-07-13 15:07:47 +00006116static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006117 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00006118 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006119 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00006120 // Do not want to attach the attribute to the namespace because that will
6121 // cause confusing diagnostic reports for uses of declarations within the
6122 // namespace.
6123 return;
6124 }
6125 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00006126
Manman Renc7890fe2016-03-16 18:50:49 +00006127 // Handle the cases where the attribute has a text message.
6128 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006129 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
6130 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00006131 return;
6132
6133 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006134 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
6135 checkAttributeAtMostNumArgs(S, AL, 1);
6136 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
Sander de Smalen44a22532018-11-26 16:38:37 +00006137 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
6138 return;
6139
6140 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
6141 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
6142
Erich Keane6a24e802019-09-13 17:39:31 +00006143 D->addAttr(::new (S.Context) DeprecatedAttr(S.Context, AL, Str, Replacement));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006144}
6145
6146static bool isGlobalVar(const Decl *D) {
6147 if (const auto *S = dyn_cast<VarDecl>(D))
6148 return S->hasGlobalStorage();
6149 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00006150}
6151
Erich Keanee891aa92018-07-13 15:07:47 +00006152static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006153 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00006154 return;
6155
Benjamin Kramer1b582012016-02-13 18:11:49 +00006156 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00006157
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006158 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00006159 StringRef SanitizerName;
6160 SourceLocation LiteralLoc;
6161
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006162 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00006163 return;
6164
Pierre Gousseauae5303d2019-03-01 10:05:15 +00006165 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) ==
6166 SanitizerMask())
Peter Collingbourne915df992015-05-15 18:33:32 +00006167 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006168 else if (isGlobalVar(D) && SanitizerName != "address")
6169 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006170 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00006171 Sanitizers.push_back(SanitizerName);
6172 }
6173
Erich Keane6a24e802019-09-13 17:39:31 +00006174 D->addAttr(::new (S.Context) NoSanitizeAttr(S.Context, AL, Sanitizers.data(),
6175 Sanitizers.size()));
Peter Collingbourne915df992015-05-15 18:33:32 +00006176}
6177
6178static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006179 const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00006180 StringRef AttrName = AL.getAttrName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00006181 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006182 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
6183 .Case("no_address_safety_analysis", "address")
6184 .Case("no_sanitize_address", "address")
6185 .Case("no_sanitize_thread", "thread")
6186 .Case("no_sanitize_memory", "memory");
6187 if (isGlobalVar(D) && SanitizerName != "address")
6188 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006189 << AL << ExpectedFunction;
Aaron Ballman31ca49b2019-05-21 17:24:49 +00006190
6191 // FIXME: Rather than create a NoSanitizeSpecificAttr, this creates a
6192 // NoSanitizeAttr object; but we need to calculate the correct spelling list
6193 // index rather than incorrectly assume the index for NoSanitizeSpecificAttr
6194 // has the same spellings as the index for NoSanitizeAttr. We don't have a
6195 // general way to "translate" between the two, so this hack attempts to work
6196 // around the issue with hard-coded indicies. This is critical for calling
6197 // getSpelling() or prettyPrint() on the resulting semantic attribute object
6198 // without failing assertions.
6199 unsigned TranslatedSpellingIndex = 0;
6200 if (AL.isC2xAttribute() || AL.isCXX11Attribute())
6201 TranslatedSpellingIndex = 1;
6202
Erich Keane6a24e802019-09-13 17:39:31 +00006203 AttributeCommonInfo Info = AL;
6204 Info.setAttributeSpellingListIndex(TranslatedSpellingIndex);
6205 D->addAttr(::new (S.Context)
6206 NoSanitizeAttr(S.Context, Info, &SanitizerName, 1));
Peter Collingbourne915df992015-05-15 18:33:32 +00006207}
6208
Erich Keanee891aa92018-07-13 15:07:47 +00006209static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006210 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006211 D->addAttr(Internal);
6212}
6213
Erich Keanee891aa92018-07-13 15:07:47 +00006214static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006215 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006216 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00006217 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006218 else
Erich Keane44bacdf2018-08-09 13:21:32 +00006219 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
6220 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006221}
6222
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006223/// Handles semantic checking for features that are common to all attributes,
6224/// such as checking whether a parameter was properly specified, or the correct
6225/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006226static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006227 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006228 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00006229 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006230 //
6231 // We also bail on unknown and ignored attributes because those are handled
6232 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00006233 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006234 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006235 // Check whether the attribute requires specific language extensions to be
6236 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006237 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00006238 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00006239 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006240 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00006241 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006242 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00006243 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006244
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006245 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006246 // If there are no optional arguments, then checking for the argument count
6247 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006248 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006249 return true;
6250 } else {
6251 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006252 if (AL.getMinArgs() &&
6253 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006254 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006255 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
6256 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006257 return true;
6258 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00006259
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006260 if (S.CheckAttrTarget(AL))
6261 return true;
6262
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006263 return false;
6264}
6265
Erich Keanee891aa92018-07-13 15:07:47 +00006266static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006267 if (D->isInvalidDecl())
6268 return;
6269
6270 // Check if there is only one access qualifier.
6271 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006272 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
6273 AL.getSemanticSpelling()) {
6274 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
Erich Keane6a24e802019-09-13 17:39:31 +00006275 << AL.getAttrName()->getName() << AL.getRange();
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006276 } else {
6277 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
6278 << D->getSourceRange();
6279 D->setInvalidDecl(true);
6280 return;
6281 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00006282 }
6283
6284 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
6285 // image object can be read and written.
6286 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
6287 // object. Using the read_write (or __read_write) qualifier with the pipe
6288 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006289 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006290 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Erich Keane6a24e802019-09-13 17:39:31 +00006291 if (AL.getAttrName()->getName().find("read_write") != StringRef::npos) {
Anastasia Stulova2c4730d2019-02-15 12:07:57 +00006292 if ((!S.getLangOpts().OpenCLCPlusPlus &&
6293 S.getLangOpts().OpenCLVersion < 200) ||
6294 DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006295 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00006296 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00006297 D->setInvalidDecl(true);
6298 return;
6299 }
6300 }
6301 }
6302
Erich Keane6a24e802019-09-13 17:39:31 +00006303 D->addAttr(::new (S.Context) OpenCLAccessAttr(S.Context, AL));
Xiuli Pan11e13f62016-02-26 03:13:03 +00006304}
6305
Erik Pilkington5a559e62018-08-21 17:24:06 +00006306static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006307 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00006308 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
6309 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
6310 return;
6311 }
6312
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006313 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00006314 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006315 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00006316 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00006317}
6318
JF Bastien14daa202018-12-18 05:12:21 +00006319static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6320 assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic &&
6321 "uninitialized is only valid on automatic duration variables");
Erich Keane6a24e802019-09-13 17:39:31 +00006322 D->addAttr(::new (S.Context) UninitializedAttr(S.Context, AL));
JF Bastien14daa202018-12-18 05:12:21 +00006323}
6324
Erik Pilkington1e368822019-01-04 18:33:06 +00006325static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD,
6326 bool DiagnoseFailure) {
6327 QualType Ty = VD->getType();
6328 if (!Ty->isObjCRetainableType()) {
6329 if (DiagnoseFailure) {
6330 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6331 << 0;
6332 }
6333 return false;
6334 }
6335
6336 Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime();
6337
6338 // Sema::inferObjCARCLifetime must run after processing decl attributes
6339 // (because __block lowers to an attribute), so if the lifetime hasn't been
6340 // explicitly specified, infer it locally now.
6341 if (LifetimeQual == Qualifiers::OCL_None)
6342 LifetimeQual = Ty->getObjCARCImplicitLifetime();
6343
6344 // The attributes only really makes sense for __strong variables; ignore any
6345 // attempts to annotate a parameter with any other lifetime qualifier.
6346 if (LifetimeQual != Qualifiers::OCL_Strong) {
6347 if (DiagnoseFailure) {
6348 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6349 << 1;
6350 }
6351 return false;
6352 }
6353
6354 // Tampering with the type of a VarDecl here is a bit of a hack, but we need
6355 // to ensure that the variable is 'const' so that we can error on
6356 // modification, which can otherwise over-release.
6357 VD->setType(Ty.withConst());
6358 VD->setARCPseudoStrong(true);
6359 return true;
6360}
6361
6362static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D,
6363 const ParsedAttr &AL) {
6364 if (auto *VD = dyn_cast<VarDecl>(D)) {
6365 assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically");
6366 if (!VD->hasLocalStorage()) {
6367 S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6368 << 0;
6369 return;
6370 }
6371
6372 if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true))
6373 return;
6374
6375 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6376 return;
6377 }
6378
6379 // If D is a function-like declaration (method, block, or function), then we
6380 // make every parameter psuedo-strong.
6381 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); I != E; ++I) {
6382 auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I));
6383 QualType Ty = PVD->getType();
6384
6385 // If a user wrote a parameter with __strong explicitly, then assume they
6386 // want "real" strong semantics for that parameter. This works because if
6387 // the parameter was written with __strong, then the strong qualifier will
6388 // be non-local.
6389 if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() ==
6390 Qualifiers::OCL_Strong)
6391 continue;
6392
6393 tryMakeVariablePseudoStrong(S, PVD, /*DiagnoseFailure=*/false);
6394 }
6395 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6396}
6397
Artem Dergachevc333d772019-02-21 00:01:02 +00006398static void handleMIGServerRoutineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6399 // Check that the return type is a `typedef int kern_return_t` or a typedef
6400 // around it, because otherwise MIG convention checks make no sense.
6401 // BlockDecl doesn't store a return type, so it's annoying to check,
6402 // so let's skip it for now.
6403 if (!isa<BlockDecl>(D)) {
6404 QualType T = getFunctionOrMethodResultType(D);
6405 bool IsKernReturnT = false;
6406 while (const auto *TT = T->getAs<TypedefType>()) {
6407 IsKernReturnT = (TT->getDecl()->getName() == "kern_return_t");
6408 T = TT->desugar();
6409 }
6410 if (!IsKernReturnT || T.getCanonicalType() != S.getASTContext().IntTy) {
6411 S.Diag(D->getBeginLoc(),
6412 diag::warn_mig_server_routine_does_not_return_kern_return_t);
6413 return;
6414 }
6415 }
6416
6417 handleSimpleAttribute<MIGServerRoutineAttr>(S, D, AL);
6418}
6419
Reid Kleckner1181c9f2019-03-25 23:20:18 +00006420static void handleMSAllocatorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6421 // Warn if the return type is not a pointer or reference type.
6422 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6423 QualType RetTy = FD->getReturnType();
6424 if (!RetTy->isPointerType() && !RetTy->isReferenceType()) {
6425 S.Diag(AL.getLoc(), diag::warn_declspec_allocator_nonpointer)
6426 << AL.getRange() << RetTy;
6427 return;
6428 }
6429 }
6430
6431 handleSimpleAttribute<MSAllocatorAttr>(S, D, AL);
6432}
6433
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00006434//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006435// Top Level Sema Entry Points
6436//===----------------------------------------------------------------------===//
6437
Richard Smithf8a75c32013-08-29 00:47:48 +00006438/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
6439/// the attribute applies to decls. If the attribute is a type attribute, just
6440/// silently ignore it if a GNU attribute.
6441static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006442 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00006443 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00006444 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00006445 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00006446
Richard Smithf8a75c32013-08-29 00:47:48 +00006447 // Ignore C++11 attributes on declarator chunks: they appertain to the type
6448 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006449 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00006450 return;
6451
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006452 // Unknown attributes are automatically warned on. Target-specific attributes
6453 // which do not apply to the current target architecture are treated as
6454 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006455 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006456 !AL.existsInTarget(S.Context.getTargetInfo())) {
Simon Pilgrimfc0ff612018-12-17 12:17:37 +00006457 S.Diag(AL.getLoc(),
6458 AL.isDeclspecAttribute()
6459 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
6460 : (unsigned)diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006461 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006462 return;
6463 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006464
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006465 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006466 return;
6467
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006468 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006469 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006470 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00006471 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006472 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00006473 break;
6474 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006475 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00006476 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006477 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006478 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006479 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006480 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006481 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006482 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006483 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006484 case ParsedAttr::AT_DLLExport:
6485 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006486 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006487 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006488 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006489 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006490 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006491 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006492 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006493 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006494 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006495 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006496 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006497 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006498 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006499 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006500 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006501 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006502 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006503 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006504 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006505 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006506 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006507 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006508 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006509 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006510 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006511 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006512 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006513 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006514 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006515 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006516 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006517 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006518 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006519 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006520 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006521 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006522 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006523 break;
Dan Gohmanb4323692019-01-24 21:08:30 +00006524 case ParsedAttr::AT_WebAssemblyImportModule:
6525 handleWebAssemblyImportModuleAttr(S, D, AL);
6526 break;
Dan Gohmancae84592019-02-01 22:25:23 +00006527 case ParsedAttr::AT_WebAssemblyImportName:
6528 handleWebAssemblyImportNameAttr(S, D, AL);
6529 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006530 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006531 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006532 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006533 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006534 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006535 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006536 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006537 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006538 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006539 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006540 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006541 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006542 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006543 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006544 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006545 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006546 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006547 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006548 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006549 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006550 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006551 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006552 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006553 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006554 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006555 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006556 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006557 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006558 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006559 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006560 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006561 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006562 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006563 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006564 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006565 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006566 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006567 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006568 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006569 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006570 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006571 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006572 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006573 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006574 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006575 case ParsedAttr::AT_CPUDispatch:
6576 case ParsedAttr::AT_CPUSpecific:
6577 handleCPUSpecificAttr(S, D, AL);
6578 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006579 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006580 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006581 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006582 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006583 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006584 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006585 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006586 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006587 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006588 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006589 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006590 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006591 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006592 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006593 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006594 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006595 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006596 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006597 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006598 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006599 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006600 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006601 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006602 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006603 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006604 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006605 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006606 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006607 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006608 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006609 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006610 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006611 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006612 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006613 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006614 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006615 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006616 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006617 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006618 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006619 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006620 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006621 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006622 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006623 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006624 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006625 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006626 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006627 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006628 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006629 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006630 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006631 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006632 break;
Johannes Doerfertac991bb2019-01-19 05:36:54 +00006633 case ParsedAttr::AT_Callback:
6634 handleCallbackAttr(S, D, AL);
6635 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006636 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006637 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006638 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006639 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006640 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006641 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006642 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006643 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006644 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006645 break;
Yaxun Liuc3dfe902019-06-26 03:47:37 +00006646 case ParsedAttr::AT_HIPPinnedShadow:
6647 handleSimpleAttributeWithExclusions<HIPPinnedShadowAttr, CUDADeviceAttr,
6648 CUDAConstantAttr>(S, D, AL);
6649 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006650 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006651 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006652 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006653 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006654 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006655 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006656 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006657 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006658 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006659 case ParsedAttr::AT_LifetimeBound:
6660 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6661 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006662 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006663 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006664 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006665 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006666 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006667 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006668 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006669 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006670 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006671 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006672 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006673 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006674 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006675 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006676 break;
Richard Smith78b239e2019-06-20 20:44:45 +00006677 case ParsedAttr::AT_NoUniqueAddress:
6678 handleSimpleAttribute<NoUniqueAddressAttr>(S, D, AL);
6679 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006680 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006681 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6682 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006683 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006684 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006685 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006686 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006687 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006688 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006689 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006690 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006691 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006692 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006693 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006694 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006695 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006696 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006697 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006698 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006699 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006700 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006701 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006702 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006703 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006704 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006705 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006706 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006707 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006708 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006709 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006710 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006711 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006712 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006713 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006714 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006715 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006716 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006717 handleNoCfCheckAttr(S, D, AL);
6718 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006719 case ParsedAttr::AT_NoThrow:
Erich Keaned02f4a12019-05-30 17:31:54 +00006720 if (!AL.isUsedAsTypeAttr())
6721 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006722 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006723 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006724 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006725 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006726 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006727 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006728 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006729 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006730 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006731 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006732 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006733 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006734 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006735 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006736 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006737 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006738 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006739 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006740 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006741 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006742 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006743 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006744 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006745 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006746 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006747 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006748 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006749 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006750 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006751 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006752 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006753 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006754 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006755 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006756 case ParsedAttr::AT_ObjCRuntimeVisible:
6757 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6758 break;
6759 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006760 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006761 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006762 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006763 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6764 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006765 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006766 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006767 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6768 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006769 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006770 case ParsedAttr::AT_CFConsumed:
6771 case ParsedAttr::AT_NSConsumed:
George Karpenkov1657f362018-11-30 02:18:37 +00006772 case ParsedAttr::AT_OSConsumed:
Erich Keane6a24e802019-09-13 17:39:31 +00006773 S.AddXConsumedAttr(D, AL, parsedAttrToRetainOwnershipKind(AL),
6774 /*IsTemplateInstantiation=*/false);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006775 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006776 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006777 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006778 break;
George Karpenkovda2c77f2018-12-06 22:06:59 +00006779 case ParsedAttr::AT_OSConsumesThis:
6780 handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL);
6781 break;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00006782 case ParsedAttr::AT_OSReturnsRetainedOnZero:
6783 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnZeroAttr>(
6784 S, D, AL, isValidOSObjectOutParameter(D),
6785 diag::warn_ns_attribute_wrong_parameter_type,
6786 /*Extra Args=*/AL, /*pointer-to-OSObject-pointer*/ 3, AL.getRange());
6787 break;
6788 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
6789 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnNonZeroAttr>(
6790 S, D, AL, isValidOSObjectOutParameter(D),
6791 diag::warn_ns_attribute_wrong_parameter_type,
6792 /*Extra Args=*/AL, /*pointer-to-OSObject-poointer*/ 3, AL.getRange());
6793 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006794 case ParsedAttr::AT_NSReturnsAutoreleased:
6795 case ParsedAttr::AT_NSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006796 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006797 case ParsedAttr::AT_CFReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006798 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006799 case ParsedAttr::AT_OSReturnsNotRetained:
6800 case ParsedAttr::AT_OSReturnsRetained:
6801 handleXReturnsXRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006802 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006803 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006804 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006805 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006806 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006807 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006808 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006809 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006810 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006811 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006812 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006813 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006814 break;
Richard Smitha6e8b682019-09-04 20:30:37 +00006815 case ParsedAttr::AT_ConstInit:
6816 handleSimpleAttribute<ConstInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006817 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006818 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006819 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006820 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006821 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006822 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006823 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006824 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006825 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006826 break;
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006827 case ParsedAttr::AT_SpeculativeLoadHardening:
Zola Bridges826ef592019-01-18 17:20:46 +00006828 handleSimpleAttributeWithExclusions<SpeculativeLoadHardeningAttr,
6829 NoSpeculativeLoadHardeningAttr>(S, D,
6830 AL);
6831 break;
6832 case ParsedAttr::AT_NoSpeculativeLoadHardening:
6833 handleSimpleAttributeWithExclusions<NoSpeculativeLoadHardeningAttr,
6834 SpeculativeLoadHardeningAttr>(S, D, AL);
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006835 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006836 case ParsedAttr::AT_CodeSeg:
6837 handleCodeSegAttr(S, D, AL);
6838 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006839 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006840 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006841 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006842 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006843 handleMinVectorWidthAttr(S, D, AL);
6844 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006845 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006846 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006847 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006848 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006849 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006850 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006851 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006852 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006853 break;
Joe Danielsf7393d22019-02-04 23:32:55 +00006854 case ParsedAttr::AT_ObjCNonLazyClass:
6855 handleSimpleAttribute<ObjCNonLazyClassAttr>(S, D, AL);
6856 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006857 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006858 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006859 break;
John McCall2c91c3b2019-05-30 04:09:01 +00006860 case ParsedAttr::AT_ObjCClassStub:
6861 handleSimpleAttribute<ObjCClassStubAttr>(S, D, AL);
6862 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006863 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006864 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006865 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006866 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006867 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006868 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006869 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006870 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006871 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006872 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006873 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006874 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006875 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006876 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6877 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006878 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006879 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00006880 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
6881 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006882 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006883 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006884 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006885 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006886 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006887 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006888 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006889 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006890 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006891 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006892 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006893 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006894 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006895 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006896 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006897 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006898 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006899 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006900 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006901 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006902 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006903 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006904 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006905 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006906 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006907 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006908 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006909 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006910 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006911 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006912 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006913 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006914 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006915 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006916 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006917 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006918 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006919 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006920 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006921 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006922 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006923 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006924 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006925 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006926 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006927 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006928 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006929 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006930 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006931 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006932 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006933 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006934 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006935 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006936 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006937 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006938 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006939 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006940 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006941 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006942 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006943 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006944 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006945 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006946 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006947 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006948 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006949 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006950 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006951 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006952 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00006953 // Interacts with -fstack-protector options.
6954 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
6955 break;
Peter Collingbourne0e497d12019-08-09 22:31:59 +00006956 case ParsedAttr::AT_CFICanonicalJumpTable:
6957 handleSimpleAttribute<CFICanonicalJumpTableAttr>(S, D, AL);
6958 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006959 case ParsedAttr::AT_StdCall:
6960 case ParsedAttr::AT_CDecl:
6961 case ParsedAttr::AT_FastCall:
6962 case ParsedAttr::AT_ThisCall:
6963 case ParsedAttr::AT_Pascal:
6964 case ParsedAttr::AT_RegCall:
6965 case ParsedAttr::AT_SwiftCall:
6966 case ParsedAttr::AT_VectorCall:
6967 case ParsedAttr::AT_MSABI:
6968 case ParsedAttr::AT_SysVABI:
6969 case ParsedAttr::AT_Pcs:
6970 case ParsedAttr::AT_IntelOclBicc:
6971 case ParsedAttr::AT_PreserveMost:
6972 case ParsedAttr::AT_PreserveAll:
Sander de Smalen44a22532018-11-26 16:38:37 +00006973 case ParsedAttr::AT_AArch64VectorPcs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006974 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006975 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006976 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006977 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006978 break;
Matthias Gehred293cbd2019-07-25 17:50:51 +00006979 case ParsedAttr::AT_Owner:
6980 case ParsedAttr::AT_Pointer:
6981 handleLifetimeCategoryAttr(S, D, AL);
6982 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006983 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006984 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006985 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006986 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006987 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006988 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006989 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006990 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006991 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006992 case ParsedAttr::AT_SwiftContext:
Erich Keane6a24e802019-09-13 17:39:31 +00006993 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006994 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006995 case ParsedAttr::AT_SwiftErrorResult:
Erich Keane6a24e802019-09-13 17:39:31 +00006996 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006997 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006998 case ParsedAttr::AT_SwiftIndirectResult:
Erich Keane6a24e802019-09-13 17:39:31 +00006999 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00007000 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007001 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007002 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00007003 break;
Louis Dionned2695792018-10-04 15:49:42 +00007004 case ParsedAttr::AT_ExcludeFromExplicitInstantiation:
7005 handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL);
7006 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007007 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007008 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00007009 break;
John McCall8d32c052012-05-22 21:28:12 +00007010
7011 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007012 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007013 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007014 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007015 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007016 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007017 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007018 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007019 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00007020 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007021 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007022 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00007023 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007024 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007025 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00007026 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007027 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007028 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00007029 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007030 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007031 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007032 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007033 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007034 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007035 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007036 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007037 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00007038 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00007039
Erich Keanee891aa92018-07-13 15:07:47 +00007040 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007041 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00007042 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007043
7044 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007045 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007046 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007047 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007048 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007049 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007050 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007051 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007052 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007053 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007054 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007055 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007056 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007057 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007058 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007059 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007060 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007061 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00007062 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007063 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007064 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00007065 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007066 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007067 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00007068 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007069 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007070 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007071 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007072 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007073 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007074 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007075 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007076 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007077 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007078 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007079 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007080 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007081 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007082 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007083 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007084 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007085 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007086 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007087 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007088 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007089 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007090 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007091 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007092 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007093
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007094 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007095 case ParsedAttr::AT_Capability:
7096 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007097 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007098 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007099 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007100 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007101 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00007102
Erich Keanee891aa92018-07-13 15:07:47 +00007103 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007104 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007105 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007106 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007107 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007108 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007109 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007110 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007111 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007112 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007113 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007114 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007115
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00007116 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007117 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007118 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00007119 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007120 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007121 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007122 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007123 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007124 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007125 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007126 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007127 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007128 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007129 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007130 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00007131 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007132 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007133 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00007134 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007135 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007136 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007137 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007138 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007139 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007140 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007141
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007142 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007143 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007144 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007145 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007146 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007147 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007148 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007149 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00007150 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00007151 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007152 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007153 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00007154 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007155 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007156 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007157 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007158 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007159 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007160 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00007161 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00007162
7163 // Move semantics attribute.
7164 case ParsedAttr::AT_Reinitializes:
7165 handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
7166 break;
Erik Pilkington5a559e62018-08-21 17:24:06 +00007167
7168 case ParsedAttr::AT_AlwaysDestroy:
7169 case ParsedAttr::AT_NoDestroy:
7170 handleDestroyAttr(S, D, AL);
7171 break;
JF Bastien14daa202018-12-18 05:12:21 +00007172
7173 case ParsedAttr::AT_Uninitialized:
7174 handleUninitializedAttr(S, D, AL);
7175 break;
Erik Pilkington1e368822019-01-04 18:33:06 +00007176
7177 case ParsedAttr::AT_ObjCExternallyRetained:
7178 handleObjCExternallyRetainedAttr(S, D, AL);
7179 break;
Erik Pilkingtone3cd7352019-02-11 23:21:39 +00007180
Artem Dergachevc333d772019-02-21 00:01:02 +00007181 case ParsedAttr::AT_MIGServerRoutine:
7182 handleMIGServerRoutineAttr(S, D, AL);
7183 break;
Reid Kleckner1181c9f2019-03-25 23:20:18 +00007184
7185 case ParsedAttr::AT_MSAllocator:
7186 handleMSAllocatorAttr(S, D, AL);
7187 break;
Simon Tatham7c11da02019-09-02 15:35:09 +01007188
7189 case ParsedAttr::AT_ArmMveAlias:
7190 handleArmMveAliasAttr(S, D, AL);
7191 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007192 }
7193}
7194
7195/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
7196/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00007197void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00007198 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00007199 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00007200 if (AttrList.empty())
7201 return;
7202
Erich Keanee891aa92018-07-13 15:07:47 +00007203 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00007204 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00007205
Joey Gouly2cd9db12013-12-13 16:15:28 +00007206 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00007207 // GCC accepts
7208 // static int a9 __attribute__((weakref));
7209 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00007210 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00007211 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
7212 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00007213 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00007214 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007215 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007216
Aaron Ballmanbe243a72014-12-04 22:45:31 +00007217 // FIXME: We should be able to handle this in TableGen as well. It would be
7218 // good to have a way to specify "these attributes must appear as a group",
7219 // for these. Additionally, it would be good to have a way to specify "these
7220 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00007221 if (!D->hasAttr<OpenCLKernelAttr>()) {
7222 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007223 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00007224 // FIXME: This emits a different error message than
7225 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00007226 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007227 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007228 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007229 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007230 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007231 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007232 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007233 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007234 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00007235 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
7236 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00007237 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
7238 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
7239 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7240 << A << ExpectedKernelFunction;
7241 D->setInvalidDecl();
7242 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
7243 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7244 << A << ExpectedKernelFunction;
7245 D->setInvalidDecl();
7246 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
7247 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7248 << A << ExpectedKernelFunction;
7249 D->setInvalidDecl();
7250 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
7251 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7252 << A << ExpectedKernelFunction;
7253 D->setInvalidDecl();
7254 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007255 }
7256 }
Erik Pilkington81d3f452019-02-13 20:32:37 +00007257
7258 // Do this check after processing D's attributes because the attribute
7259 // objc_method_family can change whether the given method is in the init
7260 // family, and it can be applied after objc_designated_initializer. This is a
7261 // bit of a hack, but we need it to be compatible with versions of clang that
7262 // processed the attribute list in the wrong order.
7263 if (D->hasAttr<ObjCDesignatedInitializerAttr>() &&
7264 cast<ObjCMethodDecl>(D)->getMethodFamily() != OMF_init) {
7265 Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
7266 D->dropAttr<ObjCDesignatedInitializerAttr>();
7267 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007268}
7269
Hiroshi Inoue939d9322017-06-30 05:40:31 +00007270// Helper for delayed processing TransparentUnion attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007271void Sema::ProcessDeclAttributeDelayed(Decl *D,
7272 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007273 for (const ParsedAttr &AL : AttrList)
7274 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00007275 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00007276 break;
7277 }
7278}
7279
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007280// Annotation attributes are the only attributes allowed after an access
7281// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00007282bool Sema::ProcessAccessDeclAttributeList(
7283 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007284 for (const ParsedAttr &AL : AttrList) {
7285 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00007286 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007287 } else {
Erich Keanec480f302018-07-12 21:09:05 +00007288 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007289 return true;
7290 }
7291 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007292 return false;
7293}
7294
John McCall42856de2011-10-01 05:17:03 +00007295/// checkUnusedDeclAttributes - Check a list of attributes to see if it
7296/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00007297static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00007298 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00007299 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007300 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
7301 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00007302 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00007303 continue;
John McCall42856de2011-10-01 05:17:03 +00007304
Erich Keanee891aa92018-07-13 15:07:47 +00007305 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00007306 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00007307 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007308 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00007309 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
7310 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007311 }
7312 }
7313}
7314
7315/// checkUnusedDeclAttributes - Given a declarator which is not being
7316/// used to build a declaration, complain about any decl attributes
7317/// which might be lying around on it.
7318void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00007319 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00007320 ::checkUnusedDeclAttributes(*this, D.getAttributes());
7321 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
7322 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
7323}
7324
Ryan Flynn7d470f32009-07-30 03:15:39 +00007325/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00007326/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00007327NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
7328 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00007329 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00007330 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007331 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00007332 FunctionDecl *NewFD;
7333 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00007334 // FIXME: Mangling?
7335 // FIXME: Is the qualifier info correct?
7336 // FIXME: Is the DeclContext correct?
Gauthier Harnisch796ed032019-06-14 08:56:20 +00007337 NewFD = FunctionDecl::Create(
7338 FD->getASTContext(), FD->getDeclContext(), Loc, Loc,
7339 DeclarationName(II), FD->getType(), FD->getTypeSourceInfo(), SC_None,
7340 false /*isInlineSpecified*/, FD->hasPrototype(), CSK_unspecified);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007341 NewD = NewFD;
7342
7343 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00007344 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00007345
7346 // Fake up parameter variables; they are declared as if this were
7347 // a typedef.
7348 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007349 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00007350 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00007351 for (const auto &AI : FT->param_types()) {
7352 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007353 Param->setScopeInfo(0, Params.size());
7354 Params.push_back(Param);
7355 }
David Blaikie9c70e042011-09-21 18:16:56 +00007356 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00007357 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007358 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00007359 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00007360 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00007361 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00007362 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007363 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00007364 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00007365 }
7366 return NewD;
7367}
7368
James Dennett634962f2012-06-14 21:40:34 +00007369/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00007370/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00007371void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00007372 if (W.getUsed()) return; // only do this once
7373 W.setUsed(true);
7374 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
7375 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00007376 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Erich Keane6a24e802019-09-13 17:39:31 +00007377 NewD->addAttr(
7378 AliasAttr::CreateImplicit(Context, NDId->getName(), W.getLocation()));
7379 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation(),
7380 AttributeCommonInfo::AS_Pragma));
Chris Lattnere6eab982009-09-08 18:10:11 +00007381 WeakTopLevelDecl.push_back(NewD);
7382 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
7383 // to insert Decl at TU scope, sorry.
7384 DeclContext *SavedContext = CurContext;
7385 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00007386 NewD->setDeclContext(CurContext);
7387 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00007388 PushOnScopeChains(NewD, S);
7389 CurContext = SavedContext;
7390 } else { // just add weak to existing
Erich Keane6a24e802019-09-13 17:39:31 +00007391 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation(),
7392 AttributeCommonInfo::AS_Pragma));
Ryan Flynn7d470f32009-07-30 03:15:39 +00007393 }
7394}
7395
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007396void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
7397 // It's valid to "forward-declare" #pragma weak, in which case we
7398 // have to do this.
7399 LoadExternalWeakUndeclaredIdentifiers();
7400 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00007401 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007402 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007403 if (VD->isExternC())
7404 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007405 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007406 if (FD->isExternC())
7407 ND = FD;
7408 if (ND) {
7409 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00007410 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007411 if (I != WeakUndeclaredIdentifiers.end()) {
7412 WeakInfo W = I->second;
7413 DeclApplyPragmaWeak(S, ND, W);
7414 WeakUndeclaredIdentifiers[Id] = W;
7415 }
7416 }
7417 }
7418 }
7419}
7420
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007421/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
7422/// it, apply them to D. This is a bit tricky because PD can have attributes
7423/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00007424void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007425 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00007426 if (!PD.getDeclSpec().getAttributes().empty())
7427 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00007428
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007429 // Walk the declarator structure, applying decl attributes that were in a type
7430 // position to the decl itself. This handles cases like:
7431 // int *__attr__(x)** D;
7432 // when X is a decl attribute.
7433 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00007434 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
7435 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00007436
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007437 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00007438 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00007439
7440 // Apply additional attributes specified by '#pragma clang attribute'.
7441 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007442}
John McCall28a6aea2009-11-04 02:18:39 +00007443
John McCall31168b02011-06-15 23:02:42 +00007444/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00007445/// If so, it'll still be annotated with an attribute that makes it
7446/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007447static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00007448 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00007449 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00007450 // Private ivars are always okay. Unfortunately, people don't
7451 // always properly make their ivars private, even in system headers.
7452 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007453 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
7454 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00007455 return false;
7456
John McCallc6af8c62015-10-28 05:03:19 +00007457 // Silently accept unsupported uses of __weak in both user and system
7458 // declarations when it's been disabled, for ease of integration with
7459 // -fno-objc-arc files. We do have to take some care against attempts
7460 // to define such things; for now, we've only done that for ivars
7461 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007462 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00007463 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
7464 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
7465 reason = UnavailableAttr::IR_ForbiddenWeak;
7466 return true;
7467 }
John McCallb61e14e2015-10-27 04:54:50 +00007468 }
7469
John McCallc6af8c62015-10-28 05:03:19 +00007470 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007471 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00007472 // Currently, all the failures dealt with this way are due to ARC
7473 // restrictions.
7474 reason = UnavailableAttr::IR_ARCForbiddenType;
7475 return true;
John McCallb61e14e2015-10-27 04:54:50 +00007476 }
7477
7478 return false;
John McCall31168b02011-06-15 23:02:42 +00007479}
7480
7481/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007482static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
7483 Decl *D) {
7484 auto Reason = UnavailableAttr::IR_None;
7485 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
7486 assert(Reason && "didn't set reason?");
7487 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00007488 return;
7489 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00007490 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007491 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00007492 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007493 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007494 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007495 DD.getForbiddenTypeDiagnostic() ==
7496 diag::err_arc_array_param_no_ownership) {
7497 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007498 return;
7499 }
7500 }
John McCall31168b02011-06-15 23:02:42 +00007501
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007502 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
7503 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
7504 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00007505}
7506
Manman Ren45b1ab12016-05-06 19:57:16 +00007507static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
7508 const Decl *D) {
7509 // Check each AvailabilityAttr to find the one for this platform.
7510 for (const auto *A : D->attrs()) {
7511 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
7512 // FIXME: this is copied from CheckAvailability. We should try to
7513 // de-duplicate.
7514
7515 // Check if this is an App Extension "platform", and if so chop off
7516 // the suffix for matching with the actual platform.
7517 StringRef ActualPlatform = Avail->getPlatform()->getName();
7518 StringRef RealizedPlatform = ActualPlatform;
7519 if (Context.getLangOpts().AppExt) {
7520 size_t suffix = RealizedPlatform.rfind("_app_extension");
7521 if (suffix != StringRef::npos)
7522 RealizedPlatform = RealizedPlatform.slice(0, suffix);
7523 }
7524
7525 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
7526
7527 // Match the platform name.
7528 if (RealizedPlatform == TargetPlatform)
7529 return Avail;
7530 }
7531 }
7532 return nullptr;
7533}
7534
Erik Pilkington9f866a72017-07-18 20:32:07 +00007535/// The diagnostic we should emit for \c D, and the declaration that
7536/// originated it, or \c AR_Available.
7537///
7538/// \param D The declaration to check.
7539/// \param Message If non-null, this will be populated with the message from
7540/// the availability attribute that is selected.
Erik Pilkington42578572018-09-10 22:20:09 +00007541/// \param ClassReceiver If we're checking the the method of a class message
7542/// send, the class. Otherwise nullptr.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007543static std::pair<AvailabilityResult, const NamedDecl *>
Erik Pilkington42578572018-09-10 22:20:09 +00007544ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D,
7545 std::string *Message,
7546 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007547 AvailabilityResult Result = D->getAvailability(Message);
7548
7549 // For typedefs, if the typedef declaration appears available look
7550 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007551 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007552 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007553 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007554 D = TT->getDecl();
7555 Result = D->getAvailability(Message);
7556 continue;
7557 }
7558 }
7559 break;
7560 }
7561
7562 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007563 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007564 if (IDecl->getDefinition()) {
7565 D = IDecl->getDefinition();
7566 Result = D->getAvailability(Message);
7567 }
7568 }
7569
7570 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7571 if (Result == AR_Available) {
7572 const DeclContext *DC = ECD->getDeclContext();
7573 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
7574 Result = TheEnumDecl->getAvailability(Message);
7575 D = TheEnumDecl;
7576 }
7577 }
7578
Erik Pilkington42578572018-09-10 22:20:09 +00007579 // For +new, infer availability from -init.
7580 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7581 if (S.NSAPIObj && ClassReceiver) {
7582 ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod(
7583 S.NSAPIObj->getInitSelector());
7584 if (Init && Result == AR_Available && MD->isClassMethod() &&
7585 MD->getSelector() == S.NSAPIObj->getNewSelector() &&
7586 MD->definedInNSObject(S.getASTContext())) {
7587 Result = Init->getAvailability(Message);
7588 D = Init;
7589 }
7590 }
7591 }
7592
Erik Pilkington9f866a72017-07-18 20:32:07 +00007593 return {Result, D};
7594}
7595
7596
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007597/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007598/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
7599/// in a deprecated context, but not the other way around.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007600static bool
7601ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
7602 VersionTuple DeclVersion, Decl *Ctx,
7603 const NamedDecl *OffendingDecl) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007604 assert(K != AR_Available && "Expected an unavailable declaration here!");
7605
7606 // Checks if we should emit the availability diagnostic in the context of C.
7607 auto CheckContext = [&](const Decl *C) {
7608 if (K == AR_NotYetIntroduced) {
7609 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
7610 if (AA->getIntroduced() >= DeclVersion)
7611 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007612 } else if (K == AR_Deprecated) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007613 if (C->isDeprecated())
7614 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007615 } else if (K == AR_Unavailable) {
7616 // It is perfectly fine to refer to an 'unavailable' Objective-C method
Alex Lorenz194d00e2019-01-17 18:12:45 +00007617 // when it is referenced from within the @implementation itself. In this
7618 // context, we interpret unavailable as a form of access control.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007619 if (const auto *MD = dyn_cast<ObjCMethodDecl>(OffendingDecl)) {
7620 if (const auto *Impl = dyn_cast<ObjCImplDecl>(C)) {
Alex Lorenz194d00e2019-01-17 18:12:45 +00007621 if (MD->getClassInterface() == Impl->getClassInterface())
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007622 return true;
7623 }
7624 }
7625 }
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007626
7627 if (C->isUnavailable())
7628 return true;
7629 return false;
7630 };
7631
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007632 do {
7633 if (CheckContext(Ctx))
7634 return false;
7635
7636 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00007637 // Unless it is "+load" method.
7638 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
7639 if (MethodD->isClassMethod() &&
7640 MethodD->getSelector().getAsString() == "load")
7641 return true;
7642
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007643 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007644 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
7645 if (CheckContext(Interface))
7646 return false;
7647 }
7648 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007649 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007650 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
7651 if (CheckContext(Interface))
7652 return false;
7653 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
7654
7655 return true;
7656}
7657
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007658static bool
7659shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
7660 const VersionTuple &DeploymentVersion,
7661 const VersionTuple &DeclVersion) {
7662 const auto &Triple = Context.getTargetInfo().getTriple();
7663 VersionTuple ForceAvailabilityFromVersion;
7664 switch (Triple.getOS()) {
7665 case llvm::Triple::IOS:
7666 case llvm::Triple::TvOS:
7667 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7668 break;
7669 case llvm::Triple::WatchOS:
7670 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7671 break;
7672 case llvm::Triple::Darwin:
7673 case llvm::Triple::MacOSX:
7674 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7675 break;
7676 default:
7677 // New targets should always warn about availability.
7678 return Triple.getVendor() == llvm::Triple::Apple;
7679 }
7680 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7681 DeclVersion >= ForceAvailabilityFromVersion;
7682}
7683
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007684static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7685 for (Decl *Ctx = OrigCtx; Ctx;
7686 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7687 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7688 return cast<NamedDecl>(Ctx);
7689 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7690 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7691 return Imp->getClassInterface();
7692 return CD;
7693 }
7694 }
7695
7696 return dyn_cast<NamedDecl>(OrigCtx);
7697}
7698
Alex Lorenz727c21e2017-07-26 13:58:02 +00007699namespace {
7700
7701struct AttributeInsertion {
7702 StringRef Prefix;
7703 SourceLocation Loc;
7704 StringRef Suffix;
7705
7706 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007707 return {" ", D->getEndLoc(), ""};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007708 }
7709 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7710 return {" ", Loc, ""};
7711 }
7712 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007713 return {"", D->getBeginLoc(), "\n"};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007714 }
7715};
7716
7717} // end anonymous namespace
7718
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007719/// Tries to parse a string as ObjC method name.
7720///
7721/// \param Name The string to parse. Expected to originate from availability
7722/// attribute argument.
7723/// \param SlotNames The vector that will be populated with slot names. In case
7724/// of unsuccessful parsing can contain invalid data.
7725/// \returns A number of method parameters if parsing was successful, None
7726/// otherwise.
7727static Optional<unsigned>
7728tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
7729 const LangOptions &LangOpts) {
7730 // Accept replacements starting with - or + as valid ObjC method names.
7731 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
7732 Name = Name.drop_front(1);
7733 if (Name.empty())
7734 return None;
7735 Name.split(SlotNames, ':');
7736 unsigned NumParams;
7737 if (Name.back() == ':') {
7738 // Remove an empty string at the end that doesn't represent any slot.
7739 SlotNames.pop_back();
7740 NumParams = SlotNames.size();
7741 } else {
7742 if (SlotNames.size() != 1)
7743 // Not a valid method name, just a colon-separated string.
7744 return None;
7745 NumParams = 0;
7746 }
7747 // Verify all slot names are valid.
7748 bool AllowDollar = LangOpts.DollarIdents;
7749 for (StringRef S : SlotNames) {
7750 if (S.empty())
7751 continue;
7752 if (!isValidIdentifier(S, AllowDollar))
7753 return None;
7754 }
7755 return NumParams;
7756}
7757
Alex Lorenz727c21e2017-07-26 13:58:02 +00007758/// Returns a source location in which it's appropriate to insert a new
7759/// attribute for the given declaration \D.
7760static Optional<AttributeInsertion>
7761createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7762 const LangOptions &LangOpts) {
7763 if (isa<ObjCPropertyDecl>(D))
7764 return AttributeInsertion::createInsertionAfter(D);
7765 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7766 if (MD->hasBody())
7767 return None;
7768 return AttributeInsertion::createInsertionAfter(D);
7769 }
7770 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7771 SourceLocation Loc =
7772 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7773 if (Loc.isInvalid())
7774 return None;
7775 // Insert after the 'struct'/whatever keyword.
7776 return AttributeInsertion::createInsertionAfter(Loc);
7777 }
7778 return AttributeInsertion::createInsertionBefore(D);
7779}
7780
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007781/// Actually emit an availability diagnostic for a reference to an unavailable
7782/// decl.
7783///
7784/// \param Ctx The context that the reference occurred in
7785/// \param ReferringDecl The exact declaration that was referenced.
7786/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007787/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007788/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7789/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7790/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007791static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007792 Decl *Ctx, const NamedDecl *ReferringDecl,
7793 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007794 StringRef Message,
7795 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007796 const ObjCInterfaceDecl *UnknownObjCClass,
7797 const ObjCPropertyDecl *ObjCProperty,
7798 bool ObjCPropertyAccess) {
7799 // Diagnostics for deprecated or unavailable.
7800 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007801 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007802 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007803
7804 // Matches 'diag::note_property_attribute' options.
7805 unsigned property_note_select;
7806
7807 // Matches diag::note_availability_specified_here.
7808 unsigned available_here_select_kind;
7809
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007810 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007811 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007812 DeclVersion = AA->getIntroduced();
7813
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007814 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx,
7815 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007816 return;
7817
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007818 SourceLocation Loc = Locs.front();
7819
Erik Pilkington8b352c42017-08-14 19:49:12 +00007820 // The declaration can have multiple availability attributes, we are looking
7821 // at one of them.
7822 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7823 if (A && A->isInherited()) {
7824 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7825 Redecl = Redecl->getPreviousDecl()) {
7826 const AvailabilityAttr *AForRedecl =
7827 getAttrForPlatform(S.Context, Redecl);
7828 if (AForRedecl && !AForRedecl->isInherited()) {
7829 // If D is a declaration with inherited attributes, the note should
7830 // point to the declaration with actual attributes.
7831 NoteLocation = Redecl->getLocation();
7832 break;
7833 }
7834 }
7835 }
7836
Aaron Ballmanfb237522014-10-15 15:37:51 +00007837 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007838 case AR_NotYetIntroduced: {
7839 // We would like to emit the diagnostic even if -Wunguarded-availability is
7840 // not specified for deployment targets >= to iOS 11 or equivalent or
7841 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7842 // later.
7843 const AvailabilityAttr *AA =
7844 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7845 VersionTuple Introduced = AA->getIntroduced();
7846
7847 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7848 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7849 Introduced);
7850 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7851 : diag::warn_unguarded_availability;
7852
Erik Pilkington0535b0f2019-01-14 19:17:31 +00007853 std::string PlatformName = AvailabilityAttr::getPrettyPlatformName(
7854 S.getASTContext().getTargetInfo().getPlatformName());
Erik Pilkington8b352c42017-08-14 19:49:12 +00007855
Erik Pilkington0535b0f2019-01-14 19:17:31 +00007856 S.Diag(Loc, Warning) << OffendingDecl << PlatformName
7857 << Introduced.getAsString();
7858
7859 S.Diag(OffendingDecl->getLocation(),
7860 diag::note_partial_availability_specified_here)
7861 << OffendingDecl << PlatformName << Introduced.getAsString()
7862 << S.Context.getTargetInfo().getPlatformMinVersion().getAsString();
Erik Pilkington8b352c42017-08-14 19:49:12 +00007863
7864 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007865 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007866 if (TD->getDeclName().isEmpty()) {
7867 S.Diag(TD->getLocation(),
7868 diag::note_decl_unguarded_availability_silence)
7869 << /*Anonymous*/ 1 << TD->getKindName();
7870 return;
7871 }
7872 auto FixitNoteDiag =
7873 S.Diag(Enclosing->getLocation(),
7874 diag::note_decl_unguarded_availability_silence)
7875 << /*Named*/ 0 << Enclosing;
7876 // Don't offer a fixit for declarations with availability attributes.
7877 if (Enclosing->hasAttr<AvailabilityAttr>())
7878 return;
7879 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7880 return;
7881 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7882 Enclosing, S.getSourceManager(), S.getLangOpts());
7883 if (!Insertion)
7884 return;
7885 std::string PlatformName =
7886 AvailabilityAttr::getPlatformNameSourceSpelling(
7887 S.getASTContext().getTargetInfo().getPlatformName())
7888 .lower();
7889 std::string Introduced =
7890 OffendingDecl->getVersionIntroduced().getAsString();
7891 FixitNoteDiag << FixItHint::CreateInsertion(
7892 Insertion->Loc,
7893 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7894 "(" + Introduced + "))" + Insertion->Suffix)
7895 .str());
7896 }
7897 return;
7898 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007899 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007900 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7901 : diag::warn_property_method_deprecated;
7902 diag_message = diag::warn_deprecated_message;
7903 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7904 property_note_select = /* deprecated */ 0;
7905 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007906 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7907 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007908 break;
7909
Erik Pilkington796a3e22016-08-05 22:59:03 +00007910 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007911 diag = !ObjCPropertyAccess ? diag::err_unavailable
7912 : diag::err_property_method_unavailable;
7913 diag_message = diag::err_unavailable_message;
7914 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7915 property_note_select = /* unavailable */ 1;
7916 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007917
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007918 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7919 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007920 // Most of these failures are due to extra restrictions in ARC;
7921 // reflect that in the primary diagnostic when applicable.
7922 auto flagARCError = [&] {
7923 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007924 S.getSourceManager().isInSystemHeader(
7925 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007926 diag = diag::err_unavailable_in_arc;
7927 };
7928
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007929 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007930 case UnavailableAttr::IR_None: break;
7931
7932 case UnavailableAttr::IR_ARCForbiddenType:
7933 flagARCError();
7934 diag_available_here = diag::note_arc_forbidden_type;
7935 break;
7936
7937 case UnavailableAttr::IR_ForbiddenWeak:
7938 if (S.getLangOpts().ObjCWeakRuntime)
7939 diag_available_here = diag::note_arc_weak_disabled;
7940 else
7941 diag_available_here = diag::note_arc_weak_no_runtime;
7942 break;
7943
7944 case UnavailableAttr::IR_ARCForbiddenConversion:
7945 flagARCError();
7946 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7947 break;
7948
7949 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7950 flagARCError();
7951 diag_available_here = diag::note_arc_init_returns_unrelated;
7952 break;
7953
7954 case UnavailableAttr::IR_ARCFieldWithOwnership:
7955 flagARCError();
7956 diag_available_here = diag::note_arc_field_with_ownership;
7957 break;
7958 }
7959 }
John McCallb61e14e2015-10-27 04:54:50 +00007960 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007961 break;
7962
Erik Pilkington796a3e22016-08-05 22:59:03 +00007963 case AR_Available:
7964 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007965 }
7966
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007967 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007968 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007969 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007970 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7971 Replacement = AL->getReplacement();
7972 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7973 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007974
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007975 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00007976 if (!Replacement.empty())
7977 UseRange =
7978 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007979 if (UseRange.isValid()) {
7980 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
7981 Selector Sel = MethodDecl->getSelector();
7982 SmallVector<StringRef, 12> SelectorSlotNames;
7983 Optional<unsigned> NumParams = tryParseObjCMethodName(
7984 Replacement, SelectorSlotNames, S.getLangOpts());
7985 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
7986 assert(SelectorSlotNames.size() == Locs.size());
7987 for (unsigned I = 0; I < Locs.size(); ++I) {
7988 if (!Sel.getNameForSlot(I).empty()) {
7989 CharSourceRange NameRange = CharSourceRange::getCharRange(
7990 Locs[I], S.getLocForEndOfToken(Locs[I]));
7991 FixIts.push_back(FixItHint::CreateReplacement(
7992 NameRange, SelectorSlotNames[I]));
7993 } else
7994 FixIts.push_back(
7995 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
7996 }
7997 } else
7998 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7999 } else
8000 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
8001 }
Manman Renc7890fe2016-03-16 18:50:49 +00008002 }
8003
Aaron Ballmanfb237522014-10-15 15:37:51 +00008004 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008005 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008006 if (ObjCProperty)
8007 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
8008 << ObjCProperty->getDeclName() << property_note_select;
8009 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008010 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008011 if (ObjCProperty)
8012 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
8013 << ObjCProperty->getDeclName() << property_note_select;
8014 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008015 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008016 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
8017 }
8018
Erik Pilkington8b352c42017-08-14 19:49:12 +00008019 S.Diag(NoteLocation, diag_available_here)
8020 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008021}
8022
8023static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
8024 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00008025 assert(DD.Kind == DelayedDiagnostic::Availability &&
8026 "Expected an availability diagnostic here");
8027
Aaron Ballmanfb237522014-10-15 15:37:51 +00008028 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00008029 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008030 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008031 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
8032 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
8033 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00008034}
8035
John McCall2ec85372012-05-07 06:16:41 +00008036void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
8037 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00008038 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00008039 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00008040
John McCall2ec85372012-05-07 06:16:41 +00008041 // When delaying diagnostics to run in the context of a parsed
8042 // declaration, we only want to actually emit anything if parsing
8043 // succeeds.
8044 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00008045
John McCall2ec85372012-05-07 06:16:41 +00008046 // We emit all the active diagnostics in this pool or any of its
8047 // parents. In general, we'll get one pool for the decl spec
8048 // and a child pool for each declarator; in a decl group like:
8049 // deprecated_typedef foo, *bar, baz();
8050 // only the declarator pops will be passed decls. This is correct;
8051 // we really do need to consider delayed diagnostics from the decl spec
8052 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00008053 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00008054 do {
Richard Smith5c9b3b72018-09-25 22:12:44 +00008055 bool AnyAccessFailures = false;
John McCall6347b682012-05-07 06:16:58 +00008056 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00008057 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
8058 // This const_cast is a bit lame. Really, Triggered should be mutable.
8059 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00008060 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00008061 continue;
8062
John McCallc1465822011-02-14 07:13:47 +00008063 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00008064 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00008065 // Don't bother giving deprecation/unavailable diagnostics if
8066 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00008067 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00008068 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00008069 break;
8070
8071 case DelayedDiagnostic::Access:
Richard Smith5c9b3b72018-09-25 22:12:44 +00008072 // Only produce one access control diagnostic for a structured binding
8073 // declaration: we don't need to tell the user that all the fields are
8074 // inaccessible one at a time.
8075 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
8076 continue;
John McCall2ec85372012-05-07 06:16:41 +00008077 HandleDelayedAccessCheck(diag, decl);
Richard Smith5c9b3b72018-09-25 22:12:44 +00008078 if (diag.Triggered)
8079 AnyAccessFailures = true;
John McCall86121512010-01-27 03:50:35 +00008080 break;
John McCall31168b02011-06-15 23:02:42 +00008081
8082 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00008083 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00008084 break;
John McCall86121512010-01-27 03:50:35 +00008085 }
8086 }
John McCall2ec85372012-05-07 06:16:41 +00008087 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00008088}
8089
John McCall6347b682012-05-07 06:16:58 +00008090/// Given a set of delayed diagnostics, re-emit them as if they had
8091/// been delayed in the current context instead of in the given pool.
8092/// Essentially, this just moves them to the current pool.
8093void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
8094 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
8095 assert(curPool && "re-emitting in undelayed context not supported");
8096 curPool->steal(pool);
8097}
8098
Erik Pilkington9f866a72017-07-18 20:32:07 +00008099static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
8100 const NamedDecl *ReferringDecl,
8101 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008102 StringRef Message,
8103 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008104 const ObjCInterfaceDecl *UnknownObjCClass,
8105 const ObjCPropertyDecl *ObjCProperty,
8106 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00008107 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00008108 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
8109 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008110 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008111 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008112 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00008113 return;
8114 }
8115
Erik Pilkington9f866a72017-07-18 20:32:07 +00008116 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
8117 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008118 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008119 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00008120}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00008121
Erik Pilkington5cd57172016-08-16 17:44:11 +00008122namespace {
8123
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008124/// Returns true if the given statement can be a body-like child of \p Parent.
8125bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
8126 switch (Parent->getStmtClass()) {
8127 case Stmt::IfStmtClass:
8128 return cast<IfStmt>(Parent)->getThen() == S ||
8129 cast<IfStmt>(Parent)->getElse() == S;
8130 case Stmt::WhileStmtClass:
8131 return cast<WhileStmt>(Parent)->getBody() == S;
8132 case Stmt::DoStmtClass:
8133 return cast<DoStmt>(Parent)->getBody() == S;
8134 case Stmt::ForStmtClass:
8135 return cast<ForStmt>(Parent)->getBody() == S;
8136 case Stmt::CXXForRangeStmtClass:
8137 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
8138 case Stmt::ObjCForCollectionStmtClass:
8139 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
8140 case Stmt::CaseStmtClass:
8141 case Stmt::DefaultStmtClass:
8142 return cast<SwitchCase>(Parent)->getSubStmt() == S;
8143 default:
8144 return false;
8145 }
8146}
8147
8148class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
8149 const Stmt *Target;
8150
8151public:
8152 bool VisitStmt(Stmt *S) { return S != Target; }
8153
8154 /// Returns true if the given statement is present in the given declaration.
8155 static bool isContained(const Stmt *Target, const Decl *D) {
8156 StmtUSEFinder Visitor;
8157 Visitor.Target = Target;
8158 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
8159 }
8160};
8161
8162/// Traverses the AST and finds the last statement that used a given
8163/// declaration.
8164class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
8165 const Decl *D;
8166
8167public:
8168 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
8169 if (DRE->getDecl() == D)
8170 return false;
8171 return true;
8172 }
8173
8174 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
8175 const CompoundStmt *Scope) {
8176 LastDeclUSEFinder Visitor;
8177 Visitor.D = D;
8178 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
8179 const Stmt *S = *I;
8180 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
8181 return S;
8182 }
8183 return nullptr;
8184 }
8185};
8186
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008187/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00008188///
8189/// This is done with a traversal of the AST of a function that makes reference
8190/// to a partially available declaration. Whenever we encounter an \c if of the
8191/// form: \c if(@available(...)), we use the version from the condition to visit
8192/// the then statement.
8193class DiagnoseUnguardedAvailability
8194 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
8195 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
8196
8197 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008198 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00008199
8200 /// Stack of potentially nested 'if (@available(...))'s.
8201 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008202 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00008203
Erik Pilkington42578572018-09-10 22:20:09 +00008204 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range,
8205 ObjCInterfaceDecl *ClassReceiver = nullptr);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008206
8207public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008208 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
8209 : SemaRef(SemaRef), Ctx(Ctx) {
8210 AvailabilityStack.push_back(
8211 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008212 }
8213
Alex Lorenz6f911122017-05-16 13:58:53 +00008214 bool TraverseDecl(Decl *D) {
8215 // Avoid visiting nested functions to prevent duplicate warnings.
8216 if (!D || isa<FunctionDecl>(D))
8217 return true;
8218 return Base::TraverseDecl(D);
8219 }
8220
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008221 bool TraverseStmt(Stmt *S) {
8222 if (!S)
8223 return true;
8224 StmtStack.push_back(S);
8225 bool Result = Base::TraverseStmt(S);
8226 StmtStack.pop_back();
8227 return Result;
8228 }
8229
Erik Pilkington5cd57172016-08-16 17:44:11 +00008230 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
8231
8232 bool TraverseIfStmt(IfStmt *If);
8233
Alex Lorenz6f911122017-05-16 13:58:53 +00008234 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
8235
Erik Pilkingtonba87c622017-08-18 20:20:56 +00008236 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
8237 // to any useful diagnostics.
8238 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
8239
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008240 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
8241 if (PRE->isClassReceiver())
8242 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
8243 return true;
8244 }
8245
Erik Pilkington5cd57172016-08-16 17:44:11 +00008246 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
Erik Pilkington42578572018-09-10 22:20:09 +00008247 if (ObjCMethodDecl *D = Msg->getMethodDecl()) {
8248 ObjCInterfaceDecl *ID = nullptr;
8249 QualType ReceiverTy = Msg->getClassReceiver();
8250 if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType())
8251 ID = ReceiverTy->getAsObjCInterfaceType()->getInterface();
8252
Erik Pilkington5cd57172016-08-16 17:44:11 +00008253 DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00008254 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID);
8255 }
Erik Pilkington5cd57172016-08-16 17:44:11 +00008256 return true;
8257 }
8258
8259 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
8260 DiagnoseDeclAvailability(DRE->getDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008261 SourceRange(DRE->getBeginLoc(), DRE->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00008262 return true;
8263 }
8264
8265 bool VisitMemberExpr(MemberExpr *ME) {
8266 DiagnoseDeclAvailability(ME->getMemberDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008267 SourceRange(ME->getBeginLoc(), ME->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00008268 return true;
8269 }
8270
Alex Lorenz0a484ba2017-05-24 15:15:29 +00008271 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008272 SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use)
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008273 << (!SemaRef.getLangOpts().ObjC);
Alex Lorenz0a484ba2017-05-24 15:15:29 +00008274 return true;
8275 }
8276
Erik Pilkington5cd57172016-08-16 17:44:11 +00008277 bool VisitTypeLoc(TypeLoc Ty);
8278};
8279
8280void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00008281 NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008282 AvailabilityResult Result;
8283 const NamedDecl *OffendingDecl;
8284 std::tie(Result, OffendingDecl) =
Erik Pilkington42578572018-09-10 22:20:09 +00008285 ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008286 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008287 // All other diagnostic kinds have already been handled in
8288 // DiagnoseAvailabilityOfDecl.
8289 if (Result != AR_NotYetIntroduced)
8290 return;
8291
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008292 const AvailabilityAttr *AA =
8293 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008294 VersionTuple Introduced = AA->getIntroduced();
8295
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008296 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008297 return;
8298
8299 // If the context of this function is less available than D, we should not
8300 // emit a diagnostic.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00008301 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx,
8302 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008303 return;
8304
Alex Lorenzc9a369f2017-06-22 17:02:24 +00008305 // We would like to emit the diagnostic even if -Wunguarded-availability is
8306 // not specified for deployment targets >= to iOS 11 or equivalent or
8307 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
8308 // later.
8309 unsigned DiagKind =
8310 shouldDiagnoseAvailabilityByDefault(
8311 SemaRef.Context,
8312 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
8313 ? diag::warn_unguarded_availability_new
8314 : diag::warn_unguarded_availability;
8315
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008316 std::string PlatformName = AvailabilityAttr::getPrettyPlatformName(
8317 SemaRef.getASTContext().getTargetInfo().getPlatformName());
8318
Alex Lorenzc9a369f2017-06-22 17:02:24 +00008319 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008320 << Range << D << PlatformName << Introduced.getAsString();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008321
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008322 SemaRef.Diag(OffendingDecl->getLocation(),
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008323 diag::note_partial_availability_specified_here)
8324 << OffendingDecl << PlatformName << Introduced.getAsString()
8325 << SemaRef.Context.getTargetInfo()
8326 .getPlatformMinVersion()
8327 .getAsString();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008328
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008329 auto FixitDiag =
8330 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
8331 << Range << D
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008332 << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0
8333 : /*__builtin_available*/ 1);
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008334
8335 // Find the statement which should be enclosed in the if @available check.
8336 if (StmtStack.empty())
8337 return;
8338 const Stmt *StmtOfUse = StmtStack.back();
8339 const CompoundStmt *Scope = nullptr;
8340 for (const Stmt *S : llvm::reverse(StmtStack)) {
8341 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
8342 Scope = CS;
8343 break;
8344 }
8345 if (isBodyLikeChildStmt(StmtOfUse, S)) {
8346 // The declaration won't be seen outside of the statement, so we don't
8347 // have to wrap the uses of any declared variables in if (@available).
8348 // Therefore we can avoid setting Scope here.
8349 break;
8350 }
8351 StmtOfUse = S;
8352 }
8353 const Stmt *LastStmtOfUse = nullptr;
8354 if (isa<DeclStmt>(StmtOfUse) && Scope) {
8355 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
8356 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
8357 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
8358 break;
8359 }
8360 }
8361 }
8362
8363 const SourceManager &SM = SemaRef.getSourceManager();
8364 SourceLocation IfInsertionLoc =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008365 SM.getExpansionLoc(StmtOfUse->getBeginLoc());
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008366 SourceLocation StmtEndLoc =
8367 SM.getExpansionRange(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008368 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc())
Richard Smithb5f81712018-04-30 05:25:48 +00008369 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008370 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
8371 return;
8372
8373 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
8374 const char *ExtraIndentation = " ";
8375 std::string FixItString;
8376 llvm::raw_string_ostream FixItOS(FixItString);
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008377 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available"
8378 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00008379 << "("
8380 << AvailabilityAttr::getPlatformNameSourceSpelling(
8381 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008382 << " " << Introduced.getAsString() << ", *)) {\n"
8383 << Indentation << ExtraIndentation;
8384 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
8385 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
8386 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
8387 /*SkipTrailingWhitespaceAndNewLine=*/false);
8388 if (ElseInsertionLoc.isInvalid())
8389 ElseInsertionLoc =
8390 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
8391 FixItOS.str().clear();
8392 FixItOS << "\n"
8393 << Indentation << "} else {\n"
8394 << Indentation << ExtraIndentation
8395 << "// Fallback on earlier versions\n"
8396 << Indentation << "}";
8397 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008398 }
8399}
8400
8401bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
8402 const Type *TyPtr = Ty.getTypePtr();
8403 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
8404
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008405 if (Range.isInvalid())
8406 return true;
8407
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008408 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008409 TagDecl *TD = TT->getDecl();
8410 DiagnoseDeclAvailability(TD, Range);
8411
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008412 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008413 TypedefNameDecl *D = TD->getDecl();
8414 DiagnoseDeclAvailability(D, Range);
8415
8416 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
8417 if (NamedDecl *D = ObjCO->getInterface())
8418 DiagnoseDeclAvailability(D, Range);
8419 }
8420
8421 return true;
8422}
8423
8424bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
8425 VersionTuple CondVersion;
8426 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
8427 CondVersion = E->getVersion();
8428
8429 // If we're using the '*' case here or if this check is redundant, then we
8430 // use the enclosing version to check both branches.
8431 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00008432 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008433 } else {
8434 // This isn't an availability checking 'if', we can just continue.
8435 return Base::TraverseIfStmt(If);
8436 }
8437
8438 AvailabilityStack.push_back(CondVersion);
8439 bool ShouldContinue = TraverseStmt(If->getThen());
8440 AvailabilityStack.pop_back();
8441
8442 return ShouldContinue && TraverseStmt(If->getElse());
8443}
8444
8445} // end anonymous namespace
8446
8447void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
8448 Stmt *Body = nullptr;
8449
8450 if (auto *FD = D->getAsFunction()) {
8451 // FIXME: We only examine the pattern decl for availability violations now,
8452 // but we should also examine instantiated templates.
8453 if (FD->isTemplateInstantiation())
8454 return;
8455
8456 Body = FD->getBody();
8457 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
8458 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00008459 else if (auto *BD = dyn_cast<BlockDecl>(D))
8460 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008461
8462 assert(Body && "Need a body here!");
8463
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008464 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008465}
Erik Pilkington9f866a72017-07-18 20:32:07 +00008466
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008467void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
8468 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008469 const ObjCInterfaceDecl *UnknownObjCClass,
8470 bool ObjCPropertyAccess,
Erik Pilkington42578572018-09-10 22:20:09 +00008471 bool AvoidPartialAvailabilityChecks,
8472 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008473 std::string Message;
8474 AvailabilityResult Result;
8475 const NamedDecl* OffendingDecl;
8476 // See if this declaration is unavailable, deprecated, or partial.
Erik Pilkington42578572018-09-10 22:20:09 +00008477 std::tie(Result, OffendingDecl) =
8478 ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver);
Erik Pilkington9f866a72017-07-18 20:32:07 +00008479 if (Result == AR_Available)
8480 return;
8481
8482 if (Result == AR_NotYetIntroduced) {
8483 if (AvoidPartialAvailabilityChecks)
8484 return;
8485
8486 // We need to know the @available context in the current function to
8487 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
8488 // when we're done parsing the current function.
8489 if (getCurFunctionOrMethodDecl()) {
8490 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
8491 return;
8492 } else if (getCurBlock() || getCurLambda()) {
8493 getCurFunction()->HasPotentialAvailabilityViolations = true;
8494 return;
8495 }
8496 }
8497
8498 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008499 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008500 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
8501 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
8502 if (PDeclResult == Result)
8503 ObjCPDecl = PD;
8504 }
8505 }
8506
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008507 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008508 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
8509}