blob: 5728df428a371962cbac9028b1a09604d1765fc8 [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
Guillaume Chatelet98f31512019-09-25 11:31:28 +02001072static void handleNoBuiltinAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1073 static constexpr const StringRef kWildcard = "*";
1074
1075 llvm::SmallVector<StringRef, 16> Names;
1076 bool HasWildcard = false;
1077
1078 const auto AddBuiltinName = [&Names, &HasWildcard](StringRef Name) {
1079 if (Name == kWildcard)
1080 HasWildcard = true;
1081 Names.push_back(Name);
1082 };
1083
1084 // Add previously defined attributes.
1085 if (const auto *NBA = D->getAttr<NoBuiltinAttr>())
1086 for (StringRef BuiltinName : NBA->builtinNames())
1087 AddBuiltinName(BuiltinName);
1088
1089 // Add current attributes.
1090 if (AL.getNumArgs() == 0)
1091 AddBuiltinName(kWildcard);
1092 else
1093 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
1094 StringRef BuiltinName;
1095 SourceLocation LiteralLoc;
1096 if (!S.checkStringLiteralArgumentAttr(AL, I, BuiltinName, &LiteralLoc))
1097 return;
1098
Guillaume Chatelet1c85a2e2019-10-29 17:28:34 +01001099 if (Builtin::Context::isBuiltinFunc(BuiltinName))
Guillaume Chatelet98f31512019-09-25 11:31:28 +02001100 AddBuiltinName(BuiltinName);
1101 else
1102 S.Diag(LiteralLoc, diag::warn_attribute_no_builtin_invalid_builtin_name)
1103 << BuiltinName << AL.getAttrName()->getName();
1104 }
1105
1106 // Repeating the same attribute is fine.
1107 llvm::sort(Names);
1108 Names.erase(std::unique(Names.begin(), Names.end()), Names.end());
1109
1110 // Empty no_builtin must be on its own.
1111 if (HasWildcard && Names.size() > 1)
1112 S.Diag(D->getLocation(),
1113 diag::err_attribute_no_builtin_wildcard_or_builtin_name)
1114 << AL.getAttrName()->getName();
1115
1116 if (D->hasAttr<NoBuiltinAttr>())
1117 D->dropAttr<NoBuiltinAttr>();
1118 D->addAttr(::new (S.Context)
1119 NoBuiltinAttr(S.Context, AL, Names.data(), Names.size()));
1120}
1121
Erich Keanee891aa92018-07-13 15:07:47 +00001122static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001123 if (D->hasAttr<PassObjectSizeAttr>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001124 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001125 return;
1126 }
1127
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001128 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001129 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001130 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001131 return;
1132
1133 // pass_object_size's argument is passed in as the second argument of
1134 // __builtin_object_size. So, it has the same constraints as that second
1135 // argument; namely, it must be in the range [0, 3].
1136 if (Type > 3) {
Aaron Ballman52c9ad22019-02-12 13:04:11 +00001137 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00001138 << AL << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001139 return;
1140 }
1141
1142 // pass_object_size is only supported on constant pointer parameters; as a
1143 // kindness to users, we allow the parameter to be non-const for declarations.
1144 // At this point, we have no clue if `D` belongs to a function declaration or
1145 // definition, so we defer the constness check until later.
1146 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001147 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001148 return;
1149 }
1150
Erich Keane6a24e802019-09-13 17:39:31 +00001151 D->addAttr(::new (S.Context) PassObjectSizeAttr(S.Context, AL, (int)Type));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001152}
1153
Erich Keanee891aa92018-07-13 15:07:47 +00001154static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001155 ConsumableAttr::ConsumedState DefaultState;
1156
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001157 if (AL.isArgIdent(0)) {
1158 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001159 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1160 DefaultState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001161 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1162 << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001163 return;
1164 }
David Blaikie16f76d22013-09-06 01:28:43 +00001165 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001166 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001167 << AL << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001168 return;
1169 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001170
Erich Keane6a24e802019-09-13 17:39:31 +00001171 D->addAttr(::new (S.Context) ConsumableAttr(S.Context, AL, DefaultState));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001172}
1173
1174static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Erich Keanee891aa92018-07-13 15:07:47 +00001175 const ParsedAttr &AL) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001176 QualType ThisType = MD->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001177
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001178 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1179 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001180 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001181 RD->getNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001182
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001183 return false;
1184 }
1185 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001186
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001187 return true;
1188}
1189
Erich Keanee891aa92018-07-13 15:07:47 +00001190static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001191 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001192 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001193
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001194 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001195 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001196
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001197 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001198 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001199 CallableWhenAttr::ConsumedState CallableState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001200
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001201 StringRef StateString;
1202 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001203 if (AL.isArgIdent(ArgIndex)) {
1204 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001205 StateString = Ident->Ident->getName();
1206 Loc = Ident->Loc;
1207 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001208 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001209 return;
1210 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001211
1212 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001213 CallableState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001214 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001215 return;
1216 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001217
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001218 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001219 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001220
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001221 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00001222 CallableWhenAttr(S.Context, AL, States.data(), States.size()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001223}
1224
Erich Keanee891aa92018-07-13 15:07:47 +00001225static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001226 ParamTypestateAttr::ConsumedState ParamState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001227
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001228 if (AL.isArgIdent(0)) {
1229 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001230 StringRef StateString = Ident->Ident->getName();
1231
1232 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1233 ParamState)) {
1234 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00001235 << AL << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001236 return;
1237 }
1238 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001239 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1240 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001241 return;
1242 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001243
DeLesley Hutchins69391772013-10-17 23:23:53 +00001244 // FIXME: This check is currently being done in the analysis. It can be
1245 // enabled here only after the parser propagates attributes at
1246 // template specialization definition, not declaration.
1247 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1248 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1249 //
1250 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001251 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001252 // ReturnType.getAsString();
1253 // return;
1254 //}
Fangrui Song6907ce22018-07-30 19:24:48 +00001255
Erich Keane6a24e802019-09-13 17:39:31 +00001256 D->addAttr(::new (S.Context) ParamTypestateAttr(S.Context, AL, ParamState));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001257}
1258
Erich Keanee891aa92018-07-13 15:07:47 +00001259static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001260 ReturnTypestateAttr::ConsumedState ReturnState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001261
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001262 if (AL.isArgIdent(0)) {
1263 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001264 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1265 ReturnState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001266 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1267 << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001268 return;
1269 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001270 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001271 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1272 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001273 return;
1274 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001275
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001276 // FIXME: This check is currently being done in the analysis. It can be
1277 // enabled here only after the parser propagates attributes at
1278 // template specialization definition, not declaration.
1279 //QualType ReturnType;
1280 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001281 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1282 // ReturnType = Param->getType();
1283 //
1284 //} else if (const CXXConstructorDecl *Constructor =
1285 // dyn_cast<CXXConstructorDecl>(D)) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001286 // ReturnType = Constructor->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001287 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001288 //} else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001289 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001290 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1291 //}
1292 //
1293 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1294 //
1295 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1296 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1297 // ReturnType.getAsString();
1298 // return;
1299 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001300
Erich Keane6a24e802019-09-13 17:39:31 +00001301 D->addAttr(::new (S.Context) ReturnTypestateAttr(S.Context, AL, ReturnState));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001302}
1303
Erich Keanee891aa92018-07-13 15:07:47 +00001304static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001305 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001306 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001307
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001308 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001309 if (AL.isArgIdent(0)) {
1310 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001311 StringRef Param = Ident->Ident->getName();
1312 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001313 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1314 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001315 return;
1316 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001317 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001318 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1319 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001320 return;
1321 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001322
Erich Keane6a24e802019-09-13 17:39:31 +00001323 D->addAttr(::new (S.Context) SetTypestateAttr(S.Context, AL, NewState));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001324}
1325
Erich Keanee891aa92018-07-13 15:07:47 +00001326static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001327 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001328 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001329
1330 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001331 if (AL.isArgIdent(0)) {
1332 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001333 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001334 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001335 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1336 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001337 return;
1338 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001339 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001340 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1341 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001342 return;
1343 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001344
Erich Keane6a24e802019-09-13 17:39:31 +00001345 D->addAttr(::new (S.Context) TestTypestateAttr(S.Context, AL, TestState));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001346}
1347
Erich Keanee891aa92018-07-13 15:07:47 +00001348static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001349 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001350 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001351}
1352
Erich Keanee891aa92018-07-13 15:07:47 +00001353static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001354 if (auto *TD = dyn_cast<TagDecl>(D))
Erich Keane6a24e802019-09-13 17:39:31 +00001355 TD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001356 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001357 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1358 !FD->getType()->isIncompleteType() &&
1359 FD->isBitField() &&
1360 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001361
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001362 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1363 if (BitfieldByteAligned)
1364 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001365 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001366 << AL << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001367 else
Erich Keane6a24e802019-09-13 17:39:31 +00001368 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001369 } else {
1370 // Report warning about changed offset in the newer compiler versions.
1371 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001372 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001373
Erich Keane6a24e802019-09-13 17:39:31 +00001374 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001375 }
1376
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001377 } else
Erich Keane44bacdf2018-08-09 13:21:32 +00001378 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001379}
1380
Erich Keanee891aa92018-07-13 15:07:47 +00001381static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001382 // The IBOutlet/IBOutletCollection attributes only apply to instance
1383 // variables or properties of Objective-C classes. The outlet must also
1384 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001385 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001386 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001387 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001388 << AL << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001389 return false;
1390 }
1391 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001392 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001393 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001394 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001395 << AL << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001396 return false;
1397 }
1398 }
1399 else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001400 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001401 return false;
1402 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001403
Ted Kremenek7fd17232011-09-29 07:02:25 +00001404 return true;
1405}
1406
Erich Keanee891aa92018-07-13 15:07:47 +00001407static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001408 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001409 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001410
Erich Keane6a24e802019-09-13 17:39:31 +00001411 D->addAttr(::new (S.Context) IBOutletAttr(S.Context, AL));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001412}
1413
Erich Keanee891aa92018-07-13 15:07:47 +00001414static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001415
1416 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001417 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001418 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001419 return;
1420 }
1421
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001422 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001423 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001424
Richard Smithb1f9a282013-10-31 01:56:18 +00001425 ParsedType PT;
1426
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001427 if (AL.hasParsedType())
1428 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001429 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001430 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001431 S.getScopeForContext(D->getDeclContext()->getParent()));
1432 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001433 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001434 return;
1435 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001436 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001437
Craig Topperc3ec1492014-05-26 06:22:03 +00001438 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001439 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1440 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001441 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001442
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001443 // Diagnose use of non-object type in iboutletcollection attribute.
1444 // FIXME. Gnu attribute extension ignores use of builtin types in
1445 // attributes. So, __attribute__((iboutletcollection(char))) will be
1446 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001447 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001448 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001449 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1450 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001451 return;
1452 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001453
Erich Keane6a24e802019-09-13 17:39:31 +00001454 D->addAttr(::new (S.Context) IBOutletCollectionAttr(S.Context, AL, QTLoc));
Ted Kremenek26bde772010-05-19 17:38:06 +00001455}
1456
Hal Finkelee90a222014-09-26 05:04:30 +00001457bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1458 if (RefOkay) {
1459 if (T->isReferenceType())
1460 return true;
1461 } else {
1462 T = T.getNonReferenceType();
1463 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001464
Hal Finkelee90a222014-09-26 05:04:30 +00001465 // The nonnull attribute, and other similar attributes, can be applied to a
1466 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001467 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001468 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1469 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001470 for (const auto *I : UD->fields()) {
1471 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001472 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1473 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001474 }
1475 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001476 }
1477
1478 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001479}
1480
Erich Keanee891aa92018-07-13 15:07:47 +00001481static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001482 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001483 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001484 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001485 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001486 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001487 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001488 << AL << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001489 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001490 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001491 << AL << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001492 return false;
1493 }
1494 return true;
1495}
1496
Erich Keanee891aa92018-07-13 15:07:47 +00001497static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001498 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001499 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1500 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001501 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001502 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001503 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001504
1505 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001506 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1507 !attrNonNullArgCheck(
1508 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1509 Ex->getSourceRange(),
1510 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001511 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001512
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001513 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001514 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001515
1516 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001517 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1518 // check if the attribute came from a macro expansion or a template
1519 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001520 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001521 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001522 bool AnyPointers = isFunctionOrMethodVariadic(D);
1523 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1524 I != E && !AnyPointers; ++I) {
1525 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001526 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001527 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001528 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001529
Richard Smith588bd9b2014-08-27 04:59:42 +00001530 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001531 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001532 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001533
Joel E. Denny81508102018-03-13 14:51:22 +00001534 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001535 unsigned Size = NonNullArgs.size();
1536 llvm::array_pod_sort(Start, Start + Size);
Erich Keane6a24e802019-09-13 17:39:31 +00001537 D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, Start, Size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001538}
1539
Jordan Rosec9399072014-02-11 17:27:59 +00001540static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00001541 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001542 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001543 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001544 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001545 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001546 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001547 << D->getSourceRange();
1548 }
1549 return;
1550 }
1551
1552 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001553 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001554 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001555 return;
1556
Erich Keane6a24e802019-09-13 17:39:31 +00001557 D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, nullptr, 0));
Jordan Rosec9399072014-02-11 17:27:59 +00001558}
1559
Erich Keanee891aa92018-07-13 15:07:47 +00001560static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001561 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001562 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001563 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001564 /* isReturnValue */ true))
1565 return;
1566
Erich Keane6a24e802019-09-13 17:39:31 +00001567 D->addAttr(::new (S.Context) ReturnsNonNullAttr(S.Context, AL));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001568}
1569
Erich Keanee891aa92018-07-13 15:07:47 +00001570static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001571 if (D->isInvalidDecl())
1572 return;
1573
1574 // noescape only applies to pointer types.
1575 QualType T = cast<ParmVarDecl>(D)->getType();
1576 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001577 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001578 << AL << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001579 return;
1580 }
1581
Erich Keane6a24e802019-09-13 17:39:31 +00001582 D->addAttr(::new (S.Context) NoEscapeAttr(S.Context, AL));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001583}
1584
Erich Keanee891aa92018-07-13 15:07:47 +00001585static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001586 Expr *E = AL.getArgAsExpr(0),
1587 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
Erich Keane6a24e802019-09-13 17:39:31 +00001588 S.AddAssumeAlignedAttr(D, AL, E, OE);
Hal Finkelee90a222014-09-26 05:04:30 +00001589}
1590
Erich Keanee891aa92018-07-13 15:07:47 +00001591static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00001592 S.AddAllocAlignAttr(D, AL, AL.getArgAsExpr(0));
Erich Keane623efd82017-03-30 21:48:55 +00001593}
1594
Erich Keane6a24e802019-09-13 17:39:31 +00001595void Sema::AddAssumeAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
1596 Expr *OE) {
Hal Finkelee90a222014-09-26 05:04:30 +00001597 QualType ResultType = getFunctionOrMethodResultType(D);
1598 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1599
Erich Keane6a24e802019-09-13 17:39:31 +00001600 AssumeAlignedAttr TmpAttr(Context, CI, E, OE);
1601 SourceLocation AttrLoc = TmpAttr.getLocation();
Hal Finkelee90a222014-09-26 05:04:30 +00001602
1603 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1604 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
Erich Keane6a24e802019-09-13 17:39:31 +00001605 << &TmpAttr << TmpAttr.getRange() << SR;
Hal Finkelee90a222014-09-26 05:04:30 +00001606 return;
1607 }
1608
1609 if (!E->isValueDependent()) {
1610 llvm::APSInt I(64);
1611 if (!E->isIntegerConstantExpr(I, Context)) {
1612 if (OE)
1613 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1614 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1615 << E->getSourceRange();
1616 else
1617 Diag(AttrLoc, diag::err_attribute_argument_type)
1618 << &TmpAttr << AANT_ArgumentIntegerConstant
1619 << E->getSourceRange();
1620 return;
1621 }
1622
1623 if (!I.isPowerOf2()) {
1624 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1625 << E->getSourceRange();
1626 return;
1627 }
1628 }
1629
1630 if (OE) {
1631 if (!OE->isValueDependent()) {
1632 llvm::APSInt I(64);
1633 if (!OE->isIntegerConstantExpr(I, Context)) {
1634 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1635 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1636 << OE->getSourceRange();
1637 return;
1638 }
1639 }
1640 }
1641
Erich Keane6a24e802019-09-13 17:39:31 +00001642 D->addAttr(::new (Context) AssumeAlignedAttr(Context, CI, E, OE));
Hal Finkelee90a222014-09-26 05:04:30 +00001643}
1644
Erich Keane6a24e802019-09-13 17:39:31 +00001645void Sema::AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI,
1646 Expr *ParamExpr) {
Erich Keane623efd82017-03-30 21:48:55 +00001647 QualType ResultType = getFunctionOrMethodResultType(D);
1648
Erich Keane6a24e802019-09-13 17:39:31 +00001649 AllocAlignAttr TmpAttr(Context, CI, ParamIdx());
1650 SourceLocation AttrLoc = CI.getLoc();
Erich Keane623efd82017-03-30 21:48:55 +00001651
1652 if (!ResultType->isDependentType() &&
1653 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1654 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
Erich Keane6a24e802019-09-13 17:39:31 +00001655 << &TmpAttr << CI.getRange() << getFunctionOrMethodResultSourceRange(D);
Erich Keane623efd82017-03-30 21:48:55 +00001656 return;
1657 }
1658
Joel E. Denny81508102018-03-13 14:51:22 +00001659 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001660 const auto *FuncDecl = cast<FunctionDecl>(D);
1661 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001662 /*AttrArgNum=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001663 return;
1664
Joel E. Denny81508102018-03-13 14:51:22 +00001665 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001666 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001667 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001668 << &TmpAttr
1669 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001670 return;
1671 }
1672
Erich Keane6a24e802019-09-13 17:39:31 +00001673 D->addAttr(::new (Context) AllocAlignAttr(Context, CI, Idx));
Erich Keane623efd82017-03-30 21:48:55 +00001674}
1675
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001676/// Normalize the attribute, __foo__ becomes foo.
1677/// Returns true if normalization was applied.
1678static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001679 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1680 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001681 AttrName = AttrName.drop_front(2).drop_back(2);
1682 return true;
1683 }
1684 return false;
1685}
1686
Erich Keanee891aa92018-07-13 15:07:47 +00001687static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001688 // This attribute must be applied to a function declaration. The first
1689 // argument to the attribute must be an identifier, the name of the resource,
1690 // for example: malloc. The following arguments must be argument indexes, the
1691 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001692 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001693 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001694 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001695
Aaron Ballman00e99962013-08-31 01:11:41 +00001696 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001697 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001698 << AL << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001699 return;
1700 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001701
Richard Smith852e9ce2013-11-27 01:46:48 +00001702 // Figure out our Kind.
1703 OwnershipAttr::OwnershipKind K =
Erich Keane6a24e802019-09-13 17:39:31 +00001704 OwnershipAttr(S.Context, AL, nullptr, nullptr, 0).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001705
Richard Smith852e9ce2013-11-27 01:46:48 +00001706 // Check arguments.
1707 switch (K) {
1708 case OwnershipAttr::Takes:
1709 case OwnershipAttr::Holds:
1710 if (AL.getNumArgs() < 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001711 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001712 return;
1713 }
1714 break;
1715 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001716 if (AL.getNumArgs() > 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001717 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001718 return;
1719 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001720 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001721 }
1722
Richard Smith852e9ce2013-11-27 01:46:48 +00001723 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001724
Richard Smith852e9ce2013-11-27 01:46:48 +00001725 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001726 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001727 Module = &S.PP.getIdentifierTable().get(ModuleName);
1728 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001729
Joel E. Denny81508102018-03-13 14:51:22 +00001730 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001731 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1732 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001733 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001734 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001735 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001736
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001737 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001738 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001739 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001740 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001741 case OwnershipAttr::Takes:
1742 case OwnershipAttr::Holds:
1743 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1744 Err = 0;
1745 break;
1746 case OwnershipAttr::Returns:
1747 if (!T->isIntegerType())
1748 Err = 1;
1749 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001750 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001751 if (-1 != Err) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001752 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1753 << Ex->getSourceRange();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001754 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001755 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001756
1757 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001758 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001759 // Cannot have two ownership attributes of different kinds for the same
1760 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001761 if (I->getOwnKind() != K && I->args_end() !=
1762 std::find(I->args_begin(), I->args_end(), Idx)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001763 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001764 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001765 } else if (K == OwnershipAttr::Returns &&
1766 I->getOwnKind() == OwnershipAttr::Returns) {
1767 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001768 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001769 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1770 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001771 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001772 if (I->args_size())
1773 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001774 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001775 return;
1776 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001777 }
1778 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001779 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001780 }
1781
Joel E. Denny81508102018-03-13 14:51:22 +00001782 ParamIdx *Start = OwnershipArgs.data();
1783 unsigned Size = OwnershipArgs.size();
1784 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001785 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00001786 OwnershipAttr(S.Context, AL, Module, Start, Size));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001787}
1788
Erich Keanee891aa92018-07-13 15:07:47 +00001789static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001790 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001791 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001792 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001793 return;
1794 }
1795
1796 // gcc rejects
1797 // class c {
1798 // static int a __attribute__((weakref ("v2")));
1799 // static int b() __attribute__((weakref ("f3")));
1800 // };
1801 // and ignores the attributes of
1802 // void f(void) {
1803 // static int a __attribute__((weakref ("v2")));
1804 // }
1805 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001806 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001807 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001808 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1809 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001810 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001811 }
1812
1813 // The GCC manual says
1814 //
1815 // At present, a declaration to which `weakref' is attached can only
1816 // be `static'.
1817 //
1818 // It also says
1819 //
1820 // Without a TARGET,
1821 // given as an argument to `weakref' or to `alias', `weakref' is
1822 // equivalent to `weak'.
1823 //
1824 // gcc 4.4.1 will accept
1825 // int a7 __attribute__((weakref));
1826 // as
1827 // int a7 __attribute__((weak));
1828 // This looks like a bug in gcc. We reject that for now. We should revisit
1829 // it if this behaviour is actually used.
1830
Rafael Espindolac18086a2010-02-23 22:00:30 +00001831 // GCC rejects
1832 // static ((alias ("y"), weakref)).
1833 // Should we? How to check that weakref is before or after alias?
1834
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001835 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1836 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1837 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001838 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001839 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001840 // GCC will accept anything as the argument of weakref. Should we
1841 // check for an existing decl?
Erich Keane6a24e802019-09-13 17:39:31 +00001842 D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001843
Erich Keane6a24e802019-09-13 17:39:31 +00001844 D->addAttr(::new (S.Context) WeakRefAttr(S.Context, AL));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001845}
1846
Erich Keanee891aa92018-07-13 15:07:47 +00001847static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001848 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001849 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001850 return;
1851
1852 // Aliases should be on declarations, not definitions.
1853 const auto *FD = cast<FunctionDecl>(D);
1854 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001855 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001856 return;
1857 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001858
Erich Keane6a24e802019-09-13 17:39:31 +00001859 D->addAttr(::new (S.Context) IFuncAttr(S.Context, AL, Str));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001860}
1861
Erich Keanee891aa92018-07-13 15:07:47 +00001862static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001863 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001864 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001865 return;
1866
Douglas Gregore8bbc122011-09-02 00:18:52 +00001867 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001868 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001869 return;
1870 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001871 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001872 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001873 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001874
David Majnemer2dc81462015-01-19 09:00:28 +00001875 // Aliases should be on declarations, not definitions.
1876 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1877 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001878 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001879 return;
1880 }
1881 } else {
1882 const auto *VD = cast<VarDecl>(D);
1883 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001884 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001885 return;
1886 }
1887 }
1888
Nick Desaulniers9b9fe412019-01-09 23:54:55 +00001889 // Mark target used to prevent unneeded-internal-declaration warnings.
1890 if (!S.LangOpts.CPlusPlus) {
1891 // FIXME: demangle Str for C++, as the attribute refers to the mangled
1892 // linkage name, not the pre-mangled identifier.
1893 const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc());
1894 LookupResult LR(S, target, Sema::LookupOrdinaryName);
1895 if (S.LookupQualifiedName(LR, S.getCurLexicalContext()))
1896 for (NamedDecl *ND : LR)
1897 ND->markUsed(S.Context);
1898 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001899
Erich Keane6a24e802019-09-13 17:39:31 +00001900 D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001901}
1902
Erich Keanee891aa92018-07-13 15:07:47 +00001903static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001904 StringRef Model;
1905 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001906 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001907 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001908 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001909
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001910 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001911 if (Model != "global-dynamic" && Model != "local-dynamic"
1912 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001913 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001914 return;
1915 }
1916
Erich Keane6a24e802019-09-13 17:39:31 +00001917 D->addAttr(::new (S.Context) TLSModelAttr(S.Context, AL, Model));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001918}
1919
Erich Keanee891aa92018-07-13 15:07:47 +00001920static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001921 QualType ResultType = getFunctionOrMethodResultType(D);
1922 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
Erich Keane6a24e802019-09-13 17:39:31 +00001923 D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL));
David Majnemer631a90b2015-02-04 07:23:21 +00001924 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001925 }
1926
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001927 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001928 << AL << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001929}
1930
Erich Keane3efe0022018-07-20 14:13:28 +00001931static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1932 FunctionDecl *FD = cast<FunctionDecl>(D);
Erich Keane659c8712018-09-10 14:31:56 +00001933
1934 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1935 if (MD->getParent()->isLambda()) {
1936 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
1937 return;
1938 }
1939 }
1940
Erich Keane3efe0022018-07-20 14:13:28 +00001941 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1942 return;
1943
1944 SmallVector<IdentifierInfo *, 8> CPUs;
1945 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
1946 if (!AL.isArgIdent(ArgNo)) {
1947 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001948 << AL << AANT_ArgumentIdentifier;
Erich Keane3efe0022018-07-20 14:13:28 +00001949 return;
1950 }
1951
1952 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1953 StringRef CPUName = CPUArg->Ident->getName().trim();
1954
1955 if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1956 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1957 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1958 return;
1959 }
1960
1961 const TargetInfo &Target = S.Context.getTargetInfo();
1962 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1963 return Target.CPUSpecificManglingCharacter(CPUName) ==
1964 Target.CPUSpecificManglingCharacter(Cur->getName());
1965 })) {
1966 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1967 return;
1968 }
1969 CPUs.push_back(CPUArg->Ident);
1970 }
1971
1972 FD->setIsMultiVersion(true);
1973 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
Erich Keane6a24e802019-09-13 17:39:31 +00001974 D->addAttr(::new (S.Context)
1975 CPUSpecificAttr(S.Context, AL, CPUs.data(), CPUs.size()));
Erich Keane3efe0022018-07-20 14:13:28 +00001976 else
Erich Keane6a24e802019-09-13 17:39:31 +00001977 D->addAttr(::new (S.Context)
1978 CPUDispatchAttr(S.Context, AL, CPUs.data(), CPUs.size()));
Erich Keane3efe0022018-07-20 14:13:28 +00001979}
1980
Erich Keanee891aa92018-07-13 15:07:47 +00001981static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001982 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001983 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00001984 << AL << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001985 return;
1986 }
1987
Erich Keane44bacdf2018-08-09 13:21:32 +00001988 if (CommonAttr *CA = S.mergeCommonAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001989 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001990}
1991
Erich Keanee891aa92018-07-13 15:07:47 +00001992static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001993 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
Charles Davis0e379112016-08-08 21:19:08 +00001994 return;
1995
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001996 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001997 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
1998 const auto &Arch = Triple.getArch();
1999 if (Arch != llvm::Triple::x86 &&
2000 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002001 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
Erich Keane44bacdf2018-08-09 13:21:32 +00002002 << AL << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002003 return;
2004 }
2005 }
2006
Erich Keane6a24e802019-09-13 17:39:31 +00002007 D->addAttr(::new (S.Context) NakedAttr(S.Context, AL));
Charles Davis0e379112016-08-08 21:19:08 +00002008}
2009
Erich Keanee891aa92018-07-13 15:07:47 +00002010static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002011 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00002012
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002013 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002014 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002015 << Attrs << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002016 return;
2017 }
2018
Erich Keane6a24e802019-09-13 17:39:31 +00002019 D->addAttr(::new (S.Context) NoReturnAttr(S.Context, Attrs));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002020}
2021
Erich Keanee891aa92018-07-13 15:07:47 +00002022static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00002023 if (!S.getLangOpts().CFProtectionBranch)
2024 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
2025 else
2026 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00002027}
2028
Erich Keanee891aa92018-07-13 15:07:47 +00002029bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002030 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
2031 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002032 return true;
2033 }
2034
2035 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002036}
2037
Erich Keanee891aa92018-07-13 15:07:47 +00002038bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002039 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002040 if (!AL.existsInTarget(Context.getTargetInfo())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002041 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002042 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002043 return true;
2044 }
2045
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002046 return false;
2047}
2048
Erich Keanee891aa92018-07-13 15:07:47 +00002049static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2050
Ted Kremenek5295ce82010-08-19 00:51:58 +00002051 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2052 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00002053 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002054 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00002055 if (!VD || (!VD->getType()->isBlockPointerType() &&
2056 !VD->getType()->isFunctionPointerType())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002057 S.Diag(AL.getLoc(), AL.isCXX11Attribute()
2058 ? diag::err_attribute_wrong_decl_type
2059 : diag::warn_attribute_wrong_decl_type)
2060 << AL << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00002061 return;
2062 }
2063 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002064
Erich Keane6a24e802019-09-13 17:39:31 +00002065 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(S.Context, AL));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002066}
2067
John Thompsoncdb847ba2010-08-09 21:53:52 +00002068// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00002069static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2070 /*
2071 Returning a Vector Class in Registers
2072
2073 According to the PPU ABI specifications, a class with a single member of
2074 vector type is returned in memory when used as the return value of a
2075 function.
2076 This results in inefficient code when implementing vector classes. To return
2077 the value in a single vector register, add the vecreturn attribute to the
2078 class definition. This attribute is also applicable to struct types.
2079
2080 Example:
2081
2082 struct Vector
2083 {
2084 __vector float xyzw;
2085 } __attribute__((vecreturn));
2086
2087 Vector Add(Vector lhs, Vector rhs)
2088 {
2089 Vector result;
2090 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2091 return result; // This will be returned in a register
2092 }
2093 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002094 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002095 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002096 return;
2097 }
2098
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002099 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002100 int count = 0;
2101
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002102 if (!isa<CXXRecordDecl>(R)) {
2103 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002104 return;
2105 }
2106
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002107 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2108 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002109 return;
2110 }
2111
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002112 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002113 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002114 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002115 return;
2116 }
2117 count++;
2118 }
2119
Erich Keane6a24e802019-09-13 17:39:31 +00002120 D->addAttr(::new (S.Context) VecReturnAttr(S.Context, AL));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002121}
2122
Richard Smithe233fbf2013-01-28 22:42:45 +00002123static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002124 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002125 if (isa<ParmVarDecl>(D)) {
2126 // [[carries_dependency]] can only be applied to a parameter if it is a
2127 // parameter of a function declaration or lambda.
2128 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002129 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002130 diag::err_carries_dependency_param_not_function_decl);
2131 return;
2132 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002133 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002134
Erich Keane6a24e802019-09-13 17:39:31 +00002135 D->addAttr(::new (S.Context) CarriesDependencyAttr(S.Context, AL));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002136}
2137
Erich Keanee891aa92018-07-13 15:07:47 +00002138static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002139 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002140
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002141 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002142 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002143 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Erich Keane44bacdf2018-08-09 13:21:32 +00002144 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002145
Erich Keane6a24e802019-09-13 17:39:31 +00002146 D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002147}
2148
Erich Keanee891aa92018-07-13 15:07:47 +00002149static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002150 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002151 if (AL.getNumArgs() &&
2152 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002153 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002154
Erich Keane6a24e802019-09-13 17:39:31 +00002155 D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00002156}
2157
Erich Keanee891aa92018-07-13 15:07:47 +00002158static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002159 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002160 if (AL.getNumArgs() &&
2161 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002162 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002163
Erich Keane6a24e802019-09-13 17:39:31 +00002164 D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00002165}
2166
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002167template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002168static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002169 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002170 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002171 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002172 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002173
Erich Keane6a24e802019-09-13 17:39:31 +00002174 D->addAttr(::new (S.Context) AttrTy(S.Context, AL, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002175}
2176
Ted Kremenek438f8db2014-02-22 01:06:05 +00002177static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002178 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002179 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002180 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
Erich Keane44bacdf2018-08-09 13:21:32 +00002181 << AL << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002182 return;
2183 }
2184
Erich Keane6a24e802019-09-13 17:39:31 +00002185 D->addAttr(::new (S.Context) ObjCExplicitProtocolImplAttr(S.Context, AL));
Ted Kremenek28eace62013-11-23 01:01:34 +00002186}
2187
Jordy Rose740b0c22012-05-08 03:27:22 +00002188static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2189 IdentifierInfo *Platform,
2190 VersionTuple Introduced,
2191 VersionTuple Deprecated,
2192 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002193 StringRef PlatformName
2194 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2195 if (PlatformName.empty())
2196 PlatformName = Platform->getName();
2197
2198 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2199 // of these steps are needed).
2200 if (!Introduced.empty() && !Deprecated.empty() &&
2201 !(Introduced <= Deprecated)) {
2202 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2203 << 1 << PlatformName << Deprecated.getAsString()
2204 << 0 << Introduced.getAsString();
2205 return true;
2206 }
2207
2208 if (!Introduced.empty() && !Obsoleted.empty() &&
2209 !(Introduced <= Obsoleted)) {
2210 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2211 << 2 << PlatformName << Obsoleted.getAsString()
2212 << 0 << Introduced.getAsString();
2213 return true;
2214 }
2215
2216 if (!Deprecated.empty() && !Obsoleted.empty() &&
2217 !(Deprecated <= Obsoleted)) {
2218 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2219 << 2 << PlatformName << Obsoleted.getAsString()
2220 << 1 << Deprecated.getAsString();
2221 return true;
2222 }
2223
2224 return false;
2225}
2226
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002227/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002228///
2229/// If either version tuple is empty, then they are assumed to match. If
2230/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2231static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2232 bool BeforeIsOkay) {
2233 if (X.empty() || Y.empty())
2234 return true;
2235
2236 if (X == Y)
2237 return true;
2238
2239 if (BeforeIsOkay && X < Y)
2240 return true;
2241
2242 return false;
2243}
2244
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002245AvailabilityAttr *Sema::mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002246 NamedDecl *D, const AttributeCommonInfo &CI, IdentifierInfo *Platform,
2247 bool Implicit, VersionTuple Introduced, VersionTuple Deprecated,
2248 VersionTuple Obsoleted, bool IsUnavailable, StringRef Message,
2249 bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK,
2250 int Priority) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002251 VersionTuple MergedIntroduced = Introduced;
2252 VersionTuple MergedDeprecated = Deprecated;
2253 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002254 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002255 bool OverrideOrImpl = false;
2256 switch (AMK) {
2257 case AMK_None:
2258 case AMK_Redeclaration:
2259 OverrideOrImpl = false;
2260 break;
2261
2262 case AMK_Override:
2263 case AMK_ProtocolImplementation:
2264 OverrideOrImpl = true;
2265 break;
2266 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002267
Rafael Espindolac67f2232012-05-10 02:50:16 +00002268 if (D->hasAttrs()) {
2269 AttrVec &Attrs = D->getAttrs();
2270 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002271 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002272 if (!OldAA) {
2273 ++i;
2274 continue;
2275 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002276
Rafael Espindolac67f2232012-05-10 02:50:16 +00002277 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2278 if (OldPlatform != Platform) {
2279 ++i;
2280 continue;
2281 }
2282
Tim Northover7a73cc72015-10-30 16:30:49 +00002283 // If there is an existing availability attribute for this platform that
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002284 // has a lower priority use the existing one and discard the new
2285 // attribute.
2286 if (OldAA->getPriority() < Priority)
Tim Northover7a73cc72015-10-30 16:30:49 +00002287 return nullptr;
Tim Northover7a73cc72015-10-30 16:30:49 +00002288
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002289 // If there is an existing attribute for this platform that has a higher
2290 // priority than the new attribute then erase the old one and continue
2291 // processing the attributes.
2292 if (OldAA->getPriority() > Priority) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002293 Attrs.erase(Attrs.begin() + i);
2294 --e;
2295 continue;
2296 }
2297
Rafael Espindolac67f2232012-05-10 02:50:16 +00002298 FoundAny = true;
2299 VersionTuple OldIntroduced = OldAA->getIntroduced();
2300 VersionTuple OldDeprecated = OldAA->getDeprecated();
2301 VersionTuple OldObsoleted = OldAA->getObsoleted();
2302 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002303
Douglas Gregord2a713e2015-09-30 21:27:42 +00002304 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2305 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2306 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002307 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002308 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2309 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002310 int Which = -1;
2311 VersionTuple FirstVersion;
2312 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002313 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002314 Which = 0;
2315 FirstVersion = OldIntroduced;
2316 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002317 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002318 Which = 1;
2319 FirstVersion = Deprecated;
2320 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002321 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002322 Which = 2;
2323 FirstVersion = Obsoleted;
2324 SecondVersion = OldObsoleted;
2325 }
2326
2327 if (Which == -1) {
2328 Diag(OldAA->getLocation(),
2329 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002330 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2331 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002332 } else {
2333 Diag(OldAA->getLocation(),
2334 diag::warn_mismatched_availability_override)
2335 << Which
2336 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002337 << FirstVersion.getAsString() << SecondVersion.getAsString()
2338 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002339 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002340 if (AMK == AMK_Override)
Erich Keane6a24e802019-09-13 17:39:31 +00002341 Diag(CI.getLoc(), diag::note_overridden_method);
Douglas Gregord2a713e2015-09-30 21:27:42 +00002342 else
Erich Keane6a24e802019-09-13 17:39:31 +00002343 Diag(CI.getLoc(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002344 } else {
2345 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
Erich Keane6a24e802019-09-13 17:39:31 +00002346 Diag(CI.getLoc(), diag::note_previous_attribute);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002347 }
2348
Rafael Espindolac67f2232012-05-10 02:50:16 +00002349 Attrs.erase(Attrs.begin() + i);
2350 --e;
2351 continue;
2352 }
2353
2354 VersionTuple MergedIntroduced2 = MergedIntroduced;
2355 VersionTuple MergedDeprecated2 = MergedDeprecated;
2356 VersionTuple MergedObsoleted2 = MergedObsoleted;
2357
2358 if (MergedIntroduced2.empty())
2359 MergedIntroduced2 = OldIntroduced;
2360 if (MergedDeprecated2.empty())
2361 MergedDeprecated2 = OldDeprecated;
2362 if (MergedObsoleted2.empty())
2363 MergedObsoleted2 = OldObsoleted;
2364
2365 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2366 MergedIntroduced2, MergedDeprecated2,
2367 MergedObsoleted2)) {
2368 Attrs.erase(Attrs.begin() + i);
2369 --e;
2370 continue;
2371 }
2372
2373 MergedIntroduced = MergedIntroduced2;
2374 MergedDeprecated = MergedDeprecated2;
2375 MergedObsoleted = MergedObsoleted2;
2376 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002377 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002378 }
2379
2380 if (FoundAny &&
2381 MergedIntroduced == Introduced &&
2382 MergedDeprecated == Deprecated &&
2383 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002384 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002385
Douglas Gregord2a713e2015-09-30 21:27:42 +00002386 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002387 // the checking.
Erich Keane6a24e802019-09-13 17:39:31 +00002388 if (!checkAvailabilityAttr(*this, CI.getRange(), Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002389 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002390 !OverrideOrImpl) {
Erich Keane6a24e802019-09-13 17:39:31 +00002391 auto *Avail = ::new (Context) AvailabilityAttr(
2392 Context, CI, Platform, Introduced, Deprecated, Obsoleted, IsUnavailable,
2393 Message, IsStrict, Replacement, Priority);
Manman Ren719a8642016-05-06 21:04:01 +00002394 Avail->setImplicit(Implicit);
2395 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002396 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002397 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002398}
2399
Erich Keanee891aa92018-07-13 15:07:47 +00002400static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002401 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002402 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002403 IdentifierLoc *Platform = AL.getArgAsIdent(0);
Fangrui Song6907ce22018-07-30 19:24:48 +00002404
Aaron Ballman00e99962013-08-31 01:11:41 +00002405 IdentifierInfo *II = Platform->Ident;
2406 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2407 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2408 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002409
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002410 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002411 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002412 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002413
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002414 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2415 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2416 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2417 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2418 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002419 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002420 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002421 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002422 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002423 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002424 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002425
Michael Wu260e9622018-11-12 02:44:33 +00002426 if (II->isStr("swift")) {
2427 if (Introduced.isValid() || Obsoleted.isValid() ||
2428 (!IsUnavailable && !Deprecated.isValid())) {
2429 S.Diag(AL.getLoc(),
2430 diag::warn_availability_swift_unavailable_deprecated_only);
2431 return;
2432 }
2433 }
2434
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002435 int PriorityModifier = AL.isPragmaClangAttribute()
2436 ? Sema::AP_PragmaClangAttribute
2437 : Sema::AP_Explicit;
2438 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002439 ND, AL, II, false /*Implicit*/, Introduced.Version, Deprecated.Version,
2440 Obsoleted.Version, IsUnavailable, Str, IsStrict, Replacement,
2441 Sema::AMK_None, PriorityModifier);
Rafael Espindola19de5612013-01-12 06:42:30 +00002442 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002443 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002444
2445 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2446 // matches before the start of the watchOS platform.
2447 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2448 IdentifierInfo *NewII = nullptr;
2449 if (II->getName() == "ios")
2450 NewII = &S.Context.Idents.get("watchos");
2451 else if (II->getName() == "ios_app_extension")
2452 NewII = &S.Context.Idents.get("watchos_app_extension");
2453
2454 if (NewII) {
2455 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2456 if (Version.empty())
2457 return Version;
2458 auto Major = Version.getMajor();
2459 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2460 if (NewMajor >= 2) {
2461 if (Version.getMinor().hasValue()) {
2462 if (Version.getSubminor().hasValue())
2463 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2464 Version.getSubminor().getValue());
2465 else
2466 return VersionTuple(NewMajor, Version.getMinor().getValue());
2467 }
Alex Lorenz0b436482019-03-20 20:02:00 +00002468 return VersionTuple(NewMajor);
Tim Northover7a73cc72015-10-30 16:30:49 +00002469 }
2470
2471 return VersionTuple(2, 0);
2472 };
2473
2474 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2475 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2476 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2477
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002478 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002479 ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated,
2480 NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
2481 Sema::AMK_None,
2482 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
Tim Northover7a73cc72015-10-30 16:30:49 +00002483 if (NewAttr)
2484 D->addAttr(NewAttr);
2485 }
2486 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2487 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2488 // matches before the start of the tvOS platform.
2489 IdentifierInfo *NewII = nullptr;
2490 if (II->getName() == "ios")
2491 NewII = &S.Context.Idents.get("tvos");
2492 else if (II->getName() == "ios_app_extension")
2493 NewII = &S.Context.Idents.get("tvos_app_extension");
2494
2495 if (NewII) {
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002496 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002497 ND, AL, NewII, true /*Implicit*/, Introduced.Version,
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002498 Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict,
2499 Replacement, Sema::AMK_None,
Erich Keane6a24e802019-09-13 17:39:31 +00002500 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
Alex Lorenz3cfe9d52019-01-24 19:14:39 +00002501 if (NewAttr)
2502 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002503 }
2504 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002505}
2506
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002507static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002508 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002509 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002510 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002511 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002512 "Invalid number of arguments in an external_source_symbol attribute");
2513
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002514 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002515 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002516 Language = SE->getString();
2517 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002518 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002519 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002520 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002521
2522 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00002523 S.Context, AL, Language, DefinedIn, IsGeneratedDeclaration));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002524}
2525
John McCalld041a9b2013-02-20 01:54:26 +00002526template <class T>
Erich Keane6a24e802019-09-13 17:39:31 +00002527static T *mergeVisibilityAttr(Sema &S, Decl *D, const AttributeCommonInfo &CI,
2528 typename T::VisibilityType value) {
John McCalld041a9b2013-02-20 01:54:26 +00002529 T *existingAttr = D->getAttr<T>();
2530 if (existingAttr) {
2531 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2532 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002533 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002534 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
Erich Keane6a24e802019-09-13 17:39:31 +00002535 S.Diag(CI.getLoc(), diag::note_previous_attribute);
John McCalld041a9b2013-02-20 01:54:26 +00002536 D->dropAttr<T>();
2537 }
Erich Keane6a24e802019-09-13 17:39:31 +00002538 return ::new (S.Context) T(S.Context, CI, value);
John McCalld041a9b2013-02-20 01:54:26 +00002539}
2540
Erich Keane6a24e802019-09-13 17:39:31 +00002541VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D,
2542 const AttributeCommonInfo &CI,
2543 VisibilityAttr::VisibilityType Vis) {
2544 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, CI, Vis);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002545}
2546
Erich Keane6a24e802019-09-13 17:39:31 +00002547TypeVisibilityAttr *
2548Sema::mergeTypeVisibilityAttr(Decl *D, const AttributeCommonInfo &CI,
2549 TypeVisibilityAttr::VisibilityType Vis) {
2550 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, CI, Vis);
John McCalld041a9b2013-02-20 01:54:26 +00002551}
2552
Erich Keanee891aa92018-07-13 15:07:47 +00002553static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002554 bool isTypeVisibility) {
2555 // Visibility attributes don't mean anything on a typedef.
2556 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002557 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002558 return;
2559 }
2560
2561 // 'type_visibility' can only go on a type or namespace.
2562 if (isTypeVisibility &&
2563 !(isa<TagDecl>(D) ||
2564 isa<ObjCInterfaceDecl>(D) ||
2565 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002566 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002567 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002568 return;
2569 }
2570
Benjamin Kramer70370212013-09-09 15:08:57 +00002571 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002572 StringRef TypeStr;
2573 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002574 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002575 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002576
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002577 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002578 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002579 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2580 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002581 return;
2582 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002583
Aaron Ballman682ee422013-09-11 19:47:58 +00002584 // Complain about attempts to use protected visibility on targets
2585 // (like Darwin) that don't support it.
2586 if (type == VisibilityAttr::Protected &&
2587 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002588 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002589 type = VisibilityAttr::Default;
2590 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002591
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002592 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002593 if (isTypeVisibility) {
Erich Keane6a24e802019-09-13 17:39:31 +00002594 newAttr = S.mergeTypeVisibilityAttr(
2595 D, AL, (TypeVisibilityAttr::VisibilityType)type);
John McCalld041a9b2013-02-20 01:54:26 +00002596 } else {
Erich Keane6a24e802019-09-13 17:39:31 +00002597 newAttr = S.mergeVisibilityAttr(D, AL, type);
John McCalld041a9b2013-02-20 01:54:26 +00002598 }
2599 if (newAttr)
2600 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002601}
2602
Erich Keanee891aa92018-07-13 15:07:47 +00002603static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002604 const auto *M = cast<ObjCMethodDecl>(D);
2605 if (!AL.isArgIdent(0)) {
2606 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002607 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002608 return;
2609 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002610
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002611 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002612 ObjCMethodFamilyAttr::FamilyKind F;
2613 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002614 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002615 return;
2616 }
2617
Alp Toker314cc812014-01-25 16:55:45 +00002618 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002619 !M->getReturnType()->isObjCObjectPointerType()) {
2620 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2621 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002622 // Ignore the attribute.
2623 return;
2624 }
2625
Erich Keane6a24e802019-09-13 17:39:31 +00002626 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(S.Context, AL, F));
John McCall86bc21f2011-03-02 11:33:24 +00002627}
2628
Erich Keanee891aa92018-07-13 15:07:47 +00002629static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002630 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002631 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002632 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002633 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2634 return;
2635 }
2636 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002637 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002638 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002639 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002640 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2641 return;
2642 }
2643 }
2644 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002645 // It is okay to include this attribute on properties, e.g.:
2646 //
2647 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2648 //
2649 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002650 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002651 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002652 }
Erich Keane6a24e802019-09-13 17:39:31 +00002653 D->addAttr(::new (S.Context) ObjCNSObjectAttr(S.Context, AL));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002654}
2655
Erich Keanee891aa92018-07-13 15:07:47 +00002656static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002657 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002658 QualType T = TD->getUnderlyingType();
2659 if (!T->isObjCObjectPointerType()) {
2660 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2661 return;
2662 }
2663 } else {
2664 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2665 return;
2666 }
Erich Keane6a24e802019-09-13 17:39:31 +00002667 D->addAttr(::new (S.Context) ObjCIndependentClassAttr(S.Context, AL));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002668}
2669
Erich Keanee891aa92018-07-13 15:07:47 +00002670static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002671 if (!AL.isArgIdent(0)) {
2672 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002673 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002674 return;
2675 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002676
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002677 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002678 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002679 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002680 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002681 return;
2682 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002683
Erich Keane6a24e802019-09-13 17:39:31 +00002684 D->addAttr(::new (S.Context) BlocksAttr(S.Context, AL, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002685}
2686
Erich Keanee891aa92018-07-13 15:07:47 +00002687static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002688 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002689 if (AL.getNumArgs() > 0) {
2690 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002691 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002692 if (E->isTypeDependent() || E->isValueDependent() ||
2693 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002694 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002695 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002696 return;
2697 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002698
John McCallb46f2872011-09-09 07:56:05 +00002699 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002700 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002701 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002702 return;
2703 }
John McCallb46f2872011-09-09 07:56:05 +00002704
2705 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002706 }
2707
Aaron Ballman18a78382013-11-21 00:28:23 +00002708 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002709 if (AL.getNumArgs() > 1) {
2710 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002711 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002712 if (E->isTypeDependent() || E->isValueDependent() ||
2713 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002714 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002715 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002716 return;
2717 }
2718 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002719
John McCallb46f2872011-09-09 07:56:05 +00002720 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002721 // FIXME: This error message could be improved, it would be nice
2722 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002723 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002724 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002725 return;
2726 }
2727 }
2728
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002729 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002730 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002731 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002732 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002733 return;
2734 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002735
Chris Lattner9363e312009-03-17 23:03:47 +00002736 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002737 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002738 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002739 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002740 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002741 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002742 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002743 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002744 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002745 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002746 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002747 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002748 return;
2749 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002750 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002751 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002752 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002753 const FunctionType *FT = Ty->isFunctionPointerType()
2754 ? D->getFunctionType()
Simon Pilgrim237d0af2019-10-04 15:02:46 +00002755 : Ty->castAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002756 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002757 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002758 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002759 return;
2760 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002761 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002762 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002763 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002764 return;
2765 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002766 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002767 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002768 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002769 return;
2770 }
Erich Keane6a24e802019-09-13 17:39:31 +00002771 D->addAttr(::new (S.Context) SentinelAttr(S.Context, AL, sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002772}
2773
Erich Keanee891aa92018-07-13 15:07:47 +00002774static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002775 if (D->getFunctionType() &&
Erich Keane46441fd2019-07-25 15:10:56 +00002776 D->getFunctionType()->getReturnType()->isVoidType() &&
2777 !isa<CXXConstructorDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002778 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002779 return;
2780 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002781 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002782 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002783 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002784 return;
2785 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002786
Aaron Ballman3bef0142019-07-20 07:56:34 +00002787 StringRef Str;
2788 if ((AL.isCXX11Attribute() || AL.isC2xAttribute()) && !AL.getScopeName()) {
2789 // If this is spelled as the standard C++17 attribute, but not in C++17,
2790 // warn about using it as an extension. If there are attribute arguments,
2791 // then claim it's a C++2a extension instead.
2792 // FIXME: If WG14 does not seem likely to adopt the same feature, add an
2793 // extension warning for C2x mode.
2794 const LangOptions &LO = S.getLangOpts();
2795 if (AL.getNumArgs() == 1) {
2796 if (LO.CPlusPlus && !LO.CPlusPlus2a)
2797 S.Diag(AL.getLoc(), diag::ext_cxx2a_attr) << AL;
2798
2799 // Since this this is spelled [[nodiscard]], get the optional string
2800 // literal. If in C++ mode, but not in C++2a mode, diagnose as an
2801 // extension.
2802 // FIXME: C2x should support this feature as well, even as an extension.
2803 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
2804 return;
2805 } else if (LO.CPlusPlus && !LO.CPlusPlus17)
2806 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
2807 }
Aaron Ballmane7964782016-03-07 22:44:55 +00002808
Erich Keane6a24e802019-09-13 17:39:31 +00002809 D->addAttr(::new (S.Context) WarnUnusedResultAttr(S.Context, AL, Str));
Chris Lattner237f2752009-02-14 07:37:35 +00002810}
2811
Erich Keanee891aa92018-07-13 15:07:47 +00002812static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002813 // weak_import only applies to variable & function declarations.
2814 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002815 if (!D->canBeWeakImported(isDef)) {
2816 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002817 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002818 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002819 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002820 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002821 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002822 // Nothing to warn about here.
2823 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002824 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002825 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002826
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002827 return;
2828 }
2829
Erich Keane6a24e802019-09-13 17:39:31 +00002830 D->addAttr(::new (S.Context) WeakImportAttr(S.Context, AL));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002831}
2832
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002833// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002834template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002835static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002836 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002837 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002838 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002839 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2840 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002841 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002842 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002843 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002844 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002845 return;
2846 }
2847 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002848
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002849 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2850 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2851 Existing->getYDim() == WGSize[1] &&
2852 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002853 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002854
Erich Keane6a24e802019-09-13 17:39:31 +00002855 D->addAttr(::new (S.Context)
2856 WorkGroupAttr(S.Context, AL, WGSize[0], WGSize[1], WGSize[2]));
Nate Begemanf2758702009-06-26 06:32:41 +00002857}
2858
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002859// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002860static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002861 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002862 const Expr *E = AL.getArgAsExpr(0);
2863 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002864 return;
2865 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002866 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002867 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002868 return;
2869 }
2870
2871 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2872 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2873 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002874 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002875
Erich Keane6a24e802019-09-13 17:39:31 +00002876 D->addAttr(::new (S.Context)
2877 OpenCLIntelReqdSubGroupSizeAttr(S.Context, AL, SGSize));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002878}
2879
Erich Keanee891aa92018-07-13 15:07:47 +00002880static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002881 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002882 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002883 return;
2884 }
2885
Craig Topperc3ec1492014-05-26 06:22:03 +00002886 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002887 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002888 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002889
2890 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2891 (ParmType->isBooleanType() ||
2892 !ParmType->isIntegralType(S.getASTContext()))) {
Matthias Gehred293cbd2019-07-25 17:50:51 +00002893 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument) << 3 << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002894 return;
2895 }
2896
Aaron Ballmana9e05402013-12-02 22:16:55 +00002897 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002898 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002899 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002900 return;
2901 }
2902 }
2903
Erich Keane6a24e802019-09-13 17:39:31 +00002904 D->addAttr(::new (S.Context) VecTypeHintAttr(S.Context, AL, ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002905}
2906
Erich Keane6a24e802019-09-13 17:39:31 +00002907SectionAttr *Sema::mergeSectionAttr(Decl *D, const AttributeCommonInfo &CI,
2908 StringRef Name) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002909 // Explicit or partial specializations do not inherit
2910 // the section attribute from the primary template.
2911 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keane6a24e802019-09-13 17:39:31 +00002912 if (CI.getAttributeSpellingListIndex() == SectionAttr::Declspec_allocate &&
Erich Keane7963e8b2018-07-18 20:04:48 +00002913 FD->isFunctionTemplateSpecialization())
2914 return nullptr;
2915 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002916 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2917 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002918 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002919 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2920 << 1 /*section*/;
Erich Keane6a24e802019-09-13 17:39:31 +00002921 Diag(CI.getLoc(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002922 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002923 }
Erich Keane6a24e802019-09-13 17:39:31 +00002924 return ::new (Context) SectionAttr(Context, CI, Name);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002925}
2926
Reid Kleckner2a133222015-03-04 23:39:17 +00002927bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2928 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2929 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002930 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002931 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002932 return false;
2933 }
2934 return true;
2935}
2936
Erich Keanee891aa92018-07-13 15:07:47 +00002937static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002938 // Make sure that there is a string literal as the sections's single
2939 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002940 StringRef Str;
2941 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002942 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002943 return;
Mike Stump11289f42009-09-09 15:08:12 +00002944
Reid Kleckner2a133222015-03-04 23:39:17 +00002945 if (!S.checkSectionName(LiteralLoc, Str))
2946 return;
2947
Chris Lattner30ba6742009-08-10 19:03:04 +00002948 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002949 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002950 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002951 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002952 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002953 return;
2954 }
Mike Stump11289f42009-09-09 15:08:12 +00002955
Erich Keane6a24e802019-09-13 17:39:31 +00002956 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002957 if (NewAttr)
2958 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002959}
2960
Nico Weber98016212019-07-09 00:02:23 +00002961// This is used for `__declspec(code_seg("segname"))` on a decl.
2962// `#pragma code_seg("segname")` uses checkSectionName() instead.
2963static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc,
2964 StringRef CodeSegName) {
2965 std::string Error =
2966 S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
Erich Keane7963e8b2018-07-18 20:04:48 +00002967 if (!Error.empty()) {
Nico Weber98016212019-07-09 00:02:23 +00002968 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
2969 << Error << 0 /*'code-seg'*/;
Erich Keane7963e8b2018-07-18 20:04:48 +00002970 return false;
2971 }
Nico Weber98016212019-07-09 00:02:23 +00002972
Erich Keane7963e8b2018-07-18 20:04:48 +00002973 return true;
2974}
2975
Erich Keane6a24e802019-09-13 17:39:31 +00002976CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, const AttributeCommonInfo &CI,
2977 StringRef Name) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002978 // Explicit or partial specializations do not inherit
2979 // the code_seg attribute from the primary template.
2980 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2981 if (FD->isFunctionTemplateSpecialization())
2982 return nullptr;
2983 }
2984 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
2985 if (ExistingAttr->getName() == Name)
2986 return nullptr;
2987 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2988 << 0 /*codeseg*/;
Erich Keane6a24e802019-09-13 17:39:31 +00002989 Diag(CI.getLoc(), diag::note_previous_attribute);
Erich Keane7963e8b2018-07-18 20:04:48 +00002990 return nullptr;
2991 }
Erich Keane6a24e802019-09-13 17:39:31 +00002992 return ::new (Context) CodeSegAttr(Context, CI, Name);
Erich Keane7963e8b2018-07-18 20:04:48 +00002993}
2994
2995static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2996 StringRef Str;
2997 SourceLocation LiteralLoc;
2998 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
2999 return;
3000 if (!checkCodeSegName(S, LiteralLoc, Str))
3001 return;
3002 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3003 if (!ExistingAttr->isImplicit()) {
3004 S.Diag(AL.getLoc(),
3005 ExistingAttr->getName() == Str
3006 ? diag::warn_duplicate_codeseg_attribute
3007 : diag::err_conflicting_codeseg_attribute);
3008 return;
3009 }
3010 D->dropAttr<CodeSegAttr>();
3011 }
Erich Keane6a24e802019-09-13 17:39:31 +00003012 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL, Str))
Erich Keane7963e8b2018-07-18 20:04:48 +00003013 D->addAttr(CSA);
3014}
3015
Erich Keane57e15cd2017-07-19 22:06:33 +00003016// Check for things we'd like to warn about. Multiversioning issues are
3017// handled later in the process, once we know how many exist.
3018bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3019 enum FirstParam { Unsupported, Duplicate };
3020 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00003021 for (auto Str : {"tune=", "fpmath="})
3022 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00003023 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3024 << Unsupported << None << Str;
3025
3026 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
3027
3028 if (!ParsedAttrs.Architecture.empty() &&
3029 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3030 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3031 << Unsupported << Architecture << ParsedAttrs.Architecture;
3032
3033 if (ParsedAttrs.DuplicateArchitecture)
3034 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3035 << Duplicate << None << "arch=";
3036
3037 for (const auto &Feature : ParsedAttrs.Features) {
3038 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3039 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3040 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3041 << Unsupported << None << CurFeature;
3042 }
3043
Momchil Velikovaa6d48f2019-11-15 11:34:47 +00003044 TargetInfo::BranchProtectionInfo BPI;
3045 StringRef Error;
3046 if (!ParsedAttrs.BranchProtection.empty() &&
3047 !Context.getTargetInfo().validateBranchProtection(
3048 ParsedAttrs.BranchProtection, BPI, Error)) {
3049 if (Error.empty())
3050 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3051 << Unsupported << None << "branch-protection";
3052 else
3053 return Diag(LiteralLoc, diag::err_invalid_branch_protection_spec)
3054 << Error;
3055 }
3056
Erich Keane29636aa2018-02-16 17:31:59 +00003057 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003058}
3059
Erich Keanee891aa92018-07-13 15:07:47 +00003060static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003061 StringRef Str;
3062 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003063 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003064 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003065 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003066
Erich Keane6a24e802019-09-13 17:39:31 +00003067 TargetAttr *NewAttr = ::new (S.Context) TargetAttr(S.Context, AL, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00003068 D->addAttr(NewAttr);
3069}
3070
Erich Keanee891aa92018-07-13 15:07:47 +00003071static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003072 Expr *E = AL.getArgAsExpr(0);
3073 uint32_t VecWidth;
3074 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3075 AL.setInvalid();
3076 return;
3077 }
3078
3079 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3080 if (Existing && Existing->getVectorWidth() != VecWidth) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003081 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Craig Topper74c10e32018-07-09 19:00:16 +00003082 return;
3083 }
3084
Erich Keane6a24e802019-09-13 17:39:31 +00003085 D->addAttr(::new (S.Context) MinVectorWidthAttr(S.Context, AL, VecWidth));
Craig Topper74c10e32018-07-09 19:00:16 +00003086}
3087
Erich Keanee891aa92018-07-13 15:07:47 +00003088static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003089 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003090 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003091 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003092 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003093
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003094 // gcc only allows for simple identifiers. Since we support more than gcc, we
3095 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003096 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003097 if (DRE->hasQualifier())
3098 S.Diag(Loc, diag::warn_cleanup_ext);
3099 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3100 NI = DRE->getNameInfo();
3101 if (!FD) {
3102 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3103 << NI.getName();
3104 return;
3105 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003106 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003107 if (ULE->hasExplicitTemplateArgs())
3108 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003109 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3110 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003111 if (!FD) {
3112 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3113 << NI.getName();
3114 if (ULE->getType() == S.Context.OverloadTy)
3115 S.NoteAllOverloadCandidates(ULE);
3116 return;
3117 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003118 } else {
3119 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003120 return;
3121 }
3122
Anders Carlssond277d792009-01-31 01:16:18 +00003123 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003124 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3125 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003126 return;
3127 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003128
Anders Carlsson723f55d2009-02-07 23:16:50 +00003129 // We're currently more strict than GCC about what function types we accept.
3130 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003131 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003132 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003133 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3134 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003135 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3136 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003137 return;
3138 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003139
Erich Keane6a24e802019-09-13 17:39:31 +00003140 D->addAttr(::new (S.Context) CleanupAttr(S.Context, AL, FD));
Anders Carlssond277d792009-01-31 01:16:18 +00003141}
3142
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003143static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003144 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003145 if (!AL.isArgIdent(0)) {
3146 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003147 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003148 return;
3149 }
3150
3151 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003152 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003153 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3154 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003155 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003156 return;
3157 }
3158
Erich Keane6a24e802019-09-13 17:39:31 +00003159 D->addAttr(::new (S.Context)
3160 EnumExtensibilityAttr(S.Context, AL, ExtensibilityKind));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003161}
3162
Mike Stumpd3bb5572009-07-24 19:02:52 +00003163/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003164/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003165static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003166 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003167 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003168 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003169 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003170
Eric Christopherb64963e2015-08-13 21:34:35 +00003171 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003172 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003173
Eric Christopherb64963e2015-08-13 21:34:35 +00003174 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3175 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003176 !isCFStringType(Ty, S.Context) &&
3177 (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003178 !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003179 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003180 << "a string type" << IdxExpr->getSourceRange()
3181 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003182 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003183 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003184 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003185 if (!isNSStringType(Ty, S.Context) &&
3186 !isCFStringType(Ty, S.Context) &&
3187 (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003188 !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003189 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003190 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003191 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003192 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003193 }
3194
Erich Keane6a24e802019-09-13 17:39:31 +00003195 D->addAttr(::new (S.Context) FormatArgAttr(S.Context, AL, Idx));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003196}
3197
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003198enum FormatAttrKind {
3199 CFStringFormat,
3200 NSStringFormat,
3201 StrftimeFormat,
3202 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003203 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003204 InvalidFormat
3205};
3206
3207/// getFormatAttrKind - Map from format attribute names to supported format
3208/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003209static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003210 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003211 // Check for formats that get handled specially.
3212 .Case("NSString", NSStringFormat)
3213 .Case("CFString", CFStringFormat)
3214 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003215
Mehdi Amini06d367c2016-10-24 20:39:34 +00003216 // Otherwise, check for supported formats.
3217 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3218 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3219 .Case("kprintf", SupportedFormat) // OpenBSD.
3220 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3221 .Case("os_trace", SupportedFormat)
3222 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003223
Mehdi Amini06d367c2016-10-24 20:39:34 +00003224 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3225 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003226}
3227
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003228/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003229/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003230static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003231 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003232 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003233 return;
3234 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003235
Aaron Ballman4a611152013-11-27 16:34:09 +00003236 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003237 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3238 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003239 return;
3240 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003241 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003242 if (S.Context.getAsArrayType(T))
3243 T = S.Context.getBaseElementType(T);
3244 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003245 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3246 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003247 return;
3248 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003249
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003250 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003251 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003252 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3253 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003254 return;
3255 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003256
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003257 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballman52c9ad22019-02-12 13:04:11 +00003258 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003259 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003260 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003261 return;
3262 }
Erich Keane6a24e802019-09-13 17:39:31 +00003263 D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003264}
3265
Erich Keane6a24e802019-09-13 17:39:31 +00003266FormatAttr *Sema::mergeFormatAttr(Decl *D, const AttributeCommonInfo &CI,
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003267 IdentifierInfo *Format, int FormatIdx,
Erich Keane6a24e802019-09-13 17:39:31 +00003268 int FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003269 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003270 for (auto *F : D->specific_attrs<FormatAttr>()) {
3271 if (F->getType() == Format &&
3272 F->getFormatIdx() == FormatIdx &&
3273 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003274 // If we don't have a valid location for this attribute, adopt the
3275 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003276 if (F->getLocation().isInvalid())
Erich Keane6a24e802019-09-13 17:39:31 +00003277 F->setRange(CI.getRange());
Craig Topperc3ec1492014-05-26 06:22:03 +00003278 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003279 }
3280 }
3281
Erich Keane6a24e802019-09-13 17:39:31 +00003282 return ::new (Context) FormatAttr(Context, CI, Format, FormatIdx, FirstArg);
Rafael Espindola92d49452012-05-11 00:36:07 +00003283}
3284
Mike Stumpd3bb5572009-07-24 19:02:52 +00003285/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003286/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003287static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003288 if (!AL.isArgIdent(0)) {
3289 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003290 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003291 return;
3292 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003293
Chandler Carruth743682b2010-11-16 08:35:43 +00003294 // In C++ the implicit 'this' function parameter also counts, and they are
3295 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003296 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003297 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003298
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003299 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003300 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003301
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003302 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003303 // If we've modified the string name, we need a new identifier for it.
3304 II = &S.Context.Idents.get(Format);
3305 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003306
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003307 // Check for supported formats.
3308 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003309
Chris Lattner12161d32010-03-22 21:08:50 +00003310 if (Kind == IgnoredFormat)
3311 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003312
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003313 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003314 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003315 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003316 return;
3317 }
3318
3319 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003320 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003321 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003322 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003323 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003324
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003325 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003326 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003327 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003328 return;
3329 }
3330
3331 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003332 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003333
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003334 if (HasImplicitThisParam) {
3335 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003336 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003337 diag::err_format_attribute_implicit_this_format_string)
3338 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003339 return;
3340 }
3341 ArgIdx--;
3342 }
Mike Stump11289f42009-09-09 15:08:12 +00003343
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003344 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003345 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003346
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003347 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003348 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003349 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003350 << "a CFString" << IdxExpr->getSourceRange()
3351 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003352 return;
3353 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003354 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003355 // FIXME: do we need to check if the type is NSString*? What are the
3356 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003357 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003358 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003359 << "an NSString" << IdxExpr->getSourceRange()
3360 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003361 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003362 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003363 } else if (!Ty->isPointerType() ||
Simon Pilgrim237d0af2019-10-04 15:02:46 +00003364 !Ty->castAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003365 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003366 << "a string type" << IdxExpr->getSourceRange()
3367 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003368 return;
3369 }
3370
3371 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003372 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003373 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003374 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003375 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003376
3377 // check if the function is variadic if the 3rd argument non-zero
3378 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003379 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003380 ++NumArgs; // +1 for ...
3381 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003382 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003383 return;
3384 }
3385 }
3386
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003387 // strftime requires FirstArg to be 0 because it doesn't read from any
3388 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003389 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003390 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003391 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003392 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003393 return;
3394 }
3395 // if 0 it disables parameter checking (to use with e.g. va_list)
3396 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003397 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003398 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003399 return;
3400 }
3401
Erich Keane6a24e802019-09-13 17:39:31 +00003402 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL, II, Idx, FirstArg);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003403 if (NewAttr)
3404 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003405}
3406
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003407/// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
3408static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3409 // The index that identifies the callback callee is mandatory.
3410 if (AL.getNumArgs() == 0) {
3411 S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee)
3412 << AL.getRange();
3413 return;
3414 }
3415
3416 bool HasImplicitThisParam = isInstanceMethod(D);
3417 int32_t NumArgs = getFunctionOrMethodNumParams(D);
3418
3419 FunctionDecl *FD = D->getAsFunction();
3420 assert(FD && "Expected a function declaration!");
3421
3422 llvm::StringMap<int> NameIdxMapping;
3423 NameIdxMapping["__"] = -1;
3424
3425 NameIdxMapping["this"] = 0;
3426
3427 int Idx = 1;
3428 for (const ParmVarDecl *PVD : FD->parameters())
3429 NameIdxMapping[PVD->getName()] = Idx++;
3430
3431 auto UnknownName = NameIdxMapping.end();
3432
3433 SmallVector<int, 8> EncodingIndices;
3434 for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) {
3435 SourceRange SR;
3436 int32_t ArgIdx;
3437
3438 if (AL.isArgIdent(I)) {
3439 IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
3440 auto It = NameIdxMapping.find(IdLoc->Ident->getName());
3441 if (It == UnknownName) {
3442 S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown)
3443 << IdLoc->Ident << IdLoc->Loc;
3444 return;
3445 }
3446
3447 SR = SourceRange(IdLoc->Loc);
3448 ArgIdx = It->second;
3449 } else if (AL.isArgExpr(I)) {
3450 Expr *IdxExpr = AL.getArgAsExpr(I);
3451
3452 // If the expression is not parseable as an int32_t we have a problem.
3453 if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1,
3454 false)) {
3455 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3456 << AL << (I + 1) << IdxExpr->getSourceRange();
3457 return;
3458 }
3459
3460 // Check oob, excluding the special values, 0 and -1.
3461 if (ArgIdx < -1 || ArgIdx > NumArgs) {
3462 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3463 << AL << (I + 1) << IdxExpr->getSourceRange();
3464 return;
3465 }
3466
3467 SR = IdxExpr->getSourceRange();
3468 } else {
3469 llvm_unreachable("Unexpected ParsedAttr argument type!");
3470 }
3471
3472 if (ArgIdx == 0 && !HasImplicitThisParam) {
3473 S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
3474 << (I + 1) << SR;
3475 return;
3476 }
3477
3478 // Adjust for the case we do not have an implicit "this" parameter. In this
3479 // case we decrease all positive values by 1 to get LLVM argument indices.
3480 if (!HasImplicitThisParam && ArgIdx > 0)
3481 ArgIdx -= 1;
3482
3483 EncodingIndices.push_back(ArgIdx);
3484 }
3485
3486 int CalleeIdx = EncodingIndices.front();
3487 // Check if the callee index is proper, thus not "this" and not "unknown".
Johannes Doerferte068d052019-01-21 14:23:46 +00003488 // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam"
3489 // is false and positive if "HasImplicitThisParam" is true.
3490 if (CalleeIdx < (int)HasImplicitThisParam) {
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003491 S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee)
3492 << AL.getRange();
3493 return;
3494 }
3495
3496 // Get the callee type, note the index adjustment as the AST doesn't contain
3497 // the this type (which the callee cannot reference anyway!).
3498 const Type *CalleeType =
3499 getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam)
3500 .getTypePtr();
3501 if (!CalleeType || !CalleeType->isFunctionPointerType()) {
3502 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3503 << AL.getRange();
3504 return;
3505 }
3506
3507 const Type *CalleeFnType =
3508 CalleeType->getPointeeType()->getUnqualifiedDesugaredType();
3509
3510 // TODO: Check the type of the callee arguments.
3511
3512 const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
3513 if (!CalleeFnProtoType) {
3514 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3515 << AL.getRange();
3516 return;
3517 }
3518
3519 if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
3520 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3521 << AL << (unsigned)(EncodingIndices.size() - 1);
3522 return;
3523 }
3524
3525 if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
3526 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3527 << AL << (unsigned)(EncodingIndices.size() - 1);
3528 return;
3529 }
3530
3531 if (CalleeFnProtoType->isVariadic()) {
3532 S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
3533 return;
3534 }
3535
3536 // Do not allow multiple callback attributes.
3537 if (D->hasAttr<CallbackAttr>()) {
3538 S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
3539 return;
3540 }
3541
3542 D->addAttr(::new (S.Context) CallbackAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00003543 S.Context, AL, EncodingIndices.data(), EncodingIndices.size()));
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003544}
3545
Erich Keanee891aa92018-07-13 15:07:47 +00003546static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003547 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003548 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003549 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003550 if (TD && TD->getUnderlyingType()->isUnionType())
3551 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3552 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003553 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003554
3555 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003556 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3557 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003558 return;
3559 }
3560
John McCallf937c022011-10-07 06:10:15 +00003561 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003562 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003563 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003564 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003565 return;
3566 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003567
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003568 RecordDecl::field_iterator Field = RD->field_begin(),
3569 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003570 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003571 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003572 return;
3573 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003574
David Blaikie40ed2972012-06-06 20:45:41 +00003575 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003576 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003577 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003578 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003579 diag::warn_transparent_union_attribute_floating)
3580 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003581 return;
3582 }
3583
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003584 if (FirstType->isIncompleteType())
3585 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003586 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3587 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3588 for (; Field != FieldEnd; ++Field) {
3589 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003590 if (FieldType->isIncompleteType())
3591 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003592 // FIXME: this isn't fully correct; we also need to test whether the
3593 // members of the union would all have the same calling convention as the
3594 // first member of the union. Checking just the size and alignment isn't
3595 // sufficient (consider structs passed on the stack instead of in registers
3596 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003597 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003598 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003599 // Warn if we drop the attribute.
3600 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003601 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003602 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003603 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003604 diag::warn_transparent_union_attribute_field_size_align)
3605 << isSize << Field->getDeclName() << FieldBits;
3606 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003607 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003608 diag::note_transparent_union_first_field_size_align)
3609 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003610 return;
3611 }
3612 }
3613
Erich Keane6a24e802019-09-13 17:39:31 +00003614 RD->addAttr(::new (S.Context) TransparentUnionAttr(S.Context, AL));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003615}
3616
Erich Keanee891aa92018-07-13 15:07:47 +00003617static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003618 // Make sure that there is a string literal as the annotation's single
3619 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003620 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003621 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003622 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003623
3624 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003625 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3626 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003627 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003628 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003629
Erich Keane6a24e802019-09-13 17:39:31 +00003630 D->addAttr(::new (S.Context) AnnotateAttr(S.Context, AL, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003631}
3632
Erich Keanee891aa92018-07-13 15:07:47 +00003633static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00003634 S.AddAlignValueAttr(D, AL, AL.getArgAsExpr(0));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003635}
3636
Erich Keane6a24e802019-09-13 17:39:31 +00003637void Sema::AddAlignValueAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E) {
3638 AlignValueAttr TmpAttr(Context, CI, E);
3639 SourceLocation AttrLoc = CI.getLoc();
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003640
3641 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003642 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003643 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003644 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003645 T = VD->getType();
3646 else
3647 llvm_unreachable("Unknown decl type for align_value");
3648
3649 if (!T->isDependentType() && !T->isAnyPointerType() &&
3650 !T->isReferenceType() && !T->isMemberPointerType()) {
3651 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3652 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3653 return;
3654 }
3655
3656 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003657 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003658 ExprResult ICE
3659 = VerifyIntegerConstantExpression(E, &Alignment,
3660 diag::err_align_value_attribute_argument_not_int,
3661 /*AllowFold*/ false);
3662 if (ICE.isInvalid())
3663 return;
3664
3665 if (!Alignment.isPowerOf2()) {
3666 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3667 << E->getSourceRange();
3668 return;
3669 }
3670
Erich Keane6a24e802019-09-13 17:39:31 +00003671 D->addAttr(::new (Context) AlignValueAttr(Context, CI, ICE.get()));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003672 return;
3673 }
3674
3675 // Save dependent expressions in the AST to be instantiated.
Erich Keane6a24e802019-09-13 17:39:31 +00003676 D->addAttr(::new (Context) AlignValueAttr(Context, CI, E));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003677}
3678
Erich Keanee891aa92018-07-13 15:07:47 +00003679static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003680 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003681 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003682 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003683 return;
3684 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003685
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003686 if (AL.getNumArgs() == 0) {
Erich Keane6a24e802019-09-13 17:39:31 +00003687 D->addAttr(::new (S.Context) AlignedAttr(S.Context, AL, true, nullptr));
Richard Smith848e1f12013-02-01 08:12:08 +00003688 return;
3689 }
3690
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003691 Expr *E = AL.getArgAsExpr(0);
3692 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3693 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003694 diag::err_pack_expansion_without_parameter_packs);
3695 return;
3696 }
3697
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003698 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003699 return;
3700
Erich Keane6a24e802019-09-13 17:39:31 +00003701 S.AddAlignedAttr(D, AL, E, AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003702}
3703
Erich Keane6a24e802019-09-13 17:39:31 +00003704void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
3705 bool IsPackExpansion) {
3706 AlignedAttr TmpAttr(Context, CI, true, E);
3707 SourceLocation AttrLoc = CI.getLoc();
Richard Smith848e1f12013-02-01 08:12:08 +00003708
Richard Smith1dba27c2013-01-29 09:02:09 +00003709 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003710 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003711 // C++11 [dcl.align]p1:
3712 // An alignment-specifier may be applied to a variable or to a class
3713 // data member, but it shall not be applied to a bit-field, a function
3714 // parameter, the formal parameter of a catch clause, or a variable
3715 // declared with the register storage class specifier. An
3716 // alignment-specifier may also be applied to the declaration of a class
3717 // or enumeration type.
3718 // C11 6.7.5/2:
3719 // An alignment attribute shall not be specified in a declaration of
3720 // a typedef, or a bit-field, or a function, or a parameter, or an
3721 // object declared with the register storage-class specifier.
3722 int DiagKind = -1;
3723 if (isa<ParmVarDecl>(D)) {
3724 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003725 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003726 if (VD->getStorageClass() == SC_Register)
3727 DiagKind = 1;
3728 if (VD->isExceptionVariable())
3729 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003730 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003731 if (FD->isBitField())
3732 DiagKind = 3;
3733 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003734 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003735 << (TmpAttr.isC11() ? ExpectedVariableOrField
3736 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003737 return;
3738 }
3739 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003740 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003741 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003742 return;
3743 }
3744 }
3745
Richard Smith90ae9672018-06-20 23:36:55 +00003746 if (E->isValueDependent()) {
3747 // We can't support a dependent alignment on a non-dependent type,
3748 // because we have no way to model that a type is "alignment-dependent"
3749 // but not dependent in any other way.
3750 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3751 if (!TND->getUnderlyingType()->isDependentType()) {
3752 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3753 << E->getSourceRange();
3754 return;
3755 }
3756 }
3757
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003758 // Save dependent expressions in the AST to be instantiated.
Erich Keane6a24e802019-09-13 17:39:31 +00003759 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, E);
Richard Smith44c247f2013-02-22 08:32:16 +00003760 AA->setPackExpansion(IsPackExpansion);
3761 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003762 return;
3763 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003764
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003765 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003766 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003767 ExprResult ICE
3768 = VerifyIntegerConstantExpression(E, &Alignment,
3769 diag::err_aligned_attribute_argument_not_int,
3770 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003771 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003772 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003773
David Majnemer0be6bd02015-07-26 09:02:21 +00003774 uint64_t AlignVal = Alignment.getZExtValue();
3775
Richard Smith848e1f12013-02-01 08:12:08 +00003776 // C++11 [dcl.align]p2:
3777 // -- if the constant expression evaluates to zero, the alignment
3778 // specifier shall have no effect
3779 // C11 6.7.5p6:
3780 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003781 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003782 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003783 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3784 << E->getSourceRange();
3785 return;
3786 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003787 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003788
David Majnemerabecae72014-02-12 20:36:10 +00003789 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003790 unsigned MaxValidAlignment =
3791 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3792 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003793 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003794 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3795 << E->getSourceRange();
3796 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003797 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003798
David Majnemer0be6bd02015-07-26 09:02:21 +00003799 if (Context.getTargetInfo().isTLSSupported()) {
3800 unsigned MaxTLSAlign =
3801 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3802 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003803 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003804 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3805 VD->getTLSKind() != VarDecl::TLS_None) {
3806 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3807 << (unsigned)AlignVal << VD << MaxTLSAlign;
3808 return;
3809 }
3810 }
3811
Erich Keane6a24e802019-09-13 17:39:31 +00003812 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, ICE.get());
Richard Smith44c247f2013-02-22 08:32:16 +00003813 AA->setPackExpansion(IsPackExpansion);
3814 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003815}
3816
Erich Keane6a24e802019-09-13 17:39:31 +00003817void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI,
3818 TypeSourceInfo *TS, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003819 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003820 // FIXME: Perform checking of type validity
Erich Keane6a24e802019-09-13 17:39:31 +00003821 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, false, TS);
Richard Smith44c247f2013-02-22 08:32:16 +00003822 AA->setPackExpansion(IsPackExpansion);
3823 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003824}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003825
Richard Smith848e1f12013-02-01 08:12:08 +00003826void Sema::CheckAlignasUnderalignment(Decl *D) {
3827 assert(D->hasAttrs() && "no attributes on decl");
3828
David Majnemer475b25e2015-01-21 10:54:38 +00003829 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003830 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003831 UnderlyingTy = DiagTy = VD->getType();
3832 } else {
3833 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003834 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003835 UnderlyingTy = ED->getIntegerType();
3836 }
3837 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003838 return;
3839
3840 // C++11 [dcl.align]p5, C11 6.7.5/4:
3841 // The combined effect of all alignment attributes in a declaration shall
3842 // not specify an alignment that is less strict than the alignment that
3843 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003844 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003845 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003846 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003847 if (I->isAlignmentDependent())
3848 return;
3849 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003850 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003851 Align = std::max(Align, I->getAlignment(Context));
3852 }
3853
3854 if (AlignasAttr && Align) {
3855 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003856 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003857 if (NaturalAlign > RequestedAlign)
3858 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003859 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003860 }
3861}
3862
David Majnemer2c4e00a2014-01-29 22:07:36 +00003863bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003864 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003865 MSInheritanceAttr::Spelling SemanticSpelling) {
3866 assert(RD->hasDefinition() && "RD has no definition!");
3867
David Majnemer98c9ee22014-02-07 00:43:07 +00003868 // We may not have seen base specifiers or any virtual methods yet. We will
3869 // have to wait until the record is defined to catch any mismatches.
3870 if (!RD->getDefinition()->isCompleteDefinition())
3871 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003872
David Majnemer98c9ee22014-02-07 00:43:07 +00003873 // The unspecified model never matches what a definition could need.
3874 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3875 return false;
3876
David Majnemer4bb09802014-02-10 19:50:15 +00003877 if (BestCase) {
3878 if (RD->calculateInheritanceModel() == SemanticSpelling)
3879 return false;
3880 } else {
3881 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3882 return false;
3883 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003884
3885 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3886 << 0 /*definition*/;
3887 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3888 << RD->getNameAsString();
3889 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003890}
3891
Alexey Bataevf278eb12015-11-19 10:13:11 +00003892/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3893/// attribute.
3894static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3895 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003896 IntegerMode = true;
3897 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003898 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003899 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003900 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003901 case 'Q':
3902 DestWidth = 8;
3903 break;
3904 case 'H':
3905 DestWidth = 16;
3906 break;
3907 case 'S':
3908 DestWidth = 32;
3909 break;
3910 case 'D':
3911 DestWidth = 64;
3912 break;
3913 case 'X':
3914 DestWidth = 96;
3915 break;
3916 case 'T':
3917 DestWidth = 128;
3918 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003919 }
3920 if (Str[1] == 'F') {
3921 IntegerMode = false;
3922 } else if (Str[1] == 'C') {
3923 IntegerMode = false;
3924 ComplexMode = true;
3925 } else if (Str[1] != 'I') {
3926 DestWidth = 0;
3927 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003928 break;
3929 case 4:
3930 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3931 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003932 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003933 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003934 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003935 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003936 break;
3937 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003938 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003939 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003940 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003941 case 11:
3942 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003943 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003944 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003945 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003946}
3947
3948/// handleModeAttr - This attribute modifies the width of a decl with primitive
3949/// type.
3950///
3951/// Despite what would be logical, the mode attribute is a decl attribute, not a
3952/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3953/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00003954static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003955 // This attribute isn't documented, but glibc uses it. It changes
3956 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003957 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003958 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
3959 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003960 return;
3961 }
3962
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003963 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003964
Erich Keane6a24e802019-09-13 17:39:31 +00003965 S.AddModeAttr(D, AL, Name);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003966}
3967
Erich Keane6a24e802019-09-13 17:39:31 +00003968void Sema::AddModeAttr(Decl *D, const AttributeCommonInfo &CI,
3969 IdentifierInfo *Name, bool InInstantiation) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003970 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003971 normalizeName(Str);
Erich Keane6a24e802019-09-13 17:39:31 +00003972 SourceLocation AttrLoc = CI.getLoc();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003973
3974 unsigned DestWidth = 0;
3975 bool IntegerMode = true;
3976 bool ComplexMode = false;
3977 llvm::APInt VectorSize(64, 0);
3978 if (Str.size() >= 4 && Str[0] == 'V') {
3979 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3980 size_t StrSize = Str.size();
3981 size_t VectorStringLength = 0;
3982 while ((VectorStringLength + 1) < StrSize &&
3983 isdigit(Str[VectorStringLength + 1]))
3984 ++VectorStringLength;
3985 if (VectorStringLength &&
3986 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3987 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003988 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003989 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003990 // Avoid duplicate warning from template instantiation.
3991 if (!InInstantiation)
3992 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003993 } else {
3994 VectorSize = 0;
3995 }
3996 }
3997
3998 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003999 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
4000
4001 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
4002 // and friends, at least with glibc.
4003 // FIXME: Make sure floating-point mappings are accurate
4004 // FIXME: Support XF and TF types
4005 if (!DestWidth) {
4006 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
4007 return;
4008 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00004009
4010 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004011 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00004012 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004013 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004014 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
4015 // Try to get type from enum declaration, default to int.
4016 OldTy = ED->getIntegerType();
4017 if (OldTy.isNull())
4018 OldTy = Context.IntTy;
4019 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004020 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00004021
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004022 if (OldTy->isDependentType()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004023 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004024 return;
4025 }
4026
Alexey Bataev326057d2015-06-19 07:46:21 +00004027 // Base type can also be a vector type (see PR17453).
4028 // Distinguish between base type and base element type.
4029 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004030 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00004031 OldElemTy = VT->getElementType();
4032
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004033 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
4034 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
4035 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
4036 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
4037 VectorSize.getBoolValue()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004038 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << CI.getRange();
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004039 return;
4040 }
4041 bool IntegralOrAnyEnumType =
4042 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
4043
4044 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
4045 !IntegralOrAnyEnumType)
4046 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00004047 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004048 if (!IntegralOrAnyEnumType)
4049 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004050 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004051 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004052 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00004053 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00004054 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004055 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004056 }
4057
Alexey Bataev326057d2015-06-19 07:46:21 +00004058 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004059
4060 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004061 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
4062 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004063 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004064 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00004065
Alexey Bataev326057d2015-06-19 07:46:21 +00004066 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004067 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004068 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004069 }
4070
Eli Friedman4735374e2009-03-03 06:41:03 +00004071 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004072 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004073 }
4074
4075 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004076 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004077 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
4078 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004079 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004080 // Complex machine mode does not support base vector types.
4081 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004082 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004083 return;
4084 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004085 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00004086 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004087 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004088 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004089 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00004090 }
4091
4092 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004093 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004094 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004095 }
4096
4097 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004098 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004099 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004100 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004101 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004102 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004103 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004104
Erich Keane6a24e802019-09-13 17:39:31 +00004105 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
Chris Lattneracbc2d22008-06-27 22:18:37 +00004106}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004107
Erich Keanee891aa92018-07-13 15:07:47 +00004108static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004109 D->addAttr(::new (S.Context) NoDebugAttr(S.Context, AL));
Anders Carlsson76187b42009-02-13 06:46:13 +00004110}
4111
Erich Keane6a24e802019-09-13 17:39:31 +00004112AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D,
4113 const AttributeCommonInfo &CI,
4114 const IdentifierInfo *Ident) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004115 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004116 Diag(CI.getLoc(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00004117 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4118 return nullptr;
4119 }
4120
4121 if (D->hasAttr<AlwaysInlineAttr>())
4122 return nullptr;
4123
Erich Keane6a24e802019-09-13 17:39:31 +00004124 return ::new (Context) AlwaysInlineAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004125}
4126
Erich Keane44bacdf2018-08-09 13:21:32 +00004127CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
4128 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004129 return nullptr;
4130
Erich Keane6a24e802019-09-13 17:39:31 +00004131 return ::new (Context) CommonAttr(Context, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004132}
4133
Erich Keane44bacdf2018-08-09 13:21:32 +00004134CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
4135 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
4136 return nullptr;
4137
Erich Keane6a24e802019-09-13 17:39:31 +00004138 return ::new (Context) CommonAttr(Context, AL);
Erich Keane44bacdf2018-08-09 13:21:32 +00004139}
4140
4141InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4142 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004143 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004144 // Attribute applies to Var but not any subclass of it (like ParmVar,
4145 // ImplicitParm or VarTemplateSpecialization).
4146 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004147 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4148 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4149 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004150 return nullptr;
4151 }
4152 // Attribute does not apply to non-static local variables.
4153 if (VD->hasLocalStorage()) {
4154 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4155 return nullptr;
4156 }
4157 }
4158
Erich Keane44bacdf2018-08-09 13:21:32 +00004159 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
4160 return nullptr;
4161
Erich Keane6a24e802019-09-13 17:39:31 +00004162 return ::new (Context) InternalLinkageAttr(Context, AL);
Erich Keane44bacdf2018-08-09 13:21:32 +00004163}
4164InternalLinkageAttr *
4165Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4166 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4167 // Attribute applies to Var but not any subclass of it (like ParmVar,
4168 // ImplicitParm or VarTemplateSpecialization).
4169 if (VD->getKind() != Decl::Var) {
4170 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4171 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4172 : ExpectedVariableOrFunction);
4173 return nullptr;
4174 }
4175 // Attribute does not apply to non-static local variables.
4176 if (VD->hasLocalStorage()) {
4177 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4178 return nullptr;
4179 }
4180 }
4181
4182 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004183 return nullptr;
4184
Erich Keane6a24e802019-09-13 17:39:31 +00004185 return ::new (Context) InternalLinkageAttr(Context, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004186}
4187
Erich Keane6a24e802019-09-13 17:39:31 +00004188MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, const AttributeCommonInfo &CI) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004189 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004190 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'minsize'";
Paul Robinson30e41fb2014-12-15 18:57:28 +00004191 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4192 return nullptr;
4193 }
4194
4195 if (D->hasAttr<MinSizeAttr>())
4196 return nullptr;
4197
Erich Keane6a24e802019-09-13 17:39:31 +00004198 return ::new (Context) MinSizeAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004199}
4200
Zola Bridges826ef592019-01-18 17:20:46 +00004201NoSpeculativeLoadHardeningAttr *Sema::mergeNoSpeculativeLoadHardeningAttr(
4202 Decl *D, const NoSpeculativeLoadHardeningAttr &AL) {
4203 if (checkAttrMutualExclusion<SpeculativeLoadHardeningAttr>(*this, D, AL))
4204 return nullptr;
4205
Erich Keane6a24e802019-09-13 17:39:31 +00004206 return ::new (Context) NoSpeculativeLoadHardeningAttr(Context, AL);
Zola Bridges826ef592019-01-18 17:20:46 +00004207}
4208
Erich Keane6a24e802019-09-13 17:39:31 +00004209OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D,
4210 const AttributeCommonInfo &CI) {
Paul Robinson30e41fb2014-12-15 18:57:28 +00004211 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4212 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
Erich Keane6a24e802019-09-13 17:39:31 +00004213 Diag(CI.getLoc(), diag::note_conflicting_attribute);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004214 D->dropAttr<AlwaysInlineAttr>();
4215 }
4216 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4217 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
Erich Keane6a24e802019-09-13 17:39:31 +00004218 Diag(CI.getLoc(), diag::note_conflicting_attribute);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004219 D->dropAttr<MinSizeAttr>();
4220 }
4221
4222 if (D->hasAttr<OptimizeNoneAttr>())
4223 return nullptr;
4224
Erich Keane6a24e802019-09-13 17:39:31 +00004225 return ::new (Context) OptimizeNoneAttr(Context, CI);
Paul Robinson30e41fb2014-12-15 18:57:28 +00004226}
4227
Zola Bridges826ef592019-01-18 17:20:46 +00004228SpeculativeLoadHardeningAttr *Sema::mergeSpeculativeLoadHardeningAttr(
4229 Decl *D, const SpeculativeLoadHardeningAttr &AL) {
4230 if (checkAttrMutualExclusion<NoSpeculativeLoadHardeningAttr>(*this, D, AL))
4231 return nullptr;
4232
Erich Keane6a24e802019-09-13 17:39:31 +00004233 return ::new (Context) SpeculativeLoadHardeningAttr(Context, AL);
Zola Bridges826ef592019-01-18 17:20:46 +00004234}
4235
Erich Keanee891aa92018-07-13 15:07:47 +00004236static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004237 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004238 return;
4239
Erich Keane6a24e802019-09-13 17:39:31 +00004240 if (AlwaysInlineAttr *Inline =
4241 S.mergeAlwaysInlineAttr(D, AL, AL.getAttrName()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004242 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004243}
4244
Erich Keanee891aa92018-07-13 15:07:47 +00004245static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004246 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(D, AL))
Paul Robinson080b1f32015-01-13 18:34:56 +00004247 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004248}
4249
Erich Keanee891aa92018-07-13 15:07:47 +00004250static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00004251 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(D, AL))
Paul Robinson080b1f32015-01-13 18:34:56 +00004252 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004253}
4254
Erich Keanee891aa92018-07-13 15:07:47 +00004255static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004256 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004257 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004258 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004259 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004260 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004261 return;
4262 }
Erich Keane6a24e802019-09-13 17:39:31 +00004263 D->addAttr(::new (S.Context) CUDAConstantAttr(S.Context, AL));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004264}
4265
Erich Keanee891aa92018-07-13 15:07:47 +00004266static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004267 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004268 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004269 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004270 // extern __shared__ is only allowed on arrays with no length (e.g.
4271 // "int x[]").
Yaxun Liu97670892018-10-02 17:48:54 +00004272 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004273 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004274 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004275 return;
4276 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004277 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004278 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004279 << S.CurrentCUDATarget())
4280 return;
Erich Keane6a24e802019-09-13 17:39:31 +00004281 D->addAttr(::new (S.Context) CUDASharedAttr(S.Context, AL));
Justin Lebar10411012016-09-30 23:57:30 +00004282}
4283
Erich Keanee891aa92018-07-13 15:07:47 +00004284static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004285 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4286 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004287 return;
4288 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004289 const auto *FD = cast<FunctionDecl>(D);
Michael Liao24337db2019-09-25 16:51:45 +00004290 if (!FD->getReturnType()->isVoidType() &&
4291 !FD->getReturnType()->getAs<AutoType>() &&
4292 !FD->getReturnType()->isInstantiationDependentType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004293 SourceRange RTRange = FD->getReturnTypeSourceRange();
4294 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004295 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004296 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4297 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004298 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004299 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004300 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4301 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004302 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004303 << Method;
4304 return;
4305 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004306 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004307 }
4308 // Only warn for "inline" when compiling for host, to cut down on noise.
4309 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004310 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004311
Erich Keane6a24e802019-09-13 17:39:31 +00004312 D->addAttr(::new (S.Context) CUDAGlobalAttr(S.Context, AL));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004313}
4314
Erich Keanee891aa92018-07-13 15:07:47 +00004315static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004316 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004317 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004318 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004319 return;
4320 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004321
Martin Storsjo71decf82019-09-27 12:25:19 +00004322 if (S.LangOpts.CPlusPlus && Fn->getStorageClass() != SC_Extern)
4323 S.Diag(AL.getLoc(), diag::warn_gnu_inline_cplusplus_without_extern);
4324
Erich Keane6a24e802019-09-13 17:39:31 +00004325 D->addAttr(::new (S.Context) GNUInlineAttr(S.Context, AL));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004326}
4327
Erich Keanee891aa92018-07-13 15:07:47 +00004328static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004329 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004330
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004331 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004332 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4333 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004334 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004335 return;
4336
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004337 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004338 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004339 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004340 return;
4341 }
4342
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004343 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004344 case ParsedAttr::AT_FastCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004345 D->addAttr(::new (S.Context) FastCallAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004346 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004347 case ParsedAttr::AT_StdCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004348 D->addAttr(::new (S.Context) StdCallAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004349 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004350 case ParsedAttr::AT_ThisCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004351 D->addAttr(::new (S.Context) ThisCallAttr(S.Context, AL));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004352 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004353 case ParsedAttr::AT_CDecl:
Erich Keane6a24e802019-09-13 17:39:31 +00004354 D->addAttr(::new (S.Context) CDeclAttr(S.Context, AL));
Abramo Bagnara50099372010-04-30 13:10:51 +00004355 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004356 case ParsedAttr::AT_Pascal:
Erich Keane6a24e802019-09-13 17:39:31 +00004357 D->addAttr(::new (S.Context) PascalAttr(S.Context, AL));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004358 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004359 case ParsedAttr::AT_SwiftCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004360 D->addAttr(::new (S.Context) SwiftCallAttr(S.Context, AL));
John McCall477f2bb2016-03-03 06:39:32 +00004361 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004362 case ParsedAttr::AT_VectorCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004363 D->addAttr(::new (S.Context) VectorCallAttr(S.Context, AL));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004364 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004365 case ParsedAttr::AT_MSABI:
Erich Keane6a24e802019-09-13 17:39:31 +00004366 D->addAttr(::new (S.Context) MSABIAttr(S.Context, AL));
Charles Davisb5a214e2013-08-30 04:39:01 +00004367 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004368 case ParsedAttr::AT_SysVABI:
Erich Keane6a24e802019-09-13 17:39:31 +00004369 D->addAttr(::new (S.Context) SysVABIAttr(S.Context, AL));
Charles Davisb5a214e2013-08-30 04:39:01 +00004370 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004371 case ParsedAttr::AT_RegCall:
Erich Keane6a24e802019-09-13 17:39:31 +00004372 D->addAttr(::new (S.Context) RegCallAttr(S.Context, AL));
Erich Keane757d3172016-11-02 18:29:35 +00004373 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004374 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004375 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004376 switch (CC) {
4377 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004378 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004379 break;
4380 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004381 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004382 break;
4383 default:
4384 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004385 }
4386
Erich Keane6a24e802019-09-13 17:39:31 +00004387 D->addAttr(::new (S.Context) PcsAttr(S.Context, AL, PCS));
Derek Schuffa2020962012-10-16 22:30:41 +00004388 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004389 }
Sander de Smalen44a22532018-11-26 16:38:37 +00004390 case ParsedAttr::AT_AArch64VectorPcs:
Erich Keane6a24e802019-09-13 17:39:31 +00004391 D->addAttr(::new (S.Context) AArch64VectorPcsAttr(S.Context, AL));
Sander de Smalen44a22532018-11-26 16:38:37 +00004392 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004393 case ParsedAttr::AT_IntelOclBicc:
Erich Keane6a24e802019-09-13 17:39:31 +00004394 D->addAttr(::new (S.Context) IntelOclBiccAttr(S.Context, AL));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004395 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004396 case ParsedAttr::AT_PreserveMost:
Erich Keane6a24e802019-09-13 17:39:31 +00004397 D->addAttr(::new (S.Context) PreserveMostAttr(S.Context, AL));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004398 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004399 case ParsedAttr::AT_PreserveAll:
Erich Keane6a24e802019-09-13 17:39:31 +00004400 D->addAttr(::new (S.Context) PreserveAllAttr(S.Context, AL));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004401 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004402 default:
4403 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004404 }
4405}
4406
Erich Keanee891aa92018-07-13 15:07:47 +00004407static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004408 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004409 return;
4410
4411 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004412 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004413 StringRef RuleName;
4414
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004415 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004416 return;
4417
4418 // FIXME: Warn if the rule name is unknown. This is tricky because only
4419 // clang-tidy knows about available rules.
4420 DiagnosticIdentifiers.push_back(RuleName);
4421 }
Erich Keane6a24e802019-09-13 17:39:31 +00004422 D->addAttr(::new (S.Context)
4423 SuppressAttr(S.Context, AL, DiagnosticIdentifiers.data(),
4424 DiagnosticIdentifiers.size()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004425}
4426
Matthias Gehred293cbd2019-07-25 17:50:51 +00004427static void handleLifetimeCategoryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4428 TypeSourceInfo *DerefTypeLoc = nullptr;
4429 QualType ParmType;
4430 if (AL.hasParsedType()) {
4431 ParmType = S.GetTypeFromParser(AL.getTypeArg(), &DerefTypeLoc);
4432
4433 unsigned SelectIdx = ~0U;
4434 if (ParmType->isVoidType())
4435 SelectIdx = 0;
4436 else if (ParmType->isReferenceType())
4437 SelectIdx = 1;
4438 else if (ParmType->isArrayType())
4439 SelectIdx = 2;
4440
4441 if (SelectIdx != ~0U) {
4442 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument)
4443 << SelectIdx << AL;
4444 return;
4445 }
4446 }
4447
4448 // To check if earlier decl attributes do not conflict the newly parsed ones
4449 // we always add (and check) the attribute to the cannonical decl.
4450 D = D->getCanonicalDecl();
4451 if (AL.getKind() == ParsedAttr::AT_Owner) {
4452 if (checkAttrMutualExclusion<PointerAttr>(S, D, AL))
4453 return;
4454 if (const auto *OAttr = D->getAttr<OwnerAttr>()) {
4455 const Type *ExistingDerefType = OAttr->getDerefTypeLoc()
4456 ? OAttr->getDerefType().getTypePtr()
4457 : nullptr;
4458 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
4459 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
4460 << AL << OAttr;
4461 S.Diag(OAttr->getLocation(), diag::note_conflicting_attribute);
4462 }
4463 return;
4464 }
Matthias Gehref64f4882019-09-06 08:56:30 +00004465 for (Decl *Redecl : D->redecls()) {
Erich Keane6a24e802019-09-13 17:39:31 +00004466 Redecl->addAttr(::new (S.Context) OwnerAttr(S.Context, AL, DerefTypeLoc));
Matthias Gehref64f4882019-09-06 08:56:30 +00004467 }
Matthias Gehred293cbd2019-07-25 17:50:51 +00004468 } else {
4469 if (checkAttrMutualExclusion<OwnerAttr>(S, D, AL))
4470 return;
4471 if (const auto *PAttr = D->getAttr<PointerAttr>()) {
4472 const Type *ExistingDerefType = PAttr->getDerefTypeLoc()
4473 ? PAttr->getDerefType().getTypePtr()
4474 : nullptr;
4475 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
4476 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
4477 << AL << PAttr;
4478 S.Diag(PAttr->getLocation(), diag::note_conflicting_attribute);
4479 }
4480 return;
4481 }
Matthias Gehref64f4882019-09-06 08:56:30 +00004482 for (Decl *Redecl : D->redecls()) {
4483 Redecl->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00004484 PointerAttr(S.Context, AL, DerefTypeLoc));
Matthias Gehref64f4882019-09-06 08:56:30 +00004485 }
Matthias Gehred293cbd2019-07-25 17:50:51 +00004486 }
4487}
4488
Erich Keanee891aa92018-07-13 15:07:47 +00004489bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004490 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004491 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004492 return true;
4493
Erich Keaneb11ebc52017-09-27 03:20:13 +00004494 if (Attrs.hasProcessingCache()) {
4495 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004496 return false;
4497 }
4498
Erich Keanee891aa92018-07-13 15:07:47 +00004499 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004500 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4501 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004502 return true;
4503 }
4504
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004505 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004506 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004507 case ParsedAttr::AT_CDecl:
4508 CC = CC_C;
4509 break;
4510 case ParsedAttr::AT_FastCall:
4511 CC = CC_X86FastCall;
4512 break;
4513 case ParsedAttr::AT_StdCall:
4514 CC = CC_X86StdCall;
4515 break;
4516 case ParsedAttr::AT_ThisCall:
4517 CC = CC_X86ThisCall;
4518 break;
4519 case ParsedAttr::AT_Pascal:
4520 CC = CC_X86Pascal;
4521 break;
4522 case ParsedAttr::AT_SwiftCall:
4523 CC = CC_Swift;
4524 break;
4525 case ParsedAttr::AT_VectorCall:
4526 CC = CC_X86VectorCall;
4527 break;
Sander de Smalen44a22532018-11-26 16:38:37 +00004528 case ParsedAttr::AT_AArch64VectorPcs:
4529 CC = CC_AArch64VectorCall;
4530 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004531 case ParsedAttr::AT_RegCall:
4532 CC = CC_X86RegCall;
4533 break;
4534 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004535 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004536 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004537 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004538 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004539 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4540 CC_C;
4541 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004542 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004543 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004544 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4545 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004546 return true;
4547 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004548 if (StrRef == "aapcs") {
4549 CC = CC_AAPCS;
4550 break;
4551 } else if (StrRef == "aapcs-vfp") {
4552 CC = CC_AAPCS_VFP;
4553 break;
4554 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004555
Erich Keaneb11ebc52017-09-27 03:20:13 +00004556 Attrs.setInvalid();
4557 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004558 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004559 }
Erich Keanee891aa92018-07-13 15:07:47 +00004560 case ParsedAttr::AT_IntelOclBicc:
4561 CC = CC_IntelOclBicc;
4562 break;
4563 case ParsedAttr::AT_PreserveMost:
4564 CC = CC_PreserveMost;
4565 break;
4566 case ParsedAttr::AT_PreserveAll:
4567 CC = CC_PreserveAll;
4568 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004569 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004570 }
4571
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004572 TargetInfo::CallingConvCheckResult A = TargetInfo::CCCR_OK;
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004573 const TargetInfo &TI = Context.getTargetInfo();
Yaxun Liu785cbd82019-02-27 15:46:29 +00004574 // CUDA functions may have host and/or device attributes which indicate
4575 // their targeted execution environment, therefore the calling convention
4576 // of functions in CUDA should be checked against the target deduced based
4577 // on their host/device attributes.
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004578 if (LangOpts.CUDA) {
Yaxun Liu785cbd82019-02-27 15:46:29 +00004579 auto *Aux = Context.getAuxTargetInfo();
Yaxun Liufa49c3a2019-02-26 22:24:49 +00004580 auto CudaTarget = IdentifyCUDATarget(FD);
4581 bool CheckHost = false, CheckDevice = false;
4582 switch (CudaTarget) {
4583 case CFT_HostDevice:
4584 CheckHost = true;
4585 CheckDevice = true;
4586 break;
4587 case CFT_Host:
4588 CheckHost = true;
4589 break;
4590 case CFT_Device:
4591 case CFT_Global:
4592 CheckDevice = true;
4593 break;
4594 case CFT_InvalidTarget:
4595 llvm_unreachable("unexpected cuda target");
4596 }
4597 auto *HostTI = LangOpts.CUDAIsDevice ? Aux : &TI;
4598 auto *DeviceTI = LangOpts.CUDAIsDevice ? &TI : Aux;
4599 if (CheckHost && HostTI)
4600 A = HostTI->checkCallingConvention(CC);
4601 if (A == TargetInfo::CCCR_OK && CheckDevice && DeviceTI)
4602 A = DeviceTI->checkCallingConvention(CC);
4603 } else {
4604 A = TI.checkCallingConvention(CC);
4605 }
Reid Kleckner4586a192019-07-09 23:17:43 +00004606
4607 switch (A) {
4608 case TargetInfo::CCCR_OK:
4609 break;
4610
4611 case TargetInfo::CCCR_Ignore:
4612 // Treat an ignored convention as if it was an explicit C calling convention
4613 // attribute. For example, __stdcall on Win x64 functions as __cdecl, so
4614 // that command line flags that change the default convention to
4615 // __vectorcall don't affect declarations marked __stdcall.
4616 CC = CC_C;
4617 break;
4618
Sunil Srivastavaf4038e72019-07-19 21:38:34 +00004619 case TargetInfo::CCCR_Error:
4620 Diag(Attrs.getLoc(), diag::error_cconv_unsupported)
4621 << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget;
4622 break;
4623
Reid Kleckner4586a192019-07-09 23:17:43 +00004624 case TargetInfo::CCCR_Warning: {
Sunil Srivastava85d667f2019-07-17 20:41:26 +00004625 Diag(Attrs.getLoc(), diag::warn_cconv_unsupported)
Reid Kleckner4586a192019-07-09 23:17:43 +00004626 << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004627
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004628 // This convention is not valid for the target. Use the default function or
4629 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004630 bool IsCXXMethod = false, IsVariadic = false;
4631 if (FD) {
4632 IsCXXMethod = FD->isCXXInstanceMember();
4633 IsVariadic = FD->isVariadic();
4634 }
4635 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Reid Kleckner4586a192019-07-09 23:17:43 +00004636 break;
4637 }
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004638 }
4639
Erich Keaneb11ebc52017-09-27 03:20:13 +00004640 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004641 return false;
4642}
4643
John McCall477f2bb2016-03-03 06:39:32 +00004644/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004645static bool isValidSwiftContextType(QualType Ty) {
4646 if (!Ty->hasPointerRepresentation())
4647 return Ty->isDependentType();
4648 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004649}
4650
4651/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004652static bool isValidSwiftIndirectResultType(QualType Ty) {
4653 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4654 Ty = PtrType->getPointeeType();
4655 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4656 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004657 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004658 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004659 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004660 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004661}
4662
4663/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004664static bool isValidSwiftErrorResultType(QualType Ty) {
4665 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4666 Ty = PtrType->getPointeeType();
4667 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4668 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004669 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004670 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004671 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004672 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004673 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004674 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004675}
4676
Erich Keane6a24e802019-09-13 17:39:31 +00004677void Sema::AddParameterABIAttr(Decl *D, const AttributeCommonInfo &CI,
4678 ParameterABI abi) {
John McCall477f2bb2016-03-03 06:39:32 +00004679
4680 QualType type = cast<ParmVarDecl>(D)->getType();
4681
4682 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4683 if (existingAttr->getABI() != abi) {
Erich Keane6a24e802019-09-13 17:39:31 +00004684 Diag(CI.getLoc(), diag::err_attributes_are_not_compatible)
4685 << getParameterABISpelling(abi) << existingAttr;
John McCall477f2bb2016-03-03 06:39:32 +00004686 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4687 return;
4688 }
4689 }
4690
4691 switch (abi) {
4692 case ParameterABI::Ordinary:
4693 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4694
4695 case ParameterABI::SwiftContext:
4696 if (!isValidSwiftContextType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004697 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4698 << getParameterABISpelling(abi) << /*pointer to pointer */ 0 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004699 }
Erich Keane6a24e802019-09-13 17:39:31 +00004700 D->addAttr(::new (Context) SwiftContextAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004701 return;
4702
4703 case ParameterABI::SwiftErrorResult:
4704 if (!isValidSwiftErrorResultType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004705 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4706 << getParameterABISpelling(abi) << /*pointer to pointer */ 1 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004707 }
Erich Keane6a24e802019-09-13 17:39:31 +00004708 D->addAttr(::new (Context) SwiftErrorResultAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004709 return;
4710
4711 case ParameterABI::SwiftIndirectResult:
4712 if (!isValidSwiftIndirectResultType(type)) {
Erich Keane6a24e802019-09-13 17:39:31 +00004713 Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4714 << getParameterABISpelling(abi) << /*pointer*/ 0 << type;
John McCall477f2bb2016-03-03 06:39:32 +00004715 }
Erich Keane6a24e802019-09-13 17:39:31 +00004716 D->addAttr(::new (Context) SwiftIndirectResultAttr(Context, CI));
John McCall477f2bb2016-03-03 06:39:32 +00004717 return;
4718 }
4719 llvm_unreachable("bad parameter ABI attribute");
4720}
4721
John McCall3882ace2011-01-05 12:14:39 +00004722/// Checks a regparm attribute, returning true if it is ill-formed and
4723/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004724bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004725 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004726 return true;
4727
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004728 if (!checkAttributeNumArgs(*this, AL, 1)) {
4729 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004730 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004731 }
Eli Friedman7044b762009-03-27 21:06:47 +00004732
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004733 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004734 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4735 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4736 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004737 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004738 }
4739
Douglas Gregore8bbc122011-09-02 00:18:52 +00004740 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004741 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004742 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004743 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004744 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004745 }
4746
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004747 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004748 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004749 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004750 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004751 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004752 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004753 }
4754
John McCall3882ace2011-01-05 12:14:39 +00004755 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004756}
4757
Artem Belevichbcec9da2016-06-06 22:54:57 +00004758// Checks whether an argument of launch_bounds attribute is
4759// acceptable, performs implicit conversion to Rvalue, and returns
4760// non-nullptr Expr result on success. Otherwise, it returns nullptr
4761// and may output an error.
4762static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004763 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004764 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004765 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004766 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004767
4768 // Accept template arguments for now as they depend on something else.
4769 // We'll get to check them when they eventually get instantiated.
4770 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004771 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004772
4773 llvm::APSInt I(64);
4774 if (!E->isIntegerConstantExpr(I, S.Context)) {
4775 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004776 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004777 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004778 }
4779 // Make sure we can fit it in 32 bits.
4780 if (!I.isIntN(32)) {
4781 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4782 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004783 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004784 }
4785 if (I < 0)
4786 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004787 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004788
Artem Belevichbcec9da2016-06-06 22:54:57 +00004789 // We may need to perform implicit conversion of the argument.
4790 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4791 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4792 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4793 assert(!ValArg.isInvalid() &&
4794 "Unexpected PerformCopyInitialization() failure.");
4795
4796 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004797}
4798
Erich Keane6a24e802019-09-13 17:39:31 +00004799void Sema::AddLaunchBoundsAttr(Decl *D, const AttributeCommonInfo &CI,
4800 Expr *MaxThreads, Expr *MinBlocks) {
4801 CUDALaunchBoundsAttr TmpAttr(Context, CI, MaxThreads, MinBlocks);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004802 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4803 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004804 return;
4805
Artem Belevichbcec9da2016-06-06 22:54:57 +00004806 if (MinBlocks) {
4807 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4808 if (MinBlocks == nullptr)
4809 return;
4810 }
Artem Belevich7093e402015-04-21 22:55:54 +00004811
Erich Keane6a24e802019-09-13 17:39:31 +00004812 D->addAttr(::new (Context)
4813 CUDALaunchBoundsAttr(Context, CI, MaxThreads, MinBlocks));
Artem Belevich7093e402015-04-21 22:55:54 +00004814}
4815
Erich Keanee891aa92018-07-13 15:07:47 +00004816static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004817 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4818 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004819 return;
4820
Erich Keane6a24e802019-09-13 17:39:31 +00004821 S.AddLaunchBoundsAttr(D, AL, AL.getArgAsExpr(0),
4822 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004823}
4824
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004825static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004826 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004827 if (!AL.isArgIdent(0)) {
4828 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004829 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004830 return;
4831 }
Joel E. Denny81508102018-03-13 14:51:22 +00004832
4833 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004834 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004835 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004836 return;
4837
Joel E. Denny81508102018-03-13 14:51:22 +00004838 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004839 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004840 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004841 return;
4842
Erich Keane6a24e802019-09-13 17:39:31 +00004843 bool IsPointer = AL.getAttrName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004844 if (IsPointer) {
4845 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004846 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4847 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4848 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004849 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004850 }
4851
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004852 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00004853 S.Context, AL, AL.getArgAsIdent(0)->Ident, ArgumentIdx, TypeTagIdx,
4854 IsPointer));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004855}
4856
4857static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004858 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004859 if (!AL.isArgIdent(0)) {
4860 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004861 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004862 return;
4863 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004864
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004865 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004866 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004867
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004868 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004869 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004870 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004871 return;
4872 }
4873
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004874 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004875 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004876 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004877 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004878
Erich Keane6a24e802019-09-13 17:39:31 +00004879 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
4880 S.Context, AL, PointerKind, MatchingCTypeLoc, AL.getLayoutCompatible(),
4881 AL.getMustBeNull()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004882}
4883
Erich Keanee891aa92018-07-13 15:07:47 +00004884static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004885 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004886
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004887 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004888 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004889 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004890 return;
4891
Joel E. Denny81508102018-03-13 14:51:22 +00004892 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
Erich Keane6a24e802019-09-13 17:39:31 +00004893 D->addAttr(::new (S.Context)
4894 XRayLogArgsAttr(S.Context, AL, ArgCount.getSourceIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004895}
4896
Simon Tatham7c11da02019-09-02 15:35:09 +01004897static bool ArmMveAliasValid(unsigned BuiltinID, StringRef AliasName) {
Simon Tatham08074cc2019-09-02 15:50:50 +01004898 if (AliasName.startswith("__arm_"))
4899 AliasName = AliasName.substr(6);
4900 switch (BuiltinID) {
4901#include "clang/Basic/arm_mve_builtin_aliases.inc"
4902 default:
4903 return false;
4904 }
Simon Tatham7c11da02019-09-02 15:35:09 +01004905}
4906
4907static void handleArmMveAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4908 if (!AL.isArgIdent(0)) {
4909 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4910 << AL << 1 << AANT_ArgumentIdentifier;
4911 return;
4912 }
4913
4914 IdentifierInfo *Ident = AL.getArgAsIdent(0)->Ident;
4915 unsigned BuiltinID = Ident->getBuiltinID();
4916
4917 if (!ArmMveAliasValid(BuiltinID,
4918 cast<FunctionDecl>(D)->getIdentifier()->getName())) {
4919 S.Diag(AL.getLoc(), diag::err_attribute_arm_mve_alias);
4920 return;
4921 }
4922
4923 D->addAttr(::new (S.Context) ArmMveAliasAttr(S.Context, AL, Ident));
4924}
4925
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004926//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004927// Checker-specific attribute handlers.
4928//===----------------------------------------------------------------------===//
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004929static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4930 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004931}
4932
George Karpenkov1657f362018-11-30 02:18:37 +00004933static bool isValidSubjectOfNSAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004934 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004935 QT->isObjCNSObjectType();
John McCalled433932011-01-25 03:31:58 +00004936}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004937
George Karpenkov1657f362018-11-30 02:18:37 +00004938static bool isValidSubjectOfCFAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004939 return QT->isDependentType() || QT->isPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004940 isValidSubjectOfNSAttribute(QT);
John McCalled433932011-01-25 03:31:58 +00004941}
4942
George Karpenkov1657f362018-11-30 02:18:37 +00004943static bool isValidSubjectOfOSAttribute(QualType QT) {
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004944 if (QT->isDependentType())
4945 return true;
4946 QualType PT = QT->getPointeeType();
4947 return !PT.isNull() && PT->getAsCXXRecordDecl() != nullptr;
John McCall3b5a8f52016-03-03 00:10:03 +00004948}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004949
Erich Keane6a24e802019-09-13 17:39:31 +00004950void Sema::AddXConsumedAttr(Decl *D, const AttributeCommonInfo &CI,
George Karpenkov1657f362018-11-30 02:18:37 +00004951 RetainOwnershipKind K,
4952 bool IsTemplateInstantiation) {
4953 ValueDecl *VD = cast<ValueDecl>(D);
4954 switch (K) {
4955 case RetainOwnershipKind::OS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004956 handleSimpleAttributeOrDiagnose<OSConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00004957 *this, VD, CI, isValidSubjectOfOSAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00004958 diag::warn_ns_attribute_wrong_parameter_type,
Erich Keane6a24e802019-09-13 17:39:31 +00004959 /*ExtraArgs=*/CI.getRange(), "os_consumed", /*pointers*/ 1);
George Karpenkov1657f362018-11-30 02:18:37 +00004960 return;
4961 case RetainOwnershipKind::NS:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004962 handleSimpleAttributeOrDiagnose<NSConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00004963 *this, VD, CI, isValidSubjectOfNSAttribute(VD->getType()),
John McCall3b5a8f52016-03-03 00:10:03 +00004964
George Karpenkov1657f362018-11-30 02:18:37 +00004965 // These attributes are normally just advisory, but in ARC, ns_consumed
4966 // is significant. Allow non-dependent code to contain inappropriate
4967 // attributes even in ARC, but require template instantiations to be
4968 // set up correctly.
4969 ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount)
4970 ? diag::err_ns_attribute_wrong_parameter_type
4971 : diag::warn_ns_attribute_wrong_parameter_type),
Erich Keane6a24e802019-09-13 17:39:31 +00004972 /*ExtraArgs=*/CI.getRange(), "ns_consumed", /*objc pointers*/ 0);
George Karpenkov1657f362018-11-30 02:18:37 +00004973 return;
4974 case RetainOwnershipKind::CF:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004975 handleSimpleAttributeOrDiagnose<CFConsumedAttr>(
Erich Keane6a24e802019-09-13 17:39:31 +00004976 *this, VD, CI, isValidSubjectOfCFAttribute(VD->getType()),
George Karpenkov1657f362018-11-30 02:18:37 +00004977 diag::warn_ns_attribute_wrong_parameter_type,
Erich Keane6a24e802019-09-13 17:39:31 +00004978 /*ExtraArgs=*/CI.getRange(), "cf_consumed", /*pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00004979 return;
4980 }
George Karpenkov1657f362018-11-30 02:18:37 +00004981}
John McCalled433932011-01-25 03:31:58 +00004982
George Karpenkov1657f362018-11-30 02:18:37 +00004983static Sema::RetainOwnershipKind
4984parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) {
4985 switch (AL.getKind()) {
4986 case ParsedAttr::AT_CFConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004987 case ParsedAttr::AT_CFReturnsRetained:
4988 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004989 return Sema::RetainOwnershipKind::CF;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004990 case ParsedAttr::AT_OSConsumesThis:
George Karpenkov1657f362018-11-30 02:18:37 +00004991 case ParsedAttr::AT_OSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004992 case ParsedAttr::AT_OSReturnsRetained:
4993 case ParsedAttr::AT_OSReturnsNotRetained:
4994 case ParsedAttr::AT_OSReturnsRetainedOnZero:
4995 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
George Karpenkov1657f362018-11-30 02:18:37 +00004996 return Sema::RetainOwnershipKind::OS;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004997 case ParsedAttr::AT_NSConsumesSelf:
George Karpenkov1657f362018-11-30 02:18:37 +00004998 case ParsedAttr::AT_NSConsumed:
George Karpenkov3a50a9f2019-01-11 18:02:08 +00004999 case ParsedAttr::AT_NSReturnsRetained:
5000 case ParsedAttr::AT_NSReturnsNotRetained:
5001 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005002 return Sema::RetainOwnershipKind::NS;
5003 default:
5004 llvm_unreachable("Wrong argument supplied");
5005 }
John McCalled433932011-01-25 03:31:58 +00005006}
5007
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005008bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
5009 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00005010 return false;
5011
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005012 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
5013 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00005014 return true;
5015}
5016
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005017/// \return whether the parameter is a pointer to OSObject pointer.
5018static bool isValidOSObjectOutParameter(const Decl *D) {
5019 const auto *PVD = dyn_cast<ParmVarDecl>(D);
5020 if (!PVD)
5021 return false;
5022 QualType QT = PVD->getType();
5023 QualType PT = QT->getPointeeType();
5024 return !PT.isNull() && isValidSubjectOfOSAttribute(PT);
5025}
5026
George Karpenkov1657f362018-11-30 02:18:37 +00005027static void handleXReturnsXRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005028 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005029 QualType ReturnType;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005030 Sema::RetainOwnershipKind K = parsedAttrToRetainOwnershipKind(AL);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005031
George Karpenkov1657f362018-11-30 02:18:37 +00005032 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005033 ReturnType = MD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005034 } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
5035 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) {
John McCall31168b02011-06-15 23:02:42 +00005036 return; // ignore: was handled as a type attribute
George Karpenkov1657f362018-11-30 02:18:37 +00005037 } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005038 ReturnType = PD->getType();
George Karpenkov1657f362018-11-30 02:18:37 +00005039 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005040 ReturnType = FD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00005041 } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
5042 // Attributes on parameters are used for out-parameters,
5043 // passed as pointers-to-pointers.
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005044 unsigned DiagID = K == Sema::RetainOwnershipKind::CF
5045 ? /*pointer-to-CF-pointer*/2
5046 : /*pointer-to-OSObject-pointer*/3;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005047 ReturnType = Param->getType()->getPointeeType();
5048 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005049 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005050 << AL << DiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005051 return;
5052 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005053 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00005054 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005055 } else {
5056 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005057 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005058 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005059 case ParsedAttr::AT_NSReturnsRetained:
5060 case ParsedAttr::AT_NSReturnsAutoreleased:
5061 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005062 ExpectedDeclKind = ExpectedFunctionOrMethod;
5063 break;
5064
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005065 case ParsedAttr::AT_OSReturnsRetained:
5066 case ParsedAttr::AT_OSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00005067 case ParsedAttr::AT_CFReturnsRetained:
5068 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005069 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
5070 break;
5071 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005072 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005073 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005074 return;
5075 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005076
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005077 bool TypeOK;
5078 bool Cf;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005079 unsigned ParmDiagID = 2; // Pointer-to-CF-pointer
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005080 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00005081 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005082 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005083 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
5084 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00005085 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005086
5087 case ParsedAttr::AT_NSReturnsAutoreleased:
5088 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005089 TypeOK = isValidSubjectOfNSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005090 Cf = false;
John McCalled433932011-01-25 03:31:58 +00005091 break;
5092
Erich Keanee891aa92018-07-13 15:07:47 +00005093 case ParsedAttr::AT_CFReturnsRetained:
5094 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005095 TypeOK = isValidSubjectOfCFAttribute(ReturnType);
5096 Cf = true;
5097 break;
5098
5099 case ParsedAttr::AT_OSReturnsRetained:
5100 case ParsedAttr::AT_OSReturnsNotRetained:
5101 TypeOK = isValidSubjectOfOSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005102 Cf = true;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005103 ParmDiagID = 3; // Pointer-to-OSObject-pointer
John McCalled433932011-01-25 03:31:58 +00005104 break;
5105 }
5106
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005107 if (!TypeOK) {
5108 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00005109 return;
5110
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005111 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005112 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov3a50a9f2019-01-11 18:02:08 +00005113 << AL << ParmDiagID << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005114 } else {
5115 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
5116 enum : unsigned {
5117 Function,
5118 Method,
5119 Property
5120 } SubjectKind = Function;
5121 if (isa<ObjCMethodDecl>(D))
5122 SubjectKind = Method;
5123 else if (isa<ObjCPropertyDecl>(D))
5124 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005125 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005126 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00005127 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005128 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00005129 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00005130
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005131 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005132 default:
David Blaikie83d382b2011-09-23 05:06:16 +00005133 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00005134 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00005135 handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL);
John McCalled433932011-01-25 03:31:58 +00005136 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005137 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005138 handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005139 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005140 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005141 handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00005142 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005143 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005144 handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005145 return;
Erich Keanee891aa92018-07-13 15:07:47 +00005146 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00005147 handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL);
5148 return;
5149 case ParsedAttr::AT_OSReturnsRetained:
5150 handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL);
5151 return;
5152 case ParsedAttr::AT_OSReturnsNotRetained:
5153 handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005154 return;
5155 };
5156}
5157
John McCallcf166702011-07-22 08:53:00 +00005158static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005159 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00005160 const int EP_ObjCMethod = 1;
5161 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00005162
Erich Keaneb11ebc52017-09-27 03:20:13 +00005163 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005164 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005165 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00005166 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00005167 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005168 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00005169
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00005170 if (!resultType->isReferenceType() &&
5171 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005172 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005173 << SourceRange(loc) << Attrs
5174 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
5175 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00005176
5177 // Drop the attribute.
5178 return;
5179 }
5180
Erich Keane6a24e802019-09-13 17:39:31 +00005181 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(S.Context, Attrs));
John McCallcf166702011-07-22 08:53:00 +00005182}
5183
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005184static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005185 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005186 const auto *Method = cast<ObjCMethodDecl>(D);
5187
5188 const DeclContext *DC = Method->getDeclContext();
5189 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005190 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005191 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005192 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
5193 return;
5194 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005195 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005196 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005197 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005198 return;
5199 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005200
Erich Keane6a24e802019-09-13 17:39:31 +00005201 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(S.Context, Attrs));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005202}
5203
Erich Keanee891aa92018-07-13 15:07:47 +00005204static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005205 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00005206
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005207 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005208 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005209 return;
5210 }
John McCall28592582015-02-01 22:34:06 +00005211
5212 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005213 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00005214 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005215 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00005216 return;
5217 }
5218
5219 // Only allow 'cv void *'.
5220 QualType T = TD->getUnderlyingType();
5221 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005222 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00005223 return;
5224 }
5225 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005226
Erich Keane6a24e802019-09-13 17:39:31 +00005227 D->addAttr(::new (S.Context) ObjCBridgeAttr(S.Context, AL, Parm->Ident));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005228}
5229
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005230static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005231 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005232 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00005233
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005234 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005235 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005236 return;
5237 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005238
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005239 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005240 ObjCBridgeMutableAttr(S.Context, AL, Parm->Ident));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005241}
5242
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005243static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005244 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005245 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005246 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005247 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005248 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005249 return;
5250 }
5251 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005252 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005253 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005254 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Erich Keane6a24e802019-09-13 17:39:31 +00005255 D->addAttr(::new (S.Context) ObjCBridgeRelatedAttr(
5256 S.Context, AL, RelatedClass, ClassMethod, InstanceMethod));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005257}
5258
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005259static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005260 const ParsedAttr &AL) {
Erik Pilkington81d3f452019-02-13 20:32:37 +00005261 DeclContext *Ctx = D->getDeclContext();
5262
5263 // This attribute can only be applied to methods in interfaces or class
5264 // extensions.
5265 if (!isa<ObjCInterfaceDecl>(Ctx) &&
5266 !(isa<ObjCCategoryDecl>(Ctx) &&
5267 cast<ObjCCategoryDecl>(Ctx)->IsClassExtension())) {
5268 S.Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
5269 return;
5270 }
5271
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005272 ObjCInterfaceDecl *IFace;
Erik Pilkington81d3f452019-02-13 20:32:37 +00005273 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(Ctx))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005274 IFace = CatDecl->getClassInterface();
5275 else
Erik Pilkington81d3f452019-02-13 20:32:37 +00005276 IFace = cast<ObjCInterfaceDecl>(Ctx);
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005277
5278 if (!IFace)
5279 return;
5280
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00005281 IFace->setHasDesignatedInitializers();
Erich Keane6a24e802019-09-13 17:39:31 +00005282 D->addAttr(::new (S.Context) ObjCDesignatedInitializerAttr(S.Context, AL));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005283}
5284
Erich Keanee891aa92018-07-13 15:07:47 +00005285static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005286 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005287 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005288 return;
5289 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005290 ObjCRuntimeNameAttr(S.Context, AL, MetaDataName));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005291}
5292
Nico Webera6916892016-06-10 18:53:04 +00005293// When a user wants to use objc_boxable with a union or struct
5294// but they don't have access to the declaration (legacy/third-party code)
5295// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00005296// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00005297static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00005298 bool notify = false;
5299
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005300 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00005301 if (RD && RD->getDefinition()) {
5302 RD = RD->getDefinition();
5303 notify = true;
5304 }
5305
5306 if (RD) {
Erich Keane6a24e802019-09-13 17:39:31 +00005307 ObjCBoxableAttr *BoxableAttr =
5308 ::new (S.Context) ObjCBoxableAttr(S.Context, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00005309 RD->addAttr(BoxableAttr);
5310 if (notify) {
5311 // we need to notify ASTReader/ASTWriter about
5312 // modification of existing declaration
5313 if (ASTMutationListener *L = S.getASTMutationListener())
5314 L->AddedAttributeToRecord(BoxableAttr, RD);
5315 }
5316 }
5317}
5318
Erich Keanee891aa92018-07-13 15:07:47 +00005319static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005320 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005321
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005322 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005323 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005324}
5325
Chandler Carruthedc2c642011-07-02 00:01:44 +00005326static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005327 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005328 const auto *VD = cast<ValueDecl>(D);
5329 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005330
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005331 if (!QT->isDependentType() &&
5332 !QT->isObjCLifetimeType()) {
5333 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5334 << QT;
John McCall31168b02011-06-15 23:02:42 +00005335 return;
5336 }
5337
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005338 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005339
5340 // If we have no lifetime yet, check the lifetime we're presumably
5341 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005342 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5343 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005344
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005345 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005346 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005347 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005348 "didn't infer lifetime for non-dependent type?");
5349 break;
5350
5351 case Qualifiers::OCL_Weak: // meaningful
5352 case Qualifiers::OCL_Strong: // meaningful
5353 break;
5354
5355 case Qualifiers::OCL_ExplicitNone:
5356 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005357 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5358 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005359 break;
5360 }
5361
Erich Keane6a24e802019-09-13 17:39:31 +00005362 D->addAttr(::new (S.Context) ObjCPreciseLifetimeAttr(S.Context, AL));
John McCall31168b02011-06-15 23:02:42 +00005363}
5364
Francois Picheta83957a2010-12-19 06:50:37 +00005365//===----------------------------------------------------------------------===//
5366// Microsoft specific attribute handlers.
5367//===----------------------------------------------------------------------===//
5368
Erich Keane6a24e802019-09-13 17:39:31 +00005369UuidAttr *Sema::mergeUuidAttr(Decl *D, const AttributeCommonInfo &CI,
5370 StringRef Uuid) {
Nico Weber88f5ed92016-09-13 18:55:26 +00005371 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005372 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005373 return nullptr;
5374 Diag(UA->getLocation(), diag::err_mismatched_uuid);
Erich Keane6a24e802019-09-13 17:39:31 +00005375 Diag(CI.getLoc(), diag::note_previous_uuid);
Nico Weber88f5ed92016-09-13 18:55:26 +00005376 D->dropAttr<UuidAttr>();
5377 }
5378
Erich Keane6a24e802019-09-13 17:39:31 +00005379 return ::new (Context) UuidAttr(Context, CI, Uuid);
Nico Weber88f5ed92016-09-13 18:55:26 +00005380}
5381
Erich Keanee891aa92018-07-13 15:07:47 +00005382static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005383 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005384 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005385 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005386 return;
5387 }
5388
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005389 StringRef StrRef;
5390 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005391 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005392 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005393
David Majnemer89085342013-08-09 08:56:20 +00005394 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5395 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005396 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5397 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005398
Reid Kleckner140c4a72013-05-17 14:04:52 +00005399 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005400 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005401 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005402 return;
5403 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005404
David Majnemer89085342013-08-09 08:56:20 +00005405 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005406 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005407 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005408 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005409 return;
5410 }
David Majnemer89085342013-08-09 08:56:20 +00005411 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005412 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005413 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005414 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005415 }
Francois Picheta83957a2010-12-19 06:50:37 +00005416
Nico Weber469891e2017-05-05 17:05:56 +00005417 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5418 // the only thing in the [] list, the [] too), and add an insertion of
5419 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5420 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005421 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005422 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005423 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5424 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005425
Erich Keane6a24e802019-09-13 17:39:31 +00005426 UuidAttr *UA = S.mergeUuidAttr(D, AL, StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005427 if (UA)
5428 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005429}
5430
Erich Keanee891aa92018-07-13 15:07:47 +00005431static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005432 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005433 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005434 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005435 return;
5436 }
5437 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Erich Keane6a24e802019-09-13 17:39:31 +00005438 D, AL, /*BestCase=*/true,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005439 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005440 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005441 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005442 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5443 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005444}
5445
Erich Keanee891aa92018-07-13 15:07:47 +00005446static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005447 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005448 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005449 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005450 return;
5451 }
5452 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005453 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005454 return;
5455 }
5456 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005457 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005458 return;
5459 }
Erich Keane6a24e802019-09-13 17:39:31 +00005460 D->addAttr(::new (S.Context) ThreadAttr(S.Context, AL));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005461}
5462
Erich Keanee891aa92018-07-13 15:07:47 +00005463static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005464 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005465 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005466 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005467 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005468 return;
5469 Tags.push_back(Tag);
5470 }
5471
5472 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5473 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005474 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005475 return;
5476 }
5477 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005478 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005479 return;
5480 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005481 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005482 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005483 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005484 return;
5485
5486 // Store tags sorted and without duplicates.
Fangrui Song55fab262018-09-26 22:16:28 +00005487 llvm::sort(Tags);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005488 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5489
5490 D->addAttr(::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00005491 AbiTagAttr(S.Context, AL, Tags.data(), Tags.size()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005492}
5493
Erich Keanee891aa92018-07-13 15:07:47 +00005494static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005495 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005496 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005497 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005498 return;
5499 }
5500
5501 StringRef Str;
5502 SourceLocation ArgLoc;
5503
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005504 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005505 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005506 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005507 return;
5508
5509 ARMInterruptAttr::InterruptType Kind;
5510 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005511 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5512 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005513 return;
5514 }
5515
Erich Keane6a24e802019-09-13 17:39:31 +00005516 D->addAttr(::new (S.Context) ARMInterruptAttr(S.Context, AL, Kind));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005517}
5518
Erich Keanee891aa92018-07-13 15:07:47 +00005519static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005520 // MSP430 'interrupt' attribute is applied to
5521 // a function with no parameters and void return type.
5522 if (!isFunctionOrMethod(D)) {
5523 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5524 << "'interrupt'" << ExpectedFunctionOrMethod;
5525 return;
5526 }
5527
5528 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005529 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5530 << /*MSP430*/ 1 << 0;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005531 return;
5532 }
5533
5534 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005535 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5536 << /*MSP430*/ 1 << 1;
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005537 return;
5538 }
5539
5540 // The attribute takes one integer argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005541 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005542 return;
5543
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005544 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005545 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5546 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005547 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005548 }
5549
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005550 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005551 llvm::APSInt NumParams(32);
5552 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005553 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005554 << AL << AANT_ArgumentIntegerConstant
5555 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005556 return;
5557 }
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005558 // The argument should be in range 0..63.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005559 unsigned Num = NumParams.getLimitedValue(255);
Anton Korobeynikov383e8272019-01-16 13:44:01 +00005560 if (Num > 63) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005561 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005562 << AL << (int)NumParams.getSExtValue()
5563 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005564 return;
5565 }
5566
Erich Keane6a24e802019-09-13 17:39:31 +00005567 D->addAttr(::new (S.Context) MSP430InterruptAttr(S.Context, AL, Num));
Aaron Ballman36a53502014-01-16 13:03:14 +00005568 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005569}
5570
Erich Keanee891aa92018-07-13 15:07:47 +00005571static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005572 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005573 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005574 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005575 return;
5576 }
5577
5578 StringRef Str;
5579 SourceLocation ArgLoc;
5580
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005581 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005582 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005583 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005584 return;
5585
5586 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5587 // a) Must be a function.
5588 // b) Must have no parameters.
5589 // c) Must have the 'void' return type.
5590 // d) Cannot have the 'mips16' attribute, as that instruction set
5591 // lacks the 'eret' instruction.
5592 // e) The attribute itself must either have no argument or one of the
5593 // valid interrupt types, see [MipsInterruptDocs].
5594
5595 if (!isFunctionOrMethod(D)) {
5596 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5597 << "'interrupt'" << ExpectedFunctionOrMethod;
5598 return;
5599 }
5600
5601 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005602 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5603 << /*MIPS*/ 0 << 0;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005604 return;
5605 }
5606
5607 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005608 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5609 << /*MIPS*/ 0 << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005610 return;
5611 }
5612
Erich Keane44bacdf2018-08-09 13:21:32 +00005613 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005614 return;
5615
5616 MipsInterruptAttr::InterruptType Kind;
5617 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005618 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005619 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005620 return;
5621 }
5622
Erich Keane6a24e802019-09-13 17:39:31 +00005623 D->addAttr(::new (S.Context) MipsInterruptAttr(S.Context, AL, Kind));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005624}
5625
Erich Keanee891aa92018-07-13 15:07:47 +00005626static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005627 // Semantic checks for a function with the 'interrupt' attribute.
5628 // a) Must be a function.
5629 // b) Must have the 'void' return type.
5630 // c) Must take 1 or 2 arguments.
5631 // d) The 1st argument must be a pointer.
5632 // e) The 2nd argument (if any) must be an unsigned integer.
5633 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5634 CXXMethodDecl::isStaticOverloadedOperator(
5635 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005636 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005637 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005638 return;
5639 }
5640 // Interrupt handler must have void return type.
5641 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5642 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5643 diag::err_anyx86_interrupt_attribute)
5644 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5645 ? 0
5646 : 1)
5647 << 0;
5648 return;
5649 }
5650 // Interrupt handler must have 1 or 2 parameters.
5651 unsigned NumParams = getFunctionOrMethodNumParams(D);
5652 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005653 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005654 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5655 ? 0
5656 : 1)
5657 << 1;
5658 return;
5659 }
5660 // The first argument must be a pointer.
5661 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5662 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5663 diag::err_anyx86_interrupt_attribute)
5664 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5665 ? 0
5666 : 1)
5667 << 2;
5668 return;
5669 }
5670 // The second argument, if present, must be an unsigned integer.
5671 unsigned TypeSize =
5672 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5673 ? 64
5674 : 32;
5675 if (NumParams == 2 &&
5676 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5677 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5678 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5679 diag::err_anyx86_interrupt_attribute)
5680 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5681 ? 0
5682 : 1)
5683 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5684 return;
5685 }
Erich Keane6a24e802019-09-13 17:39:31 +00005686 D->addAttr(::new (S.Context) AnyX86InterruptAttr(S.Context, AL));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005687 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5688}
5689
Erich Keanee891aa92018-07-13 15:07:47 +00005690static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005691 if (!isFunctionOrMethod(D)) {
5692 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5693 << "'interrupt'" << ExpectedFunction;
5694 return;
5695 }
5696
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005697 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005698 return;
5699
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005700 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005701}
5702
Erich Keanee891aa92018-07-13 15:07:47 +00005703static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005704 if (!isFunctionOrMethod(D)) {
5705 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5706 << "'signal'" << ExpectedFunction;
5707 return;
5708 }
5709
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005710 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005711 return;
5712
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005713 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005714}
5715
Yonghong Song4e2ce222019-11-01 22:16:59 -07005716static void handleBPFPreserveAIRecord(Sema &S, RecordDecl *RD) {
5717 // Add preserve_access_index attribute to all fields and inner records.
5718 for (auto D : RD->decls()) {
5719 if (D->hasAttr<BPFPreserveAccessIndexAttr>())
5720 continue;
5721
5722 D->addAttr(BPFPreserveAccessIndexAttr::CreateImplicit(S.Context));
5723 if (auto *Rec = dyn_cast<RecordDecl>(D))
5724 handleBPFPreserveAIRecord(S, Rec);
5725 }
5726}
5727
5728static void handleBPFPreserveAccessIndexAttr(Sema &S, Decl *D,
5729 const ParsedAttr &AL) {
5730 auto *Rec = cast<RecordDecl>(D);
5731 handleBPFPreserveAIRecord(S, Rec);
5732 Rec->addAttr(::new (S.Context) BPFPreserveAccessIndexAttr(S.Context, AL));
5733}
5734
Dan Gohmanb4323692019-01-24 21:08:30 +00005735static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5736 if (!isFunctionOrMethod(D)) {
5737 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5738 << "'import_module'" << ExpectedFunction;
5739 return;
5740 }
5741
5742 auto *FD = cast<FunctionDecl>(D);
5743 if (FD->isThisDeclarationADefinition()) {
5744 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5745 return;
5746 }
5747
5748 StringRef Str;
5749 SourceLocation ArgLoc;
5750 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5751 return;
5752
Erich Keane6a24e802019-09-13 17:39:31 +00005753 FD->addAttr(::new (S.Context)
5754 WebAssemblyImportModuleAttr(S.Context, AL, Str));
Dan Gohmanb4323692019-01-24 21:08:30 +00005755}
Ana Pazos1eee1b72018-07-26 17:37:45 +00005756
Dan Gohmancae84592019-02-01 22:25:23 +00005757static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5758 if (!isFunctionOrMethod(D)) {
5759 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5760 << "'import_name'" << ExpectedFunction;
5761 return;
5762 }
5763
5764 auto *FD = cast<FunctionDecl>(D);
5765 if (FD->isThisDeclarationADefinition()) {
5766 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5767 return;
5768 }
5769
5770 StringRef Str;
5771 SourceLocation ArgLoc;
5772 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5773 return;
5774
Erich Keane6a24e802019-09-13 17:39:31 +00005775 FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr(S.Context, AL, Str));
Dan Gohmancae84592019-02-01 22:25:23 +00005776}
5777
Ana Pazos1eee1b72018-07-26 17:37:45 +00005778static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5779 const ParsedAttr &AL) {
5780 // Warn about repeated attributes.
5781 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5782 S.Diag(AL.getRange().getBegin(),
5783 diag::warn_riscv_repeated_interrupt_attribute);
5784 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5785 return;
5786 }
5787
5788 // Check the attribute argument. Argument is optional.
5789 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5790 return;
5791
5792 StringRef Str;
5793 SourceLocation ArgLoc;
5794
5795 // 'machine'is the default interrupt mode.
5796 if (AL.getNumArgs() == 0)
5797 Str = "machine";
5798 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5799 return;
5800
5801 // Semantic checks for a function with the 'interrupt' attribute:
5802 // - Must be a function.
5803 // - Must have no parameters.
5804 // - Must have the 'void' return type.
5805 // - The attribute itself must either have no argument or one of the
5806 // valid interrupt types, see [RISCVInterruptDocs].
5807
5808 if (D->getFunctionType() == nullptr) {
5809 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5810 << "'interrupt'" << ExpectedFunction;
5811 return;
5812 }
5813
5814 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005815 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5816 << /*RISC-V*/ 2 << 0;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005817 return;
5818 }
5819
5820 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
Aaron Ballmanb0d74bf2019-01-23 18:02:17 +00005821 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
5822 << /*RISC-V*/ 2 << 1;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005823 return;
5824 }
5825
5826 RISCVInterruptAttr::InterruptType Kind;
5827 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005828 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5829 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005830 return;
5831 }
5832
Erich Keane6a24e802019-09-13 17:39:31 +00005833 D->addAttr(::new (S.Context) RISCVInterruptAttr(S.Context, AL, Kind));
Ana Pazos1eee1b72018-07-26 17:37:45 +00005834}
5835
Erich Keanee891aa92018-07-13 15:07:47 +00005836static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005837 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005838 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5839 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005840 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005841 break;
5842 case llvm::Triple::mipsel:
5843 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005844 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005845 break;
5846 case llvm::Triple::x86:
5847 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005848 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005849 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005850 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005851 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005852 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005853 case llvm::Triple::riscv32:
5854 case llvm::Triple::riscv64:
5855 handleRISCVInterruptAttr(S, D, AL);
5856 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005857 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005858 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005859 break;
5860 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005861}
5862
Michael Liao7557afa2019-02-26 18:49:36 +00005863static bool
5864checkAMDGPUFlatWorkGroupSizeArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr,
5865 const AMDGPUFlatWorkGroupSizeAttr &Attr) {
5866 // Accept template arguments for now as they depend on something else.
5867 // We'll get to check them when they eventually get instantiated.
5868 if (MinExpr->isValueDependent() || MaxExpr->isValueDependent())
5869 return false;
5870
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005871 uint32_t Min = 0;
Michael Liao7557afa2019-02-26 18:49:36 +00005872 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
5873 return true;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005874
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005875 uint32_t Max = 0;
Michael Liao7557afa2019-02-26 18:49:36 +00005876 if (!checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
5877 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005878
5879 if (Min == 0 && Max != 0) {
Michael Liao7557afa2019-02-26 18:49:36 +00005880 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5881 << &Attr << 0;
5882 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005883 }
5884 if (Min > Max) {
Michael Liao7557afa2019-02-26 18:49:36 +00005885 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5886 << &Attr << 1;
5887 return true;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005888 }
5889
Michael Liao7557afa2019-02-26 18:49:36 +00005890 return false;
5891}
5892
Erich Keane6a24e802019-09-13 17:39:31 +00005893void Sema::addAMDGPUFlatWorkGroupSizeAttr(Decl *D,
5894 const AttributeCommonInfo &CI,
5895 Expr *MinExpr, Expr *MaxExpr) {
5896 AMDGPUFlatWorkGroupSizeAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00005897
5898 if (checkAMDGPUFlatWorkGroupSizeArguments(*this, MinExpr, MaxExpr, TmpAttr))
5899 return;
5900
Erich Keane6a24e802019-09-13 17:39:31 +00005901 D->addAttr(::new (Context)
5902 AMDGPUFlatWorkGroupSizeAttr(Context, CI, MinExpr, MaxExpr));
Michael Liao7557afa2019-02-26 18:49:36 +00005903}
5904
5905static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
5906 const ParsedAttr &AL) {
5907 Expr *MinExpr = AL.getArgAsExpr(0);
5908 Expr *MaxExpr = AL.getArgAsExpr(1);
5909
Erich Keane6a24e802019-09-13 17:39:31 +00005910 S.addAMDGPUFlatWorkGroupSizeAttr(D, AL, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00005911}
5912
5913static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr,
5914 Expr *MaxExpr,
5915 const AMDGPUWavesPerEUAttr &Attr) {
5916 if (S.DiagnoseUnexpandedParameterPack(MinExpr) ||
5917 (MaxExpr && S.DiagnoseUnexpandedParameterPack(MaxExpr)))
5918 return true;
5919
5920 // Accept template arguments for now as they depend on something else.
5921 // We'll get to check them when they eventually get instantiated.
5922 if (MinExpr->isValueDependent() || (MaxExpr && MaxExpr->isValueDependent()))
5923 return false;
5924
5925 uint32_t Min = 0;
5926 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
5927 return true;
5928
5929 uint32_t Max = 0;
5930 if (MaxExpr && !checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
5931 return true;
5932
5933 if (Min == 0 && Max != 0) {
5934 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5935 << &Attr << 0;
5936 return true;
5937 }
5938 if (Max != 0 && Min > Max) {
5939 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
5940 << &Attr << 1;
5941 return true;
5942 }
5943
5944 return false;
5945}
5946
Erich Keane6a24e802019-09-13 17:39:31 +00005947void Sema::addAMDGPUWavesPerEUAttr(Decl *D, const AttributeCommonInfo &CI,
5948 Expr *MinExpr, Expr *MaxExpr) {
5949 AMDGPUWavesPerEUAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
Michael Liao7557afa2019-02-26 18:49:36 +00005950
5951 if (checkAMDGPUWavesPerEUArguments(*this, MinExpr, MaxExpr, TmpAttr))
5952 return;
5953
Erich Keane6a24e802019-09-13 17:39:31 +00005954 D->addAttr(::new (Context)
5955 AMDGPUWavesPerEUAttr(Context, CI, MinExpr, MaxExpr));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005956}
5957
Erich Keanee891aa92018-07-13 15:07:47 +00005958static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Liao7557afa2019-02-26 18:49:36 +00005959 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
5960 !checkAttributeAtMostNumArgs(S, AL, 2))
5961 return;
5962
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005963 Expr *MinExpr = AL.getArgAsExpr(0);
Michael Liao7557afa2019-02-26 18:49:36 +00005964 Expr *MaxExpr = (AL.getNumArgs() > 1) ? AL.getArgAsExpr(1) : nullptr;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005965
Erich Keane6a24e802019-09-13 17:39:31 +00005966 S.addAMDGPUWavesPerEUAttr(D, AL, MinExpr, MaxExpr);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005967}
5968
Erich Keanee891aa92018-07-13 15:07:47 +00005969static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005970 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005971 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5972 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005973 return;
5974
Erich Keane6a24e802019-09-13 17:39:31 +00005975 D->addAttr(::new (S.Context) AMDGPUNumSGPRAttr(S.Context, AL, NumSGPR));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005976}
5977
Erich Keanee891aa92018-07-13 15:07:47 +00005978static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005979 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005980 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5981 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005982 return;
5983
Erich Keane6a24e802019-09-13 17:39:31 +00005984 D->addAttr(::new (S.Context) AMDGPUNumVGPRAttr(S.Context, AL, NumVGPR));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005985}
5986
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005987static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005988 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005989 // If we try to apply it to a function pointer, don't warn, but don't
5990 // do anything, either. It doesn't matter anyway, because there's nothing
5991 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005992 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005993 if (VD && VD->getType()->isFunctionPointerType())
5994 return;
5995 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005996 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005997 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5998 TD->getUnderlyingType()->isFunctionType()))
5999 return;
6000 // Attribute can only be applied to function types.
6001 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006002 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006003 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006004 return;
6005 }
6006
Erich Keane6a24e802019-09-13 17:39:31 +00006007 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(S.Context, AL));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006008}
6009
Erich Keanee891aa92018-07-13 15:07:47 +00006010static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00006011 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006012 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
6013 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00006014 return;
6015
6016 // TODO: Investigate what happens with the next major version of MSVC.
Reid Kleckner1a94d872018-12-17 23:16:43 +00006017 if (Version != LangOptions::MSVC2015 / 100) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006018 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00006019 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00006020 return;
6021 }
6022
Reid Kleckner1a94d872018-12-17 23:16:43 +00006023 // The attribute expects a "major" version number like 19, but new versions of
6024 // MSVC have moved to updating the "minor", or less significant numbers, so we
6025 // have to multiply by 100 now.
6026 Version *= 100;
6027
Erich Keane6a24e802019-09-13 17:39:31 +00006028 D->addAttr(::new (S.Context) LayoutVersionAttr(S.Context, AL, Version));
David Majnemercd3ebfe2016-05-23 17:16:12 +00006029}
6030
Erich Keane6a24e802019-09-13 17:39:31 +00006031DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D,
6032 const AttributeCommonInfo &CI) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006033 if (D->hasAttr<DLLExportAttr>()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006034 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00006035 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006036 }
6037
6038 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00006039 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006040
Erich Keane6a24e802019-09-13 17:39:31 +00006041 return ::new (Context) DLLImportAttr(Context, CI);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006042}
6043
Erich Keane6a24e802019-09-13 17:39:31 +00006044DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D,
6045 const AttributeCommonInfo &CI) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006046 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00006047 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006048 D->dropAttr<DLLImportAttr>();
6049 }
6050
6051 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00006052 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006053
Erich Keane6a24e802019-09-13 17:39:31 +00006054 return ::new (Context) DLLExportAttr(Context, CI);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006055}
6056
Erich Keanee891aa92018-07-13 15:07:47 +00006057static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00006058 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
6059 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006060 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00006061 return;
6062 }
6063
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006064 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00006065 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00006066 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
6067 // MinGW doesn't allow dllimport on inline functions.
6068 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00006069 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00006070 return;
6071 }
6072 }
6073
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006074 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00006075 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
6076 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006077 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00006078 return;
6079 }
6080 }
6081
Erich Keanee891aa92018-07-13 15:07:47 +00006082 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Erich Keane6a24e802019-09-13 17:39:31 +00006083 ? (Attr *)S.mergeDLLExportAttr(D, A)
6084 : (Attr *)S.mergeDLLImportAttr(D, A);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006085 if (NewAttr)
6086 D->addAttr(NewAttr);
6087}
6088
David Majnemer2c4e00a2014-01-29 22:07:36 +00006089MSInheritanceAttr *
Erich Keane6a24e802019-09-13 17:39:31 +00006090Sema::mergeMSInheritanceAttr(Decl *D, const AttributeCommonInfo &CI,
6091 bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00006092 MSInheritanceAttr::Spelling SemanticSpelling) {
6093 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
6094 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00006095 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006096 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
6097 << 1 /*previous declaration*/;
Erich Keane6a24e802019-09-13 17:39:31 +00006098 Diag(CI.getLoc(), diag::note_previous_ms_inheritance);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006099 D->dropAttr<MSInheritanceAttr>();
6100 }
6101
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006102 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006103 if (RD->hasDefinition()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006104 if (checkMSInheritanceAttrOnDefinition(RD, CI.getRange(), BestCase,
David Majnemer4bb09802014-02-10 19:50:15 +00006105 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006106 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006107 }
6108 } else {
6109 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
Erich Keane6a24e802019-09-13 17:39:31 +00006110 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
David Majnemer2c4e00a2014-01-29 22:07:36 +00006111 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006112 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006113 }
6114 if (RD->getDescribedClassTemplate()) {
Erich Keane6a24e802019-09-13 17:39:31 +00006115 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
David Majnemer2c4e00a2014-01-29 22:07:36 +00006116 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00006117 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00006118 }
6119 }
6120
Erich Keane6a24e802019-09-13 17:39:31 +00006121 return ::new (Context) MSInheritanceAttr(Context, CI, BestCase);
David Majnemer2c4e00a2014-01-29 22:07:36 +00006122}
6123
Erich Keanee891aa92018-07-13 15:07:47 +00006124static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006125 // The capability attributes take a single string parameter for the name of
6126 // the capability they represent. The lockable attribute does not take any
6127 // parameters. However, semantically, both attributes represent the same
6128 // concept, and so they use the same semantic attribute. Eventually, the
6129 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00006130 //
Alp Toker958027b2014-07-14 19:42:55 +00006131 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00006132 // literal will be considered a "mutex."
6133 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006134 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00006135 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006136 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006137 return;
6138
Aaron Ballman6c810072014-03-05 21:47:13 +00006139 // Currently, there are only two names allowed for a capability: role and
6140 // mutex (case insensitive). Diagnose other capability names.
6141 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
6142 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
6143
Erich Keane6a24e802019-09-13 17:39:31 +00006144 D->addAttr(::new (S.Context) CapabilityAttr(S.Context, AL, N));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006145}
6146
Erich Keanee891aa92018-07-13 15:07:47 +00006147static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00006148 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006149 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00006150 return;
6151
Erich Keane6a24e802019-09-13 17:39:31 +00006152 D->addAttr(::new (S.Context)
6153 AssertCapabilityAttr(S.Context, AL, Args.data(), Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006154}
6155
6156static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006157 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006158 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006159 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006160 return;
6161
Erich Keane6a24e802019-09-13 17:39:31 +00006162 D->addAttr(::new (S.Context) AcquireCapabilityAttr(S.Context, AL, Args.data(),
6163 Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006164}
6165
6166static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006167 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006168 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006169 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006170 return;
6171
Erich Keane6a24e802019-09-13 17:39:31 +00006172 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(
6173 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006174}
6175
6176static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006177 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006178 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00006179 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006180 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006181
Erich Keane6a24e802019-09-13 17:39:31 +00006182 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(S.Context, AL, Args.data(),
6183 Args.size()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006184}
6185
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006186static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006187 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006188 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006189 return;
6190
6191 // check that all arguments are lockable objects
6192 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006193 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006194 if (Args.empty())
6195 return;
6196
6197 RequiresCapabilityAttr *RCA = ::new (S.Context)
Erich Keane6a24e802019-09-13 17:39:31 +00006198 RequiresCapabilityAttr(S.Context, AL, Args.data(), Args.size());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006199
6200 D->addAttr(RCA);
6201}
6202
Erich Keanee891aa92018-07-13 15:07:47 +00006203static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006204 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00006205 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006206 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00006207 // Do not want to attach the attribute to the namespace because that will
6208 // cause confusing diagnostic reports for uses of declarations within the
6209 // namespace.
6210 return;
6211 }
6212 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00006213
Manman Renc7890fe2016-03-16 18:50:49 +00006214 // Handle the cases where the attribute has a text message.
6215 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006216 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
6217 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00006218 return;
6219
6220 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006221 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
6222 checkAttributeAtMostNumArgs(S, AL, 1);
6223 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
Sander de Smalen44a22532018-11-26 16:38:37 +00006224 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
6225 return;
6226
6227 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
6228 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
6229
Erich Keane6a24e802019-09-13 17:39:31 +00006230 D->addAttr(::new (S.Context) DeprecatedAttr(S.Context, AL, Str, Replacement));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006231}
6232
6233static bool isGlobalVar(const Decl *D) {
6234 if (const auto *S = dyn_cast<VarDecl>(D))
6235 return S->hasGlobalStorage();
6236 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00006237}
6238
Erich Keanee891aa92018-07-13 15:07:47 +00006239static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006240 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00006241 return;
6242
Benjamin Kramer1b582012016-02-13 18:11:49 +00006243 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00006244
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006245 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00006246 StringRef SanitizerName;
6247 SourceLocation LiteralLoc;
6248
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006249 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00006250 return;
6251
Pierre Gousseauae5303d2019-03-01 10:05:15 +00006252 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) ==
6253 SanitizerMask())
Peter Collingbourne915df992015-05-15 18:33:32 +00006254 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006255 else if (isGlobalVar(D) && SanitizerName != "address")
6256 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006257 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00006258 Sanitizers.push_back(SanitizerName);
6259 }
6260
Erich Keane6a24e802019-09-13 17:39:31 +00006261 D->addAttr(::new (S.Context) NoSanitizeAttr(S.Context, AL, Sanitizers.data(),
6262 Sanitizers.size()));
Peter Collingbourne915df992015-05-15 18:33:32 +00006263}
6264
6265static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006266 const ParsedAttr &AL) {
Erich Keane6a24e802019-09-13 17:39:31 +00006267 StringRef AttrName = AL.getAttrName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00006268 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00006269 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
6270 .Case("no_address_safety_analysis", "address")
6271 .Case("no_sanitize_address", "address")
6272 .Case("no_sanitize_thread", "thread")
6273 .Case("no_sanitize_memory", "memory");
6274 if (isGlobalVar(D) && SanitizerName != "address")
6275 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00006276 << AL << ExpectedFunction;
Aaron Ballman31ca49b2019-05-21 17:24:49 +00006277
6278 // FIXME: Rather than create a NoSanitizeSpecificAttr, this creates a
6279 // NoSanitizeAttr object; but we need to calculate the correct spelling list
6280 // index rather than incorrectly assume the index for NoSanitizeSpecificAttr
6281 // has the same spellings as the index for NoSanitizeAttr. We don't have a
6282 // general way to "translate" between the two, so this hack attempts to work
6283 // around the issue with hard-coded indicies. This is critical for calling
6284 // getSpelling() or prettyPrint() on the resulting semantic attribute object
6285 // without failing assertions.
6286 unsigned TranslatedSpellingIndex = 0;
6287 if (AL.isC2xAttribute() || AL.isCXX11Attribute())
6288 TranslatedSpellingIndex = 1;
6289
Erich Keane6a24e802019-09-13 17:39:31 +00006290 AttributeCommonInfo Info = AL;
6291 Info.setAttributeSpellingListIndex(TranslatedSpellingIndex);
6292 D->addAttr(::new (S.Context)
6293 NoSanitizeAttr(S.Context, Info, &SanitizerName, 1));
Peter Collingbourne915df992015-05-15 18:33:32 +00006294}
6295
Erich Keanee891aa92018-07-13 15:07:47 +00006296static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00006297 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006298 D->addAttr(Internal);
6299}
6300
Erich Keanee891aa92018-07-13 15:07:47 +00006301static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006302 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006303 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00006304 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006305 else
Erich Keane44bacdf2018-08-09 13:21:32 +00006306 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
6307 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00006308}
6309
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006310/// Handles semantic checking for features that are common to all attributes,
6311/// such as checking whether a parameter was properly specified, or the correct
6312/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006313static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006314 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006315 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00006316 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006317 //
6318 // We also bail on unknown and ignored attributes because those are handled
6319 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00006320 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006321 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006322 // Check whether the attribute requires specific language extensions to be
6323 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006324 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00006325 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00006326 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006327 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00006328 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006329 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00006330 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006331
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006332 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006333 // If there are no optional arguments, then checking for the argument count
6334 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006335 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006336 return true;
6337 } else {
6338 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006339 if (AL.getMinArgs() &&
6340 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006341 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006342 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
6343 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006344 return true;
6345 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00006346
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006347 if (S.CheckAttrTarget(AL))
6348 return true;
6349
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006350 return false;
6351}
6352
Erich Keanee891aa92018-07-13 15:07:47 +00006353static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006354 if (D->isInvalidDecl())
6355 return;
6356
6357 // Check if there is only one access qualifier.
6358 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006359 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
6360 AL.getSemanticSpelling()) {
6361 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
Erich Keane6a24e802019-09-13 17:39:31 +00006362 << AL.getAttrName()->getName() << AL.getRange();
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006363 } else {
6364 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
6365 << D->getSourceRange();
6366 D->setInvalidDecl(true);
6367 return;
6368 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00006369 }
6370
6371 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
6372 // image object can be read and written.
6373 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
6374 // object. Using the read_write (or __read_write) qualifier with the pipe
6375 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006376 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006377 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Erich Keane6a24e802019-09-13 17:39:31 +00006378 if (AL.getAttrName()->getName().find("read_write") != StringRef::npos) {
Anastasia Stulova2c4730d2019-02-15 12:07:57 +00006379 if ((!S.getLangOpts().OpenCLCPlusPlus &&
6380 S.getLangOpts().OpenCLVersion < 200) ||
6381 DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006382 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00006383 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00006384 D->setInvalidDecl(true);
6385 return;
6386 }
6387 }
6388 }
6389
Erich Keane6a24e802019-09-13 17:39:31 +00006390 D->addAttr(::new (S.Context) OpenCLAccessAttr(S.Context, AL));
Xiuli Pan11e13f62016-02-26 03:13:03 +00006391}
6392
Erik Pilkington5a559e62018-08-21 17:24:06 +00006393static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006394 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00006395 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
6396 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
6397 return;
6398 }
6399
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006400 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00006401 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006402 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00006403 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00006404}
6405
JF Bastien14daa202018-12-18 05:12:21 +00006406static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6407 assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic &&
6408 "uninitialized is only valid on automatic duration variables");
Erich Keane6a24e802019-09-13 17:39:31 +00006409 D->addAttr(::new (S.Context) UninitializedAttr(S.Context, AL));
JF Bastien14daa202018-12-18 05:12:21 +00006410}
6411
Erik Pilkington1e368822019-01-04 18:33:06 +00006412static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD,
6413 bool DiagnoseFailure) {
6414 QualType Ty = VD->getType();
6415 if (!Ty->isObjCRetainableType()) {
6416 if (DiagnoseFailure) {
6417 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6418 << 0;
6419 }
6420 return false;
6421 }
6422
6423 Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime();
6424
6425 // Sema::inferObjCARCLifetime must run after processing decl attributes
6426 // (because __block lowers to an attribute), so if the lifetime hasn't been
6427 // explicitly specified, infer it locally now.
6428 if (LifetimeQual == Qualifiers::OCL_None)
6429 LifetimeQual = Ty->getObjCARCImplicitLifetime();
6430
6431 // The attributes only really makes sense for __strong variables; ignore any
6432 // attempts to annotate a parameter with any other lifetime qualifier.
6433 if (LifetimeQual != Qualifiers::OCL_Strong) {
6434 if (DiagnoseFailure) {
6435 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6436 << 1;
6437 }
6438 return false;
6439 }
6440
6441 // Tampering with the type of a VarDecl here is a bit of a hack, but we need
6442 // to ensure that the variable is 'const' so that we can error on
6443 // modification, which can otherwise over-release.
6444 VD->setType(Ty.withConst());
6445 VD->setARCPseudoStrong(true);
6446 return true;
6447}
6448
6449static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D,
6450 const ParsedAttr &AL) {
6451 if (auto *VD = dyn_cast<VarDecl>(D)) {
6452 assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically");
6453 if (!VD->hasLocalStorage()) {
6454 S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6455 << 0;
6456 return;
6457 }
6458
6459 if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true))
6460 return;
6461
6462 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6463 return;
6464 }
6465
6466 // If D is a function-like declaration (method, block, or function), then we
6467 // make every parameter psuedo-strong.
6468 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); I != E; ++I) {
6469 auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I));
6470 QualType Ty = PVD->getType();
6471
6472 // If a user wrote a parameter with __strong explicitly, then assume they
6473 // want "real" strong semantics for that parameter. This works because if
6474 // the parameter was written with __strong, then the strong qualifier will
6475 // be non-local.
6476 if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() ==
6477 Qualifiers::OCL_Strong)
6478 continue;
6479
6480 tryMakeVariablePseudoStrong(S, PVD, /*DiagnoseFailure=*/false);
6481 }
6482 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6483}
6484
Artem Dergachevc333d772019-02-21 00:01:02 +00006485static void handleMIGServerRoutineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6486 // Check that the return type is a `typedef int kern_return_t` or a typedef
6487 // around it, because otherwise MIG convention checks make no sense.
6488 // BlockDecl doesn't store a return type, so it's annoying to check,
6489 // so let's skip it for now.
6490 if (!isa<BlockDecl>(D)) {
6491 QualType T = getFunctionOrMethodResultType(D);
6492 bool IsKernReturnT = false;
6493 while (const auto *TT = T->getAs<TypedefType>()) {
6494 IsKernReturnT = (TT->getDecl()->getName() == "kern_return_t");
6495 T = TT->desugar();
6496 }
6497 if (!IsKernReturnT || T.getCanonicalType() != S.getASTContext().IntTy) {
6498 S.Diag(D->getBeginLoc(),
6499 diag::warn_mig_server_routine_does_not_return_kern_return_t);
6500 return;
6501 }
6502 }
6503
6504 handleSimpleAttribute<MIGServerRoutineAttr>(S, D, AL);
6505}
6506
Reid Kleckner1181c9f2019-03-25 23:20:18 +00006507static void handleMSAllocatorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6508 // Warn if the return type is not a pointer or reference type.
6509 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6510 QualType RetTy = FD->getReturnType();
6511 if (!RetTy->isPointerType() && !RetTy->isReferenceType()) {
6512 S.Diag(AL.getLoc(), diag::warn_declspec_allocator_nonpointer)
6513 << AL.getRange() << RetTy;
6514 return;
6515 }
6516 }
6517
6518 handleSimpleAttribute<MSAllocatorAttr>(S, D, AL);
6519}
6520
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00006521//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006522// Top Level Sema Entry Points
6523//===----------------------------------------------------------------------===//
6524
Richard Smithf8a75c32013-08-29 00:47:48 +00006525/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
6526/// the attribute applies to decls. If the attribute is a type attribute, just
6527/// silently ignore it if a GNU attribute.
6528static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006529 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00006530 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00006531 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00006532 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00006533
Richard Smithf8a75c32013-08-29 00:47:48 +00006534 // Ignore C++11 attributes on declarator chunks: they appertain to the type
6535 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006536 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00006537 return;
6538
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006539 // Unknown attributes are automatically warned on. Target-specific attributes
6540 // which do not apply to the current target architecture are treated as
6541 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006542 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006543 !AL.existsInTarget(S.Context.getTargetInfo())) {
Simon Pilgrimfc0ff612018-12-17 12:17:37 +00006544 S.Diag(AL.getLoc(),
6545 AL.isDeclspecAttribute()
6546 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
6547 : (unsigned)diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006548 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006549 return;
6550 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006551
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006552 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006553 return;
6554
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006555 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006556 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006557 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00006558 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006559 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00006560 break;
6561 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006562 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00006563 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006564 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006565 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006566 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006567 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006568 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006569 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006570 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006571 case ParsedAttr::AT_DLLExport:
6572 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006573 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006574 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006575 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006576 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006577 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006578 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006579 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006580 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006581 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006582 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006583 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006584 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006585 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006586 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006587 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006588 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006589 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006590 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006591 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006592 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006593 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006594 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006595 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006596 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006597 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006598 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006599 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006600 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006601 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006602 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006603 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006604 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006605 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006606 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006607 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006608 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006609 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006610 break;
Yonghong Song4e2ce222019-11-01 22:16:59 -07006611 case ParsedAttr::AT_BPFPreserveAccessIndex:
6612 handleBPFPreserveAccessIndexAttr(S, D, AL);
6613 break;
Dan Gohmanb4323692019-01-24 21:08:30 +00006614 case ParsedAttr::AT_WebAssemblyImportModule:
6615 handleWebAssemblyImportModuleAttr(S, D, AL);
6616 break;
Dan Gohmancae84592019-02-01 22:25:23 +00006617 case ParsedAttr::AT_WebAssemblyImportName:
6618 handleWebAssemblyImportNameAttr(S, D, AL);
6619 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006620 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006621 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006622 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006623 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006624 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006625 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006626 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006627 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006628 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006629 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006630 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006631 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006632 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006633 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006634 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006635 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006636 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006637 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006638 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006639 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006640 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006641 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006642 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006643 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006644 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006645 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006646 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006647 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006648 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006649 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006650 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006651 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006652 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006653 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006654 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006655 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006656 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006657 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006658 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006659 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006660 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006661 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006662 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006663 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006664 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006665 case ParsedAttr::AT_CPUDispatch:
6666 case ParsedAttr::AT_CPUSpecific:
6667 handleCPUSpecificAttr(S, D, AL);
6668 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006669 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006670 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006671 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006672 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006673 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006674 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006675 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006676 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006677 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006678 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006679 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006680 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006681 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006682 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006683 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006684 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006685 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006686 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006687 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006688 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006689 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006690 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006691 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006692 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006693 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006694 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006695 break;
Guillaume Chatelet98f31512019-09-25 11:31:28 +02006696 case ParsedAttr::AT_NoBuiltin:
6697 handleNoBuiltinAttr(S, D, AL);
6698 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006699 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006700 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006701 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006702 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006703 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006704 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006705 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006706 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006707 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006708 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006709 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006710 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006711 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006712 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006713 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006714 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006715 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006716 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006717 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006718 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006719 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006720 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006721 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006722 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006723 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006724 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006725 break;
Johannes Doerfertac991bb2019-01-19 05:36:54 +00006726 case ParsedAttr::AT_Callback:
6727 handleCallbackAttr(S, D, AL);
6728 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006729 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006730 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006731 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006732 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006733 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006734 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006735 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006736 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006737 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006738 break;
Yaxun Liuc3dfe902019-06-26 03:47:37 +00006739 case ParsedAttr::AT_HIPPinnedShadow:
6740 handleSimpleAttributeWithExclusions<HIPPinnedShadowAttr, CUDADeviceAttr,
6741 CUDAConstantAttr>(S, D, AL);
6742 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006743 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006744 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006745 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006746 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006747 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006748 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006749 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006750 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006751 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006752 case ParsedAttr::AT_LifetimeBound:
6753 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6754 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006755 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006756 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006757 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006758 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006759 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006760 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006761 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006762 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006763 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006764 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006765 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006766 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006767 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006768 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006769 break;
Richard Smith78b239e2019-06-20 20:44:45 +00006770 case ParsedAttr::AT_NoUniqueAddress:
6771 handleSimpleAttribute<NoUniqueAddressAttr>(S, D, AL);
6772 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006773 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006774 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6775 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006776 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006777 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006778 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006779 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006780 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006781 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006782 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006783 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006784 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006785 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006786 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006787 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006788 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006789 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006790 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006791 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006792 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006793 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006794 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006795 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006796 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006797 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006798 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006799 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006800 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006801 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006802 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006803 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006804 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006805 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006806 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006807 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006808 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006809 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006810 handleNoCfCheckAttr(S, D, AL);
6811 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006812 case ParsedAttr::AT_NoThrow:
Erich Keaned02f4a12019-05-30 17:31:54 +00006813 if (!AL.isUsedAsTypeAttr())
6814 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006815 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006816 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006817 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006818 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006819 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006820 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006821 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006822 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006823 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006824 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006825 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006826 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006827 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006828 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006829 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006830 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006831 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006832 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006833 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006834 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006835 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006836 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006837 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006838 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006839 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006840 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006841 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006842 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006843 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006844 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006845 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006846 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006847 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006848 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006849 case ParsedAttr::AT_ObjCRuntimeVisible:
6850 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6851 break;
6852 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006853 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006854 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006855 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006856 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6857 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006858 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006859 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006860 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6861 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006862 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006863 case ParsedAttr::AT_CFConsumed:
6864 case ParsedAttr::AT_NSConsumed:
George Karpenkov1657f362018-11-30 02:18:37 +00006865 case ParsedAttr::AT_OSConsumed:
Erich Keane6a24e802019-09-13 17:39:31 +00006866 S.AddXConsumedAttr(D, AL, parsedAttrToRetainOwnershipKind(AL),
6867 /*IsTemplateInstantiation=*/false);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006868 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006869 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006870 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006871 break;
George Karpenkovda2c77f2018-12-06 22:06:59 +00006872 case ParsedAttr::AT_OSConsumesThis:
6873 handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL);
6874 break;
George Karpenkov3a50a9f2019-01-11 18:02:08 +00006875 case ParsedAttr::AT_OSReturnsRetainedOnZero:
6876 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnZeroAttr>(
6877 S, D, AL, isValidOSObjectOutParameter(D),
6878 diag::warn_ns_attribute_wrong_parameter_type,
6879 /*Extra Args=*/AL, /*pointer-to-OSObject-pointer*/ 3, AL.getRange());
6880 break;
6881 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
6882 handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnNonZeroAttr>(
6883 S, D, AL, isValidOSObjectOutParameter(D),
6884 diag::warn_ns_attribute_wrong_parameter_type,
6885 /*Extra Args=*/AL, /*pointer-to-OSObject-poointer*/ 3, AL.getRange());
6886 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006887 case ParsedAttr::AT_NSReturnsAutoreleased:
6888 case ParsedAttr::AT_NSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006889 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006890 case ParsedAttr::AT_CFReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006891 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006892 case ParsedAttr::AT_OSReturnsNotRetained:
6893 case ParsedAttr::AT_OSReturnsRetained:
6894 handleXReturnsXRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006895 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006896 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006897 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006898 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006899 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006900 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006901 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006902 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006903 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006904 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006905 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006906 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006907 break;
Richard Smitha6e8b682019-09-04 20:30:37 +00006908 case ParsedAttr::AT_ConstInit:
6909 handleSimpleAttribute<ConstInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006910 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006911 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006912 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006913 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006914 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006915 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006916 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006917 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006918 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006919 break;
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006920 case ParsedAttr::AT_SpeculativeLoadHardening:
Zola Bridges826ef592019-01-18 17:20:46 +00006921 handleSimpleAttributeWithExclusions<SpeculativeLoadHardeningAttr,
6922 NoSpeculativeLoadHardeningAttr>(S, D,
6923 AL);
6924 break;
6925 case ParsedAttr::AT_NoSpeculativeLoadHardening:
6926 handleSimpleAttributeWithExclusions<NoSpeculativeLoadHardeningAttr,
6927 SpeculativeLoadHardeningAttr>(S, D, AL);
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006928 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006929 case ParsedAttr::AT_CodeSeg:
6930 handleCodeSegAttr(S, D, AL);
6931 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006932 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006933 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006934 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006935 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006936 handleMinVectorWidthAttr(S, D, AL);
6937 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006938 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006939 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006940 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006941 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006942 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006943 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006944 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006945 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006946 break;
Joe Danielsf7393d22019-02-04 23:32:55 +00006947 case ParsedAttr::AT_ObjCNonLazyClass:
6948 handleSimpleAttribute<ObjCNonLazyClassAttr>(S, D, AL);
6949 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006950 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006951 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006952 break;
John McCall2c91c3b2019-05-30 04:09:01 +00006953 case ParsedAttr::AT_ObjCClassStub:
6954 handleSimpleAttribute<ObjCClassStubAttr>(S, D, AL);
6955 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006956 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006957 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006958 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006959 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006960 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006961 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006962 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006963 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006964 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006965 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006966 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006967 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006968 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006969 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6970 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006971 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006972 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00006973 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
6974 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006975 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006976 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006977 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006978 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006979 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006980 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006981 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006982 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006983 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006984 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006985 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006986 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006987 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006988 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006989 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006990 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006991 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006992 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006993 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006994 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006995 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006996 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006997 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006998 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006999 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007000 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007001 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007002 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007003 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007004 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007005 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007006 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007007 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00007008 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007009 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007010 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007011 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007012 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007013 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00007014 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007015 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007016 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007017 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007018 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007019 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007020 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007021 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007022 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007023 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007024 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007025 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007026 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007027 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007028 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007029 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007030 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007031 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007032 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007033 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007034 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007035 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007036 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007037 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00007038 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007039 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007040 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007041 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007042 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007043 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007044 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007045 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00007046 // Interacts with -fstack-protector options.
7047 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
7048 break;
Peter Collingbourne0e497d12019-08-09 22:31:59 +00007049 case ParsedAttr::AT_CFICanonicalJumpTable:
7050 handleSimpleAttribute<CFICanonicalJumpTableAttr>(S, D, AL);
7051 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007052 case ParsedAttr::AT_StdCall:
7053 case ParsedAttr::AT_CDecl:
7054 case ParsedAttr::AT_FastCall:
7055 case ParsedAttr::AT_ThisCall:
7056 case ParsedAttr::AT_Pascal:
7057 case ParsedAttr::AT_RegCall:
7058 case ParsedAttr::AT_SwiftCall:
7059 case ParsedAttr::AT_VectorCall:
7060 case ParsedAttr::AT_MSABI:
7061 case ParsedAttr::AT_SysVABI:
7062 case ParsedAttr::AT_Pcs:
7063 case ParsedAttr::AT_IntelOclBicc:
7064 case ParsedAttr::AT_PreserveMost:
7065 case ParsedAttr::AT_PreserveAll:
Sander de Smalen44a22532018-11-26 16:38:37 +00007066 case ParsedAttr::AT_AArch64VectorPcs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007067 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00007068 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007069 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007070 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00007071 break;
Matthias Gehred293cbd2019-07-25 17:50:51 +00007072 case ParsedAttr::AT_Owner:
7073 case ParsedAttr::AT_Pointer:
7074 handleLifetimeCategoryAttr(S, D, AL);
7075 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007076 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007077 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007078 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007079 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007080 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007081 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007082 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007083 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00007084 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007085 case ParsedAttr::AT_SwiftContext:
Erich Keane6a24e802019-09-13 17:39:31 +00007086 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00007087 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007088 case ParsedAttr::AT_SwiftErrorResult:
Erich Keane6a24e802019-09-13 17:39:31 +00007089 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00007090 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007091 case ParsedAttr::AT_SwiftIndirectResult:
Erich Keane6a24e802019-09-13 17:39:31 +00007092 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00007093 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007094 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007095 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00007096 break;
Louis Dionned2695792018-10-04 15:49:42 +00007097 case ParsedAttr::AT_ExcludeFromExplicitInstantiation:
7098 handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL);
7099 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007100 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007101 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00007102 break;
John McCall8d32c052012-05-22 21:28:12 +00007103
7104 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007105 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007106 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007107 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007108 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007109 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00007110 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007111 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007112 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00007113 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007114 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007115 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00007116 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007117 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007118 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00007119 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007120 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007121 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00007122 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007123 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007124 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007125 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007126 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007127 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007128 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007129 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007130 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00007131 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00007132
Erich Keanee891aa92018-07-13 15:07:47 +00007133 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007134 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00007135 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007136
7137 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00007138 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007139 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007140 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007141 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007142 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00007143 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007144 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007145 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007146 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007147 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007148 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007149 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007150 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007151 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007152 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007153 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007154 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00007155 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007156 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007157 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00007158 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007159 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007160 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00007161 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007162 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007163 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007164 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007165 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007166 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007167 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007168 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007169 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007170 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007171 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007172 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007173 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007174 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007175 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007176 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007177 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007178 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007179 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007180 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007181 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007182 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007183 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007184 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00007185 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00007186
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007187 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007188 case ParsedAttr::AT_Capability:
7189 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007190 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007191 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007192 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007193 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007194 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00007195
Erich Keanee891aa92018-07-13 15:07:47 +00007196 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007197 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007198 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007199 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007200 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007201 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007202 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007203 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007204 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007205 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007206 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007207 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00007208
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00007209 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007210 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007211 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00007212 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007213 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007214 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007215 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007216 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007217 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00007218 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007219 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007220 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007221 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007222 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007223 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00007224 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007225 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007226 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00007227 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007228 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007229 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007230 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007231 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007232 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00007233 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00007234
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007235 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007236 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007237 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007238 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007239 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007240 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00007241 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007242 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00007243 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00007244 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007245 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007246 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00007247 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007248 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00007249 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007250 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00007251 break;
Erich Keanee891aa92018-07-13 15:07:47 +00007252 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007253 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00007254 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00007255
7256 // Move semantics attribute.
7257 case ParsedAttr::AT_Reinitializes:
7258 handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
7259 break;
Erik Pilkington5a559e62018-08-21 17:24:06 +00007260
7261 case ParsedAttr::AT_AlwaysDestroy:
7262 case ParsedAttr::AT_NoDestroy:
7263 handleDestroyAttr(S, D, AL);
7264 break;
JF Bastien14daa202018-12-18 05:12:21 +00007265
7266 case ParsedAttr::AT_Uninitialized:
7267 handleUninitializedAttr(S, D, AL);
7268 break;
Erik Pilkington1e368822019-01-04 18:33:06 +00007269
7270 case ParsedAttr::AT_ObjCExternallyRetained:
7271 handleObjCExternallyRetainedAttr(S, D, AL);
7272 break;
Erik Pilkingtone3cd7352019-02-11 23:21:39 +00007273
Artem Dergachevc333d772019-02-21 00:01:02 +00007274 case ParsedAttr::AT_MIGServerRoutine:
7275 handleMIGServerRoutineAttr(S, D, AL);
7276 break;
Reid Kleckner1181c9f2019-03-25 23:20:18 +00007277
7278 case ParsedAttr::AT_MSAllocator:
7279 handleMSAllocatorAttr(S, D, AL);
7280 break;
Simon Tatham7c11da02019-09-02 15:35:09 +01007281
7282 case ParsedAttr::AT_ArmMveAlias:
7283 handleArmMveAliasAttr(S, D, AL);
7284 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007285 }
7286}
7287
7288/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
7289/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00007290void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00007291 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00007292 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00007293 if (AttrList.empty())
7294 return;
7295
Erich Keanee891aa92018-07-13 15:07:47 +00007296 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00007297 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00007298
Joey Gouly2cd9db12013-12-13 16:15:28 +00007299 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00007300 // GCC accepts
7301 // static int a9 __attribute__((weakref));
7302 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00007303 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00007304 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
7305 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00007306 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00007307 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007308 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007309
Aaron Ballmanbe243a72014-12-04 22:45:31 +00007310 // FIXME: We should be able to handle this in TableGen as well. It would be
7311 // good to have a way to specify "these attributes must appear as a group",
7312 // for these. Additionally, it would be good to have a way to specify "these
7313 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00007314 if (!D->hasAttr<OpenCLKernelAttr>()) {
7315 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007316 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00007317 // FIXME: This emits a different error message than
7318 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00007319 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007320 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007321 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007322 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007323 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007324 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00007325 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00007326 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007327 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00007328 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
7329 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00007330 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
7331 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
7332 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7333 << A << ExpectedKernelFunction;
7334 D->setInvalidDecl();
7335 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
7336 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7337 << A << ExpectedKernelFunction;
7338 D->setInvalidDecl();
7339 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
7340 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7341 << A << ExpectedKernelFunction;
7342 D->setInvalidDecl();
7343 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
7344 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
7345 << A << ExpectedKernelFunction;
7346 D->setInvalidDecl();
7347 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00007348 }
7349 }
Erik Pilkington81d3f452019-02-13 20:32:37 +00007350
7351 // Do this check after processing D's attributes because the attribute
7352 // objc_method_family can change whether the given method is in the init
7353 // family, and it can be applied after objc_designated_initializer. This is a
7354 // bit of a hack, but we need it to be compatible with versions of clang that
7355 // processed the attribute list in the wrong order.
7356 if (D->hasAttr<ObjCDesignatedInitializerAttr>() &&
7357 cast<ObjCMethodDecl>(D)->getMethodFamily() != OMF_init) {
7358 Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
7359 D->dropAttr<ObjCDesignatedInitializerAttr>();
7360 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00007361}
7362
Yonghong Song4e2ce222019-11-01 22:16:59 -07007363// Helper for delayed processing TransparentUnion or BPFPreserveAccessIndexAttr
7364// attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007365void Sema::ProcessDeclAttributeDelayed(Decl *D,
7366 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007367 for (const ParsedAttr &AL : AttrList)
7368 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00007369 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00007370 break;
7371 }
Yonghong Song4e2ce222019-11-01 22:16:59 -07007372
7373 // For BPFPreserveAccessIndexAttr, we want to populate the attributes
7374 // to fields and inner records as well.
7375 if (D && D->hasAttr<BPFPreserveAccessIndexAttr>())
7376 handleBPFPreserveAIRecord(*this, cast<RecordDecl>(D));
Erich Keane2fe684b2017-02-28 20:44:39 +00007377}
7378
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007379// Annotation attributes are the only attributes allowed after an access
7380// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00007381bool Sema::ProcessAccessDeclAttributeList(
7382 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00007383 for (const ParsedAttr &AL : AttrList) {
7384 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00007385 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007386 } else {
Erich Keanec480f302018-07-12 21:09:05 +00007387 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007388 return true;
7389 }
7390 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00007391 return false;
7392}
7393
John McCall42856de2011-10-01 05:17:03 +00007394/// checkUnusedDeclAttributes - Check a list of attributes to see if it
7395/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00007396static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00007397 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00007398 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00007399 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
7400 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00007401 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00007402 continue;
John McCall42856de2011-10-01 05:17:03 +00007403
Erich Keanee891aa92018-07-13 15:07:47 +00007404 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00007405 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00007406 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007407 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00007408 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
7409 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00007410 }
7411 }
7412}
7413
7414/// checkUnusedDeclAttributes - Given a declarator which is not being
7415/// used to build a declaration, complain about any decl attributes
7416/// which might be lying around on it.
7417void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00007418 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00007419 ::checkUnusedDeclAttributes(*this, D.getAttributes());
7420 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
7421 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
7422}
7423
Ryan Flynn7d470f32009-07-30 03:15:39 +00007424/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00007425/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00007426NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
7427 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00007428 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00007429 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007430 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00007431 FunctionDecl *NewFD;
7432 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00007433 // FIXME: Mangling?
7434 // FIXME: Is the qualifier info correct?
7435 // FIXME: Is the DeclContext correct?
Gauthier Harnisch796ed032019-06-14 08:56:20 +00007436 NewFD = FunctionDecl::Create(
7437 FD->getASTContext(), FD->getDeclContext(), Loc, Loc,
7438 DeclarationName(II), FD->getType(), FD->getTypeSourceInfo(), SC_None,
7439 false /*isInlineSpecified*/, FD->hasPrototype(), CSK_unspecified);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007440 NewD = NewFD;
7441
7442 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00007443 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00007444
7445 // Fake up parameter variables; they are declared as if this were
7446 // a typedef.
7447 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007448 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00007449 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00007450 for (const auto &AI : FT->param_types()) {
7451 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007452 Param->setScopeInfo(0, Params.size());
7453 Params.push_back(Param);
7454 }
David Blaikie9c70e042011-09-21 18:16:56 +00007455 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00007456 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007457 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00007458 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00007459 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00007460 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00007461 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007462 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00007463 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00007464 }
7465 return NewD;
7466}
7467
James Dennett634962f2012-06-14 21:40:34 +00007468/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00007469/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00007470void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00007471 if (W.getUsed()) return; // only do this once
7472 W.setUsed(true);
7473 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
7474 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00007475 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Erich Keane6a24e802019-09-13 17:39:31 +00007476 NewD->addAttr(
7477 AliasAttr::CreateImplicit(Context, NDId->getName(), W.getLocation()));
7478 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation(),
7479 AttributeCommonInfo::AS_Pragma));
Chris Lattnere6eab982009-09-08 18:10:11 +00007480 WeakTopLevelDecl.push_back(NewD);
7481 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
7482 // to insert Decl at TU scope, sorry.
7483 DeclContext *SavedContext = CurContext;
7484 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00007485 NewD->setDeclContext(CurContext);
7486 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00007487 PushOnScopeChains(NewD, S);
7488 CurContext = SavedContext;
7489 } else { // just add weak to existing
Erich Keane6a24e802019-09-13 17:39:31 +00007490 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation(),
7491 AttributeCommonInfo::AS_Pragma));
Ryan Flynn7d470f32009-07-30 03:15:39 +00007492 }
7493}
7494
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007495void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
7496 // It's valid to "forward-declare" #pragma weak, in which case we
7497 // have to do this.
7498 LoadExternalWeakUndeclaredIdentifiers();
7499 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00007500 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007501 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007502 if (VD->isExternC())
7503 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007504 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007505 if (FD->isExternC())
7506 ND = FD;
7507 if (ND) {
7508 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00007509 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007510 if (I != WeakUndeclaredIdentifiers.end()) {
7511 WeakInfo W = I->second;
7512 DeclApplyPragmaWeak(S, ND, W);
7513 WeakUndeclaredIdentifiers[Id] = W;
7514 }
7515 }
7516 }
7517 }
7518}
7519
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007520/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
7521/// it, apply them to D. This is a bit tricky because PD can have attributes
7522/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00007523void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007524 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00007525 if (!PD.getDeclSpec().getAttributes().empty())
7526 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00007527
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007528 // Walk the declarator structure, applying decl attributes that were in a type
7529 // position to the decl itself. This handles cases like:
7530 // int *__attr__(x)** D;
7531 // when X is a decl attribute.
7532 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00007533 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
7534 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00007535
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007536 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00007537 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00007538
7539 // Apply additional attributes specified by '#pragma clang attribute'.
7540 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007541}
John McCall28a6aea2009-11-04 02:18:39 +00007542
John McCall31168b02011-06-15 23:02:42 +00007543/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00007544/// If so, it'll still be annotated with an attribute that makes it
7545/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007546static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00007547 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00007548 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00007549 // Private ivars are always okay. Unfortunately, people don't
7550 // always properly make their ivars private, even in system headers.
7551 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007552 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
7553 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00007554 return false;
7555
John McCallc6af8c62015-10-28 05:03:19 +00007556 // Silently accept unsupported uses of __weak in both user and system
7557 // declarations when it's been disabled, for ease of integration with
7558 // -fno-objc-arc files. We do have to take some care against attempts
7559 // to define such things; for now, we've only done that for ivars
7560 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007561 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00007562 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
7563 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
7564 reason = UnavailableAttr::IR_ForbiddenWeak;
7565 return true;
7566 }
John McCallb61e14e2015-10-27 04:54:50 +00007567 }
7568
John McCallc6af8c62015-10-28 05:03:19 +00007569 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007570 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00007571 // Currently, all the failures dealt with this way are due to ARC
7572 // restrictions.
7573 reason = UnavailableAttr::IR_ARCForbiddenType;
7574 return true;
John McCallb61e14e2015-10-27 04:54:50 +00007575 }
7576
7577 return false;
John McCall31168b02011-06-15 23:02:42 +00007578}
7579
7580/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007581static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
7582 Decl *D) {
7583 auto Reason = UnavailableAttr::IR_None;
7584 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
7585 assert(Reason && "didn't set reason?");
7586 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00007587 return;
7588 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00007589 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007590 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00007591 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007592 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007593 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007594 DD.getForbiddenTypeDiagnostic() ==
7595 diag::err_arc_array_param_no_ownership) {
7596 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007597 return;
7598 }
7599 }
John McCall31168b02011-06-15 23:02:42 +00007600
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007601 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
7602 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
7603 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00007604}
7605
Manman Ren45b1ab12016-05-06 19:57:16 +00007606static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
7607 const Decl *D) {
7608 // Check each AvailabilityAttr to find the one for this platform.
7609 for (const auto *A : D->attrs()) {
7610 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
7611 // FIXME: this is copied from CheckAvailability. We should try to
7612 // de-duplicate.
7613
7614 // Check if this is an App Extension "platform", and if so chop off
7615 // the suffix for matching with the actual platform.
7616 StringRef ActualPlatform = Avail->getPlatform()->getName();
7617 StringRef RealizedPlatform = ActualPlatform;
7618 if (Context.getLangOpts().AppExt) {
7619 size_t suffix = RealizedPlatform.rfind("_app_extension");
7620 if (suffix != StringRef::npos)
7621 RealizedPlatform = RealizedPlatform.slice(0, suffix);
7622 }
7623
7624 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
7625
7626 // Match the platform name.
7627 if (RealizedPlatform == TargetPlatform)
7628 return Avail;
7629 }
7630 }
7631 return nullptr;
7632}
7633
Erik Pilkington9f866a72017-07-18 20:32:07 +00007634/// The diagnostic we should emit for \c D, and the declaration that
7635/// originated it, or \c AR_Available.
7636///
7637/// \param D The declaration to check.
7638/// \param Message If non-null, this will be populated with the message from
7639/// the availability attribute that is selected.
Erik Pilkington42578572018-09-10 22:20:09 +00007640/// \param ClassReceiver If we're checking the the method of a class message
7641/// send, the class. Otherwise nullptr.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007642static std::pair<AvailabilityResult, const NamedDecl *>
Erik Pilkington42578572018-09-10 22:20:09 +00007643ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D,
7644 std::string *Message,
7645 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007646 AvailabilityResult Result = D->getAvailability(Message);
7647
7648 // For typedefs, if the typedef declaration appears available look
7649 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007650 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007651 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007652 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007653 D = TT->getDecl();
7654 Result = D->getAvailability(Message);
7655 continue;
7656 }
7657 }
7658 break;
7659 }
7660
7661 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007662 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007663 if (IDecl->getDefinition()) {
7664 D = IDecl->getDefinition();
7665 Result = D->getAvailability(Message);
7666 }
7667 }
7668
7669 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7670 if (Result == AR_Available) {
7671 const DeclContext *DC = ECD->getDeclContext();
7672 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
7673 Result = TheEnumDecl->getAvailability(Message);
7674 D = TheEnumDecl;
7675 }
7676 }
7677
Erik Pilkington42578572018-09-10 22:20:09 +00007678 // For +new, infer availability from -init.
7679 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7680 if (S.NSAPIObj && ClassReceiver) {
7681 ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod(
7682 S.NSAPIObj->getInitSelector());
7683 if (Init && Result == AR_Available && MD->isClassMethod() &&
7684 MD->getSelector() == S.NSAPIObj->getNewSelector() &&
7685 MD->definedInNSObject(S.getASTContext())) {
7686 Result = Init->getAvailability(Message);
7687 D = Init;
7688 }
7689 }
7690 }
7691
Erik Pilkington9f866a72017-07-18 20:32:07 +00007692 return {Result, D};
7693}
7694
7695
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007696/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007697/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
7698/// in a deprecated context, but not the other way around.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007699static bool
7700ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
7701 VersionTuple DeclVersion, Decl *Ctx,
7702 const NamedDecl *OffendingDecl) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007703 assert(K != AR_Available && "Expected an unavailable declaration here!");
7704
7705 // Checks if we should emit the availability diagnostic in the context of C.
7706 auto CheckContext = [&](const Decl *C) {
7707 if (K == AR_NotYetIntroduced) {
7708 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
7709 if (AA->getIntroduced() >= DeclVersion)
7710 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007711 } else if (K == AR_Deprecated) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007712 if (C->isDeprecated())
7713 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007714 } else if (K == AR_Unavailable) {
7715 // It is perfectly fine to refer to an 'unavailable' Objective-C method
Alex Lorenz194d00e2019-01-17 18:12:45 +00007716 // when it is referenced from within the @implementation itself. In this
7717 // context, we interpret unavailable as a form of access control.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007718 if (const auto *MD = dyn_cast<ObjCMethodDecl>(OffendingDecl)) {
7719 if (const auto *Impl = dyn_cast<ObjCImplDecl>(C)) {
Alex Lorenz194d00e2019-01-17 18:12:45 +00007720 if (MD->getClassInterface() == Impl->getClassInterface())
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007721 return true;
7722 }
7723 }
7724 }
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007725
7726 if (C->isUnavailable())
7727 return true;
7728 return false;
7729 };
7730
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007731 do {
7732 if (CheckContext(Ctx))
7733 return false;
7734
7735 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00007736 // Unless it is "+load" method.
7737 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
7738 if (MethodD->isClassMethod() &&
7739 MethodD->getSelector().getAsString() == "load")
7740 return true;
7741
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007742 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007743 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
7744 if (CheckContext(Interface))
7745 return false;
7746 }
7747 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007748 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007749 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
7750 if (CheckContext(Interface))
7751 return false;
7752 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
7753
7754 return true;
7755}
7756
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007757static bool
7758shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
7759 const VersionTuple &DeploymentVersion,
7760 const VersionTuple &DeclVersion) {
7761 const auto &Triple = Context.getTargetInfo().getTriple();
7762 VersionTuple ForceAvailabilityFromVersion;
7763 switch (Triple.getOS()) {
7764 case llvm::Triple::IOS:
7765 case llvm::Triple::TvOS:
7766 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7767 break;
7768 case llvm::Triple::WatchOS:
7769 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7770 break;
7771 case llvm::Triple::Darwin:
7772 case llvm::Triple::MacOSX:
7773 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7774 break;
7775 default:
7776 // New targets should always warn about availability.
7777 return Triple.getVendor() == llvm::Triple::Apple;
7778 }
7779 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7780 DeclVersion >= ForceAvailabilityFromVersion;
7781}
7782
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007783static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7784 for (Decl *Ctx = OrigCtx; Ctx;
7785 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7786 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7787 return cast<NamedDecl>(Ctx);
7788 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7789 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7790 return Imp->getClassInterface();
7791 return CD;
7792 }
7793 }
7794
7795 return dyn_cast<NamedDecl>(OrigCtx);
7796}
7797
Alex Lorenz727c21e2017-07-26 13:58:02 +00007798namespace {
7799
7800struct AttributeInsertion {
7801 StringRef Prefix;
7802 SourceLocation Loc;
7803 StringRef Suffix;
7804
7805 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007806 return {" ", D->getEndLoc(), ""};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007807 }
7808 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7809 return {" ", Loc, ""};
7810 }
7811 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007812 return {"", D->getBeginLoc(), "\n"};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007813 }
7814};
7815
7816} // end anonymous namespace
7817
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007818/// Tries to parse a string as ObjC method name.
7819///
7820/// \param Name The string to parse. Expected to originate from availability
7821/// attribute argument.
7822/// \param SlotNames The vector that will be populated with slot names. In case
7823/// of unsuccessful parsing can contain invalid data.
7824/// \returns A number of method parameters if parsing was successful, None
7825/// otherwise.
7826static Optional<unsigned>
7827tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
7828 const LangOptions &LangOpts) {
7829 // Accept replacements starting with - or + as valid ObjC method names.
7830 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
7831 Name = Name.drop_front(1);
7832 if (Name.empty())
7833 return None;
7834 Name.split(SlotNames, ':');
7835 unsigned NumParams;
7836 if (Name.back() == ':') {
7837 // Remove an empty string at the end that doesn't represent any slot.
7838 SlotNames.pop_back();
7839 NumParams = SlotNames.size();
7840 } else {
7841 if (SlotNames.size() != 1)
7842 // Not a valid method name, just a colon-separated string.
7843 return None;
7844 NumParams = 0;
7845 }
7846 // Verify all slot names are valid.
7847 bool AllowDollar = LangOpts.DollarIdents;
7848 for (StringRef S : SlotNames) {
7849 if (S.empty())
7850 continue;
7851 if (!isValidIdentifier(S, AllowDollar))
7852 return None;
7853 }
7854 return NumParams;
7855}
7856
Alex Lorenz727c21e2017-07-26 13:58:02 +00007857/// Returns a source location in which it's appropriate to insert a new
7858/// attribute for the given declaration \D.
7859static Optional<AttributeInsertion>
7860createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7861 const LangOptions &LangOpts) {
7862 if (isa<ObjCPropertyDecl>(D))
7863 return AttributeInsertion::createInsertionAfter(D);
7864 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7865 if (MD->hasBody())
7866 return None;
7867 return AttributeInsertion::createInsertionAfter(D);
7868 }
7869 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7870 SourceLocation Loc =
7871 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7872 if (Loc.isInvalid())
7873 return None;
7874 // Insert after the 'struct'/whatever keyword.
7875 return AttributeInsertion::createInsertionAfter(Loc);
7876 }
7877 return AttributeInsertion::createInsertionBefore(D);
7878}
7879
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007880/// Actually emit an availability diagnostic for a reference to an unavailable
7881/// decl.
7882///
7883/// \param Ctx The context that the reference occurred in
7884/// \param ReferringDecl The exact declaration that was referenced.
7885/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007886/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007887/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7888/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7889/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007890static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007891 Decl *Ctx, const NamedDecl *ReferringDecl,
7892 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007893 StringRef Message,
7894 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007895 const ObjCInterfaceDecl *UnknownObjCClass,
7896 const ObjCPropertyDecl *ObjCProperty,
7897 bool ObjCPropertyAccess) {
7898 // Diagnostics for deprecated or unavailable.
7899 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007900 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007901 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007902
7903 // Matches 'diag::note_property_attribute' options.
7904 unsigned property_note_select;
7905
7906 // Matches diag::note_availability_specified_here.
7907 unsigned available_here_select_kind;
7908
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007909 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007910 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007911 DeclVersion = AA->getIntroduced();
7912
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007913 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx,
7914 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007915 return;
7916
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007917 SourceLocation Loc = Locs.front();
7918
Erik Pilkington8b352c42017-08-14 19:49:12 +00007919 // The declaration can have multiple availability attributes, we are looking
7920 // at one of them.
7921 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7922 if (A && A->isInherited()) {
7923 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7924 Redecl = Redecl->getPreviousDecl()) {
7925 const AvailabilityAttr *AForRedecl =
7926 getAttrForPlatform(S.Context, Redecl);
7927 if (AForRedecl && !AForRedecl->isInherited()) {
7928 // If D is a declaration with inherited attributes, the note should
7929 // point to the declaration with actual attributes.
7930 NoteLocation = Redecl->getLocation();
7931 break;
7932 }
7933 }
7934 }
7935
Aaron Ballmanfb237522014-10-15 15:37:51 +00007936 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007937 case AR_NotYetIntroduced: {
7938 // We would like to emit the diagnostic even if -Wunguarded-availability is
7939 // not specified for deployment targets >= to iOS 11 or equivalent or
7940 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7941 // later.
7942 const AvailabilityAttr *AA =
7943 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7944 VersionTuple Introduced = AA->getIntroduced();
7945
7946 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7947 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7948 Introduced);
7949 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7950 : diag::warn_unguarded_availability;
7951
Erik Pilkington0535b0f2019-01-14 19:17:31 +00007952 std::string PlatformName = AvailabilityAttr::getPrettyPlatformName(
7953 S.getASTContext().getTargetInfo().getPlatformName());
Erik Pilkington8b352c42017-08-14 19:49:12 +00007954
Erik Pilkington0535b0f2019-01-14 19:17:31 +00007955 S.Diag(Loc, Warning) << OffendingDecl << PlatformName
7956 << Introduced.getAsString();
7957
7958 S.Diag(OffendingDecl->getLocation(),
7959 diag::note_partial_availability_specified_here)
7960 << OffendingDecl << PlatformName << Introduced.getAsString()
7961 << S.Context.getTargetInfo().getPlatformMinVersion().getAsString();
Erik Pilkington8b352c42017-08-14 19:49:12 +00007962
7963 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007964 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007965 if (TD->getDeclName().isEmpty()) {
7966 S.Diag(TD->getLocation(),
7967 diag::note_decl_unguarded_availability_silence)
7968 << /*Anonymous*/ 1 << TD->getKindName();
7969 return;
7970 }
7971 auto FixitNoteDiag =
7972 S.Diag(Enclosing->getLocation(),
7973 diag::note_decl_unguarded_availability_silence)
7974 << /*Named*/ 0 << Enclosing;
7975 // Don't offer a fixit for declarations with availability attributes.
7976 if (Enclosing->hasAttr<AvailabilityAttr>())
7977 return;
7978 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7979 return;
7980 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7981 Enclosing, S.getSourceManager(), S.getLangOpts());
7982 if (!Insertion)
7983 return;
7984 std::string PlatformName =
7985 AvailabilityAttr::getPlatformNameSourceSpelling(
7986 S.getASTContext().getTargetInfo().getPlatformName())
7987 .lower();
7988 std::string Introduced =
7989 OffendingDecl->getVersionIntroduced().getAsString();
7990 FixitNoteDiag << FixItHint::CreateInsertion(
7991 Insertion->Loc,
7992 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7993 "(" + Introduced + "))" + Insertion->Suffix)
7994 .str());
7995 }
7996 return;
7997 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007998 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007999 diag = !ObjCPropertyAccess ? diag::warn_deprecated
8000 : diag::warn_property_method_deprecated;
8001 diag_message = diag::warn_deprecated_message;
8002 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
8003 property_note_select = /* deprecated */ 0;
8004 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008005 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
8006 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00008007 break;
8008
Erik Pilkington796a3e22016-08-05 22:59:03 +00008009 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00008010 diag = !ObjCPropertyAccess ? diag::err_unavailable
8011 : diag::err_property_method_unavailable;
8012 diag_message = diag::err_unavailable_message;
8013 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
8014 property_note_select = /* unavailable */ 1;
8015 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00008016
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008017 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
8018 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00008019 // Most of these failures are due to extra restrictions in ARC;
8020 // reflect that in the primary diagnostic when applicable.
8021 auto flagARCError = [&] {
8022 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008023 S.getSourceManager().isInSystemHeader(
8024 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00008025 diag = diag::err_unavailable_in_arc;
8026 };
8027
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008028 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00008029 case UnavailableAttr::IR_None: break;
8030
8031 case UnavailableAttr::IR_ARCForbiddenType:
8032 flagARCError();
8033 diag_available_here = diag::note_arc_forbidden_type;
8034 break;
8035
8036 case UnavailableAttr::IR_ForbiddenWeak:
8037 if (S.getLangOpts().ObjCWeakRuntime)
8038 diag_available_here = diag::note_arc_weak_disabled;
8039 else
8040 diag_available_here = diag::note_arc_weak_no_runtime;
8041 break;
8042
8043 case UnavailableAttr::IR_ARCForbiddenConversion:
8044 flagARCError();
8045 diag_available_here = diag::note_performs_forbidden_arc_conversion;
8046 break;
8047
8048 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
8049 flagARCError();
8050 diag_available_here = diag::note_arc_init_returns_unrelated;
8051 break;
8052
8053 case UnavailableAttr::IR_ARCFieldWithOwnership:
8054 flagARCError();
8055 diag_available_here = diag::note_arc_field_with_ownership;
8056 break;
8057 }
8058 }
John McCallb61e14e2015-10-27 04:54:50 +00008059 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00008060 break;
8061
Erik Pilkington796a3e22016-08-05 22:59:03 +00008062 case AR_Available:
8063 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00008064 }
8065
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008066 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00008067 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008068 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008069 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
8070 Replacement = AL->getReplacement();
8071 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
8072 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00008073
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008074 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00008075 if (!Replacement.empty())
8076 UseRange =
8077 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008078 if (UseRange.isValid()) {
8079 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
8080 Selector Sel = MethodDecl->getSelector();
8081 SmallVector<StringRef, 12> SelectorSlotNames;
8082 Optional<unsigned> NumParams = tryParseObjCMethodName(
8083 Replacement, SelectorSlotNames, S.getLangOpts());
8084 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
8085 assert(SelectorSlotNames.size() == Locs.size());
8086 for (unsigned I = 0; I < Locs.size(); ++I) {
8087 if (!Sel.getNameForSlot(I).empty()) {
8088 CharSourceRange NameRange = CharSourceRange::getCharRange(
8089 Locs[I], S.getLocForEndOfToken(Locs[I]));
8090 FixIts.push_back(FixItHint::CreateReplacement(
8091 NameRange, SelectorSlotNames[I]));
8092 } else
8093 FixIts.push_back(
8094 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
8095 }
8096 } else
8097 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
8098 } else
8099 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
8100 }
Manman Renc7890fe2016-03-16 18:50:49 +00008101 }
8102
Aaron Ballmanfb237522014-10-15 15:37:51 +00008103 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008104 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008105 if (ObjCProperty)
8106 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
8107 << ObjCProperty->getDeclName() << property_note_select;
8108 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008109 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008110 if (ObjCProperty)
8111 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
8112 << ObjCProperty->getDeclName() << property_note_select;
8113 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008114 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008115 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
8116 }
8117
Erik Pilkington8b352c42017-08-14 19:49:12 +00008118 S.Diag(NoteLocation, diag_available_here)
8119 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00008120}
8121
8122static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
8123 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00008124 assert(DD.Kind == DelayedDiagnostic::Availability &&
8125 "Expected an availability diagnostic here");
8126
Aaron Ballmanfb237522014-10-15 15:37:51 +00008127 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00008128 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008129 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008130 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
8131 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
8132 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00008133}
8134
John McCall2ec85372012-05-07 06:16:41 +00008135void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
8136 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00008137 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00008138 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00008139
John McCall2ec85372012-05-07 06:16:41 +00008140 // When delaying diagnostics to run in the context of a parsed
8141 // declaration, we only want to actually emit anything if parsing
8142 // succeeds.
8143 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00008144
John McCall2ec85372012-05-07 06:16:41 +00008145 // We emit all the active diagnostics in this pool or any of its
8146 // parents. In general, we'll get one pool for the decl spec
8147 // and a child pool for each declarator; in a decl group like:
8148 // deprecated_typedef foo, *bar, baz();
8149 // only the declarator pops will be passed decls. This is correct;
8150 // we really do need to consider delayed diagnostics from the decl spec
8151 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00008152 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00008153 do {
Richard Smith5c9b3b72018-09-25 22:12:44 +00008154 bool AnyAccessFailures = false;
John McCall6347b682012-05-07 06:16:58 +00008155 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00008156 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
8157 // This const_cast is a bit lame. Really, Triggered should be mutable.
8158 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00008159 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00008160 continue;
8161
John McCallc1465822011-02-14 07:13:47 +00008162 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00008163 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00008164 // Don't bother giving deprecation/unavailable diagnostics if
8165 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00008166 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00008167 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00008168 break;
8169
8170 case DelayedDiagnostic::Access:
Richard Smith5c9b3b72018-09-25 22:12:44 +00008171 // Only produce one access control diagnostic for a structured binding
8172 // declaration: we don't need to tell the user that all the fields are
8173 // inaccessible one at a time.
8174 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
8175 continue;
John McCall2ec85372012-05-07 06:16:41 +00008176 HandleDelayedAccessCheck(diag, decl);
Richard Smith5c9b3b72018-09-25 22:12:44 +00008177 if (diag.Triggered)
8178 AnyAccessFailures = true;
John McCall86121512010-01-27 03:50:35 +00008179 break;
John McCall31168b02011-06-15 23:02:42 +00008180
8181 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00008182 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00008183 break;
John McCall86121512010-01-27 03:50:35 +00008184 }
8185 }
John McCall2ec85372012-05-07 06:16:41 +00008186 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00008187}
8188
John McCall6347b682012-05-07 06:16:58 +00008189/// Given a set of delayed diagnostics, re-emit them as if they had
8190/// been delayed in the current context instead of in the given pool.
8191/// Essentially, this just moves them to the current pool.
8192void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
8193 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
8194 assert(curPool && "re-emitting in undelayed context not supported");
8195 curPool->steal(pool);
8196}
8197
Erik Pilkington9f866a72017-07-18 20:32:07 +00008198static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
8199 const NamedDecl *ReferringDecl,
8200 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008201 StringRef Message,
8202 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008203 const ObjCInterfaceDecl *UnknownObjCClass,
8204 const ObjCPropertyDecl *ObjCProperty,
8205 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00008206 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00008207 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
8208 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008209 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008210 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008211 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00008212 return;
8213 }
8214
Erik Pilkington9f866a72017-07-18 20:32:07 +00008215 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
8216 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008217 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008218 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00008219}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00008220
Erik Pilkington5cd57172016-08-16 17:44:11 +00008221namespace {
8222
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008223/// Returns true if the given statement can be a body-like child of \p Parent.
8224bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
8225 switch (Parent->getStmtClass()) {
8226 case Stmt::IfStmtClass:
8227 return cast<IfStmt>(Parent)->getThen() == S ||
8228 cast<IfStmt>(Parent)->getElse() == S;
8229 case Stmt::WhileStmtClass:
8230 return cast<WhileStmt>(Parent)->getBody() == S;
8231 case Stmt::DoStmtClass:
8232 return cast<DoStmt>(Parent)->getBody() == S;
8233 case Stmt::ForStmtClass:
8234 return cast<ForStmt>(Parent)->getBody() == S;
8235 case Stmt::CXXForRangeStmtClass:
8236 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
8237 case Stmt::ObjCForCollectionStmtClass:
8238 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
8239 case Stmt::CaseStmtClass:
8240 case Stmt::DefaultStmtClass:
8241 return cast<SwitchCase>(Parent)->getSubStmt() == S;
8242 default:
8243 return false;
8244 }
8245}
8246
8247class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
8248 const Stmt *Target;
8249
8250public:
8251 bool VisitStmt(Stmt *S) { return S != Target; }
8252
8253 /// Returns true if the given statement is present in the given declaration.
8254 static bool isContained(const Stmt *Target, const Decl *D) {
8255 StmtUSEFinder Visitor;
8256 Visitor.Target = Target;
8257 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
8258 }
8259};
8260
8261/// Traverses the AST and finds the last statement that used a given
8262/// declaration.
8263class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
8264 const Decl *D;
8265
8266public:
8267 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
8268 if (DRE->getDecl() == D)
8269 return false;
8270 return true;
8271 }
8272
8273 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
8274 const CompoundStmt *Scope) {
8275 LastDeclUSEFinder Visitor;
8276 Visitor.D = D;
8277 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
8278 const Stmt *S = *I;
8279 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
8280 return S;
8281 }
8282 return nullptr;
8283 }
8284};
8285
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008286/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00008287///
8288/// This is done with a traversal of the AST of a function that makes reference
8289/// to a partially available declaration. Whenever we encounter an \c if of the
8290/// form: \c if(@available(...)), we use the version from the condition to visit
8291/// the then statement.
8292class DiagnoseUnguardedAvailability
8293 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
8294 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
8295
8296 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008297 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00008298
8299 /// Stack of potentially nested 'if (@available(...))'s.
8300 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008301 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00008302
Erik Pilkington42578572018-09-10 22:20:09 +00008303 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range,
8304 ObjCInterfaceDecl *ClassReceiver = nullptr);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008305
8306public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008307 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
8308 : SemaRef(SemaRef), Ctx(Ctx) {
8309 AvailabilityStack.push_back(
8310 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008311 }
8312
Alex Lorenz6f911122017-05-16 13:58:53 +00008313 bool TraverseDecl(Decl *D) {
8314 // Avoid visiting nested functions to prevent duplicate warnings.
8315 if (!D || isa<FunctionDecl>(D))
8316 return true;
8317 return Base::TraverseDecl(D);
8318 }
8319
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008320 bool TraverseStmt(Stmt *S) {
8321 if (!S)
8322 return true;
8323 StmtStack.push_back(S);
8324 bool Result = Base::TraverseStmt(S);
8325 StmtStack.pop_back();
8326 return Result;
8327 }
8328
Erik Pilkington5cd57172016-08-16 17:44:11 +00008329 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
8330
8331 bool TraverseIfStmt(IfStmt *If);
8332
Alex Lorenz6f911122017-05-16 13:58:53 +00008333 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
8334
Erik Pilkingtonba87c622017-08-18 20:20:56 +00008335 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
8336 // to any useful diagnostics.
8337 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
8338
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008339 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
8340 if (PRE->isClassReceiver())
8341 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
8342 return true;
8343 }
8344
Erik Pilkington5cd57172016-08-16 17:44:11 +00008345 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
Erik Pilkington42578572018-09-10 22:20:09 +00008346 if (ObjCMethodDecl *D = Msg->getMethodDecl()) {
8347 ObjCInterfaceDecl *ID = nullptr;
8348 QualType ReceiverTy = Msg->getClassReceiver();
8349 if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType())
8350 ID = ReceiverTy->getAsObjCInterfaceType()->getInterface();
8351
Erik Pilkington5cd57172016-08-16 17:44:11 +00008352 DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00008353 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID);
8354 }
Erik Pilkington5cd57172016-08-16 17:44:11 +00008355 return true;
8356 }
8357
8358 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
8359 DiagnoseDeclAvailability(DRE->getDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008360 SourceRange(DRE->getBeginLoc(), DRE->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00008361 return true;
8362 }
8363
8364 bool VisitMemberExpr(MemberExpr *ME) {
8365 DiagnoseDeclAvailability(ME->getMemberDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008366 SourceRange(ME->getBeginLoc(), ME->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00008367 return true;
8368 }
8369
Alex Lorenz0a484ba2017-05-24 15:15:29 +00008370 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008371 SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use)
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008372 << (!SemaRef.getLangOpts().ObjC);
Alex Lorenz0a484ba2017-05-24 15:15:29 +00008373 return true;
8374 }
8375
Erik Pilkington5cd57172016-08-16 17:44:11 +00008376 bool VisitTypeLoc(TypeLoc Ty);
8377};
8378
8379void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00008380 NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008381 AvailabilityResult Result;
8382 const NamedDecl *OffendingDecl;
8383 std::tie(Result, OffendingDecl) =
Erik Pilkington42578572018-09-10 22:20:09 +00008384 ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008385 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008386 // All other diagnostic kinds have already been handled in
8387 // DiagnoseAvailabilityOfDecl.
8388 if (Result != AR_NotYetIntroduced)
8389 return;
8390
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008391 const AvailabilityAttr *AA =
8392 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008393 VersionTuple Introduced = AA->getIntroduced();
8394
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008395 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008396 return;
8397
8398 // If the context of this function is less available than D, we should not
8399 // emit a diagnostic.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00008400 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx,
8401 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008402 return;
8403
Alex Lorenzc9a369f2017-06-22 17:02:24 +00008404 // We would like to emit the diagnostic even if -Wunguarded-availability is
8405 // not specified for deployment targets >= to iOS 11 or equivalent or
8406 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
8407 // later.
8408 unsigned DiagKind =
8409 shouldDiagnoseAvailabilityByDefault(
8410 SemaRef.Context,
8411 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
8412 ? diag::warn_unguarded_availability_new
8413 : diag::warn_unguarded_availability;
8414
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008415 std::string PlatformName = AvailabilityAttr::getPrettyPlatformName(
8416 SemaRef.getASTContext().getTargetInfo().getPlatformName());
8417
Alex Lorenzc9a369f2017-06-22 17:02:24 +00008418 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008419 << Range << D << PlatformName << Introduced.getAsString();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008420
Erik Pilkington4042f3c2017-07-05 17:08:56 +00008421 SemaRef.Diag(OffendingDecl->getLocation(),
Erik Pilkington0535b0f2019-01-14 19:17:31 +00008422 diag::note_partial_availability_specified_here)
8423 << OffendingDecl << PlatformName << Introduced.getAsString()
8424 << SemaRef.Context.getTargetInfo()
8425 .getPlatformMinVersion()
8426 .getAsString();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008427
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008428 auto FixitDiag =
8429 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
8430 << Range << D
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008431 << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0
8432 : /*__builtin_available*/ 1);
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008433
8434 // Find the statement which should be enclosed in the if @available check.
8435 if (StmtStack.empty())
8436 return;
8437 const Stmt *StmtOfUse = StmtStack.back();
8438 const CompoundStmt *Scope = nullptr;
8439 for (const Stmt *S : llvm::reverse(StmtStack)) {
8440 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
8441 Scope = CS;
8442 break;
8443 }
8444 if (isBodyLikeChildStmt(StmtOfUse, S)) {
8445 // The declaration won't be seen outside of the statement, so we don't
8446 // have to wrap the uses of any declared variables in if (@available).
8447 // Therefore we can avoid setting Scope here.
8448 break;
8449 }
8450 StmtOfUse = S;
8451 }
8452 const Stmt *LastStmtOfUse = nullptr;
8453 if (isa<DeclStmt>(StmtOfUse) && Scope) {
8454 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
8455 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
8456 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
8457 break;
8458 }
8459 }
8460 }
8461
8462 const SourceManager &SM = SemaRef.getSourceManager();
8463 SourceLocation IfInsertionLoc =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008464 SM.getExpansionLoc(StmtOfUse->getBeginLoc());
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008465 SourceLocation StmtEndLoc =
8466 SM.getExpansionRange(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008467 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc())
Richard Smithb5f81712018-04-30 05:25:48 +00008468 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008469 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
8470 return;
8471
8472 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
8473 const char *ExtraIndentation = " ";
8474 std::string FixItString;
8475 llvm::raw_string_ostream FixItOS(FixItString);
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008476 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available"
8477 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00008478 << "("
8479 << AvailabilityAttr::getPlatformNameSourceSpelling(
8480 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008481 << " " << Introduced.getAsString() << ", *)) {\n"
8482 << Indentation << ExtraIndentation;
8483 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
8484 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
8485 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
8486 /*SkipTrailingWhitespaceAndNewLine=*/false);
8487 if (ElseInsertionLoc.isInvalid())
8488 ElseInsertionLoc =
8489 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
8490 FixItOS.str().clear();
8491 FixItOS << "\n"
8492 << Indentation << "} else {\n"
8493 << Indentation << ExtraIndentation
8494 << "// Fallback on earlier versions\n"
8495 << Indentation << "}";
8496 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008497 }
8498}
8499
8500bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
8501 const Type *TyPtr = Ty.getTypePtr();
8502 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
8503
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008504 if (Range.isInvalid())
8505 return true;
8506
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008507 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008508 TagDecl *TD = TT->getDecl();
8509 DiagnoseDeclAvailability(TD, Range);
8510
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008511 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008512 TypedefNameDecl *D = TD->getDecl();
8513 DiagnoseDeclAvailability(D, Range);
8514
8515 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
8516 if (NamedDecl *D = ObjCO->getInterface())
8517 DiagnoseDeclAvailability(D, Range);
8518 }
8519
8520 return true;
8521}
8522
8523bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
8524 VersionTuple CondVersion;
8525 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
8526 CondVersion = E->getVersion();
8527
8528 // If we're using the '*' case here or if this check is redundant, then we
8529 // use the enclosing version to check both branches.
8530 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00008531 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008532 } else {
8533 // This isn't an availability checking 'if', we can just continue.
8534 return Base::TraverseIfStmt(If);
8535 }
8536
8537 AvailabilityStack.push_back(CondVersion);
8538 bool ShouldContinue = TraverseStmt(If->getThen());
8539 AvailabilityStack.pop_back();
8540
8541 return ShouldContinue && TraverseStmt(If->getElse());
8542}
8543
8544} // end anonymous namespace
8545
8546void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
8547 Stmt *Body = nullptr;
8548
8549 if (auto *FD = D->getAsFunction()) {
8550 // FIXME: We only examine the pattern decl for availability violations now,
8551 // but we should also examine instantiated templates.
8552 if (FD->isTemplateInstantiation())
8553 return;
8554
8555 Body = FD->getBody();
8556 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
8557 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00008558 else if (auto *BD = dyn_cast<BlockDecl>(D))
8559 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008560
8561 assert(Body && "Need a body here!");
8562
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008563 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008564}
Erik Pilkington9f866a72017-07-18 20:32:07 +00008565
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008566void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
8567 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008568 const ObjCInterfaceDecl *UnknownObjCClass,
8569 bool ObjCPropertyAccess,
Erik Pilkington42578572018-09-10 22:20:09 +00008570 bool AvoidPartialAvailabilityChecks,
8571 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008572 std::string Message;
8573 AvailabilityResult Result;
8574 const NamedDecl* OffendingDecl;
8575 // See if this declaration is unavailable, deprecated, or partial.
Erik Pilkington42578572018-09-10 22:20:09 +00008576 std::tie(Result, OffendingDecl) =
8577 ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver);
Erik Pilkington9f866a72017-07-18 20:32:07 +00008578 if (Result == AR_Available)
8579 return;
8580
8581 if (Result == AR_NotYetIntroduced) {
8582 if (AvoidPartialAvailabilityChecks)
8583 return;
8584
8585 // We need to know the @available context in the current function to
8586 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
8587 // when we're done parsing the current function.
8588 if (getCurFunctionOrMethodDecl()) {
8589 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
8590 return;
8591 } else if (getCurBlock() || getCurLambda()) {
8592 getCurFunction()->HasPotentialAvailabilityViolations = true;
8593 return;
8594 }
8595 }
8596
8597 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008598 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008599 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
8600 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
8601 if (PDeclResult == Result)
8602 ObjCPDecl = PD;
8603 }
8604 }
8605
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008606 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008607 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
8608}