blob: b77b77d9ba19cd8e28ca2b00557fea356180771d [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
David Majnemer929025d2016-01-26 19:30:26 +000014#include "clang/AST/ASTConsumer.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000016#include "clang/AST/ASTMutationListener.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000022#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000023#include "clang/AST/Mangle.h"
Erik Pilkington5cd57172016-08-16 17:44:11 +000024#include "clang/AST/RecursiveASTVisitor.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000025#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000026#include "clang/Basic/SourceManager.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
Alp Toker601b22c2014-01-21 23:35:24 +000096static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000097 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000098 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Aaron Ballmana70c6b52018-02-15 16:20:20 +000099 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000100 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000101
Alp Toker03376dc2014-07-07 09:02:20 +0000102 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000103}
104
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000105static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
106 if (const auto *FD = dyn_cast<FunctionDecl>(D))
107 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000108 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000109 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000110 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000111 return BD->getParamDecl(Idx)->getSourceRange();
112 return SourceRange();
113}
114
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000116 if (const FunctionType *FnTy = D->getFunctionType())
George Burgess IV00f70bd2018-03-01 05:43:23 +0000117 return FnTy->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000118 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000119}
120
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000121static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
122 if (const auto *FD = dyn_cast<FunctionDecl>(D))
123 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000124 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000125 return MD->getReturnTypeSourceRange();
126 return SourceRange();
127}
128
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000129static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000130 if (const FunctionType *FnTy = D->getFunctionType())
131 return cast<FunctionProtoType>(FnTy)->isVariadic();
132 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballmane13d0092014-08-01 17:02:34 +0000133 return BD->isVariadic();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000134 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000135}
136
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137static bool isInstanceMethod(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000138 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000139 return MethodDecl->isInstance();
140 return false;
141}
142
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000143static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000144 const auto *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000145 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000146 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000147
John McCall96fa4842010-05-17 21:00:27 +0000148 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
149 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000150 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000151
John McCall96fa4842010-05-17 21:00:27 +0000152 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000153
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000154 // FIXME: Should we walk the chain of classes?
155 return ClsName == &Ctx.Idents.get("NSString") ||
156 ClsName == &Ctx.Idents.get("NSMutableString");
157}
158
Daniel Dunbar980c6692008-09-26 03:32:58 +0000159static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000160 const auto *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000161 if (!PT)
162 return false;
163
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000164 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000165 if (!RT)
166 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167
Daniel Dunbar980c6692008-09-26 03:32:58 +0000168 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000169 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000170 return false;
171
172 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
173}
174
Erich Keanee891aa92018-07-13 15:07:47 +0000175static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
Richard Smithb87c4652013-10-31 21:23:20 +0000176 // FIXME: Include the type in the argument list.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000177 return AL.getNumArgs() + AL.hasParsedType();
Richard Smithb87c4652013-10-31 21:23:20 +0000178}
179
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000180template <typename Compare>
Erich Keanee891aa92018-07-13 15:07:47 +0000181static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000182 unsigned Num, unsigned Diag,
183 Compare Comp) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000184 if (Comp(getNumAttributeArgs(AL), Num)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000185 S.Diag(AL.getLoc(), Diag) << AL << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000186 return false;
187 }
188
189 return true;
190}
191
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000192/// Check if the attribute has exactly as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000193/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000194static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000195 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000196 diag::err_attribute_wrong_number_arguments,
197 std::not_equal_to<unsigned>());
198}
199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000200/// Check if the attribute has at least as many args as Num. May
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000201/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000202static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL,
Richard Smithb87c4652013-10-31 21:23:20 +0000203 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000204 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000205 diag::err_attribute_too_few_arguments,
206 std::less<unsigned>());
207}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000208
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000209/// Check if the attribute has at most as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000210/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000211static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL,
212 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000213 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000214 diag::err_attribute_too_many_arguments,
215 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000216}
217
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000218/// A helper function to provide Attribute Location for the Attr types
Erich Keanee891aa92018-07-13 15:07:47 +0000219/// AND the ParsedAttr.
Erich Keane623efd82017-03-30 21:48:55 +0000220template <typename AttrInfo>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000221static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value,
Erich Keane623efd82017-03-30 21:48:55 +0000222 SourceLocation>::type
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000223getAttrLoc(const AttrInfo &AL) {
224 return AL.getLocation();
Erich Keane623efd82017-03-30 21:48:55 +0000225}
Erich Keanee891aa92018-07-13 15:07:47 +0000226static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); }
Erich Keane623efd82017-03-30 21:48:55 +0000227
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000228/// If Expr is a valid integer constant, get the value of the integer
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000229/// expression and return success or failure. May output an error.
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000230///
231/// Negative argument is implicitly converted to unsigned, unless
232/// \p StrictlyUnsigned is true.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000233template <typename AttrInfo>
234static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000235 uint32_t &Val, unsigned Idx = UINT_MAX,
236 bool StrictlyUnsigned = false) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000237 llvm::APSInt I(32);
238 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
239 !Expr->isIntegerConstantExpr(I, S.Context)) {
240 if (Idx != UINT_MAX)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000241 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000242 << AI << Idx << AANT_ArgumentIntegerConstant
243 << Expr->getSourceRange();
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000244 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000245 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000246 << AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange();
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000247 return false;
248 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000249
250 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000251 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
252 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000253 return false;
254 }
255
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000256 if (StrictlyUnsigned && I.isSigned() && I.isNegative()) {
Andrew Savonichev1a5623482018-09-17 10:39:46 +0000257 S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
258 << AI << /*non-negative*/ 1;
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000259 return false;
260 }
261
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000262 Val = (uint32_t)I.getZExtValue();
263 return true;
264}
265
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000266/// Wrapper around checkUInt32Argument, with an extra check to be sure
George Burgess IVe3763372016-12-22 02:50:20 +0000267/// that the result will fit into a regular (signed) int. All args have the same
268/// purpose as they do in checkUInt32Argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000269template <typename AttrInfo>
270static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000271 int &Val, unsigned Idx = UINT_MAX) {
George Burgess IVe3763372016-12-22 02:50:20 +0000272 uint32_t UVal;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000273 if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
George Burgess IVe3763372016-12-22 02:50:20 +0000274 return false;
275
George Burgess IVa8049572016-12-22 19:00:31 +0000276 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
George Burgess IVe3763372016-12-22 02:50:20 +0000277 llvm::APSInt I(32); // for toString
278 I = UVal;
279 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
280 << I.toString(10, false) << 32 << /* Unsigned */ 0;
281 return false;
282 }
283
284 Val = UVal;
285 return true;
286}
287
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000288/// Diagnose mutually exclusive attributes when present on a given
Aaron Ballmanfb763042013-12-02 18:05:46 +0000289/// declaration. Returns true if diagnosed.
290template <typename AttrTy>
Erich Keane44bacdf2018-08-09 13:21:32 +0000291static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000292 if (const auto *A = D->getAttr<AttrTy>()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000293 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
294 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
295 return true;
296 }
297 return false;
298}
299
300template <typename AttrTy>
301static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
302 if (const auto *A = D->getAttr<AttrTy>()) {
303 S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000304 << A;
305 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000306 return true;
307 }
308 return false;
309}
310
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000311/// Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000312/// instance method D. May output an error.
313///
314/// \returns true if IdxExpr is a valid index.
Erich Keane623efd82017-03-30 21:48:55 +0000315template <typename AttrInfo>
316static bool checkFunctionOrMethodParameterIndex(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000317 Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
Joel E. Denny81508102018-03-13 14:51:22 +0000318 const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
David Majnemer06864812015-04-07 06:01:53 +0000319 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000320
321 // In C++ the implicit 'this' function parameter also counts.
322 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000323 bool HP = hasFunctionProto(D);
324 bool HasImplicitThisParam = isInstanceMethod(D);
325 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000326 unsigned NumParams =
327 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000328
329 llvm::APSInt IdxInt;
330 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
331 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000332 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000333 << &AI << AttrArgNum << AANT_ArgumentIntegerConstant
334 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000335 return false;
336 }
337
Joel E. Denny81508102018-03-13 14:51:22 +0000338 unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX);
339 if (IdxSource < 1 || (!IV && IdxSource > NumParams)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000340 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +0000341 << &AI << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000342 return false;
343 }
Joel E. Denny81508102018-03-13 14:51:22 +0000344 if (HasImplicitThisParam && !CanIndexImplicitThis) {
345 if (IdxSource == 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000346 S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
347 << &AI << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000348 return false;
349 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000350 }
351
Joel E. Denny81508102018-03-13 14:51:22 +0000352 Idx = ParamIdx(IdxSource, D);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000353 return true;
354}
355
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000356/// Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000357/// If not emit an error and return false. If the argument is an identifier it
358/// will emit an error with a fixit hint and treat it as if it was a string
359/// literal.
Erich Keanee891aa92018-07-13 15:07:47 +0000360bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
361 StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000362 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000363 // Look for identifiers. If we have one emit a hint to fix it to a literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000364 if (AL.isArgIdent(ArgNum)) {
365 IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000366 Diag(Loc->Loc, diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000367 << AL << AANT_ArgumentString
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000368 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000369 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000370 Str = Loc->Ident->getName();
371 if (ArgLocation)
372 *ArgLocation = Loc->Loc;
373 return true;
374 }
375
376 // Now check for an actual string literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000377 Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
378 const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000379 if (ArgLocation)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000380 *ArgLocation = ArgExpr->getBeginLoc();
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000381
382 if (!Literal || !Literal->isAscii()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000383 Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000384 << AL << AANT_ArgumentString;
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000385 return false;
386 }
387
388 Str = Literal->getString();
389 return true;
390}
391
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000392/// Applies the given attribute to the Decl without performing any
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000393/// additional semantic checking.
394template <typename AttrType>
Erich Keanee891aa92018-07-13 15:07:47 +0000395static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000396 D->addAttr(::new (S.Context) AttrType(AL.getRange(), S.Context,
397 AL.getAttributeSpellingListIndex()));
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000398}
399
Justin Lebar3eaaf862016-01-13 01:07:35 +0000400template <typename AttrType>
401static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000402 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000403 handleSimpleAttribute<AttrType>(S, D, AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000404}
405
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000406/// Applies the given attribute to the Decl so long as the Decl doesn't
Justin Lebar3eaaf862016-01-13 01:07:35 +0000407/// already have one of the given incompatible attributes.
408template <typename AttrType, typename IncompatibleAttrType,
409 typename... IncompatibleAttrTypes>
410static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000411 const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000412 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
Justin Lebar3eaaf862016-01-13 01:07:35 +0000413 return;
414 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000415 AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000416}
417
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000418/// Check if the passed-in expression is of type int or bool.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000419static bool isIntOrBool(Expr *Exp) {
420 QualType QT = Exp->getType();
421 return QT->isBooleanType() || QT->isIntegerType();
422}
423
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000424
425// Check to see if the type is a smart pointer of some kind. We assume
426// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000427static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Trieu8d3fa392018-09-22 01:50:52 +0000428 auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record,
429 OverloadedOperatorKind Op) {
430 DeclContextLookupResult Result =
431 Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op));
432 return !Result.empty();
433 };
434
435 const RecordDecl *Record = RT->getDecl();
436 bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
437 bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
438 if (foundStarOperator && foundArrowOperator)
439 return true;
440
441 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
442 if (!CXXRecord)
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000443 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000444
Richard Trieu8d3fa392018-09-22 01:50:52 +0000445 for (auto BaseSpecifier : CXXRecord->bases()) {
446 if (!foundStarOperator)
447 foundStarOperator = IsOverloadedOperatorPresent(
448 BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
449 if (!foundArrowOperator)
450 foundArrowOperator = IsOverloadedOperatorPresent(
451 BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
452 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000453
Richard Trieu8d3fa392018-09-22 01:50:52 +0000454 if (foundStarOperator && foundArrowOperator)
455 return true;
456
457 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000458}
459
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000460/// Check if passed in Decl is a pointer type.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000461/// Note that this function may produce an error message.
462/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000463static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000464 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000465 const auto *VD = cast<ValueDecl>(D);
466 QualType QT = VD->getType();
Aaron Ballman553e6812013-12-26 14:54:11 +0000467 if (QT->isAnyPointerType())
468 return true;
469
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000470 if (const auto *RT = QT->getAs<RecordType>()) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000471 // If it's an incomplete type, it could be a smart pointer; skip it.
472 // (We don't want to force template instantiation if we can avoid it,
473 // since that would alter the order in which templates are instantiated.)
474 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000475 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000476
Aaron Ballman553e6812013-12-26 14:54:11 +0000477 if (threadSafetyCheckIsSmartPointer(S, RT))
478 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000479 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000480
Erich Keane44bacdf2018-08-09 13:21:32 +0000481 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000482 return false;
483}
484
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000485/// Checks that the passed in QualType either is of RecordType or points
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000486/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000487static const RecordType *getRecordType(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000488 if (const auto *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000489 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000490
491 // Now check if we point to record type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000492 if (const auto *PT = QT->getAs<PointerType>())
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000493 return PT->getPointeeType()->getAs<RecordType>();
494
Craig Topperc3ec1492014-05-26 06:22:03 +0000495 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000496}
497
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000498template <typename AttrType>
499static bool checkRecordDeclForAttr(const RecordDecl *RD) {
500 // Check if the record itself has the attribute.
501 if (RD->hasAttr<AttrType>())
502 return true;
503
504 // Else check if any base classes have the attribute.
505 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
506 CXXBasePaths BPaths(false, false);
507 if (CRD->lookupInBases(
508 [](const CXXBaseSpecifier *BS, CXXBasePath &) {
509 const auto &Ty = *BS->getType();
510 // If it's type-dependent, we assume it could have the attribute.
511 if (Ty.isDependentType())
512 return true;
513 return Ty.getAs<RecordType>()->getDecl()->hasAttr<AttrType>();
514 },
515 BPaths, true))
516 return true;
517 }
518 return false;
519}
520
Josh Gao55afa752017-08-11 07:54:35 +0000521static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000522 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000523
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000524 if (!RT)
525 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000526
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000527 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000528 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000529 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000530
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000531 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000532 // FIXME -- Check the type that the smart pointer points to.
533 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000534 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000535
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000536 return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl());
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000537}
538
Josh Gao55afa752017-08-11 07:54:35 +0000539static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000540 const auto *TD = Ty->getAs<TypedefType>();
541 if (!TD)
542 return false;
543
544 TypedefNameDecl *TN = TD->getDecl();
545 if (!TN)
546 return false;
547
Josh Gao55afa752017-08-11 07:54:35 +0000548 return TN->hasAttr<CapabilityAttr>();
Aaron Ballman76050722014-04-04 15:13:57 +0000549}
550
Josh Gaob40c1772017-08-08 19:44:35 +0000551static bool typeHasCapability(Sema &S, QualType Ty) {
Josh Gao55afa752017-08-11 07:54:35 +0000552 if (checkTypedefTypeForCapability(Ty))
553 return true;
Josh Gaob40c1772017-08-08 19:44:35 +0000554
Josh Gao55afa752017-08-11 07:54:35 +0000555 if (checkRecordTypeForCapability(S, Ty))
556 return true;
557
558 return false;
Josh Gaob40c1772017-08-08 19:44:35 +0000559}
560
Aaron Ballman76050722014-04-04 15:13:57 +0000561static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
562 // Capability expressions are simple expressions involving the boolean logic
563 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
564 // a DeclRefExpr is found, its type should be checked to determine whether it
565 // is a capability or not.
566
Yi Kong2d58d192017-12-14 22:24:45 +0000567 if (const auto *E = dyn_cast<CastExpr>(Ex))
Aaron Ballman76050722014-04-04 15:13:57 +0000568 return isCapabilityExpr(S, E->getSubExpr());
569 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
570 return isCapabilityExpr(S, E->getSubExpr());
571 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
Yi Kong2d58d192017-12-14 22:24:45 +0000572 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
573 E->getOpcode() == UO_Deref)
Aaron Ballman76050722014-04-04 15:13:57 +0000574 return isCapabilityExpr(S, E->getSubExpr());
575 return false;
576 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
577 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
578 return isCapabilityExpr(S, E->getLHS()) &&
579 isCapabilityExpr(S, E->getRHS());
580 return false;
581 }
582
Yi Kong2d58d192017-12-14 22:24:45 +0000583 return typeHasCapability(S, Ex->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000584}
585
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000586/// Checks that all attribute arguments, starting from Sidx, resolve to
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000587/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000588/// \param Sidx The attribute argument index to start checking with.
589/// \param ParamIdxOk Whether an argument can be indexing into a function
590/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000591static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000592 const ParsedAttr &AL,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000593 SmallVectorImpl<Expr *> &Args,
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000594 unsigned Sidx = 0,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000595 bool ParamIdxOk = false) {
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000596 if (Sidx == AL.getNumArgs()) {
597 // If we don't have any capability arguments, the attribute implicitly
598 // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
599 // a non-static method, and that the class is a (scoped) capability.
600 const auto *MD = dyn_cast<const CXXMethodDecl>(D);
601 if (MD && !MD->isStatic()) {
602 const CXXRecordDecl *RD = MD->getParent();
603 // FIXME -- need to check this again on template instantiation
604 if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
605 !checkRecordDeclForAttr<ScopedLockableAttr>(RD))
606 S.Diag(AL.getLoc(),
607 diag::warn_thread_attribute_not_on_capability_member)
608 << AL << MD->getParent();
609 } else {
610 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
611 << AL;
612 }
613 }
614
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000615 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
616 Expr *ArgExp = AL.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000617
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000618 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000619 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000620 Args.push_back(ArgExp);
621 continue;
622 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000623
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000624 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000625 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000626 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000627 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000628 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000629 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000630 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000631 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000632
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000633 // We allow constant strings to be used as a placeholder for expressions
634 // that are not valid C++ syntax, but warn that they are ignored.
Erich Keane44bacdf2018-08-09 13:21:32 +0000635 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000636 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000637 continue;
638 }
639
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000640 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000641
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000642 // A pointer to member expression of the form &MyClass::mu is treated
643 // specially -- we need to look at the type of the member.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000644 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000645 if (UOp->getOpcode() == UO_AddrOf)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000646 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000647 if (DRE->getDecl()->isCXXInstanceMember())
648 ArgTy = DRE->getDecl()->getType();
649
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000650 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000651 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000652
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000653 // Now check if we index into a record type function param.
Josh Gao55afa752017-08-11 07:54:35 +0000654 if(!RT && ParamIdxOk) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000655 const auto *FD = dyn_cast<FunctionDecl>(D);
656 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
Josh Gao55afa752017-08-11 07:54:35 +0000657 if(FD && IL) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000658 unsigned int NumParams = FD->getNumParams();
659 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000660 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
661 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000662 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
663 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +0000664 << AL << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000665 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000666 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000667 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000668 }
669 }
670
Aaron Ballman76050722014-04-04 15:13:57 +0000671 // If the type does not have a capability, see if the components of the
672 // expression have capabilities. This allows for writing C code where the
673 // capability may be on the type, and the expression is a capability
674 // boolean logic expression. Eg) requires_capability(A || B && !C)
675 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000676 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Erich Keane44bacdf2018-08-09 13:21:32 +0000677 << AL << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000678
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000679 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000680 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000681}
682
Chris Lattner58418ff2008-06-29 00:16:31 +0000683//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000684// Attribute Implementations
685//===----------------------------------------------------------------------===//
686
Erich Keanee891aa92018-07-13 15:07:47 +0000687static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000688 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000689 return;
690
Michael Han99315932013-01-24 16:46:58 +0000691 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000692 PtGuardedVarAttr(AL.getRange(), S.Context,
693 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000694}
695
Erich Keanee891aa92018-07-13 15:07:47 +0000696static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000697 Expr *&Arg) {
698 SmallVector<Expr *, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000699 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000700 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000701 unsigned Size = Args.size();
702 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000703 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000704
Michael Han3be3b442012-07-23 18:48:41 +0000705 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000706
Michael Han3be3b442012-07-23 18:48:41 +0000707 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000708}
709
Erich Keanee891aa92018-07-13 15:07:47 +0000710static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000711 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000712 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000713 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000714
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000715 D->addAttr(::new (S.Context) GuardedByAttr(
716 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000717}
718
Erich Keanee891aa92018-07-13 15:07:47 +0000719static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000720 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000721 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000722 return;
723
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000724 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000725 return;
726
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000727 D->addAttr(::new (S.Context) PtGuardedByAttr(
728 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000729}
730
Erich Keanee891aa92018-07-13 15:07:47 +0000731static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000732 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000733 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000734 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000735
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000736 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000737 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000738 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000739 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000740 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000741 }
742
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000743 // Check that all arguments are lockable objects.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000744 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000745 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000746 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000747
Michael Han3be3b442012-07-23 18:48:41 +0000748 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000749}
750
Erich Keanee891aa92018-07-13 15:07:47 +0000751static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000752 SmallVector<Expr *, 1> Args;
753 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000754 return;
755
756 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000757 D->addAttr(::new (S.Context) AcquiredAfterAttr(
758 AL.getRange(), S.Context, StartArg, Args.size(),
759 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000760}
761
Erich Keanee891aa92018-07-13 15:07:47 +0000762static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000763 SmallVector<Expr *, 1> Args;
764 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000765 return;
766
767 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000768 D->addAttr(::new (S.Context) AcquiredBeforeAttr(
769 AL.getRange(), S.Context, StartArg, Args.size(),
770 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000771}
772
Erich Keanee891aa92018-07-13 15:07:47 +0000773static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000774 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000775 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000776 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000777 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000778
Michael Han3be3b442012-07-23 18:48:41 +0000779 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000780}
781
Erich Keanee891aa92018-07-13 15:07:47 +0000782static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000783 SmallVector<Expr *, 1> Args;
784 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000785 return;
786
787 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000788 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000789 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000790 AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size,
791 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000792}
793
794static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000795 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000796 SmallVector<Expr *, 1> Args;
797 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000798 return;
799
800 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000801 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000802 D->addAttr(::new (S.Context) AssertExclusiveLockAttr(
803 AL.getRange(), S.Context, StartArg, Size,
804 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000805}
806
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000807/// Checks to be sure that the given parameter number is in bounds, and
Aaron Ballman836684a2018-02-25 20:40:06 +0000808/// is an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000809/// false.
810///
Aaron Ballman836684a2018-02-25 20:40:06 +0000811/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000812template <typename AttrInfo>
813static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Joel E. Denny81508102018-03-13 14:51:22 +0000814 const AttrInfo &AI, unsigned AttrArgNo) {
Aaron Ballman836684a2018-02-25 20:40:06 +0000815 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
816 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
Joel E. Denny81508102018-03-13 14:51:22 +0000817 ParamIdx Idx;
Aaron Ballman836684a2018-02-25 20:40:06 +0000818 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000819 Idx))
820 return false;
821
Joel E. Denny81508102018-03-13 14:51:22 +0000822 const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +0000823 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000824 SourceLocation SrcLoc = AttrArg->getBeginLoc();
Erich Keane623efd82017-03-30 21:48:55 +0000825 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +0000826 << AI << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000827 return false;
828 }
829 return true;
830}
831
Erich Keanee891aa92018-07-13 15:07:47 +0000832static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000833 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
834 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000835 return;
836
837 const auto *FD = cast<FunctionDecl>(D);
838 if (!FD->getReturnType()->isPointerType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000839 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
George Burgess IVe3763372016-12-22 02:50:20 +0000840 return;
841 }
842
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000843 const Expr *SizeExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +0000844 int SizeArgNoVal;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000845 // Parameter indices are 1-indexed, hence Index=1
Joel E. Denny81508102018-03-13 14:51:22 +0000846 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Index=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000847 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000848 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0))
George Burgess IVe3763372016-12-22 02:50:20 +0000849 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000850 ParamIdx SizeArgNo(SizeArgNoVal, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000851
Joel E. Denny81508102018-03-13 14:51:22 +0000852 ParamIdx NumberArgNo;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000853 if (AL.getNumArgs() == 2) {
854 const Expr *NumberExpr = AL.getArgAsExpr(1);
Joel E. Denny81508102018-03-13 14:51:22 +0000855 int Val;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000856 // Parameter indices are 1-based, hence Index=2
Joel E. Denny81508102018-03-13 14:51:22 +0000857 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Index=*/2))
George Burgess IVe3763372016-12-22 02:50:20 +0000858 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000859 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000860 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000861 NumberArgNo = ParamIdx(Val, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000862 }
863
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000864 D->addAttr(::new (S.Context)
865 AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo,
866 AL.getAttributeSpellingListIndex()));
George Burgess IVe3763372016-12-22 02:50:20 +0000867}
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000868
Erich Keanee891aa92018-07-13 15:07:47 +0000869static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000870 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000871 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000872 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000873
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000874 if (!isIntOrBool(AL.getArgAsExpr(0))) {
875 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000876 << AL << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000877 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000878 }
879
880 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000881 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000882
Michael Han3be3b442012-07-23 18:48:41 +0000883 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000884}
885
Michael Hana9171bc2012-08-03 17:40:43 +0000886static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000887 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000888 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000889 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000890 return;
891
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000892 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
893 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(),
894 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000895}
896
Michael Hana9171bc2012-08-03 17:40:43 +0000897static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000898 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000899 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000900 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000901 return;
902
Nico Weber462fd1e2015-01-07 23:50:05 +0000903 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000904 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(),
905 Args.size(), AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000906}
907
Erich Keanee891aa92018-07-13 15:07:47 +0000908static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000909 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000910 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000911 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000912 unsigned Size = Args.size();
913 if (Size == 0)
914 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000915
Michael Han99315932013-01-24 16:46:58 +0000916 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000917 LockReturnedAttr(AL.getRange(), S.Context, Args[0],
918 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000919}
920
Erich Keanee891aa92018-07-13 15:07:47 +0000921static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000922 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000923 return;
924
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000925 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000926 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000927 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000928 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000929 if (Size == 0)
930 return;
931 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000932
Michael Han99315932013-01-24 16:46:58 +0000933 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000934 LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size,
935 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000936}
937
Erich Keanee891aa92018-07-13 15:07:47 +0000938static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
George Burgess IV177399e2017-01-09 04:12:14 +0000939 Expr *&Cond, StringRef &Msg) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000940 Cond = AL.getArgAsExpr(0);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000941 if (!Cond->isTypeDependent()) {
942 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
943 if (Converted.isInvalid())
George Burgess IV177399e2017-01-09 04:12:14 +0000944 return false;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000945 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000946 }
947
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000948 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000949 return false;
950
951 if (Msg.empty())
952 Msg = "<no message provided>";
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000953
954 SmallVector<PartialDiagnosticAt, 8> Diags;
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000955 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000956 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
957 Diags)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000958 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
George Burgess IV0d546532016-11-10 21:47:12 +0000959 for (const PartialDiagnosticAt &PDiag : Diags)
960 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +0000961 return false;
962 }
963 return true;
964}
965
Erich Keanee891aa92018-07-13 15:07:47 +0000966static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000967 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +0000968
969 Expr *Cond;
970 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000971 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000972 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000973 EnableIfAttr(AL.getRange(), S.Context, Cond, Msg,
974 AL.getAttributeSpellingListIndex()));
George Burgess IV177399e2017-01-09 04:12:14 +0000975}
976
977namespace {
978/// Determines if a given Expr references any of the given function's
979/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
980class ArgumentDependenceChecker
981 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
982#ifndef NDEBUG
983 const CXXRecordDecl *ClassType;
984#endif
985 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
986 bool Result;
987
988public:
989 ArgumentDependenceChecker(const FunctionDecl *FD) {
990#ifndef NDEBUG
991 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
992 ClassType = MD->getParent();
993 else
994 ClassType = nullptr;
995#endif
996 Parms.insert(FD->param_begin(), FD->param_end());
997 }
998
999 bool referencesArgs(Expr *E) {
1000 Result = false;
1001 TraverseStmt(E);
1002 return Result;
1003 }
1004
1005 bool VisitCXXThisExpr(CXXThisExpr *E) {
1006 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
1007 "`this` doesn't refer to the enclosing class?");
1008 Result = true;
1009 return false;
1010 }
1011
1012 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1013 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1014 if (Parms.count(PVD)) {
1015 Result = true;
1016 return false;
1017 }
1018 return true;
1019 }
1020};
1021}
1022
Erich Keanee891aa92018-07-13 15:07:47 +00001023static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001024 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001025
1026 Expr *Cond;
1027 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001028 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001029 return;
1030
1031 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001032 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +00001033 return;
1034
1035 DiagnoseIfAttr::DiagnosticType DiagType;
1036 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001037 S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
George Burgess IV177399e2017-01-09 04:12:14 +00001038 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001039 return;
1040 }
1041
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001042 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +00001043 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001044 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +00001045 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001046 AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent,
1047 cast<NamedDecl>(D), AL.getAttributeSpellingListIndex()));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001048}
1049
Erich Keanee891aa92018-07-13 15:07:47 +00001050static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001051 if (D->hasAttr<PassObjectSizeAttr>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001052 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001053 return;
1054 }
1055
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001056 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001057 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001058 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001059 return;
1060
1061 // pass_object_size's argument is passed in as the second argument of
1062 // __builtin_object_size. So, it has the same constraints as that second
1063 // argument; namely, it must be in the range [0, 3].
1064 if (Type > 3) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001065 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00001066 << AL << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001067 return;
1068 }
1069
1070 // pass_object_size is only supported on constant pointer parameters; as a
1071 // kindness to users, we allow the parameter to be non-const for declarations.
1072 // At this point, we have no clue if `D` belongs to a function declaration or
1073 // definition, so we defer the constness check until later.
1074 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001075 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001076 return;
1077 }
1078
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001079 D->addAttr(::new (S.Context) PassObjectSizeAttr(
1080 AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex()));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001081}
1082
Erich Keanee891aa92018-07-13 15:07:47 +00001083static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001084 ConsumableAttr::ConsumedState DefaultState;
1085
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001086 if (AL.isArgIdent(0)) {
1087 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001088 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1089 DefaultState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001090 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1091 << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001092 return;
1093 }
David Blaikie16f76d22013-09-06 01:28:43 +00001094 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001095 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001096 << AL << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001097 return;
1098 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001099
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001100 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001101 ConsumableAttr(AL.getRange(), S.Context, DefaultState,
1102 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001103}
1104
1105static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Erich Keanee891aa92018-07-13 15:07:47 +00001106 const ParsedAttr &AL) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001107 ASTContext &CurrContext = S.getASTContext();
1108 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001109
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001110 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1111 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001112 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001113 RD->getNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001114
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001115 return false;
1116 }
1117 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001118
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001119 return true;
1120}
1121
Erich Keanee891aa92018-07-13 15:07:47 +00001122static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001123 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001124 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001125
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001126 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001127 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001128
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001129 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001130 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001131 CallableWhenAttr::ConsumedState CallableState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001132
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001133 StringRef StateString;
1134 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001135 if (AL.isArgIdent(ArgIndex)) {
1136 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001137 StateString = Ident->Ident->getName();
1138 Loc = Ident->Loc;
1139 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001140 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001141 return;
1142 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001143
1144 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001145 CallableState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001146 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001147 return;
1148 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001149
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001150 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001151 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001152
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001153 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001154 CallableWhenAttr(AL.getRange(), S.Context, States.data(),
1155 States.size(), AL.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001156}
1157
Erich Keanee891aa92018-07-13 15:07:47 +00001158static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001159 ParamTypestateAttr::ConsumedState ParamState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001160
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001161 if (AL.isArgIdent(0)) {
1162 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001163 StringRef StateString = Ident->Ident->getName();
1164
1165 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1166 ParamState)) {
1167 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00001168 << AL << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001169 return;
1170 }
1171 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001172 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1173 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001174 return;
1175 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001176
DeLesley Hutchins69391772013-10-17 23:23:53 +00001177 // FIXME: This check is currently being done in the analysis. It can be
1178 // enabled here only after the parser propagates attributes at
1179 // template specialization definition, not declaration.
1180 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1181 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1182 //
1183 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001184 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001185 // ReturnType.getAsString();
1186 // return;
1187 //}
Fangrui Song6907ce22018-07-30 19:24:48 +00001188
DeLesley Hutchins69391772013-10-17 23:23:53 +00001189 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001190 ParamTypestateAttr(AL.getRange(), S.Context, ParamState,
1191 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001192}
1193
Erich Keanee891aa92018-07-13 15:07:47 +00001194static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001195 ReturnTypestateAttr::ConsumedState ReturnState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001196
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001197 if (AL.isArgIdent(0)) {
1198 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001199 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1200 ReturnState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001201 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1202 << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001203 return;
1204 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001205 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001206 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1207 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001208 return;
1209 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001210
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001211 // FIXME: This check is currently being done in the analysis. It can be
1212 // enabled here only after the parser propagates attributes at
1213 // template specialization definition, not declaration.
1214 //QualType ReturnType;
1215 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001216 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1217 // ReturnType = Param->getType();
1218 //
1219 //} else if (const CXXConstructorDecl *Constructor =
1220 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001221 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001222 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001223 //} else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001224 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001225 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1226 //}
1227 //
1228 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1229 //
1230 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1231 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1232 // ReturnType.getAsString();
1233 // return;
1234 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001235
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001236 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001237 ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState,
1238 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001239}
1240
Erich Keanee891aa92018-07-13 15:07:47 +00001241static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001242 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001243 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001244
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001245 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001246 if (AL.isArgIdent(0)) {
1247 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001248 StringRef Param = Ident->Ident->getName();
1249 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001250 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1251 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001252 return;
1253 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001254 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001255 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1256 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001257 return;
1258 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001259
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001260 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001261 SetTypestateAttr(AL.getRange(), S.Context, NewState,
1262 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001263}
1264
Erich Keanee891aa92018-07-13 15:07:47 +00001265static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001266 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001267 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001268
1269 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001270 if (AL.isArgIdent(0)) {
1271 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001272 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001273 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001274 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1275 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001276 return;
1277 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001278 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001279 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1280 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001281 return;
1282 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001283
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001284 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001285 TestTypestateAttr(AL.getRange(), S.Context, TestState,
1286 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001287}
1288
Erich Keanee891aa92018-07-13 15:07:47 +00001289static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001290 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001291 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001292}
1293
Erich Keanee891aa92018-07-13 15:07:47 +00001294static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001295 if (auto *TD = dyn_cast<TagDecl>(D))
1296 TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context,
1297 AL.getAttributeSpellingListIndex()));
1298 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001299 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1300 !FD->getType()->isIncompleteType() &&
1301 FD->isBitField() &&
1302 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001303
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001304 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1305 if (BitfieldByteAligned)
1306 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001307 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001308 << AL << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001309 else
1310 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001311 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001312 } else {
1313 // Report warning about changed offset in the newer compiler versions.
1314 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001315 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001316
1317 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001318 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001319 }
1320
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001321 } else
Erich Keane44bacdf2018-08-09 13:21:32 +00001322 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001323}
1324
Erich Keanee891aa92018-07-13 15:07:47 +00001325static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001326 // The IBOutlet/IBOutletCollection attributes only apply to instance
1327 // variables or properties of Objective-C classes. The outlet must also
1328 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001329 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001330 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001331 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001332 << AL << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001333 return false;
1334 }
1335 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001336 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001337 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001338 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001339 << AL << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001340 return false;
1341 }
1342 }
1343 else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001344 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001345 return false;
1346 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001347
Ted Kremenek7fd17232011-09-29 07:02:25 +00001348 return true;
1349}
1350
Erich Keanee891aa92018-07-13 15:07:47 +00001351static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001352 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001353 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001354
Michael Han99315932013-01-24 16:46:58 +00001355 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001356 IBOutletAttr(AL.getRange(), S.Context,
1357 AL.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001358}
1359
Erich Keanee891aa92018-07-13 15:07:47 +00001360static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001361
1362 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001363 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001364 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001365 return;
1366 }
1367
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001368 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001369 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001370
Richard Smithb1f9a282013-10-31 01:56:18 +00001371 ParsedType PT;
1372
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001373 if (AL.hasParsedType())
1374 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001375 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001376 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001377 S.getScopeForContext(D->getDeclContext()->getParent()));
1378 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001379 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001380 return;
1381 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001382 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001383
Craig Topperc3ec1492014-05-26 06:22:03 +00001384 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001385 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1386 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001387 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001388
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001389 // Diagnose use of non-object type in iboutletcollection attribute.
1390 // FIXME. Gnu attribute extension ignores use of builtin types in
1391 // attributes. So, __attribute__((iboutletcollection(char))) will be
1392 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001393 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001394 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001395 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1396 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001397 return;
1398 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001399
Michael Han99315932013-01-24 16:46:58 +00001400 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001401 IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc,
1402 AL.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001403}
1404
Hal Finkelee90a222014-09-26 05:04:30 +00001405bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1406 if (RefOkay) {
1407 if (T->isReferenceType())
1408 return true;
1409 } else {
1410 T = T.getNonReferenceType();
1411 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001412
Hal Finkelee90a222014-09-26 05:04:30 +00001413 // The nonnull attribute, and other similar attributes, can be applied to a
1414 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001415 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001416 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1417 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001418 for (const auto *I : UD->fields()) {
1419 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001420 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1421 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001422 }
1423 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001424 }
1425
1426 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001427}
1428
Erich Keanee891aa92018-07-13 15:07:47 +00001429static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001430 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001431 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001432 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001433 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001434 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001435 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001436 << AL << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001437 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001438 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001439 << AL << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001440 return false;
1441 }
1442 return true;
1443}
1444
Erich Keanee891aa92018-07-13 15:07:47 +00001445static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001446 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001447 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1448 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001449 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001450 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001451 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001452
1453 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001454 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1455 !attrNonNullArgCheck(
1456 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1457 Ex->getSourceRange(),
1458 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001459 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001460
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001461 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001462 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001463
1464 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001465 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1466 // check if the attribute came from a macro expansion or a template
1467 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001468 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001469 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001470 bool AnyPointers = isFunctionOrMethodVariadic(D);
1471 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1472 I != E && !AnyPointers; ++I) {
1473 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001474 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001475 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001476 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001477
Richard Smith588bd9b2014-08-27 04:59:42 +00001478 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001479 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001480 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001481
Joel E. Denny81508102018-03-13 14:51:22 +00001482 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001483 unsigned Size = NonNullArgs.size();
1484 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001485 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001486 NonNullAttr(AL.getRange(), S.Context, Start, Size,
1487 AL.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001488}
1489
Jordan Rosec9399072014-02-11 17:27:59 +00001490static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00001491 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001492 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001493 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001494 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001495 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001496 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001497 << D->getSourceRange();
1498 }
1499 return;
1500 }
1501
1502 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001503 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001504 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001505 return;
1506
1507 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001508 NonNullAttr(AL.getRange(), S.Context, nullptr, 0,
1509 AL.getAttributeSpellingListIndex()));
Jordan Rosec9399072014-02-11 17:27:59 +00001510}
1511
Erich Keanee891aa92018-07-13 15:07:47 +00001512static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001513 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001514 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001515 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001516 /* isReturnValue */ true))
1517 return;
1518
1519 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001520 ReturnsNonNullAttr(AL.getRange(), S.Context,
1521 AL.getAttributeSpellingListIndex()));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001522}
1523
Erich Keanee891aa92018-07-13 15:07:47 +00001524static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001525 if (D->isInvalidDecl())
1526 return;
1527
1528 // noescape only applies to pointer types.
1529 QualType T = cast<ParmVarDecl>(D)->getType();
1530 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001531 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001532 << AL << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001533 return;
1534 }
1535
1536 D->addAttr(::new (S.Context) NoEscapeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001537 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001538}
1539
Erich Keanee891aa92018-07-13 15:07:47 +00001540static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001541 Expr *E = AL.getArgAsExpr(0),
1542 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1543 S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE,
1544 AL.getAttributeSpellingListIndex());
Hal Finkelee90a222014-09-26 05:04:30 +00001545}
1546
Erich Keanee891aa92018-07-13 15:07:47 +00001547static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001548 S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0),
1549 AL.getAttributeSpellingListIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001550}
1551
Hal Finkelee90a222014-09-26 05:04:30 +00001552void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1553 Expr *OE, unsigned SpellingListIndex) {
1554 QualType ResultType = getFunctionOrMethodResultType(D);
1555 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1556
1557 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1558 SourceLocation AttrLoc = AttrRange.getBegin();
1559
1560 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1561 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1562 << &TmpAttr << AttrRange << SR;
1563 return;
1564 }
1565
1566 if (!E->isValueDependent()) {
1567 llvm::APSInt I(64);
1568 if (!E->isIntegerConstantExpr(I, Context)) {
1569 if (OE)
1570 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1571 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1572 << E->getSourceRange();
1573 else
1574 Diag(AttrLoc, diag::err_attribute_argument_type)
1575 << &TmpAttr << AANT_ArgumentIntegerConstant
1576 << E->getSourceRange();
1577 return;
1578 }
1579
1580 if (!I.isPowerOf2()) {
1581 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1582 << E->getSourceRange();
1583 return;
1584 }
1585 }
1586
1587 if (OE) {
1588 if (!OE->isValueDependent()) {
1589 llvm::APSInt I(64);
1590 if (!OE->isIntegerConstantExpr(I, Context)) {
1591 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1592 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1593 << OE->getSourceRange();
1594 return;
1595 }
1596 }
1597 }
1598
1599 D->addAttr(::new (Context)
1600 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1601}
1602
Erich Keane623efd82017-03-30 21:48:55 +00001603void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr,
1604 unsigned SpellingListIndex) {
1605 QualType ResultType = getFunctionOrMethodResultType(D);
1606
Joel E. Denny81508102018-03-13 14:51:22 +00001607 AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex);
Erich Keane623efd82017-03-30 21:48:55 +00001608 SourceLocation AttrLoc = AttrRange.getBegin();
1609
1610 if (!ResultType->isDependentType() &&
1611 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1612 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1613 << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D);
1614 return;
1615 }
1616
Joel E. Denny81508102018-03-13 14:51:22 +00001617 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001618 const auto *FuncDecl = cast<FunctionDecl>(D);
1619 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Joel E. Denny81508102018-03-13 14:51:22 +00001620 /*AttrArgNo=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001621 return;
1622
Joel E. Denny81508102018-03-13 14:51:22 +00001623 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001624 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001625 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001626 << &TmpAttr
1627 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001628 return;
1629 }
1630
Joel E. Denny81508102018-03-13 14:51:22 +00001631 D->addAttr(::new (Context)
1632 AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex));
Erich Keane623efd82017-03-30 21:48:55 +00001633}
1634
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001635/// Normalize the attribute, __foo__ becomes foo.
1636/// Returns true if normalization was applied.
1637static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001638 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1639 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001640 AttrName = AttrName.drop_front(2).drop_back(2);
1641 return true;
1642 }
1643 return false;
1644}
1645
Erich Keanee891aa92018-07-13 15:07:47 +00001646static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001647 // This attribute must be applied to a function declaration. The first
1648 // argument to the attribute must be an identifier, the name of the resource,
1649 // for example: malloc. The following arguments must be argument indexes, the
1650 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001651 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001652 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001653 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001654
Aaron Ballman00e99962013-08-31 01:11:41 +00001655 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001656 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001657 << AL << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001658 return;
1659 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001660
Richard Smith852e9ce2013-11-27 01:46:48 +00001661 // Figure out our Kind.
1662 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001663 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001664 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001665
Richard Smith852e9ce2013-11-27 01:46:48 +00001666 // Check arguments.
1667 switch (K) {
1668 case OwnershipAttr::Takes:
1669 case OwnershipAttr::Holds:
1670 if (AL.getNumArgs() < 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001671 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001672 return;
1673 }
1674 break;
1675 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001676 if (AL.getNumArgs() > 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001677 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001678 return;
1679 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001680 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001681 }
1682
Richard Smith852e9ce2013-11-27 01:46:48 +00001683 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001684
Richard Smith852e9ce2013-11-27 01:46:48 +00001685 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001686 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001687 Module = &S.PP.getIdentifierTable().get(ModuleName);
1688 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001689
Joel E. Denny81508102018-03-13 14:51:22 +00001690 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001691 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1692 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001693 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001694 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001695 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001696
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001697 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001698 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001699 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001700 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001701 case OwnershipAttr::Takes:
1702 case OwnershipAttr::Holds:
1703 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1704 Err = 0;
1705 break;
1706 case OwnershipAttr::Returns:
1707 if (!T->isIntegerType())
1708 Err = 1;
1709 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001710 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001711 if (-1 != Err) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001712 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1713 << Ex->getSourceRange();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001714 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001715 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001716
1717 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001718 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001719 // Cannot have two ownership attributes of different kinds for the same
1720 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001721 if (I->getOwnKind() != K && I->args_end() !=
1722 std::find(I->args_begin(), I->args_end(), Idx)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001723 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001724 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001725 } else if (K == OwnershipAttr::Returns &&
1726 I->getOwnKind() == OwnershipAttr::Returns) {
1727 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001728 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001729 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1730 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001731 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001732 if (I->args_size())
1733 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001734 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001735 return;
1736 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001737 }
1738 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001739 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001740 }
1741
Joel E. Denny81508102018-03-13 14:51:22 +00001742 ParamIdx *Start = OwnershipArgs.data();
1743 unsigned Size = OwnershipArgs.size();
1744 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001745 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001746 OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size,
1747 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001748}
1749
Erich Keanee891aa92018-07-13 15:07:47 +00001750static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001751 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001752 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001753 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001754 return;
1755 }
1756
1757 // gcc rejects
1758 // class c {
1759 // static int a __attribute__((weakref ("v2")));
1760 // static int b() __attribute__((weakref ("f3")));
1761 // };
1762 // and ignores the attributes of
1763 // void f(void) {
1764 // static int a __attribute__((weakref ("v2")));
1765 // }
1766 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001767 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001768 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001769 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1770 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001771 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001772 }
1773
1774 // The GCC manual says
1775 //
1776 // At present, a declaration to which `weakref' is attached can only
1777 // be `static'.
1778 //
1779 // It also says
1780 //
1781 // Without a TARGET,
1782 // given as an argument to `weakref' or to `alias', `weakref' is
1783 // equivalent to `weak'.
1784 //
1785 // gcc 4.4.1 will accept
1786 // int a7 __attribute__((weakref));
1787 // as
1788 // int a7 __attribute__((weak));
1789 // This looks like a bug in gcc. We reject that for now. We should revisit
1790 // it if this behaviour is actually used.
1791
Rafael Espindolac18086a2010-02-23 22:00:30 +00001792 // GCC rejects
1793 // static ((alias ("y"), weakref)).
1794 // Should we? How to check that weakref is before or after alias?
1795
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001796 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1797 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1798 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001799 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001800 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001801 // GCC will accept anything as the argument of weakref. Should we
1802 // check for an existing decl?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001803 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1804 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001805
Michael Han99315932013-01-24 16:46:58 +00001806 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001807 WeakRefAttr(AL.getRange(), S.Context,
1808 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001809}
1810
Erich Keanee891aa92018-07-13 15:07:47 +00001811static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001812 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001813 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001814 return;
1815
1816 // Aliases should be on declarations, not definitions.
1817 const auto *FD = cast<FunctionDecl>(D);
1818 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001819 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001820 return;
1821 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001822
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001823 D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str,
1824 AL.getAttributeSpellingListIndex()));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001825}
1826
Erich Keanee891aa92018-07-13 15:07:47 +00001827static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001828 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001829 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001830 return;
1831
Douglas Gregore8bbc122011-09-02 00:18:52 +00001832 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001833 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001834 return;
1835 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001836 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001837 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001838 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001839
David Majnemer2dc81462015-01-19 09:00:28 +00001840 // Aliases should be on declarations, not definitions.
1841 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1842 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001843 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001844 return;
1845 }
1846 } else {
1847 const auto *VD = cast<VarDecl>(D);
1848 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001849 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001850 return;
1851 }
1852 }
1853
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001854 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001855
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001856 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1857 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001858}
1859
Erich Keanee891aa92018-07-13 15:07:47 +00001860static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001861 StringRef Model;
1862 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001863 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001864 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001865 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001866
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001867 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001868 if (Model != "global-dynamic" && Model != "local-dynamic"
1869 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001870 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001871 return;
1872 }
1873
Michael Han99315932013-01-24 16:46:58 +00001874 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001875 TLSModelAttr(AL.getRange(), S.Context, Model,
1876 AL.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001877}
1878
Erich Keanee891aa92018-07-13 15:07:47 +00001879static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001880 QualType ResultType = getFunctionOrMethodResultType(D);
1881 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1882 D->addAttr(::new (S.Context) RestrictAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001883 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
David Majnemer631a90b2015-02-04 07:23:21 +00001884 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001885 }
1886
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001887 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001888 << AL << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001889}
1890
Erich Keane3efe0022018-07-20 14:13:28 +00001891static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1892 FunctionDecl *FD = cast<FunctionDecl>(D);
Erich Keane659c8712018-09-10 14:31:56 +00001893
1894 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1895 if (MD->getParent()->isLambda()) {
1896 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
1897 return;
1898 }
1899 }
1900
Erich Keane3efe0022018-07-20 14:13:28 +00001901 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1902 return;
1903
1904 SmallVector<IdentifierInfo *, 8> CPUs;
1905 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
1906 if (!AL.isArgIdent(ArgNo)) {
1907 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001908 << AL << AANT_ArgumentIdentifier;
Erich Keane3efe0022018-07-20 14:13:28 +00001909 return;
1910 }
1911
1912 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1913 StringRef CPUName = CPUArg->Ident->getName().trim();
1914
1915 if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1916 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1917 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1918 return;
1919 }
1920
1921 const TargetInfo &Target = S.Context.getTargetInfo();
1922 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1923 return Target.CPUSpecificManglingCharacter(CPUName) ==
1924 Target.CPUSpecificManglingCharacter(Cur->getName());
1925 })) {
1926 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1927 return;
1928 }
1929 CPUs.push_back(CPUArg->Ident);
1930 }
1931
1932 FD->setIsMultiVersion(true);
1933 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
1934 D->addAttr(::new (S.Context) CPUSpecificAttr(
1935 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
1936 AL.getAttributeSpellingListIndex()));
1937 else
1938 D->addAttr(::new (S.Context) CPUDispatchAttr(
1939 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
1940 AL.getAttributeSpellingListIndex()));
1941}
1942
Erich Keanee891aa92018-07-13 15:07:47 +00001943static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001944 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001945 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00001946 << AL << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001947 return;
1948 }
1949
Erich Keane44bacdf2018-08-09 13:21:32 +00001950 if (CommonAttr *CA = S.mergeCommonAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001951 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001952}
1953
Erich Keanee891aa92018-07-13 15:07:47 +00001954static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001955 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
Charles Davis0e379112016-08-08 21:19:08 +00001956 return;
1957
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001958 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001959 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
1960 const auto &Arch = Triple.getArch();
1961 if (Arch != llvm::Triple::x86 &&
1962 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001963 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
Erich Keane44bacdf2018-08-09 13:21:32 +00001964 << AL << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001965 return;
1966 }
1967 }
1968
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001969 D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context,
1970 AL.getAttributeSpellingListIndex()));
Charles Davis0e379112016-08-08 21:19:08 +00001971}
1972
Erich Keanee891aa92018-07-13 15:07:47 +00001973static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001974 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001975
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001976 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001977 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001978 << Attrs << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001979 return;
1980 }
1981
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001982 D->addAttr(::new (S.Context) NoReturnAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00001983 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001984}
1985
Erich Keanee891aa92018-07-13 15:07:47 +00001986static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00001987 if (!S.getLangOpts().CFProtectionBranch)
1988 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
1989 else
1990 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00001991}
1992
Erich Keanee891aa92018-07-13 15:07:47 +00001993bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001994 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
1995 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00001996 return true;
1997 }
1998
1999 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002000}
2001
Erich Keanee891aa92018-07-13 15:07:47 +00002002bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002003 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002004 if (!AL.existsInTarget(Context.getTargetInfo())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002005 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002006 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002007 return true;
2008 }
2009
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002010 return false;
2011}
2012
Erich Keanee891aa92018-07-13 15:07:47 +00002013static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2014
Ted Kremenek5295ce82010-08-19 00:51:58 +00002015 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2016 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00002017 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002018 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00002019 if (!VD || (!VD->getType()->isBlockPointerType() &&
2020 !VD->getType()->isFunctionPointerType())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002021 S.Diag(AL.getLoc(), AL.isCXX11Attribute()
2022 ? diag::err_attribute_wrong_decl_type
2023 : diag::warn_attribute_wrong_decl_type)
2024 << AL << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00002025 return;
2026 }
2027 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002028
Michael Han99315932013-01-24 16:46:58 +00002029 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002030 AnalyzerNoReturnAttr(AL.getRange(), S.Context,
2031 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002032}
2033
John Thompsoncdb847ba2010-08-09 21:53:52 +00002034// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00002035static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2036 /*
2037 Returning a Vector Class in Registers
2038
2039 According to the PPU ABI specifications, a class with a single member of
2040 vector type is returned in memory when used as the return value of a
2041 function.
2042 This results in inefficient code when implementing vector classes. To return
2043 the value in a single vector register, add the vecreturn attribute to the
2044 class definition. This attribute is also applicable to struct types.
2045
2046 Example:
2047
2048 struct Vector
2049 {
2050 __vector float xyzw;
2051 } __attribute__((vecreturn));
2052
2053 Vector Add(Vector lhs, Vector rhs)
2054 {
2055 Vector result;
2056 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2057 return result; // This will be returned in a register
2058 }
2059 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002060 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002061 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002062 return;
2063 }
2064
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002065 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002066 int count = 0;
2067
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002068 if (!isa<CXXRecordDecl>(R)) {
2069 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002070 return;
2071 }
2072
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002073 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2074 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002075 return;
2076 }
2077
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002078 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002079 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002080 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002081 return;
2082 }
2083 count++;
2084 }
2085
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002086 D->addAttr(::new (S.Context) VecReturnAttr(
2087 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002088}
2089
Richard Smithe233fbf2013-01-28 22:42:45 +00002090static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002091 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002092 if (isa<ParmVarDecl>(D)) {
2093 // [[carries_dependency]] can only be applied to a parameter if it is a
2094 // parameter of a function declaration or lambda.
2095 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002096 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002097 diag::err_carries_dependency_param_not_function_decl);
2098 return;
2099 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002100 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002101
2102 D->addAttr(::new (S.Context) CarriesDependencyAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002103 AL.getRange(), S.Context,
2104 AL.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002105}
2106
Erich Keanee891aa92018-07-13 15:07:47 +00002107static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002108 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002109
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002110 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002111 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002112 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Erich Keane44bacdf2018-08-09 13:21:32 +00002113 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002114
2115 D->addAttr(::new (S.Context) UnusedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002116 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002117}
2118
Erich Keanee891aa92018-07-13 15:07:47 +00002119static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002120 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002121 if (AL.getNumArgs() &&
2122 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002123 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002124
Michael Han99315932013-01-24 16:46:58 +00002125 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002126 ConstructorAttr(AL.getRange(), S.Context, priority,
2127 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002128}
2129
Erich Keanee891aa92018-07-13 15:07:47 +00002130static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002131 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002132 if (AL.getNumArgs() &&
2133 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002134 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002135
Michael Han99315932013-01-24 16:46:58 +00002136 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002137 DestructorAttr(AL.getRange(), S.Context, priority,
2138 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002139}
2140
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002141template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002142static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002143 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002144 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002145 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002146 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002147
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002148 D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str,
2149 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002150}
2151
Ted Kremenek438f8db2014-02-22 01:06:05 +00002152static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002153 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002154 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002155 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
Erich Keane44bacdf2018-08-09 13:21:32 +00002156 << AL << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002157 return;
2158 }
2159
Ted Kremenek28eace62013-11-23 01:01:34 +00002160 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002161 ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context,
2162 AL.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00002163}
2164
Jordy Rose740b0c22012-05-08 03:27:22 +00002165static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2166 IdentifierInfo *Platform,
2167 VersionTuple Introduced,
2168 VersionTuple Deprecated,
2169 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002170 StringRef PlatformName
2171 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2172 if (PlatformName.empty())
2173 PlatformName = Platform->getName();
2174
2175 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2176 // of these steps are needed).
2177 if (!Introduced.empty() && !Deprecated.empty() &&
2178 !(Introduced <= Deprecated)) {
2179 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2180 << 1 << PlatformName << Deprecated.getAsString()
2181 << 0 << Introduced.getAsString();
2182 return true;
2183 }
2184
2185 if (!Introduced.empty() && !Obsoleted.empty() &&
2186 !(Introduced <= Obsoleted)) {
2187 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2188 << 2 << PlatformName << Obsoleted.getAsString()
2189 << 0 << Introduced.getAsString();
2190 return true;
2191 }
2192
2193 if (!Deprecated.empty() && !Obsoleted.empty() &&
2194 !(Deprecated <= Obsoleted)) {
2195 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2196 << 2 << PlatformName << Obsoleted.getAsString()
2197 << 1 << Deprecated.getAsString();
2198 return true;
2199 }
2200
2201 return false;
2202}
2203
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002204/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002205///
2206/// If either version tuple is empty, then they are assumed to match. If
2207/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2208static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2209 bool BeforeIsOkay) {
2210 if (X.empty() || Y.empty())
2211 return true;
2212
2213 if (X == Y)
2214 return true;
2215
2216 if (BeforeIsOkay && X < Y)
2217 return true;
2218
2219 return false;
2220}
2221
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002222AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002223 IdentifierInfo *Platform,
Manman Ren719a8642016-05-06 21:04:01 +00002224 bool Implicit,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002225 VersionTuple Introduced,
2226 VersionTuple Deprecated,
2227 VersionTuple Obsoleted,
2228 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002229 StringRef Message,
Manman Rend8039df2016-02-22 04:47:24 +00002230 bool IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002231 StringRef Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002232 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00002233 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002234 VersionTuple MergedIntroduced = Introduced;
2235 VersionTuple MergedDeprecated = Deprecated;
2236 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002237 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002238 bool OverrideOrImpl = false;
2239 switch (AMK) {
2240 case AMK_None:
2241 case AMK_Redeclaration:
2242 OverrideOrImpl = false;
2243 break;
2244
2245 case AMK_Override:
2246 case AMK_ProtocolImplementation:
2247 OverrideOrImpl = true;
2248 break;
2249 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002250
Rafael Espindolac67f2232012-05-10 02:50:16 +00002251 if (D->hasAttrs()) {
2252 AttrVec &Attrs = D->getAttrs();
2253 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002254 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002255 if (!OldAA) {
2256 ++i;
2257 continue;
2258 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002259
Rafael Espindolac67f2232012-05-10 02:50:16 +00002260 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2261 if (OldPlatform != Platform) {
2262 ++i;
2263 continue;
2264 }
2265
Tim Northover7a73cc72015-10-30 16:30:49 +00002266 // If there is an existing availability attribute for this platform that
2267 // is explicit and the new one is implicit use the explicit one and
2268 // discard the new implicit attribute.
Manman Ren719a8642016-05-06 21:04:01 +00002269 if (!OldAA->isImplicit() && Implicit) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002270 return nullptr;
2271 }
2272
2273 // If there is an existing attribute for this platform that is implicit
2274 // and the new attribute is explicit then erase the old one and
2275 // continue processing the attributes.
Manman Ren719a8642016-05-06 21:04:01 +00002276 if (!Implicit && OldAA->isImplicit()) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002277 Attrs.erase(Attrs.begin() + i);
2278 --e;
2279 continue;
2280 }
2281
Rafael Espindolac67f2232012-05-10 02:50:16 +00002282 FoundAny = true;
2283 VersionTuple OldIntroduced = OldAA->getIntroduced();
2284 VersionTuple OldDeprecated = OldAA->getDeprecated();
2285 VersionTuple OldObsoleted = OldAA->getObsoleted();
2286 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002287
Douglas Gregord2a713e2015-09-30 21:27:42 +00002288 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2289 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2290 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002291 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002292 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2293 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002294 int Which = -1;
2295 VersionTuple FirstVersion;
2296 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002297 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002298 Which = 0;
2299 FirstVersion = OldIntroduced;
2300 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002301 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002302 Which = 1;
2303 FirstVersion = Deprecated;
2304 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002305 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002306 Which = 2;
2307 FirstVersion = Obsoleted;
2308 SecondVersion = OldObsoleted;
2309 }
2310
2311 if (Which == -1) {
2312 Diag(OldAA->getLocation(),
2313 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002314 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2315 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002316 } else {
2317 Diag(OldAA->getLocation(),
2318 diag::warn_mismatched_availability_override)
2319 << Which
2320 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002321 << FirstVersion.getAsString() << SecondVersion.getAsString()
2322 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002323 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002324 if (AMK == AMK_Override)
2325 Diag(Range.getBegin(), diag::note_overridden_method);
2326 else
2327 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002328 } else {
2329 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2330 Diag(Range.getBegin(), diag::note_previous_attribute);
2331 }
2332
Rafael Espindolac67f2232012-05-10 02:50:16 +00002333 Attrs.erase(Attrs.begin() + i);
2334 --e;
2335 continue;
2336 }
2337
2338 VersionTuple MergedIntroduced2 = MergedIntroduced;
2339 VersionTuple MergedDeprecated2 = MergedDeprecated;
2340 VersionTuple MergedObsoleted2 = MergedObsoleted;
2341
2342 if (MergedIntroduced2.empty())
2343 MergedIntroduced2 = OldIntroduced;
2344 if (MergedDeprecated2.empty())
2345 MergedDeprecated2 = OldDeprecated;
2346 if (MergedObsoleted2.empty())
2347 MergedObsoleted2 = OldObsoleted;
2348
2349 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2350 MergedIntroduced2, MergedDeprecated2,
2351 MergedObsoleted2)) {
2352 Attrs.erase(Attrs.begin() + i);
2353 --e;
2354 continue;
2355 }
2356
2357 MergedIntroduced = MergedIntroduced2;
2358 MergedDeprecated = MergedDeprecated2;
2359 MergedObsoleted = MergedObsoleted2;
2360 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002361 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002362 }
2363
2364 if (FoundAny &&
2365 MergedIntroduced == Introduced &&
2366 MergedDeprecated == Deprecated &&
2367 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002368 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002369
Douglas Gregord2a713e2015-09-30 21:27:42 +00002370 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002371 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002372 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002373 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002374 !OverrideOrImpl) {
Manman Ren719a8642016-05-06 21:04:01 +00002375 auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002376 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002377 Obsoleted, IsUnavailable, Message,
Manman Ren75bc6762016-03-21 17:30:55 +00002378 IsStrict, Replacement,
2379 AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002380 Avail->setImplicit(Implicit);
2381 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002382 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002383 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002384}
2385
Erich Keanee891aa92018-07-13 15:07:47 +00002386static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002387 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002388 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002389 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2390 unsigned Index = AL.getAttributeSpellingListIndex();
Fangrui Song6907ce22018-07-30 19:24:48 +00002391
Aaron Ballman00e99962013-08-31 01:11:41 +00002392 IdentifierInfo *II = Platform->Ident;
2393 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2394 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2395 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002396
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002397 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002398 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002399 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002400
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002401 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2402 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2403 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2404 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2405 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002406 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002407 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002408 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002409 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002410 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002411 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002412
Michael Wu260e9622018-11-12 02:44:33 +00002413 if (II->isStr("swift")) {
2414 if (Introduced.isValid() || Obsoleted.isValid() ||
2415 (!IsUnavailable && !Deprecated.isValid())) {
2416 S.Diag(AL.getLoc(),
2417 diag::warn_availability_swift_unavailable_deprecated_only);
2418 return;
2419 }
2420 }
2421
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002422 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II,
Manman Ren719a8642016-05-06 21:04:01 +00002423 false/*Implicit*/,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002424 Introduced.Version,
2425 Deprecated.Version,
2426 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002427 IsUnavailable, Str,
Manman Ren75bc6762016-03-21 17:30:55 +00002428 IsStrict, Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002429 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002430 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002431 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002432 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002433
2434 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2435 // matches before the start of the watchOS platform.
2436 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2437 IdentifierInfo *NewII = nullptr;
2438 if (II->getName() == "ios")
2439 NewII = &S.Context.Idents.get("watchos");
2440 else if (II->getName() == "ios_app_extension")
2441 NewII = &S.Context.Idents.get("watchos_app_extension");
2442
2443 if (NewII) {
2444 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2445 if (Version.empty())
2446 return Version;
2447 auto Major = Version.getMajor();
2448 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2449 if (NewMajor >= 2) {
2450 if (Version.getMinor().hasValue()) {
2451 if (Version.getSubminor().hasValue())
2452 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2453 Version.getSubminor().getValue());
2454 else
2455 return VersionTuple(NewMajor, Version.getMinor().getValue());
2456 }
2457 }
2458
2459 return VersionTuple(2, 0);
2460 };
2461
2462 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2463 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2464 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2465
2466 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002467 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002468 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002469 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002470 NewIntroduced,
2471 NewDeprecated,
2472 NewObsoleted,
2473 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002474 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002475 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002476 Sema::AMK_None,
2477 Index);
2478 if (NewAttr)
2479 D->addAttr(NewAttr);
2480 }
2481 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2482 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2483 // matches before the start of the tvOS platform.
2484 IdentifierInfo *NewII = nullptr;
2485 if (II->getName() == "ios")
2486 NewII = &S.Context.Idents.get("tvos");
2487 else if (II->getName() == "ios_app_extension")
2488 NewII = &S.Context.Idents.get("tvos_app_extension");
2489
2490 if (NewII) {
2491 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002492 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002493 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002494 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002495 Introduced.Version,
2496 Deprecated.Version,
2497 Obsoleted.Version,
2498 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002499 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002500 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002501 Sema::AMK_None,
2502 Index);
2503 if (NewAttr)
2504 D->addAttr(NewAttr);
2505 }
2506 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002507}
2508
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002509static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002510 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002511 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002512 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002513 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002514 "Invalid number of arguments in an external_source_symbol attribute");
2515
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002516 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002517 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002518 Language = SE->getString();
2519 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002520 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002521 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002522 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002523
2524 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002525 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2526 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002527}
2528
John McCalld041a9b2013-02-20 01:54:26 +00002529template <class T>
2530static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2531 typename T::VisibilityType value,
2532 unsigned attrSpellingListIndex) {
2533 T *existingAttr = D->getAttr<T>();
2534 if (existingAttr) {
2535 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2536 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002537 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002538 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2539 S.Diag(range.getBegin(), diag::note_previous_attribute);
2540 D->dropAttr<T>();
2541 }
2542 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2543}
2544
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002545VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002546 VisibilityAttr::VisibilityType Vis,
2547 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002548 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2549 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002550}
2551
John McCalld041a9b2013-02-20 01:54:26 +00002552TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2553 TypeVisibilityAttr::VisibilityType Vis,
2554 unsigned AttrSpellingListIndex) {
2555 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2556 AttrSpellingListIndex);
2557}
2558
Erich Keanee891aa92018-07-13 15:07:47 +00002559static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002560 bool isTypeVisibility) {
2561 // Visibility attributes don't mean anything on a typedef.
2562 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002563 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002564 return;
2565 }
2566
2567 // 'type_visibility' can only go on a type or namespace.
2568 if (isTypeVisibility &&
2569 !(isa<TagDecl>(D) ||
2570 isa<ObjCInterfaceDecl>(D) ||
2571 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002572 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002573 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002574 return;
2575 }
2576
Benjamin Kramer70370212013-09-09 15:08:57 +00002577 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002578 StringRef TypeStr;
2579 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002580 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002581 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002582
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002583 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002584 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002585 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2586 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002587 return;
2588 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002589
Aaron Ballman682ee422013-09-11 19:47:58 +00002590 // Complain about attempts to use protected visibility on targets
2591 // (like Darwin) that don't support it.
2592 if (type == VisibilityAttr::Protected &&
2593 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002594 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002595 type = VisibilityAttr::Default;
2596 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002597
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002598 unsigned Index = AL.getAttributeSpellingListIndex();
2599 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002600 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002601 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002602 (TypeVisibilityAttr::VisibilityType) type,
2603 Index);
2604 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002605 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002606 }
2607 if (newAttr)
2608 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002609}
2610
Erich Keanee891aa92018-07-13 15:07:47 +00002611static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002612 const auto *M = cast<ObjCMethodDecl>(D);
2613 if (!AL.isArgIdent(0)) {
2614 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002615 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002616 return;
2617 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002618
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002619 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002620 ObjCMethodFamilyAttr::FamilyKind F;
2621 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002622 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002623 return;
2624 }
2625
Alp Toker314cc812014-01-25 16:55:45 +00002626 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002627 !M->getReturnType()->isObjCObjectPointerType()) {
2628 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2629 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002630 // Ignore the attribute.
2631 return;
2632 }
2633
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002634 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2635 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002636}
2637
Erich Keanee891aa92018-07-13 15:07:47 +00002638static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002639 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002640 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002641 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002642 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2643 return;
2644 }
2645 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002646 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002647 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002648 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002649 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2650 return;
2651 }
2652 }
2653 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002654 // It is okay to include this attribute on properties, e.g.:
2655 //
2656 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2657 //
2658 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002659 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002660 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002661 }
Michael Han99315932013-01-24 16:46:58 +00002662 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002663 ObjCNSObjectAttr(AL.getRange(), S.Context,
2664 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002665}
2666
Erich Keanee891aa92018-07-13 15:07:47 +00002667static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002668 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002669 QualType T = TD->getUnderlyingType();
2670 if (!T->isObjCObjectPointerType()) {
2671 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2672 return;
2673 }
2674 } else {
2675 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2676 return;
2677 }
2678 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002679 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2680 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002681}
2682
Erich Keanee891aa92018-07-13 15:07:47 +00002683static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002684 if (!AL.isArgIdent(0)) {
2685 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002686 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002687 return;
2688 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002689
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002690 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002691 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002692 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002693 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002694 return;
2695 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002696
Michael Han99315932013-01-24 16:46:58 +00002697 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002698 BlocksAttr(AL.getRange(), S.Context, type,
2699 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002700}
2701
Erich Keanee891aa92018-07-13 15:07:47 +00002702static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002703 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002704 if (AL.getNumArgs() > 0) {
2705 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002706 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002707 if (E->isTypeDependent() || E->isValueDependent() ||
2708 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002709 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002710 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002711 return;
2712 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002713
John McCallb46f2872011-09-09 07:56:05 +00002714 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002715 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002716 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002717 return;
2718 }
John McCallb46f2872011-09-09 07:56:05 +00002719
2720 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002721 }
2722
Aaron Ballman18a78382013-11-21 00:28:23 +00002723 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002724 if (AL.getNumArgs() > 1) {
2725 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002726 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002727 if (E->isTypeDependent() || E->isValueDependent() ||
2728 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002729 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002730 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002731 return;
2732 }
2733 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002734
John McCallb46f2872011-09-09 07:56:05 +00002735 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002736 // FIXME: This error message could be improved, it would be nice
2737 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002738 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002739 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002740 return;
2741 }
2742 }
2743
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002744 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002745 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002746 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002747 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002748 return;
2749 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002750
Chris Lattner9363e312009-03-17 23:03:47 +00002751 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002752 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002753 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002754 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002755 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002756 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002757 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002758 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002759 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002760 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002761 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002762 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002763 return;
2764 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002765 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002766 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002767 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002768 const FunctionType *FT = Ty->isFunctionPointerType()
2769 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002770 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002771 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002772 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002773 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002774 return;
2775 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002776 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002777 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002778 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002779 return;
2780 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002781 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002782 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002783 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002784 return;
2785 }
Michael Han99315932013-01-24 16:46:58 +00002786 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002787 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2788 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002789}
2790
Erich Keanee891aa92018-07-13 15:07:47 +00002791static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002792 if (D->getFunctionType() &&
2793 D->getFunctionType()->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002794 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002795 return;
2796 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002797 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002798 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002799 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002800 return;
2801 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002802
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002803 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002804 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002805 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2806 !AL.getScopeName())
Erich Keane44bacdf2018-08-09 13:21:32 +00002807 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballmane7964782016-03-07 22:44:55 +00002808
Fangrui Song6907ce22018-07-30 19:24:48 +00002809 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002810 WarnUnusedResultAttr(AL.getRange(), S.Context,
2811 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002812}
2813
Erich Keanee891aa92018-07-13 15:07:47 +00002814static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002815 // weak_import only applies to variable & function declarations.
2816 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002817 if (!D->canBeWeakImported(isDef)) {
2818 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002819 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002820 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002821 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002822 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002823 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002824 // Nothing to warn about here.
2825 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002826 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002827 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002828
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002829 return;
2830 }
2831
Michael Han99315932013-01-24 16:46:58 +00002832 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002833 WeakImportAttr(AL.getRange(), S.Context,
2834 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002835}
2836
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002837// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002838template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002839static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002840 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002841 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002842 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002843 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2844 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002845 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002846 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002847 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002848 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002849 return;
2850 }
2851 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002852
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002853 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2854 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2855 Existing->getYDim() == WGSize[1] &&
2856 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002857 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002858
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002859 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002860 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002861 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002862}
2863
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002864// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002865static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002866 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002867 const Expr *E = AL.getArgAsExpr(0);
2868 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002869 return;
2870 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002871 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002872 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002873 return;
2874 }
2875
2876 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2877 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2878 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002879 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002880
2881 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002882 AL.getRange(), S.Context, SGSize,
2883 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002884}
2885
Erich Keanee891aa92018-07-13 15:07:47 +00002886static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002887 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002888 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002889 return;
2890 }
2891
Craig Topperc3ec1492014-05-26 06:22:03 +00002892 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002893 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002894 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002895
2896 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2897 (ParmType->isBooleanType() ||
2898 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002899 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002900 << ParmType;
2901 return;
2902 }
2903
Aaron Ballmana9e05402013-12-02 22:16:55 +00002904 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002905 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002906 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002907 return;
2908 }
2909 }
2910
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002911 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002912 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002913 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002914}
2915
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002916SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002917 StringRef Name,
2918 unsigned AttrSpellingListIndex) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002919 // Explicit or partial specializations do not inherit
2920 // the section attribute from the primary template.
2921 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2922 if (AttrSpellingListIndex == SectionAttr::Declspec_allocate &&
2923 FD->isFunctionTemplateSpecialization())
2924 return nullptr;
2925 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002926 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2927 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002928 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002929 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2930 << 1 /*section*/;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002931 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002932 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002933 }
Michael Han99315932013-01-24 16:46:58 +00002934 return ::new (Context) SectionAttr(Range, Context, Name,
2935 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002936}
2937
Reid Kleckner2a133222015-03-04 23:39:17 +00002938bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2939 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2940 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002941 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002942 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002943 return false;
2944 }
2945 return true;
2946}
2947
Erich Keanee891aa92018-07-13 15:07:47 +00002948static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002949 // Make sure that there is a string literal as the sections's single
2950 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002951 StringRef Str;
2952 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002953 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002954 return;
Mike Stump11289f42009-09-09 15:08:12 +00002955
Reid Kleckner2a133222015-03-04 23:39:17 +00002956 if (!S.checkSectionName(LiteralLoc, Str))
2957 return;
2958
Chris Lattner30ba6742009-08-10 19:03:04 +00002959 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002960 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002961 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002962 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002963 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002964 return;
2965 }
Mike Stump11289f42009-09-09 15:08:12 +00002966
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002967 unsigned Index = AL.getAttributeSpellingListIndex();
2968 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002969 if (NewAttr)
2970 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002971}
2972
Erich Keane7963e8b2018-07-18 20:04:48 +00002973static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) {
2974 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
2975 if (!Error.empty()) {
2976 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
2977 << 0 /*'code-seg'*/;
2978 return false;
2979 }
2980 return true;
2981}
2982
2983CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range,
2984 StringRef Name,
2985 unsigned AttrSpellingListIndex) {
2986 // Explicit or partial specializations do not inherit
2987 // the code_seg attribute from the primary template.
2988 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2989 if (FD->isFunctionTemplateSpecialization())
2990 return nullptr;
2991 }
2992 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
2993 if (ExistingAttr->getName() == Name)
2994 return nullptr;
2995 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2996 << 0 /*codeseg*/;
2997 Diag(Range.getBegin(), diag::note_previous_attribute);
2998 return nullptr;
2999 }
3000 return ::new (Context) CodeSegAttr(Range, Context, Name,
3001 AttrSpellingListIndex);
3002}
3003
3004static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3005 StringRef Str;
3006 SourceLocation LiteralLoc;
3007 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3008 return;
3009 if (!checkCodeSegName(S, LiteralLoc, Str))
3010 return;
3011 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3012 if (!ExistingAttr->isImplicit()) {
3013 S.Diag(AL.getLoc(),
3014 ExistingAttr->getName() == Str
3015 ? diag::warn_duplicate_codeseg_attribute
3016 : diag::err_conflicting_codeseg_attribute);
3017 return;
3018 }
3019 D->dropAttr<CodeSegAttr>();
3020 }
3021 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str,
3022 AL.getAttributeSpellingListIndex()))
3023 D->addAttr(CSA);
3024}
3025
Erich Keane57e15cd2017-07-19 22:06:33 +00003026// Check for things we'd like to warn about. Multiversioning issues are
3027// handled later in the process, once we know how many exist.
3028bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3029 enum FirstParam { Unsupported, Duplicate };
3030 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00003031 for (auto Str : {"tune=", "fpmath="})
3032 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00003033 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3034 << Unsupported << None << Str;
3035
3036 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
3037
3038 if (!ParsedAttrs.Architecture.empty() &&
3039 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3040 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3041 << Unsupported << Architecture << ParsedAttrs.Architecture;
3042
3043 if (ParsedAttrs.DuplicateArchitecture)
3044 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3045 << Duplicate << None << "arch=";
3046
3047 for (const auto &Feature : ParsedAttrs.Features) {
3048 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3049 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3050 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3051 << Unsupported << None << CurFeature;
3052 }
3053
Erich Keane29636aa2018-02-16 17:31:59 +00003054 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003055}
3056
Erich Keanee891aa92018-07-13 15:07:47 +00003057static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003058 StringRef Str;
3059 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003060 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003061 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003062 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003063
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003064 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00003065 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003066 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00003067 D->addAttr(NewAttr);
3068}
3069
Erich Keanee891aa92018-07-13 15:07:47 +00003070static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003071 Expr *E = AL.getArgAsExpr(0);
3072 uint32_t VecWidth;
3073 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3074 AL.setInvalid();
3075 return;
3076 }
3077
3078 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3079 if (Existing && Existing->getVectorWidth() != VecWidth) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003080 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Craig Topper74c10e32018-07-09 19:00:16 +00003081 return;
3082 }
3083
3084 D->addAttr(::new (S.Context)
3085 MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth,
3086 AL.getAttributeSpellingListIndex()));
3087}
3088
Erich Keanee891aa92018-07-13 15:07:47 +00003089static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003090 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003091 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003092 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003093 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003094
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003095 // gcc only allows for simple identifiers. Since we support more than gcc, we
3096 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003097 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003098 if (DRE->hasQualifier())
3099 S.Diag(Loc, diag::warn_cleanup_ext);
3100 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3101 NI = DRE->getNameInfo();
3102 if (!FD) {
3103 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3104 << NI.getName();
3105 return;
3106 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003107 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003108 if (ULE->hasExplicitTemplateArgs())
3109 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003110 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3111 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003112 if (!FD) {
3113 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3114 << NI.getName();
3115 if (ULE->getType() == S.Context.OverloadTy)
3116 S.NoteAllOverloadCandidates(ULE);
3117 return;
3118 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003119 } else {
3120 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003121 return;
3122 }
3123
Anders Carlssond277d792009-01-31 01:16:18 +00003124 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003125 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3126 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003127 return;
3128 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003129
Anders Carlsson723f55d2009-02-07 23:16:50 +00003130 // We're currently more strict than GCC about what function types we accept.
3131 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003132 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003133 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003134 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3135 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003136 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3137 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003138 return;
3139 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003140
Michael Han99315932013-01-24 16:46:58 +00003141 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003142 CleanupAttr(AL.getRange(), S.Context, FD,
3143 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003144}
3145
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003146static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003147 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003148 if (!AL.isArgIdent(0)) {
3149 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003150 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003151 return;
3152 }
3153
3154 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003155 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003156 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3157 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003158 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003159 return;
3160 }
3161
3162 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003163 AL.getRange(), S.Context, ExtensibilityKind,
3164 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003165}
3166
Mike Stumpd3bb5572009-07-24 19:02:52 +00003167/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003168/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003169static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003170 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003171 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003172 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003173 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003174
Eric Christopherb64963e2015-08-13 21:34:35 +00003175 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003176 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003177
Eric Christopherb64963e2015-08-13 21:34:35 +00003178 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3179 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003180 !isCFStringType(Ty, S.Context) &&
3181 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003182 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003183 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003184 << "a string type" << IdxExpr->getSourceRange()
3185 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003186 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003187 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003188 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003189 if (!isNSStringType(Ty, S.Context) &&
3190 !isCFStringType(Ty, S.Context) &&
3191 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003192 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003193 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003194 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003195 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003196 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003197 }
3198
Joel E. Denny81508102018-03-13 14:51:22 +00003199 D->addAttr(::new (S.Context) FormatArgAttr(
3200 AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003201}
3202
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003203enum FormatAttrKind {
3204 CFStringFormat,
3205 NSStringFormat,
3206 StrftimeFormat,
3207 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003208 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003209 InvalidFormat
3210};
3211
3212/// getFormatAttrKind - Map from format attribute names to supported format
3213/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003214static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003215 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003216 // Check for formats that get handled specially.
3217 .Case("NSString", NSStringFormat)
3218 .Case("CFString", CFStringFormat)
3219 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003220
Mehdi Amini06d367c2016-10-24 20:39:34 +00003221 // Otherwise, check for supported formats.
3222 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3223 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3224 .Case("kprintf", SupportedFormat) // OpenBSD.
3225 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3226 .Case("os_trace", SupportedFormat)
3227 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003228
Mehdi Amini06d367c2016-10-24 20:39:34 +00003229 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3230 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003231}
3232
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003233/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003234/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003235static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003236 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003237 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003238 return;
3239 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003240
Aaron Ballman4a611152013-11-27 16:34:09 +00003241 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003242 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3243 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003244 return;
3245 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003246 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003247 if (S.Context.getAsArrayType(T))
3248 T = S.Context.getBaseElementType(T);
3249 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003250 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3251 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003252 return;
3253 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003254
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003255 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003256 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003257 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3258 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003259 return;
3260 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003261
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003262 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003263 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003264 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003265 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003266 return;
3267 }
Michael Han99315932013-01-24 16:46:58 +00003268 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003269 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3270 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003271}
3272
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003273FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3274 IdentifierInfo *Format, int FormatIdx,
3275 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003276 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003277 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003278 for (auto *F : D->specific_attrs<FormatAttr>()) {
3279 if (F->getType() == Format &&
3280 F->getFormatIdx() == FormatIdx &&
3281 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003282 // If we don't have a valid location for this attribute, adopt the
3283 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003284 if (F->getLocation().isInvalid())
3285 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003286 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003287 }
3288 }
3289
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003290 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3291 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003292}
3293
Mike Stumpd3bb5572009-07-24 19:02:52 +00003294/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003295/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003296static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003297 if (!AL.isArgIdent(0)) {
3298 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003299 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003300 return;
3301 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003302
Chandler Carruth743682b2010-11-16 08:35:43 +00003303 // In C++ the implicit 'this' function parameter also counts, and they are
3304 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003305 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003306 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003307
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003308 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003309 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003310
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003311 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003312 // If we've modified the string name, we need a new identifier for it.
3313 II = &S.Context.Idents.get(Format);
3314 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003315
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003316 // Check for supported formats.
3317 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003318
Chris Lattner12161d32010-03-22 21:08:50 +00003319 if (Kind == IgnoredFormat)
3320 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003321
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003322 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003323 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003324 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003325 return;
3326 }
3327
3328 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003329 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003330 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003331 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003332 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003333
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003334 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003335 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003336 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003337 return;
3338 }
3339
3340 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003341 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003342
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003343 if (HasImplicitThisParam) {
3344 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003345 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003346 diag::err_format_attribute_implicit_this_format_string)
3347 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003348 return;
3349 }
3350 ArgIdx--;
3351 }
Mike Stump11289f42009-09-09 15:08:12 +00003352
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003353 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003354 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003355
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003356 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003357 if (!isCFStringType(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 << "a CFString" << IdxExpr->getSourceRange()
3360 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003361 return;
3362 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003363 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003364 // FIXME: do we need to check if the type is NSString*? What are the
3365 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003366 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003367 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003368 << "an NSString" << IdxExpr->getSourceRange()
3369 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003370 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003371 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003372 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003373 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003374 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003375 << "a string type" << IdxExpr->getSourceRange()
3376 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003377 return;
3378 }
3379
3380 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003381 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003382 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003383 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003384 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003385
3386 // check if the function is variadic if the 3rd argument non-zero
3387 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003388 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003389 ++NumArgs; // +1 for ...
3390 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003391 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003392 return;
3393 }
3394 }
3395
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003396 // strftime requires FirstArg to be 0 because it doesn't read from any
3397 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003398 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003399 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003400 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003401 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003402 return;
3403 }
3404 // if 0 it disables parameter checking (to use with e.g. va_list)
3405 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003406 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003407 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003408 return;
3409 }
3410
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003411 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003412 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003413 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003414 if (NewAttr)
3415 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003416}
3417
Erich Keanee891aa92018-07-13 15:07:47 +00003418static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003419 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003420 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003421 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003422 if (TD && TD->getUnderlyingType()->isUnionType())
3423 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3424 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003425 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003426
3427 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003428 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3429 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003430 return;
3431 }
3432
John McCallf937c022011-10-07 06:10:15 +00003433 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003434 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003435 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003436 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003437 return;
3438 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003439
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003440 RecordDecl::field_iterator Field = RD->field_begin(),
3441 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003442 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003443 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003444 return;
3445 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003446
David Blaikie40ed2972012-06-06 20:45:41 +00003447 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003448 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003449 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003450 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003451 diag::warn_transparent_union_attribute_floating)
3452 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003453 return;
3454 }
3455
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003456 if (FirstType->isIncompleteType())
3457 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003458 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3459 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3460 for (; Field != FieldEnd; ++Field) {
3461 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003462 if (FieldType->isIncompleteType())
3463 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003464 // FIXME: this isn't fully correct; we also need to test whether the
3465 // members of the union would all have the same calling convention as the
3466 // first member of the union. Checking just the size and alignment isn't
3467 // sufficient (consider structs passed on the stack instead of in registers
3468 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003469 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003470 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003471 // Warn if we drop the attribute.
3472 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003473 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003474 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003475 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003476 diag::warn_transparent_union_attribute_field_size_align)
3477 << isSize << Field->getDeclName() << FieldBits;
3478 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003479 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003480 diag::note_transparent_union_first_field_size_align)
3481 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003482 return;
3483 }
3484 }
3485
Michael Han99315932013-01-24 16:46:58 +00003486 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003487 TransparentUnionAttr(AL.getRange(), S.Context,
3488 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003489}
3490
Erich Keanee891aa92018-07-13 15:07:47 +00003491static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003492 // Make sure that there is a string literal as the annotation's single
3493 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003494 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003495 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003496 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003497
3498 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003499 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3500 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003501 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003502 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003503
Michael Han99315932013-01-24 16:46:58 +00003504 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003505 AnnotateAttr(AL.getRange(), S.Context, Str,
3506 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003507}
3508
Erich Keanee891aa92018-07-13 15:07:47 +00003509static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003510 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3511 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003512}
3513
3514void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3515 unsigned SpellingListIndex) {
3516 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3517 SourceLocation AttrLoc = AttrRange.getBegin();
3518
3519 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003520 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003521 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003522 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003523 T = VD->getType();
3524 else
3525 llvm_unreachable("Unknown decl type for align_value");
3526
3527 if (!T->isDependentType() && !T->isAnyPointerType() &&
3528 !T->isReferenceType() && !T->isMemberPointerType()) {
3529 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3530 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3531 return;
3532 }
3533
3534 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003535 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003536 ExprResult ICE
3537 = VerifyIntegerConstantExpression(E, &Alignment,
3538 diag::err_align_value_attribute_argument_not_int,
3539 /*AllowFold*/ false);
3540 if (ICE.isInvalid())
3541 return;
3542
3543 if (!Alignment.isPowerOf2()) {
3544 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3545 << E->getSourceRange();
3546 return;
3547 }
3548
3549 D->addAttr(::new (Context)
3550 AlignValueAttr(AttrRange, Context, ICE.get(),
3551 SpellingListIndex));
3552 return;
3553 }
3554
3555 // Save dependent expressions in the AST to be instantiated.
3556 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003557}
3558
Erich Keanee891aa92018-07-13 15:07:47 +00003559static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003560 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003561 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003562 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003563 return;
3564 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003565
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003566 if (AL.getNumArgs() == 0) {
3567 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3568 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003569 return;
3570 }
3571
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003572 Expr *E = AL.getArgAsExpr(0);
3573 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3574 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003575 diag::err_pack_expansion_without_parameter_packs);
3576 return;
3577 }
3578
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003579 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003580 return;
3581
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003582 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3583 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003584}
3585
3586void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003587 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003588 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3589 SourceLocation AttrLoc = AttrRange.getBegin();
3590
Richard Smith1dba27c2013-01-29 09:02:09 +00003591 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003592 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003593 // C++11 [dcl.align]p1:
3594 // An alignment-specifier may be applied to a variable or to a class
3595 // data member, but it shall not be applied to a bit-field, a function
3596 // parameter, the formal parameter of a catch clause, or a variable
3597 // declared with the register storage class specifier. An
3598 // alignment-specifier may also be applied to the declaration of a class
3599 // or enumeration type.
3600 // C11 6.7.5/2:
3601 // An alignment attribute shall not be specified in a declaration of
3602 // a typedef, or a bit-field, or a function, or a parameter, or an
3603 // object declared with the register storage-class specifier.
3604 int DiagKind = -1;
3605 if (isa<ParmVarDecl>(D)) {
3606 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003607 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003608 if (VD->getStorageClass() == SC_Register)
3609 DiagKind = 1;
3610 if (VD->isExceptionVariable())
3611 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003612 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003613 if (FD->isBitField())
3614 DiagKind = 3;
3615 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003616 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003617 << (TmpAttr.isC11() ? ExpectedVariableOrField
3618 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003619 return;
3620 }
3621 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003622 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003623 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003624 return;
3625 }
3626 }
3627
Richard Smith90ae9672018-06-20 23:36:55 +00003628 if (E->isValueDependent()) {
3629 // We can't support a dependent alignment on a non-dependent type,
3630 // because we have no way to model that a type is "alignment-dependent"
3631 // but not dependent in any other way.
3632 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3633 if (!TND->getUnderlyingType()->isDependentType()) {
3634 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3635 << E->getSourceRange();
3636 return;
3637 }
3638 }
3639
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003640 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003641 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3642 AA->setPackExpansion(IsPackExpansion);
3643 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003644 return;
3645 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003646
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003647 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003648 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003649 ExprResult ICE
3650 = VerifyIntegerConstantExpression(E, &Alignment,
3651 diag::err_aligned_attribute_argument_not_int,
3652 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003653 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003654 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003655
David Majnemer0be6bd02015-07-26 09:02:21 +00003656 uint64_t AlignVal = Alignment.getZExtValue();
3657
Richard Smith848e1f12013-02-01 08:12:08 +00003658 // C++11 [dcl.align]p2:
3659 // -- if the constant expression evaluates to zero, the alignment
3660 // specifier shall have no effect
3661 // C11 6.7.5p6:
3662 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003663 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003664 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003665 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3666 << E->getSourceRange();
3667 return;
3668 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003669 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003670
David Majnemerabecae72014-02-12 20:36:10 +00003671 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003672 unsigned MaxValidAlignment =
3673 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3674 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003675 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003676 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3677 << E->getSourceRange();
3678 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003679 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003680
David Majnemer0be6bd02015-07-26 09:02:21 +00003681 if (Context.getTargetInfo().isTLSSupported()) {
3682 unsigned MaxTLSAlign =
3683 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3684 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003685 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003686 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3687 VD->getTLSKind() != VarDecl::TLS_None) {
3688 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3689 << (unsigned)AlignVal << VD << MaxTLSAlign;
3690 return;
3691 }
3692 }
3693
Richard Smith44c247f2013-02-22 08:32:16 +00003694 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003695 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003696 AA->setPackExpansion(IsPackExpansion);
3697 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003698}
3699
Michael Hanaf02bbe2013-02-01 01:19:17 +00003700void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003701 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003702 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003703 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003704 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3705 SpellingListIndex);
3706 AA->setPackExpansion(IsPackExpansion);
3707 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003708}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003709
Richard Smith848e1f12013-02-01 08:12:08 +00003710void Sema::CheckAlignasUnderalignment(Decl *D) {
3711 assert(D->hasAttrs() && "no attributes on decl");
3712
David Majnemer475b25e2015-01-21 10:54:38 +00003713 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003714 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003715 UnderlyingTy = DiagTy = VD->getType();
3716 } else {
3717 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003718 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003719 UnderlyingTy = ED->getIntegerType();
3720 }
3721 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003722 return;
3723
3724 // C++11 [dcl.align]p5, C11 6.7.5/4:
3725 // The combined effect of all alignment attributes in a declaration shall
3726 // not specify an alignment that is less strict than the alignment that
3727 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003728 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003729 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003730 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003731 if (I->isAlignmentDependent())
3732 return;
3733 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003734 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003735 Align = std::max(Align, I->getAlignment(Context));
3736 }
3737
3738 if (AlignasAttr && Align) {
3739 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003740 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003741 if (NaturalAlign > RequestedAlign)
3742 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003743 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003744 }
3745}
3746
David Majnemer2c4e00a2014-01-29 22:07:36 +00003747bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003748 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003749 MSInheritanceAttr::Spelling SemanticSpelling) {
3750 assert(RD->hasDefinition() && "RD has no definition!");
3751
David Majnemer98c9ee22014-02-07 00:43:07 +00003752 // We may not have seen base specifiers or any virtual methods yet. We will
3753 // have to wait until the record is defined to catch any mismatches.
3754 if (!RD->getDefinition()->isCompleteDefinition())
3755 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003756
David Majnemer98c9ee22014-02-07 00:43:07 +00003757 // The unspecified model never matches what a definition could need.
3758 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3759 return false;
3760
David Majnemer4bb09802014-02-10 19:50:15 +00003761 if (BestCase) {
3762 if (RD->calculateInheritanceModel() == SemanticSpelling)
3763 return false;
3764 } else {
3765 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3766 return false;
3767 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003768
3769 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3770 << 0 /*definition*/;
3771 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3772 << RD->getNameAsString();
3773 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003774}
3775
Alexey Bataevf278eb12015-11-19 10:13:11 +00003776/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3777/// attribute.
3778static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3779 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003780 IntegerMode = true;
3781 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003782 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003783 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003784 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003785 case 'Q':
3786 DestWidth = 8;
3787 break;
3788 case 'H':
3789 DestWidth = 16;
3790 break;
3791 case 'S':
3792 DestWidth = 32;
3793 break;
3794 case 'D':
3795 DestWidth = 64;
3796 break;
3797 case 'X':
3798 DestWidth = 96;
3799 break;
3800 case 'T':
3801 DestWidth = 128;
3802 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003803 }
3804 if (Str[1] == 'F') {
3805 IntegerMode = false;
3806 } else if (Str[1] == 'C') {
3807 IntegerMode = false;
3808 ComplexMode = true;
3809 } else if (Str[1] != 'I') {
3810 DestWidth = 0;
3811 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003812 break;
3813 case 4:
3814 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3815 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003816 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003817 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003818 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003819 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003820 break;
3821 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003822 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003823 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003824 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003825 case 11:
3826 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003827 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003828 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003829 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003830}
3831
3832/// handleModeAttr - This attribute modifies the width of a decl with primitive
3833/// type.
3834///
3835/// Despite what would be logical, the mode attribute is a decl attribute, not a
3836/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3837/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00003838static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003839 // This attribute isn't documented, but glibc uses it. It changes
3840 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003841 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003842 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
3843 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003844 return;
3845 }
3846
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003847 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003848
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003849 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003850}
3851
3852void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3853 unsigned SpellingListIndex, bool InInstantiation) {
3854 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003855 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003856 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003857
3858 unsigned DestWidth = 0;
3859 bool IntegerMode = true;
3860 bool ComplexMode = false;
3861 llvm::APInt VectorSize(64, 0);
3862 if (Str.size() >= 4 && Str[0] == 'V') {
3863 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3864 size_t StrSize = Str.size();
3865 size_t VectorStringLength = 0;
3866 while ((VectorStringLength + 1) < StrSize &&
3867 isdigit(Str[VectorStringLength + 1]))
3868 ++VectorStringLength;
3869 if (VectorStringLength &&
3870 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3871 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003872 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003873 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003874 // Avoid duplicate warning from template instantiation.
3875 if (!InInstantiation)
3876 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003877 } else {
3878 VectorSize = 0;
3879 }
3880 }
3881
3882 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003883 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3884
3885 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3886 // and friends, at least with glibc.
3887 // FIXME: Make sure floating-point mappings are accurate
3888 // FIXME: Support XF and TF types
3889 if (!DestWidth) {
3890 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3891 return;
3892 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003893
3894 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003895 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003896 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003897 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003898 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3899 // Try to get type from enum declaration, default to int.
3900 OldTy = ED->getIntegerType();
3901 if (OldTy.isNull())
3902 OldTy = Context.IntTy;
3903 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003904 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003905
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003906 if (OldTy->isDependentType()) {
3907 D->addAttr(::new (Context)
3908 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3909 return;
3910 }
3911
Alexey Bataev326057d2015-06-19 07:46:21 +00003912 // Base type can also be a vector type (see PR17453).
3913 // Distinguish between base type and base element type.
3914 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003915 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00003916 OldElemTy = VT->getElementType();
3917
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003918 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3919 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3920 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3921 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3922 VectorSize.getBoolValue()) {
3923 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3924 return;
3925 }
3926 bool IntegralOrAnyEnumType =
3927 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3928
3929 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3930 !IntegralOrAnyEnumType)
3931 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003932 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003933 if (!IntegralOrAnyEnumType)
3934 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003935 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003936 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003937 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003938 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003939 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003940 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003941 }
3942
Alexey Bataev326057d2015-06-19 07:46:21 +00003943 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003944
3945 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003946 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3947 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003948 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003949 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003950
Alexey Bataev326057d2015-06-19 07:46:21 +00003951 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003952 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003953 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003954 }
3955
Eli Friedman4735374e2009-03-03 06:41:03 +00003956 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003957 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003958 }
3959
3960 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003961 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003962 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3963 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003964 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003965 // Complex machine mode does not support base vector types.
3966 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003967 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003968 return;
3969 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003970 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00003971 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003972 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003973 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003974 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00003975 }
3976
3977 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003978 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003979 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003980 }
3981
3982 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003983 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003984 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003985 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003986 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003987 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003988 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003989
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003990 D->addAttr(::new (Context)
3991 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003992}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003993
Erich Keanee891aa92018-07-13 15:07:47 +00003994static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Han99315932013-01-24 16:46:58 +00003995 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003996 NoDebugAttr(AL.getRange(), S.Context,
3997 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003998}
3999
Paul Robinson30e41fb2014-12-15 18:57:28 +00004000AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00004001 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00004002 unsigned AttrSpellingListIndex) {
4003 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004004 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00004005 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4006 return nullptr;
4007 }
4008
4009 if (D->hasAttr<AlwaysInlineAttr>())
4010 return nullptr;
4011
4012 return ::new (Context) AlwaysInlineAttr(Range, Context,
4013 AttrSpellingListIndex);
4014}
4015
Erich Keane44bacdf2018-08-09 13:21:32 +00004016CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
4017 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004018 return nullptr;
4019
Erich Keane44bacdf2018-08-09 13:21:32 +00004020 return ::new (Context)
4021 CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004022}
4023
Erich Keane44bacdf2018-08-09 13:21:32 +00004024CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
4025 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
4026 return nullptr;
4027
4028 return ::new (Context)
4029 CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex());
4030}
4031
4032InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4033 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004034 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004035 // Attribute applies to Var but not any subclass of it (like ParmVar,
4036 // ImplicitParm or VarTemplateSpecialization).
4037 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004038 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4039 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4040 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004041 return nullptr;
4042 }
4043 // Attribute does not apply to non-static local variables.
4044 if (VD->hasLocalStorage()) {
4045 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4046 return nullptr;
4047 }
4048 }
4049
Erich Keane44bacdf2018-08-09 13:21:32 +00004050 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
4051 return nullptr;
4052
4053 return ::new (Context) InternalLinkageAttr(
4054 AL.getRange(), Context, AL.getAttributeSpellingListIndex());
4055}
4056InternalLinkageAttr *
4057Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4058 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4059 // Attribute applies to Var but not any subclass of it (like ParmVar,
4060 // ImplicitParm or VarTemplateSpecialization).
4061 if (VD->getKind() != Decl::Var) {
4062 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4063 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4064 : ExpectedVariableOrFunction);
4065 return nullptr;
4066 }
4067 // Attribute does not apply to non-static local variables.
4068 if (VD->hasLocalStorage()) {
4069 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4070 return nullptr;
4071 }
4072 }
4073
4074 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004075 return nullptr;
4076
4077 return ::new (Context)
Erich Keane44bacdf2018-08-09 13:21:32 +00004078 InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004079}
4080
Paul Robinson30e41fb2014-12-15 18:57:28 +00004081MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
4082 unsigned AttrSpellingListIndex) {
4083 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4084 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
4085 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4086 return nullptr;
4087 }
4088
4089 if (D->hasAttr<MinSizeAttr>())
4090 return nullptr;
4091
4092 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
4093}
4094
4095OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
4096 unsigned AttrSpellingListIndex) {
4097 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4098 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4099 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4100 D->dropAttr<AlwaysInlineAttr>();
4101 }
4102 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4103 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4104 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4105 D->dropAttr<MinSizeAttr>();
4106 }
4107
4108 if (D->hasAttr<OptimizeNoneAttr>())
4109 return nullptr;
4110
4111 return ::new (Context) OptimizeNoneAttr(Range, Context,
4112 AttrSpellingListIndex);
4113}
4114
Erich Keanee891aa92018-07-13 15:07:47 +00004115static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004116 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004117 return;
4118
Paul Robinson080b1f32015-01-13 18:34:56 +00004119 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004120 D, AL.getRange(), AL.getName(),
4121 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004122 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004123}
4124
Erich Keanee891aa92018-07-13 15:07:47 +00004125static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004126 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004127 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004128 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004129}
4130
Erich Keanee891aa92018-07-13 15:07:47 +00004131static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004132 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004133 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004134 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004135}
4136
Erich Keanee891aa92018-07-13 15:07:47 +00004137static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004138 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004139 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004140 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004141 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004142 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004143 return;
4144 }
4145 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004146 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004147}
4148
Erich Keanee891aa92018-07-13 15:07:47 +00004149static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004150 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004151 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004152 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004153 // extern __shared__ is only allowed on arrays with no length (e.g.
4154 // "int x[]").
Yaxun Liu97670892018-10-02 17:48:54 +00004155 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004156 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004157 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004158 return;
4159 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004160 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004161 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004162 << S.CurrentCUDATarget())
4163 return;
Justin Lebar10411012016-09-30 23:57:30 +00004164 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004165 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004166}
4167
Erich Keanee891aa92018-07-13 15:07:47 +00004168static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004169 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4170 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004171 return;
4172 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004173 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004174 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004175 SourceRange RTRange = FD->getReturnTypeSourceRange();
4176 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004177 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004178 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4179 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004180 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004181 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004182 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4183 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004184 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004185 << Method;
4186 return;
4187 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004188 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004189 }
4190 // Only warn for "inline" when compiling for host, to cut down on noise.
4191 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004192 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004193
Aaron Ballman3aff6332013-12-02 19:30:36 +00004194 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004195 CUDAGlobalAttr(AL.getRange(), S.Context,
4196 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004197}
4198
Erich Keanee891aa92018-07-13 15:07:47 +00004199static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004200 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004201 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004202 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004203 return;
4204 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004205
Michael Han99315932013-01-24 16:46:58 +00004206 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004207 GNUInlineAttr(AL.getRange(), S.Context,
4208 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004209}
4210
Erich Keanee891aa92018-07-13 15:07:47 +00004211static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004212 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004213
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004214 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004215 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4216 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004217 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004218 return;
4219
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004220 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004221 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004222 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004223 return;
4224 }
4225
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004226 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004227 case ParsedAttr::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004228 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004229 FastCallAttr(AL.getRange(), S.Context,
4230 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004231 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004232 case ParsedAttr::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004233 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004234 StdCallAttr(AL.getRange(), S.Context,
4235 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004236 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004237 case ParsedAttr::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004238 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004239 ThisCallAttr(AL.getRange(), S.Context,
4240 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004241 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004242 case ParsedAttr::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004243 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004244 CDeclAttr(AL.getRange(), S.Context,
4245 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004246 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004247 case ParsedAttr::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004248 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004249 PascalAttr(AL.getRange(), S.Context,
4250 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004251 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004252 case ParsedAttr::AT_SwiftCall:
John McCall477f2bb2016-03-03 06:39:32 +00004253 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004254 SwiftCallAttr(AL.getRange(), S.Context,
4255 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004256 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004257 case ParsedAttr::AT_VectorCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004258 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004259 VectorCallAttr(AL.getRange(), S.Context,
4260 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004261 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004262 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004263 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004264 MSABIAttr(AL.getRange(), S.Context,
4265 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004266 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004267 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004268 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004269 SysVABIAttr(AL.getRange(), S.Context,
4270 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004271 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004272 case ParsedAttr::AT_RegCall:
Erich Keane757d3172016-11-02 18:29:35 +00004273 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004274 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004275 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004276 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004277 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004278 switch (CC) {
4279 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004280 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004281 break;
4282 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004283 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004284 break;
4285 default:
4286 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004287 }
4288
Michael Han99315932013-01-24 16:46:58 +00004289 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004290 PcsAttr(AL.getRange(), S.Context, PCS,
4291 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004292 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004293 }
Erich Keanee891aa92018-07-13 15:07:47 +00004294 case ParsedAttr::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004295 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004296 IntelOclBiccAttr(AL.getRange(), S.Context,
4297 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004298 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004299 case ParsedAttr::AT_PreserveMost:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004300 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004301 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004302 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004303 case ParsedAttr::AT_PreserveAll:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004304 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004305 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004306 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004307 default:
4308 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004309 }
4310}
4311
Erich Keanee891aa92018-07-13 15:07:47 +00004312static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004313 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004314 return;
4315
4316 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004317 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004318 StringRef RuleName;
4319
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004320 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004321 return;
4322
4323 // FIXME: Warn if the rule name is unknown. This is tricky because only
4324 // clang-tidy knows about available rules.
4325 DiagnosticIdentifiers.push_back(RuleName);
4326 }
4327 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004328 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4329 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004330}
4331
Erich Keanee891aa92018-07-13 15:07:47 +00004332bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004333 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004334 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004335 return true;
4336
Erich Keaneb11ebc52017-09-27 03:20:13 +00004337 if (Attrs.hasProcessingCache()) {
4338 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004339 return false;
4340 }
4341
Erich Keanee891aa92018-07-13 15:07:47 +00004342 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004343 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4344 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004345 return true;
4346 }
4347
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004348 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004349 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004350 case ParsedAttr::AT_CDecl:
4351 CC = CC_C;
4352 break;
4353 case ParsedAttr::AT_FastCall:
4354 CC = CC_X86FastCall;
4355 break;
4356 case ParsedAttr::AT_StdCall:
4357 CC = CC_X86StdCall;
4358 break;
4359 case ParsedAttr::AT_ThisCall:
4360 CC = CC_X86ThisCall;
4361 break;
4362 case ParsedAttr::AT_Pascal:
4363 CC = CC_X86Pascal;
4364 break;
4365 case ParsedAttr::AT_SwiftCall:
4366 CC = CC_Swift;
4367 break;
4368 case ParsedAttr::AT_VectorCall:
4369 CC = CC_X86VectorCall;
4370 break;
4371 case ParsedAttr::AT_RegCall:
4372 CC = CC_X86RegCall;
4373 break;
4374 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004375 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004376 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004377 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004378 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004379 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4380 CC_C;
4381 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004382 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004383 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004384 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4385 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004386 return true;
4387 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004388 if (StrRef == "aapcs") {
4389 CC = CC_AAPCS;
4390 break;
4391 } else if (StrRef == "aapcs-vfp") {
4392 CC = CC_AAPCS_VFP;
4393 break;
4394 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004395
Erich Keaneb11ebc52017-09-27 03:20:13 +00004396 Attrs.setInvalid();
4397 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004398 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004399 }
Erich Keanee891aa92018-07-13 15:07:47 +00004400 case ParsedAttr::AT_IntelOclBicc:
4401 CC = CC_IntelOclBicc;
4402 break;
4403 case ParsedAttr::AT_PreserveMost:
4404 CC = CC_PreserveMost;
4405 break;
4406 case ParsedAttr::AT_PreserveAll:
4407 CC = CC_PreserveAll;
4408 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004409 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004410 }
4411
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004412 const TargetInfo &TI = Context.getTargetInfo();
4413 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004414 if (A != TargetInfo::CCCR_OK) {
4415 if (A == TargetInfo::CCCR_Warning)
Erich Keane44bacdf2018-08-09 13:21:32 +00004416 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004417
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004418 // This convention is not valid for the target. Use the default function or
4419 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004420 bool IsCXXMethod = false, IsVariadic = false;
4421 if (FD) {
4422 IsCXXMethod = FD->isCXXInstanceMember();
4423 IsVariadic = FD->isVariadic();
4424 }
4425 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004426 }
4427
Erich Keaneb11ebc52017-09-27 03:20:13 +00004428 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004429 return false;
4430}
4431
John McCall477f2bb2016-03-03 06:39:32 +00004432/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004433static bool isValidSwiftContextType(QualType Ty) {
4434 if (!Ty->hasPointerRepresentation())
4435 return Ty->isDependentType();
4436 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004437}
4438
4439/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004440static bool isValidSwiftIndirectResultType(QualType Ty) {
4441 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4442 Ty = PtrType->getPointeeType();
4443 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4444 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004445 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004446 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004447 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004448 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004449}
4450
4451/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004452static bool isValidSwiftErrorResultType(QualType Ty) {
4453 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4454 Ty = PtrType->getPointeeType();
4455 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4456 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004457 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004458 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004459 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004460 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004461 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004462 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004463}
4464
Erich Keanee891aa92018-07-13 15:07:47 +00004465static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004466 ParameterABI Abi) {
4467 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4468 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004469}
4470
4471void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4472 unsigned spellingIndex) {
4473
4474 QualType type = cast<ParmVarDecl>(D)->getType();
4475
4476 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4477 if (existingAttr->getABI() != abi) {
4478 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4479 << getParameterABISpelling(abi) << existingAttr;
4480 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4481 return;
4482 }
4483 }
4484
4485 switch (abi) {
4486 case ParameterABI::Ordinary:
4487 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4488
4489 case ParameterABI::SwiftContext:
4490 if (!isValidSwiftContextType(type)) {
4491 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4492 << getParameterABISpelling(abi)
4493 << /*pointer to pointer */ 0 << type;
4494 }
4495 D->addAttr(::new (Context)
4496 SwiftContextAttr(range, Context, spellingIndex));
4497 return;
4498
4499 case ParameterABI::SwiftErrorResult:
4500 if (!isValidSwiftErrorResultType(type)) {
4501 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4502 << getParameterABISpelling(abi)
4503 << /*pointer to pointer */ 1 << type;
4504 }
4505 D->addAttr(::new (Context)
4506 SwiftErrorResultAttr(range, Context, spellingIndex));
4507 return;
4508
4509 case ParameterABI::SwiftIndirectResult:
4510 if (!isValidSwiftIndirectResultType(type)) {
4511 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4512 << getParameterABISpelling(abi)
4513 << /*pointer*/ 0 << type;
4514 }
4515 D->addAttr(::new (Context)
4516 SwiftIndirectResultAttr(range, Context, spellingIndex));
4517 return;
4518 }
4519 llvm_unreachable("bad parameter ABI attribute");
4520}
4521
John McCall3882ace2011-01-05 12:14:39 +00004522/// Checks a regparm attribute, returning true if it is ill-formed and
4523/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004524bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004525 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004526 return true;
4527
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004528 if (!checkAttributeNumArgs(*this, AL, 1)) {
4529 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004530 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004531 }
Eli Friedman7044b762009-03-27 21:06:47 +00004532
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004533 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004534 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4535 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4536 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004537 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004538 }
4539
Douglas Gregore8bbc122011-09-02 00:18:52 +00004540 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004541 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004542 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004543 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004544 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004545 }
4546
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004547 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004548 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004549 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004550 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004551 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004552 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004553 }
4554
John McCall3882ace2011-01-05 12:14:39 +00004555 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004556}
4557
Artem Belevichbcec9da2016-06-06 22:54:57 +00004558// Checks whether an argument of launch_bounds attribute is
4559// acceptable, performs implicit conversion to Rvalue, and returns
4560// non-nullptr Expr result on success. Otherwise, it returns nullptr
4561// and may output an error.
4562static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004563 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004564 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004565 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004566 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004567
4568 // Accept template arguments for now as they depend on something else.
4569 // We'll get to check them when they eventually get instantiated.
4570 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004571 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004572
4573 llvm::APSInt I(64);
4574 if (!E->isIntegerConstantExpr(I, S.Context)) {
4575 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004576 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004577 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004578 }
4579 // Make sure we can fit it in 32 bits.
4580 if (!I.isIntN(32)) {
4581 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4582 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004583 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004584 }
4585 if (I < 0)
4586 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004587 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004588
Artem Belevichbcec9da2016-06-06 22:54:57 +00004589 // We may need to perform implicit conversion of the argument.
4590 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4591 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4592 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4593 assert(!ValArg.isInvalid() &&
4594 "Unexpected PerformCopyInitialization() failure.");
4595
4596 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004597}
4598
4599void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4600 Expr *MinBlocks, unsigned SpellingListIndex) {
4601 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4602 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004603 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4604 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004605 return;
4606
Artem Belevichbcec9da2016-06-06 22:54:57 +00004607 if (MinBlocks) {
4608 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4609 if (MinBlocks == nullptr)
4610 return;
4611 }
Artem Belevich7093e402015-04-21 22:55:54 +00004612
4613 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4614 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4615}
4616
Erich Keanee891aa92018-07-13 15:07:47 +00004617static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004618 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4619 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004620 return;
4621
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004622 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4623 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4624 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004625}
4626
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004627static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004628 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004629 if (!AL.isArgIdent(0)) {
4630 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004631 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004632 return;
4633 }
Joel E. Denny81508102018-03-13 14:51:22 +00004634
4635 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004636 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004637 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004638 return;
4639
Joel E. Denny81508102018-03-13 14:51:22 +00004640 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004641 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004642 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004643 return;
4644
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004645 bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004646 if (IsPointer) {
4647 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004648 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4649 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4650 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004651 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004652 }
4653
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004654 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
4655 AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx,
4656 TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004657}
4658
4659static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004660 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004661 if (!AL.isArgIdent(0)) {
4662 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004663 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004664 return;
4665 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004666
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004667 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004668 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004669
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004670 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004671 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004672 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004673 return;
4674 }
4675
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004676 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004677 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004678 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004679 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004680
Michael Han99315932013-01-24 16:46:58 +00004681 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004682 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004683 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004684 AL.getLayoutCompatible(),
4685 AL.getMustBeNull(),
4686 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004687}
4688
Erich Keanee891aa92018-07-13 15:07:47 +00004689static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004690 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004691
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004692 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004693 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004694 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004695 return;
4696
Joel E. Denny81508102018-03-13 14:51:22 +00004697 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
4698 D->addAttr(::new (S.Context) XRayLogArgsAttr(
4699 AL.getRange(), S.Context, ArgCount.getSourceIndex(),
4700 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004701}
4702
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004703//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004704// Checker-specific attribute handlers.
4705//===----------------------------------------------------------------------===//
4706
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004707static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4708 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004709}
4710
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004711static bool isValidSubjectOfNSAttribute(Sema &S, QualType QT) {
4712 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
4713 S.Context.isObjCNSObjectType(QT);
John McCalled433932011-01-25 03:31:58 +00004714}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004715
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004716static bool isValidSubjectOfCFAttribute(Sema &S, QualType QT) {
4717 return QT->isDependentType() || QT->isPointerType() ||
4718 isValidSubjectOfNSAttribute(S, QT);
John McCalled433932011-01-25 03:31:58 +00004719}
4720
Erich Keanee891aa92018-07-13 15:07:47 +00004721static void handleNSConsumedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004722 S.AddNSConsumedAttr(AL.getRange(), D, AL.getAttributeSpellingListIndex(),
Erich Keanee891aa92018-07-13 15:07:47 +00004723 AL.getKind() == ParsedAttr::AT_NSConsumed,
John McCall3b5a8f52016-03-03 00:10:03 +00004724 /*template instantiation*/ false);
4725}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004726
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004727void Sema::AddNSConsumedAttr(SourceRange AttrRange, Decl *D,
4728 unsigned SpellingIndex, bool IsNSConsumed,
4729 bool IsTemplateInstantiation) {
4730 const auto *Param = cast<ParmVarDecl>(D);
4731 bool TypeOK;
John McCall3b5a8f52016-03-03 00:10:03 +00004732
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004733 if (IsNSConsumed)
4734 TypeOK = isValidSubjectOfNSAttribute(*this, Param->getType());
4735 else
4736 TypeOK = isValidSubjectOfCFAttribute(*this, Param->getType());
John McCalled433932011-01-25 03:31:58 +00004737
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004738 if (!TypeOK) {
John McCall3b5a8f52016-03-03 00:10:03 +00004739 // These attributes are normally just advisory, but in ARC, ns_consumed
4740 // is significant. Allow non-dependent code to contain inappropriate
4741 // attributes even in ARC, but require template instantiations to be
4742 // set up correctly.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004743 Diag(D->getBeginLoc(), (IsTemplateInstantiation && IsNSConsumed &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004744 getLangOpts().ObjCAutoRefCount
4745 ? diag::err_ns_attribute_wrong_parameter_type
4746 : diag::warn_ns_attribute_wrong_parameter_type))
4747 << AttrRange << (IsNSConsumed ? "ns_consumed" : "cf_consumed")
4748 << (IsNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00004749 return;
4750 }
4751
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004752 if (IsNSConsumed)
4753 D->addAttr(::new (Context)
4754 NSConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004755 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004756 D->addAttr(::new (Context)
4757 CFConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004758}
4759
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004760bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4761 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004762 return false;
4763
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004764 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4765 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004766 return true;
4767}
4768
Chandler Carruthedc2c642011-07-02 00:01:44 +00004769static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004770 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004771 QualType ReturnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004772
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004773 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
4774 ReturnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004775 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Erich Keanee891aa92018-07-13 15:07:47 +00004776 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004777 return; // ignore: was handled as a type attribute
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004778 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D))
4779 ReturnType = PD->getType();
4780 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
4781 ReturnType = FD->getReturnType();
4782 else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
4783 ReturnType = Param->getType()->getPointeeType();
4784 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004785 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004786 << AL << /*pointer-to-CF*/ 2 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004787 return;
4788 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004789 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00004790 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004791 } else {
4792 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004793 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004794 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004795 case ParsedAttr::AT_NSReturnsRetained:
4796 case ParsedAttr::AT_NSReturnsAutoreleased:
4797 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004798 ExpectedDeclKind = ExpectedFunctionOrMethod;
4799 break;
4800
Erich Keanee891aa92018-07-13 15:07:47 +00004801 case ParsedAttr::AT_CFReturnsRetained:
4802 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004803 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4804 break;
4805 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004806 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004807 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004808 return;
4809 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004810
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004811 bool TypeOK;
4812 bool Cf;
4813 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004814 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004815 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004816 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
4817 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004818 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004819
4820 case ParsedAttr::AT_NSReturnsAutoreleased:
4821 case ParsedAttr::AT_NSReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004822 TypeOK = isValidSubjectOfNSAttribute(S, ReturnType);
4823 Cf = false;
John McCalled433932011-01-25 03:31:58 +00004824 break;
4825
Erich Keanee891aa92018-07-13 15:07:47 +00004826 case ParsedAttr::AT_CFReturnsRetained:
4827 case ParsedAttr::AT_CFReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004828 TypeOK = isValidSubjectOfCFAttribute(S, ReturnType);
4829 Cf = true;
John McCalled433932011-01-25 03:31:58 +00004830 break;
4831 }
4832
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004833 if (!TypeOK) {
4834 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00004835 return;
4836
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004837 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004838 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004839 << AL << /*pointer-to-CF*/ 2 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004840 } else {
4841 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4842 enum : unsigned {
4843 Function,
4844 Method,
4845 Property
4846 } SubjectKind = Function;
4847 if (isa<ObjCMethodDecl>(D))
4848 SubjectKind = Method;
4849 else if (isa<ObjCPropertyDecl>(D))
4850 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004851 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004852 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004853 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004854 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004855 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004856
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004857 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004858 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004859 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004860 case ParsedAttr::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004861 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004862 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004863 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004864 case ParsedAttr::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004865 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004866 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004867 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004868 case ParsedAttr::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004869 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004870 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004871 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004872 case ParsedAttr::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004873 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004874 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004875 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004876 case ParsedAttr::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004877 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004878 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004879 return;
4880 };
4881}
4882
John McCallcf166702011-07-22 08:53:00 +00004883static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004884 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004885 const int EP_ObjCMethod = 1;
4886 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00004887
Erich Keaneb11ebc52017-09-27 03:20:13 +00004888 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004889 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004890 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004891 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004892 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004893 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004894
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004895 if (!resultType->isReferenceType() &&
4896 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004897 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004898 << SourceRange(loc) << Attrs
4899 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
4900 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004901
4902 // Drop the attribute.
4903 return;
4904 }
4905
Nico Weber462fd1e2015-01-07 23:50:05 +00004906 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00004907 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004908}
4909
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004910static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004911 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004912 const auto *Method = cast<ObjCMethodDecl>(D);
4913
4914 const DeclContext *DC = Method->getDeclContext();
4915 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004916 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00004917 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004918 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4919 return;
4920 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004921 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004922 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00004923 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004924 return;
4925 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004926
4927 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
4928 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004929}
4930
Erich Keanee891aa92018-07-13 15:07:47 +00004931static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004932 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004933
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004934 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004935 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004936 return;
4937 }
John McCall28592582015-02-01 22:34:06 +00004938
4939 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004940 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00004941 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004942 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00004943 return;
4944 }
4945
4946 // Only allow 'cv void *'.
4947 QualType T = TD->getUnderlyingType();
4948 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004949 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00004950 return;
4951 }
4952 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004953
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004954 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004955 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
4956 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004957}
4958
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004959static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004960 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004961 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00004962
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004963 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004964 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004965 return;
4966 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004967
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004968 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004969 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
4970 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004971}
4972
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004973static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004974 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004975 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004976 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004977 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004978 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004979 return;
4980 }
4981 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004982 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004983 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004984 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004985 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004986 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004987 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004988 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004989}
4990
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004991static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004992 const ParsedAttr &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004993 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004994 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004995 IFace = CatDecl->getClassInterface();
4996 else
4997 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004998
4999 if (!IFace)
5000 return;
5001
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00005002 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00005003 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005004 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
5005 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005006}
5007
Erich Keanee891aa92018-07-13 15:07:47 +00005008static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005009 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005010 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005011 return;
5012 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005013 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005014 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005015 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005016}
5017
Nico Webera6916892016-06-10 18:53:04 +00005018// When a user wants to use objc_boxable with a union or struct
5019// but they don't have access to the declaration (legacy/third-party code)
5020// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00005021// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00005022static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00005023 bool notify = false;
5024
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005025 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00005026 if (RD && RD->getDefinition()) {
5027 RD = RD->getDefinition();
5028 notify = true;
5029 }
5030
5031 if (RD) {
5032 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005033 ObjCBoxableAttr(AL.getRange(), S.Context,
5034 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00005035 RD->addAttr(BoxableAttr);
5036 if (notify) {
5037 // we need to notify ASTReader/ASTWriter about
5038 // modification of existing declaration
5039 if (ASTMutationListener *L = S.getASTMutationListener())
5040 L->AddedAttributeToRecord(BoxableAttr, RD);
5041 }
5042 }
5043}
5044
Erich Keanee891aa92018-07-13 15:07:47 +00005045static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005046 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005047
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005048 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005049 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005050}
5051
Chandler Carruthedc2c642011-07-02 00:01:44 +00005052static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005053 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005054 const auto *VD = cast<ValueDecl>(D);
5055 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005056
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005057 if (!QT->isDependentType() &&
5058 !QT->isObjCLifetimeType()) {
5059 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5060 << QT;
John McCall31168b02011-06-15 23:02:42 +00005061 return;
5062 }
5063
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005064 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005065
5066 // If we have no lifetime yet, check the lifetime we're presumably
5067 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005068 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5069 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005070
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005071 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005072 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005073 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005074 "didn't infer lifetime for non-dependent type?");
5075 break;
5076
5077 case Qualifiers::OCL_Weak: // meaningful
5078 case Qualifiers::OCL_Strong: // meaningful
5079 break;
5080
5081 case Qualifiers::OCL_ExplicitNone:
5082 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005083 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5084 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005085 break;
5086 }
5087
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005088 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005089 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
5090 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00005091}
5092
Francois Picheta83957a2010-12-19 06:50:37 +00005093//===----------------------------------------------------------------------===//
5094// Microsoft specific attribute handlers.
5095//===----------------------------------------------------------------------===//
5096
Nico Weber88f5ed92016-09-13 18:55:26 +00005097UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
5098 unsigned AttrSpellingListIndex, StringRef Uuid) {
5099 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005100 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005101 return nullptr;
5102 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5103 Diag(Range.getBegin(), diag::note_previous_uuid);
5104 D->dropAttr<UuidAttr>();
5105 }
5106
5107 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5108}
5109
Erich Keanee891aa92018-07-13 15:07:47 +00005110static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005111 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005112 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005113 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005114 return;
5115 }
5116
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005117 StringRef StrRef;
5118 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005119 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005120 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005121
David Majnemer89085342013-08-09 08:56:20 +00005122 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5123 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005124 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5125 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005126
Reid Kleckner140c4a72013-05-17 14:04:52 +00005127 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005128 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005129 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005130 return;
5131 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005132
David Majnemer89085342013-08-09 08:56:20 +00005133 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005134 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005135 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005136 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005137 return;
5138 }
David Majnemer89085342013-08-09 08:56:20 +00005139 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005140 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005141 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005142 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005143 }
Francois Picheta83957a2010-12-19 06:50:37 +00005144
Nico Weber469891e2017-05-05 17:05:56 +00005145 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5146 // the only thing in the [] list, the [] too), and add an insertion of
5147 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5148 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005149 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005150 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005151 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5152 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005153
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005154 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5155 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005156 if (UA)
5157 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005158}
5159
Erich Keanee891aa92018-07-13 15:07:47 +00005160static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005161 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005162 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005163 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005164 return;
5165 }
5166 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005167 D, AL.getRange(), /*BestCase=*/true,
5168 AL.getAttributeSpellingListIndex(),
5169 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005170 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005171 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005172 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5173 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005174}
5175
Erich Keanee891aa92018-07-13 15:07:47 +00005176static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005177 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005178 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005179 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005180 return;
5181 }
5182 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005183 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005184 return;
5185 }
5186 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005187 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005188 return;
5189 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005190 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5191 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005192}
5193
Erich Keanee891aa92018-07-13 15:07:47 +00005194static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005195 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005196 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005197 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005198 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005199 return;
5200 Tags.push_back(Tag);
5201 }
5202
5203 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5204 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005205 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005206 return;
5207 }
5208 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005209 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005210 return;
5211 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005212 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005213 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005214 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005215 return;
5216
5217 // Store tags sorted and without duplicates.
Fangrui Song55fab262018-09-26 22:16:28 +00005218 llvm::sort(Tags);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005219 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5220
5221 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005222 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5223 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005224}
5225
Erich Keanee891aa92018-07-13 15:07:47 +00005226static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005227 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005228 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005229 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005230 return;
5231 }
5232
5233 StringRef Str;
5234 SourceLocation ArgLoc;
5235
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005236 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005237 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005238 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005239 return;
5240
5241 ARMInterruptAttr::InterruptType Kind;
5242 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005243 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5244 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005245 return;
5246 }
5247
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005248 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005249 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005250 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005251}
5252
Erich Keanee891aa92018-07-13 15:07:47 +00005253static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005254 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005255 return;
5256
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005257 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005258 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5259 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005260 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005261 }
5262
5263 // FIXME: Check for decl - it should be void ()(void).
5264
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005265 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005266 llvm::APSInt NumParams(32);
5267 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005268 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005269 << AL << AANT_ArgumentIntegerConstant
5270 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005271 return;
5272 }
5273
5274 unsigned Num = NumParams.getLimitedValue(255);
5275 if ((Num & 1) || Num > 30) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005276 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005277 << AL << (int)NumParams.getSExtValue()
5278 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005279 return;
5280 }
5281
Aaron Ballman36a53502014-01-16 13:03:14 +00005282 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005283 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5284 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005285 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005286}
5287
Erich Keanee891aa92018-07-13 15:07:47 +00005288static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005289 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005290 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005291 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005292 return;
5293 }
5294
5295 StringRef Str;
5296 SourceLocation ArgLoc;
5297
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005298 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005299 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005300 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005301 return;
5302
5303 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5304 // a) Must be a function.
5305 // b) Must have no parameters.
5306 // c) Must have the 'void' return type.
5307 // d) Cannot have the 'mips16' attribute, as that instruction set
5308 // lacks the 'eret' instruction.
5309 // e) The attribute itself must either have no argument or one of the
5310 // valid interrupt types, see [MipsInterruptDocs].
5311
5312 if (!isFunctionOrMethod(D)) {
5313 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5314 << "'interrupt'" << ExpectedFunctionOrMethod;
5315 return;
5316 }
5317
5318 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5319 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5320 << 0;
5321 return;
5322 }
5323
5324 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5325 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5326 << 1;
5327 return;
5328 }
5329
Erich Keane44bacdf2018-08-09 13:21:32 +00005330 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005331 return;
5332
5333 MipsInterruptAttr::InterruptType Kind;
5334 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005335 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005336 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005337 return;
5338 }
5339
5340 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005341 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005342}
5343
Erich Keanee891aa92018-07-13 15:07:47 +00005344static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005345 // Semantic checks for a function with the 'interrupt' attribute.
5346 // a) Must be a function.
5347 // b) Must have the 'void' return type.
5348 // c) Must take 1 or 2 arguments.
5349 // d) The 1st argument must be a pointer.
5350 // e) The 2nd argument (if any) must be an unsigned integer.
5351 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5352 CXXMethodDecl::isStaticOverloadedOperator(
5353 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005354 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005355 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005356 return;
5357 }
5358 // Interrupt handler must have void return type.
5359 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5360 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5361 diag::err_anyx86_interrupt_attribute)
5362 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5363 ? 0
5364 : 1)
5365 << 0;
5366 return;
5367 }
5368 // Interrupt handler must have 1 or 2 parameters.
5369 unsigned NumParams = getFunctionOrMethodNumParams(D);
5370 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005371 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005372 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5373 ? 0
5374 : 1)
5375 << 1;
5376 return;
5377 }
5378 // The first argument must be a pointer.
5379 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5380 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5381 diag::err_anyx86_interrupt_attribute)
5382 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5383 ? 0
5384 : 1)
5385 << 2;
5386 return;
5387 }
5388 // The second argument, if present, must be an unsigned integer.
5389 unsigned TypeSize =
5390 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5391 ? 64
5392 : 32;
5393 if (NumParams == 2 &&
5394 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5395 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5396 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5397 diag::err_anyx86_interrupt_attribute)
5398 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5399 ? 0
5400 : 1)
5401 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5402 return;
5403 }
5404 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005405 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005406 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5407}
5408
Erich Keanee891aa92018-07-13 15:07:47 +00005409static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005410 if (!isFunctionOrMethod(D)) {
5411 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5412 << "'interrupt'" << ExpectedFunction;
5413 return;
5414 }
5415
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005416 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005417 return;
5418
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005419 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005420}
5421
Erich Keanee891aa92018-07-13 15:07:47 +00005422static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005423 if (!isFunctionOrMethod(D)) {
5424 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5425 << "'signal'" << ExpectedFunction;
5426 return;
5427 }
5428
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005429 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005430 return;
5431
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005432 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005433}
5434
Ana Pazos1eee1b72018-07-26 17:37:45 +00005435
5436static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5437 const ParsedAttr &AL) {
5438 // Warn about repeated attributes.
5439 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5440 S.Diag(AL.getRange().getBegin(),
5441 diag::warn_riscv_repeated_interrupt_attribute);
5442 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5443 return;
5444 }
5445
5446 // Check the attribute argument. Argument is optional.
5447 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5448 return;
5449
5450 StringRef Str;
5451 SourceLocation ArgLoc;
5452
5453 // 'machine'is the default interrupt mode.
5454 if (AL.getNumArgs() == 0)
5455 Str = "machine";
5456 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5457 return;
5458
5459 // Semantic checks for a function with the 'interrupt' attribute:
5460 // - Must be a function.
5461 // - Must have no parameters.
5462 // - Must have the 'void' return type.
5463 // - The attribute itself must either have no argument or one of the
5464 // valid interrupt types, see [RISCVInterruptDocs].
5465
5466 if (D->getFunctionType() == nullptr) {
5467 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5468 << "'interrupt'" << ExpectedFunction;
5469 return;
5470 }
5471
5472 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5473 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 0;
5474 return;
5475 }
5476
5477 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5478 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 1;
5479 return;
5480 }
5481
5482 RISCVInterruptAttr::InterruptType Kind;
5483 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005484 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5485 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005486 return;
5487 }
5488
5489 D->addAttr(::new (S.Context) RISCVInterruptAttr(
5490 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
5491}
5492
Erich Keanee891aa92018-07-13 15:07:47 +00005493static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005494 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005495 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5496 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005497 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005498 break;
5499 case llvm::Triple::mipsel:
5500 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005501 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005502 break;
5503 case llvm::Triple::x86:
5504 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005505 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005506 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005507 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005508 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005509 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005510 case llvm::Triple::riscv32:
5511 case llvm::Triple::riscv64:
5512 handleRISCVInterruptAttr(S, D, AL);
5513 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005514 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005515 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005516 break;
5517 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005518}
5519
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005520static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005521 const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005522 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005523 Expr *MinExpr = AL.getArgAsExpr(0);
5524 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005525 return;
5526
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005527 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005528 Expr *MaxExpr = AL.getArgAsExpr(1);
5529 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005530 return;
5531
5532 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005533 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005534 return;
5535 }
5536 if (Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005537 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005538 return;
5539 }
5540
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005541 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005542 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5543 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005544}
5545
Erich Keanee891aa92018-07-13 15:07:47 +00005546static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005547 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005548 Expr *MinExpr = AL.getArgAsExpr(0);
5549 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005550 return;
5551
5552 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005553 if (AL.getNumArgs() == 2) {
5554 Expr *MaxExpr = AL.getArgAsExpr(1);
5555 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005556 return;
5557 }
5558
5559 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005560 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005561 return;
5562 }
5563 if (Max != 0 && Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005564 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005565 return;
5566 }
5567
5568 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005569 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5570 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005571}
5572
Erich Keanee891aa92018-07-13 15:07:47 +00005573static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005574 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005575 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5576 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005577 return;
5578
5579 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005580 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5581 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005582}
5583
Erich Keanee891aa92018-07-13 15:07:47 +00005584static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005585 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005586 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5587 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005588 return;
5589
5590 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005591 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5592 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005593}
5594
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005595static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005596 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005597 // If we try to apply it to a function pointer, don't warn, but don't
5598 // do anything, either. It doesn't matter anyway, because there's nothing
5599 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005600 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005601 if (VD && VD->getType()->isFunctionPointerType())
5602 return;
5603 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005604 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005605 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5606 TD->getUnderlyingType()->isFunctionType()))
5607 return;
5608 // Attribute can only be applied to function types.
5609 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005610 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005611 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005612 return;
5613 }
5614
Aaron Ballman36a53502014-01-16 13:03:14 +00005615 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005616 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5617 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005618}
5619
Erich Keanee891aa92018-07-13 15:07:47 +00005620static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005621 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005622 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5623 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005624 return;
5625
5626 // TODO: Investigate what happens with the next major version of MSVC.
5627 if (Version != LangOptions::MSVC2015) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005628 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005629 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005630 return;
5631 }
5632
5633 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005634 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5635 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005636}
5637
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005638DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5639 unsigned AttrSpellingListIndex) {
5640 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005641 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005642 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005643 }
5644
5645 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005646 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005647
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005648 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005649}
5650
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005651DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5652 unsigned AttrSpellingListIndex) {
5653 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005654 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005655 D->dropAttr<DLLImportAttr>();
5656 }
5657
5658 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005659 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005660
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005661 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005662}
5663
Erich Keanee891aa92018-07-13 15:07:47 +00005664static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005665 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5666 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005667 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00005668 return;
5669 }
5670
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005671 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00005672 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005673 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5674 // MinGW doesn't allow dllimport on inline functions.
5675 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00005676 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005677 return;
5678 }
5679 }
5680
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005681 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005682 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5683 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005684 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00005685 return;
5686 }
5687 }
5688
Hans Wennborge82f19c2014-06-24 23:57:05 +00005689 unsigned Index = A.getAttributeSpellingListIndex();
Erich Keanee891aa92018-07-13 15:07:47 +00005690 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Hans Wennborge82f19c2014-06-24 23:57:05 +00005691 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5692 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005693 if (NewAttr)
5694 D->addAttr(NewAttr);
5695}
5696
David Majnemer2c4e00a2014-01-29 22:07:36 +00005697MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00005698Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00005699 unsigned AttrSpellingListIndex,
5700 MSInheritanceAttr::Spelling SemanticSpelling) {
5701 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
5702 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00005703 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005704 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
5705 << 1 /*previous declaration*/;
5706 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
5707 D->dropAttr<MSInheritanceAttr>();
5708 }
5709
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005710 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005711 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00005712 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
5713 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005714 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005715 }
5716 } else {
5717 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
5718 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5719 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005720 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005721 }
5722 if (RD->getDescribedClassTemplate()) {
5723 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5724 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005725 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005726 }
5727 }
5728
5729 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00005730 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005731}
5732
Erich Keanee891aa92018-07-13 15:07:47 +00005733static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005734 // The capability attributes take a single string parameter for the name of
5735 // the capability they represent. The lockable attribute does not take any
5736 // parameters. However, semantically, both attributes represent the same
5737 // concept, and so they use the same semantic attribute. Eventually, the
5738 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00005739 //
Alp Toker958027b2014-07-14 19:42:55 +00005740 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00005741 // literal will be considered a "mutex."
5742 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005743 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00005744 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005745 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005746 return;
5747
Aaron Ballman6c810072014-03-05 21:47:13 +00005748 // Currently, there are only two names allowed for a capability: role and
5749 // mutex (case insensitive). Diagnose other capability names.
5750 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
5751 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
5752
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005753 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
5754 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005755}
5756
Erich Keanee891aa92018-07-13 15:07:47 +00005757static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00005758 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005759 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00005760 return;
5761
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005762 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00005763 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005764 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005765}
5766
5767static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005768 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005769 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005770 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005771 return;
5772
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005773 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005774 S.Context,
5775 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005776 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005777}
5778
5779static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005780 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005781 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005782 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005783 return;
5784
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005785 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005786 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005787 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005788 Args.data(),
5789 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005790 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005791}
5792
5793static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005794 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005795 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005796 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005797 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005798
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005799 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005800 AL.getRange(), S.Context, Args.data(), Args.size(),
5801 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005802}
5803
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005804static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005805 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005806 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005807 return;
5808
5809 // check that all arguments are lockable objects
5810 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005811 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005812 if (Args.empty())
5813 return;
5814
5815 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005816 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
5817 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005818
5819 D->addAttr(RCA);
5820}
5821
Erich Keanee891aa92018-07-13 15:07:47 +00005822static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005823 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005824 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005825 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00005826 // Do not want to attach the attribute to the namespace because that will
5827 // cause confusing diagnostic reports for uses of declarations within the
5828 // namespace.
5829 return;
5830 }
5831 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005832
Manman Renc7890fe2016-03-16 18:50:49 +00005833 // Handle the cases where the attribute has a text message.
5834 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005835 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
5836 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00005837 return;
5838
5839 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005840 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
5841 checkAttributeAtMostNumArgs(S, AL, 1);
5842 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
Aaron Ballmanad672ff2018-10-24 12:26:23 +00005843 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
5844 return;
5845
5846 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
5847 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
5848
5849 D->addAttr(::new (S.Context)
5850 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005851 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005852}
5853
5854static bool isGlobalVar(const Decl *D) {
5855 if (const auto *S = dyn_cast<VarDecl>(D))
5856 return S->hasGlobalStorage();
5857 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00005858}
5859
Erich Keanee891aa92018-07-13 15:07:47 +00005860static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005861 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00005862 return;
5863
Benjamin Kramer1b582012016-02-13 18:11:49 +00005864 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00005865
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005866 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00005867 StringRef SanitizerName;
5868 SourceLocation LiteralLoc;
5869
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005870 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00005871 return;
5872
5873 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
5874 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005875 else if (isGlobalVar(D) && SanitizerName != "address")
5876 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005877 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00005878 Sanitizers.push_back(SanitizerName);
5879 }
5880
5881 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005882 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
5883 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005884}
5885
5886static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005887 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005888 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00005889 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005890 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
5891 .Case("no_address_safety_analysis", "address")
5892 .Case("no_sanitize_address", "address")
5893 .Case("no_sanitize_thread", "thread")
5894 .Case("no_sanitize_memory", "memory");
5895 if (isGlobalVar(D) && SanitizerName != "address")
5896 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005897 << AL << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00005898 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005899 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
5900 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005901}
5902
Erich Keanee891aa92018-07-13 15:07:47 +00005903static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005904 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005905 D->addAttr(Internal);
5906}
5907
Erich Keanee891aa92018-07-13 15:07:47 +00005908static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005909 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005910 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00005911 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005912 else
Erich Keane44bacdf2018-08-09 13:21:32 +00005913 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
5914 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005915}
5916
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005917/// Handles semantic checking for features that are common to all attributes,
5918/// such as checking whether a parameter was properly specified, or the correct
5919/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005920static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005921 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005922 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00005923 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005924 //
5925 // We also bail on unknown and ignored attributes because those are handled
5926 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00005927 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005928 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005929 // Check whether the attribute requires specific language extensions to be
5930 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005931 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00005932 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00005933 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005934 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00005935 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005936 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00005937 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005938
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005939 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005940 // If there are no optional arguments, then checking for the argument count
5941 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005942 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005943 return true;
5944 } else {
5945 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005946 if (AL.getMinArgs() &&
5947 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005948 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005949 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
5950 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005951 return true;
5952 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005953
Oren Ben Simhon220671a2018-03-17 13:31:35 +00005954 if (S.CheckAttrTarget(AL))
5955 return true;
5956
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005957 return false;
5958}
5959
Erich Keanee891aa92018-07-13 15:07:47 +00005960static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005961 if (D->isInvalidDecl())
5962 return;
5963
5964 // Check if there is only one access qualifier.
5965 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00005966 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
5967 AL.getSemanticSpelling()) {
5968 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
5969 << AL.getName()->getName() << AL.getRange();
5970 } else {
5971 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
5972 << D->getSourceRange();
5973 D->setInvalidDecl(true);
5974 return;
5975 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00005976 }
5977
5978 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
5979 // image object can be read and written.
5980 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
5981 // object. Using the read_write (or __read_write) qualifier with the pipe
5982 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005983 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005984 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005985 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005986 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005987 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00005988 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00005989 D->setInvalidDecl(true);
5990 return;
5991 }
5992 }
5993 }
5994
5995 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005996 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00005997}
5998
Erik Pilkington5a559e62018-08-21 17:24:06 +00005999static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006000 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00006001 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
6002 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
6003 return;
6004 }
6005
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006006 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00006007 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006008 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00006009 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00006010}
6011
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00006012//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006013// Top Level Sema Entry Points
6014//===----------------------------------------------------------------------===//
6015
Richard Smithf8a75c32013-08-29 00:47:48 +00006016/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
6017/// the attribute applies to decls. If the attribute is a type attribute, just
6018/// silently ignore it if a GNU attribute.
6019static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006020 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00006021 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00006022 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00006023 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00006024
Richard Smithf8a75c32013-08-29 00:47:48 +00006025 // Ignore C++11 attributes on declarator chunks: they appertain to the type
6026 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006027 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00006028 return;
6029
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006030 // Unknown attributes are automatically warned on. Target-specific attributes
6031 // which do not apply to the current target architecture are treated as
6032 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006033 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006034 !AL.existsInTarget(S.Context.getTargetInfo())) {
6035 S.Diag(AL.getLoc(), AL.isDeclspecAttribute()
Erich Keanec480f302018-07-12 21:09:05 +00006036 ? diag::warn_unhandled_ms_attribute_ignored
6037 : diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006038 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006039 return;
6040 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006041
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006042 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006043 return;
6044
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006045 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006046 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006047 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00006048 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006049 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00006050 break;
6051 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006052 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00006053 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006054 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006055 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006056 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006057 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006058 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006059 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006060 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006061 case ParsedAttr::AT_DLLExport:
6062 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006063 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006064 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006065 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006066 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006067 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006068 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006069 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006070 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006071 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006072 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006073 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006074 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006075 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006076 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006077 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006078 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006079 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006080 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006081 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006082 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006083 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006084 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006085 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006086 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006087 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006088 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006089 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006090 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006091 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006092 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006093 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006094 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006095 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006096 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006097 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006098 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006099 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006100 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006101 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006102 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006103 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006104 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006105 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006106 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006107 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006108 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006109 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006110 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006111 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006112 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006113 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006114 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006115 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006116 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006117 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006118 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006119 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006120 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006121 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006122 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006123 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006124 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006125 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006126 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006127 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006128 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006129 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006130 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006131 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006132 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006133 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006134 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006135 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006136 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006137 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006138 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006139 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006140 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006141 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006142 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006143 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006144 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006145 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006146 case ParsedAttr::AT_CPUDispatch:
6147 case ParsedAttr::AT_CPUSpecific:
6148 handleCPUSpecificAttr(S, D, AL);
6149 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006150 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006151 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006152 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006153 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006154 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006155 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006156 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006157 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006158 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006159 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006160 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006161 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006162 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006163 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006164 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006165 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006166 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006167 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006168 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006169 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006170 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006171 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006172 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006173 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006174 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006175 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006176 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006177 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006178 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006179 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006180 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006181 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006182 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006183 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006184 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006185 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006186 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006187 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006188 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006189 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006190 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006191 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006192 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006193 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006194 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006195 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006196 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006197 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006198 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006199 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006200 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006201 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006202 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006203 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006204 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006205 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006206 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006207 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006208 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006209 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006210 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006211 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006212 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006213 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006214 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006215 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006216 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006217 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006218 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006219 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006220 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006221 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006222 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006223 case ParsedAttr::AT_LifetimeBound:
6224 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6225 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006226 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006227 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006228 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006229 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006230 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006231 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006232 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006233 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006234 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006235 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006236 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006237 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006238 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006239 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006240 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006241 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006242 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6243 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006244 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006245 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006246 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006247 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006248 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006249 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006250 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006251 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006252 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006253 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006254 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006255 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006256 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006257 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006258 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006259 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006260 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006261 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006262 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006263 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006264 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006265 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006266 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006267 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006268 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006269 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006270 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006271 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006272 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006273 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006274 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006275 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006276 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006277 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006278 handleNoCfCheckAttr(S, D, AL);
6279 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006280 case ParsedAttr::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006281 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006282 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006283 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006284 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006285 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006286 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006287 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006288 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006289 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006290 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006291 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006292 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006293 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006294 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006295 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006296 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006297 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006298 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006299 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006300 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006301 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006302 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006303 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006304 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006305 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006306 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006307 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006308 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006309 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006310 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006311 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006312 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006313 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006314 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006315 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006316 case ParsedAttr::AT_ObjCRuntimeVisible:
6317 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6318 break;
6319 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006320 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006321 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006322 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006323 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6324 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006325 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006326 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006327 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6328 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006329 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006330 case ParsedAttr::AT_CFConsumed:
6331 case ParsedAttr::AT_NSConsumed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006332 handleNSConsumedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006333 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006334 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006335 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006336 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006337 case ParsedAttr::AT_NSReturnsAutoreleased:
6338 case ParsedAttr::AT_NSReturnsNotRetained:
6339 case ParsedAttr::AT_CFReturnsNotRetained:
6340 case ParsedAttr::AT_NSReturnsRetained:
6341 case ParsedAttr::AT_CFReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006342 handleNSReturnsRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006343 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006344 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006345 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006346 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006347 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006348 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006349 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006350 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006351 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006352 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006353 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006354 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006355 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006356 case ParsedAttr::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006357 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006358 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006359 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006360 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006361 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006362 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006363 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006364 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006365 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006366 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006367 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006368 case ParsedAttr::AT_CodeSeg:
6369 handleCodeSegAttr(S, D, AL);
6370 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006371 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006372 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006373 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006374 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006375 handleMinVectorWidthAttr(S, D, AL);
6376 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006377 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006378 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006379 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006380 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006381 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006382 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006383 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006384 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006385 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006386 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006387 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006388 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006389 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006390 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006391 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006392 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006393 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006394 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006395 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006396 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006397 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006398 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006399 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006400 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006401 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006402 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6403 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006404 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006405 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00006406 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
6407 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006408 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006409 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006410 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006411 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006412 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006413 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006414 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006415 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006416 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006417 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006418 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006419 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006420 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006421 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006422 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006423 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006424 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006425 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006426 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006427 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006428 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006429 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006430 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006431 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006432 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006433 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006434 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006435 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006436 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006437 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006438 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006439 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006440 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006441 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006442 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006443 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006444 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006445 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006446 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006447 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006448 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006449 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006450 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006451 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006452 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006453 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006454 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006455 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006456 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006457 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006458 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006459 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006460 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006461 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006462 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006463 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006464 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006465 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006466 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006467 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006468 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006469 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006470 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006471 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006472 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006473 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006474 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006475 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006476 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006477 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006478 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00006479 // Interacts with -fstack-protector options.
6480 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
6481 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006482 case ParsedAttr::AT_StdCall:
6483 case ParsedAttr::AT_CDecl:
6484 case ParsedAttr::AT_FastCall:
6485 case ParsedAttr::AT_ThisCall:
6486 case ParsedAttr::AT_Pascal:
6487 case ParsedAttr::AT_RegCall:
6488 case ParsedAttr::AT_SwiftCall:
6489 case ParsedAttr::AT_VectorCall:
6490 case ParsedAttr::AT_MSABI:
6491 case ParsedAttr::AT_SysVABI:
6492 case ParsedAttr::AT_Pcs:
6493 case ParsedAttr::AT_IntelOclBicc:
6494 case ParsedAttr::AT_PreserveMost:
6495 case ParsedAttr::AT_PreserveAll:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006496 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006497 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006498 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006499 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006500 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006501 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006502 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006503 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006504 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006505 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006506 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006507 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006508 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006509 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006510 case ParsedAttr::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006511 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006512 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006513 case ParsedAttr::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006514 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006515 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006516 case ParsedAttr::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006517 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006518 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006519 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006520 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006521 break;
Louis Dionned2695792018-10-04 15:49:42 +00006522 case ParsedAttr::AT_ExcludeFromExplicitInstantiation:
6523 handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL);
6524 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006525 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006526 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006527 break;
John McCall8d32c052012-05-22 21:28:12 +00006528
6529 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006530 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006531 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006532 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006533 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006534 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006535 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006536 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006537 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006538 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006539 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006540 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00006541 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006542 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006543 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00006544 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006545 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006546 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00006547 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006548 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006549 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006550 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006551 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006552 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006553 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006554 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006555 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006556 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00006557
Erich Keanee891aa92018-07-13 15:07:47 +00006558 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006559 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006560 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006561
6562 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006563 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006564 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006565 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006566 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006567 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006568 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006569 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006570 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006571 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006572 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006573 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006574 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006575 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006576 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006577 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006578 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006579 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00006580 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006581 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006582 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00006583 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006584 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006585 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00006586 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006587 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006588 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006589 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006590 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006591 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006592 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006593 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006594 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006595 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006596 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006597 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006598 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006599 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006600 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006601 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006602 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006603 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006604 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006605 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006606 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006607 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006608 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006609 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006610 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006611
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006612 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006613 case ParsedAttr::AT_Capability:
6614 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006615 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006616 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006617 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006618 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006619 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006620
Erich Keanee891aa92018-07-13 15:07:47 +00006621 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006622 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006623 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006624 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006625 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006626 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006627 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006628 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006629 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006630 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006631 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006632 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006633
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00006634 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006635 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006636 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006637 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006638 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006639 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006640 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006641 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006642 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006643 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006644 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006645 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006646 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006647 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006648 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00006649 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006650 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006651 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006652 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006653 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006654 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006655 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006656 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006657 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006658 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006659
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006660 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006661 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006662 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006663 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006664 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006665 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006666 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006667 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006668 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006669 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006670 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006671 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00006672 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006673 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006674 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006675 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006676 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006677 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006678 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006679 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00006680
6681 // Move semantics attribute.
6682 case ParsedAttr::AT_Reinitializes:
6683 handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
6684 break;
Erik Pilkington5a559e62018-08-21 17:24:06 +00006685
6686 case ParsedAttr::AT_AlwaysDestroy:
6687 case ParsedAttr::AT_NoDestroy:
6688 handleDestroyAttr(S, D, AL);
6689 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006690 }
6691}
6692
6693/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
6694/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00006695void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00006696 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00006697 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00006698 if (AttrList.empty())
6699 return;
6700
Erich Keanee891aa92018-07-13 15:07:47 +00006701 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00006702 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00006703
Joey Gouly2cd9db12013-12-13 16:15:28 +00006704 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00006705 // GCC accepts
6706 // static int a9 __attribute__((weakref));
6707 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00006708 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00006709 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
6710 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00006711 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00006712 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006713 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006714
Aaron Ballmanbe243a72014-12-04 22:45:31 +00006715 // FIXME: We should be able to handle this in TableGen as well. It would be
6716 // good to have a way to specify "these attributes must appear as a group",
6717 // for these. Additionally, it would be good to have a way to specify "these
6718 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00006719 if (!D->hasAttr<OpenCLKernelAttr>()) {
6720 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006721 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006722 // FIXME: This emits a different error message than
6723 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00006724 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006725 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006726 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006727 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006728 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006729 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006730 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006731 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006732 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006733 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
6734 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00006735 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
6736 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
6737 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6738 << A << ExpectedKernelFunction;
6739 D->setInvalidDecl();
6740 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
6741 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6742 << A << ExpectedKernelFunction;
6743 D->setInvalidDecl();
6744 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
6745 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6746 << A << ExpectedKernelFunction;
6747 D->setInvalidDecl();
6748 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
6749 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6750 << A << ExpectedKernelFunction;
6751 D->setInvalidDecl();
6752 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006753 }
6754 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006755}
6756
Hiroshi Inoue939d9322017-06-30 05:40:31 +00006757// Helper for delayed processing TransparentUnion attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006758void Sema::ProcessDeclAttributeDelayed(Decl *D,
6759 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006760 for (const ParsedAttr &AL : AttrList)
6761 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00006762 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00006763 break;
6764 }
6765}
6766
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006767// Annotation attributes are the only attributes allowed after an access
6768// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00006769bool Sema::ProcessAccessDeclAttributeList(
6770 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006771 for (const ParsedAttr &AL : AttrList) {
6772 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00006773 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006774 } else {
Erich Keanec480f302018-07-12 21:09:05 +00006775 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006776 return true;
6777 }
6778 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006779 return false;
6780}
6781
John McCall42856de2011-10-01 05:17:03 +00006782/// checkUnusedDeclAttributes - Check a list of attributes to see if it
6783/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00006784static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00006785 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00006786 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006787 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
6788 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00006789 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00006790 continue;
John McCall42856de2011-10-01 05:17:03 +00006791
Erich Keanee891aa92018-07-13 15:07:47 +00006792 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00006793 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006794 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006795 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00006796 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
6797 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006798 }
6799 }
6800}
6801
6802/// checkUnusedDeclAttributes - Given a declarator which is not being
6803/// used to build a declaration, complain about any decl attributes
6804/// which might be lying around on it.
6805void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00006806 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00006807 ::checkUnusedDeclAttributes(*this, D.getAttributes());
6808 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
6809 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
6810}
6811
Ryan Flynn7d470f32009-07-30 03:15:39 +00006812/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00006813/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00006814NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
6815 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00006816 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00006817 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006818 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00006819 FunctionDecl *NewFD;
6820 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00006821 // FIXME: Mangling?
6822 // FIXME: Is the qualifier info correct?
6823 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00006824 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
6825 Loc, Loc, DeclarationName(II),
6826 FD->getType(), FD->getTypeSourceInfo(),
6827 SC_None, false/*isInlineSpecified*/,
6828 FD->hasPrototype(),
6829 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006830 NewD = NewFD;
6831
6832 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00006833 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00006834
6835 // Fake up parameter variables; they are declared as if this were
6836 // a typedef.
6837 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006838 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00006839 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00006840 for (const auto &AI : FT->param_types()) {
6841 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006842 Param->setScopeInfo(0, Params.size());
6843 Params.push_back(Param);
6844 }
David Blaikie9c70e042011-09-21 18:16:56 +00006845 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00006846 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006847 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00006848 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00006849 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00006850 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00006851 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006852 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00006853 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00006854 }
6855 return NewD;
6856}
6857
James Dennett634962f2012-06-14 21:40:34 +00006858/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00006859/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00006860void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00006861 if (W.getUsed()) return; // only do this once
6862 W.setUsed(true);
6863 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
6864 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00006865 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00006866 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
6867 W.getLocation()));
6868 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00006869 WeakTopLevelDecl.push_back(NewD);
6870 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
6871 // to insert Decl at TU scope, sorry.
6872 DeclContext *SavedContext = CurContext;
6873 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00006874 NewD->setDeclContext(CurContext);
6875 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00006876 PushOnScopeChains(NewD, S);
6877 CurContext = SavedContext;
6878 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00006879 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00006880 }
6881}
6882
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006883void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
6884 // It's valid to "forward-declare" #pragma weak, in which case we
6885 // have to do this.
6886 LoadExternalWeakUndeclaredIdentifiers();
6887 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006888 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006889 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006890 if (VD->isExternC())
6891 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006892 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006893 if (FD->isExternC())
6894 ND = FD;
6895 if (ND) {
6896 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00006897 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006898 if (I != WeakUndeclaredIdentifiers.end()) {
6899 WeakInfo W = I->second;
6900 DeclApplyPragmaWeak(S, ND, W);
6901 WeakUndeclaredIdentifiers[Id] = W;
6902 }
6903 }
6904 }
6905 }
6906}
6907
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006908/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
6909/// it, apply them to D. This is a bit tricky because PD can have attributes
6910/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00006911void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006912 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00006913 if (!PD.getDeclSpec().getAttributes().empty())
6914 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00006915
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006916 // Walk the declarator structure, applying decl attributes that were in a type
6917 // position to the decl itself. This handles cases like:
6918 // int *__attr__(x)** D;
6919 // when X is a decl attribute.
6920 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00006921 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
6922 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006923
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006924 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00006925 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00006926
6927 // Apply additional attributes specified by '#pragma clang attribute'.
6928 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006929}
John McCall28a6aea2009-11-04 02:18:39 +00006930
John McCall31168b02011-06-15 23:02:42 +00006931/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00006932/// If so, it'll still be annotated with an attribute that makes it
6933/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006934static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00006935 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00006936 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00006937 // Private ivars are always okay. Unfortunately, people don't
6938 // always properly make their ivars private, even in system headers.
6939 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006940 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
6941 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00006942 return false;
6943
John McCallc6af8c62015-10-28 05:03:19 +00006944 // Silently accept unsupported uses of __weak in both user and system
6945 // declarations when it's been disabled, for ease of integration with
6946 // -fno-objc-arc files. We do have to take some care against attempts
6947 // to define such things; for now, we've only done that for ivars
6948 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006949 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00006950 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
6951 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
6952 reason = UnavailableAttr::IR_ForbiddenWeak;
6953 return true;
6954 }
John McCallb61e14e2015-10-27 04:54:50 +00006955 }
6956
John McCallc6af8c62015-10-28 05:03:19 +00006957 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006958 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00006959 // Currently, all the failures dealt with this way are due to ARC
6960 // restrictions.
6961 reason = UnavailableAttr::IR_ARCForbiddenType;
6962 return true;
John McCallb61e14e2015-10-27 04:54:50 +00006963 }
6964
6965 return false;
John McCall31168b02011-06-15 23:02:42 +00006966}
6967
6968/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006969static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
6970 Decl *D) {
6971 auto Reason = UnavailableAttr::IR_None;
6972 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
6973 assert(Reason && "didn't set reason?");
6974 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00006975 return;
6976 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00006977 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006978 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00006979 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006980 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006981 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006982 DD.getForbiddenTypeDiagnostic() ==
6983 diag::err_arc_array_param_no_ownership) {
6984 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006985 return;
6986 }
6987 }
John McCall31168b02011-06-15 23:02:42 +00006988
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006989 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
6990 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
6991 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00006992}
6993
Manman Ren45b1ab12016-05-06 19:57:16 +00006994static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
6995 const Decl *D) {
6996 // Check each AvailabilityAttr to find the one for this platform.
6997 for (const auto *A : D->attrs()) {
6998 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
6999 // FIXME: this is copied from CheckAvailability. We should try to
7000 // de-duplicate.
7001
7002 // Check if this is an App Extension "platform", and if so chop off
7003 // the suffix for matching with the actual platform.
7004 StringRef ActualPlatform = Avail->getPlatform()->getName();
7005 StringRef RealizedPlatform = ActualPlatform;
7006 if (Context.getLangOpts().AppExt) {
7007 size_t suffix = RealizedPlatform.rfind("_app_extension");
7008 if (suffix != StringRef::npos)
7009 RealizedPlatform = RealizedPlatform.slice(0, suffix);
7010 }
7011
7012 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
7013
7014 // Match the platform name.
7015 if (RealizedPlatform == TargetPlatform)
7016 return Avail;
7017 }
7018 }
7019 return nullptr;
7020}
7021
Erik Pilkington9f866a72017-07-18 20:32:07 +00007022/// The diagnostic we should emit for \c D, and the declaration that
7023/// originated it, or \c AR_Available.
7024///
7025/// \param D The declaration to check.
7026/// \param Message If non-null, this will be populated with the message from
7027/// the availability attribute that is selected.
Erik Pilkington42578572018-09-10 22:20:09 +00007028/// \param ClassReceiver If we're checking the the method of a class message
7029/// send, the class. Otherwise nullptr.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007030static std::pair<AvailabilityResult, const NamedDecl *>
Erik Pilkington42578572018-09-10 22:20:09 +00007031ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D,
7032 std::string *Message,
7033 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007034 AvailabilityResult Result = D->getAvailability(Message);
7035
7036 // For typedefs, if the typedef declaration appears available look
7037 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007038 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007039 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007040 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007041 D = TT->getDecl();
7042 Result = D->getAvailability(Message);
7043 continue;
7044 }
7045 }
7046 break;
7047 }
7048
7049 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007050 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007051 if (IDecl->getDefinition()) {
7052 D = IDecl->getDefinition();
7053 Result = D->getAvailability(Message);
7054 }
7055 }
7056
7057 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7058 if (Result == AR_Available) {
7059 const DeclContext *DC = ECD->getDeclContext();
7060 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
7061 Result = TheEnumDecl->getAvailability(Message);
7062 D = TheEnumDecl;
7063 }
7064 }
7065
Erik Pilkington42578572018-09-10 22:20:09 +00007066 // For +new, infer availability from -init.
7067 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7068 if (S.NSAPIObj && ClassReceiver) {
7069 ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod(
7070 S.NSAPIObj->getInitSelector());
7071 if (Init && Result == AR_Available && MD->isClassMethod() &&
7072 MD->getSelector() == S.NSAPIObj->getNewSelector() &&
7073 MD->definedInNSObject(S.getASTContext())) {
7074 Result = Init->getAvailability(Message);
7075 D = Init;
7076 }
7077 }
7078 }
7079
Erik Pilkington9f866a72017-07-18 20:32:07 +00007080 return {Result, D};
7081}
7082
7083
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007084/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007085/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
7086/// in a deprecated context, but not the other way around.
7087static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
7088 VersionTuple DeclVersion,
7089 Decl *Ctx) {
7090 assert(K != AR_Available && "Expected an unavailable declaration here!");
7091
7092 // Checks if we should emit the availability diagnostic in the context of C.
7093 auto CheckContext = [&](const Decl *C) {
7094 if (K == AR_NotYetIntroduced) {
7095 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
7096 if (AA->getIntroduced() >= DeclVersion)
7097 return true;
7098 } else if (K == AR_Deprecated)
7099 if (C->isDeprecated())
7100 return true;
7101
7102 if (C->isUnavailable())
7103 return true;
7104 return false;
7105 };
7106
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007107 do {
7108 if (CheckContext(Ctx))
7109 return false;
7110
7111 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00007112 // Unless it is "+load" method.
7113 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
7114 if (MethodD->isClassMethod() &&
7115 MethodD->getSelector().getAsString() == "load")
7116 return true;
7117
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007118 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007119 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
7120 if (CheckContext(Interface))
7121 return false;
7122 }
7123 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007124 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007125 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
7126 if (CheckContext(Interface))
7127 return false;
7128 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
7129
7130 return true;
7131}
7132
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007133static bool
7134shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
7135 const VersionTuple &DeploymentVersion,
7136 const VersionTuple &DeclVersion) {
7137 const auto &Triple = Context.getTargetInfo().getTriple();
7138 VersionTuple ForceAvailabilityFromVersion;
7139 switch (Triple.getOS()) {
7140 case llvm::Triple::IOS:
7141 case llvm::Triple::TvOS:
7142 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7143 break;
7144 case llvm::Triple::WatchOS:
7145 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7146 break;
7147 case llvm::Triple::Darwin:
7148 case llvm::Triple::MacOSX:
7149 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7150 break;
7151 default:
7152 // New targets should always warn about availability.
7153 return Triple.getVendor() == llvm::Triple::Apple;
7154 }
7155 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7156 DeclVersion >= ForceAvailabilityFromVersion;
7157}
7158
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007159static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7160 for (Decl *Ctx = OrigCtx; Ctx;
7161 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7162 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7163 return cast<NamedDecl>(Ctx);
7164 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7165 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7166 return Imp->getClassInterface();
7167 return CD;
7168 }
7169 }
7170
7171 return dyn_cast<NamedDecl>(OrigCtx);
7172}
7173
Alex Lorenz727c21e2017-07-26 13:58:02 +00007174namespace {
7175
7176struct AttributeInsertion {
7177 StringRef Prefix;
7178 SourceLocation Loc;
7179 StringRef Suffix;
7180
7181 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007182 return {" ", D->getEndLoc(), ""};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007183 }
7184 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7185 return {" ", Loc, ""};
7186 }
7187 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007188 return {"", D->getBeginLoc(), "\n"};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007189 }
7190};
7191
7192} // end anonymous namespace
7193
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007194/// Tries to parse a string as ObjC method name.
7195///
7196/// \param Name The string to parse. Expected to originate from availability
7197/// attribute argument.
7198/// \param SlotNames The vector that will be populated with slot names. In case
7199/// of unsuccessful parsing can contain invalid data.
7200/// \returns A number of method parameters if parsing was successful, None
7201/// otherwise.
7202static Optional<unsigned>
7203tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
7204 const LangOptions &LangOpts) {
7205 // Accept replacements starting with - or + as valid ObjC method names.
7206 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
7207 Name = Name.drop_front(1);
7208 if (Name.empty())
7209 return None;
7210 Name.split(SlotNames, ':');
7211 unsigned NumParams;
7212 if (Name.back() == ':') {
7213 // Remove an empty string at the end that doesn't represent any slot.
7214 SlotNames.pop_back();
7215 NumParams = SlotNames.size();
7216 } else {
7217 if (SlotNames.size() != 1)
7218 // Not a valid method name, just a colon-separated string.
7219 return None;
7220 NumParams = 0;
7221 }
7222 // Verify all slot names are valid.
7223 bool AllowDollar = LangOpts.DollarIdents;
7224 for (StringRef S : SlotNames) {
7225 if (S.empty())
7226 continue;
7227 if (!isValidIdentifier(S, AllowDollar))
7228 return None;
7229 }
7230 return NumParams;
7231}
7232
Alex Lorenz727c21e2017-07-26 13:58:02 +00007233/// Returns a source location in which it's appropriate to insert a new
7234/// attribute for the given declaration \D.
7235static Optional<AttributeInsertion>
7236createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7237 const LangOptions &LangOpts) {
7238 if (isa<ObjCPropertyDecl>(D))
7239 return AttributeInsertion::createInsertionAfter(D);
7240 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7241 if (MD->hasBody())
7242 return None;
7243 return AttributeInsertion::createInsertionAfter(D);
7244 }
7245 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7246 SourceLocation Loc =
7247 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7248 if (Loc.isInvalid())
7249 return None;
7250 // Insert after the 'struct'/whatever keyword.
7251 return AttributeInsertion::createInsertionAfter(Loc);
7252 }
7253 return AttributeInsertion::createInsertionBefore(D);
7254}
7255
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007256/// Actually emit an availability diagnostic for a reference to an unavailable
7257/// decl.
7258///
7259/// \param Ctx The context that the reference occurred in
7260/// \param ReferringDecl The exact declaration that was referenced.
7261/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007262/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007263/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7264/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7265/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007266static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007267 Decl *Ctx, const NamedDecl *ReferringDecl,
7268 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007269 StringRef Message,
7270 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007271 const ObjCInterfaceDecl *UnknownObjCClass,
7272 const ObjCPropertyDecl *ObjCProperty,
7273 bool ObjCPropertyAccess) {
7274 // Diagnostics for deprecated or unavailable.
7275 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007276 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007277 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007278
7279 // Matches 'diag::note_property_attribute' options.
7280 unsigned property_note_select;
7281
7282 // Matches diag::note_availability_specified_here.
7283 unsigned available_here_select_kind;
7284
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007285 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007286 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007287 DeclVersion = AA->getIntroduced();
7288
7289 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx))
7290 return;
7291
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007292 SourceLocation Loc = Locs.front();
7293
Erik Pilkington8b352c42017-08-14 19:49:12 +00007294 // The declaration can have multiple availability attributes, we are looking
7295 // at one of them.
7296 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7297 if (A && A->isInherited()) {
7298 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7299 Redecl = Redecl->getPreviousDecl()) {
7300 const AvailabilityAttr *AForRedecl =
7301 getAttrForPlatform(S.Context, Redecl);
7302 if (AForRedecl && !AForRedecl->isInherited()) {
7303 // If D is a declaration with inherited attributes, the note should
7304 // point to the declaration with actual attributes.
7305 NoteLocation = Redecl->getLocation();
7306 break;
7307 }
7308 }
7309 }
7310
Aaron Ballmanfb237522014-10-15 15:37:51 +00007311 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007312 case AR_NotYetIntroduced: {
7313 // We would like to emit the diagnostic even if -Wunguarded-availability is
7314 // not specified for deployment targets >= to iOS 11 or equivalent or
7315 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7316 // later.
7317 const AvailabilityAttr *AA =
7318 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7319 VersionTuple Introduced = AA->getIntroduced();
7320
7321 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7322 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7323 Introduced);
7324 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7325 : diag::warn_unguarded_availability;
7326
7327 S.Diag(Loc, Warning)
7328 << OffendingDecl
7329 << AvailabilityAttr::getPrettyPlatformName(
7330 S.getASTContext().getTargetInfo().getPlatformName())
7331 << Introduced.getAsString();
7332
7333 S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here)
7334 << OffendingDecl << /* partial */ 3;
7335
7336 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007337 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007338 if (TD->getDeclName().isEmpty()) {
7339 S.Diag(TD->getLocation(),
7340 diag::note_decl_unguarded_availability_silence)
7341 << /*Anonymous*/ 1 << TD->getKindName();
7342 return;
7343 }
7344 auto FixitNoteDiag =
7345 S.Diag(Enclosing->getLocation(),
7346 diag::note_decl_unguarded_availability_silence)
7347 << /*Named*/ 0 << Enclosing;
7348 // Don't offer a fixit for declarations with availability attributes.
7349 if (Enclosing->hasAttr<AvailabilityAttr>())
7350 return;
7351 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7352 return;
7353 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7354 Enclosing, S.getSourceManager(), S.getLangOpts());
7355 if (!Insertion)
7356 return;
7357 std::string PlatformName =
7358 AvailabilityAttr::getPlatformNameSourceSpelling(
7359 S.getASTContext().getTargetInfo().getPlatformName())
7360 .lower();
7361 std::string Introduced =
7362 OffendingDecl->getVersionIntroduced().getAsString();
7363 FixitNoteDiag << FixItHint::CreateInsertion(
7364 Insertion->Loc,
7365 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7366 "(" + Introduced + "))" + Insertion->Suffix)
7367 .str());
7368 }
7369 return;
7370 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007371 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007372 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7373 : diag::warn_property_method_deprecated;
7374 diag_message = diag::warn_deprecated_message;
7375 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7376 property_note_select = /* deprecated */ 0;
7377 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007378 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7379 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007380 break;
7381
Erik Pilkington796a3e22016-08-05 22:59:03 +00007382 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007383 diag = !ObjCPropertyAccess ? diag::err_unavailable
7384 : diag::err_property_method_unavailable;
7385 diag_message = diag::err_unavailable_message;
7386 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7387 property_note_select = /* unavailable */ 1;
7388 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007389
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007390 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7391 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007392 // Most of these failures are due to extra restrictions in ARC;
7393 // reflect that in the primary diagnostic when applicable.
7394 auto flagARCError = [&] {
7395 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007396 S.getSourceManager().isInSystemHeader(
7397 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007398 diag = diag::err_unavailable_in_arc;
7399 };
7400
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007401 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007402 case UnavailableAttr::IR_None: break;
7403
7404 case UnavailableAttr::IR_ARCForbiddenType:
7405 flagARCError();
7406 diag_available_here = diag::note_arc_forbidden_type;
7407 break;
7408
7409 case UnavailableAttr::IR_ForbiddenWeak:
7410 if (S.getLangOpts().ObjCWeakRuntime)
7411 diag_available_here = diag::note_arc_weak_disabled;
7412 else
7413 diag_available_here = diag::note_arc_weak_no_runtime;
7414 break;
7415
7416 case UnavailableAttr::IR_ARCForbiddenConversion:
7417 flagARCError();
7418 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7419 break;
7420
7421 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7422 flagARCError();
7423 diag_available_here = diag::note_arc_init_returns_unrelated;
7424 break;
7425
7426 case UnavailableAttr::IR_ARCFieldWithOwnership:
7427 flagARCError();
7428 diag_available_here = diag::note_arc_field_with_ownership;
7429 break;
7430 }
7431 }
John McCallb61e14e2015-10-27 04:54:50 +00007432 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007433 break;
7434
Erik Pilkington796a3e22016-08-05 22:59:03 +00007435 case AR_Available:
7436 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007437 }
7438
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007439 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007440 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007441 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007442 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7443 Replacement = AL->getReplacement();
7444 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7445 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007446
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007447 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00007448 if (!Replacement.empty())
7449 UseRange =
7450 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007451 if (UseRange.isValid()) {
7452 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
7453 Selector Sel = MethodDecl->getSelector();
7454 SmallVector<StringRef, 12> SelectorSlotNames;
7455 Optional<unsigned> NumParams = tryParseObjCMethodName(
7456 Replacement, SelectorSlotNames, S.getLangOpts());
7457 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
7458 assert(SelectorSlotNames.size() == Locs.size());
7459 for (unsigned I = 0; I < Locs.size(); ++I) {
7460 if (!Sel.getNameForSlot(I).empty()) {
7461 CharSourceRange NameRange = CharSourceRange::getCharRange(
7462 Locs[I], S.getLocForEndOfToken(Locs[I]));
7463 FixIts.push_back(FixItHint::CreateReplacement(
7464 NameRange, SelectorSlotNames[I]));
7465 } else
7466 FixIts.push_back(
7467 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
7468 }
7469 } else
7470 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7471 } else
7472 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7473 }
Manman Renc7890fe2016-03-16 18:50:49 +00007474 }
7475
Aaron Ballmanfb237522014-10-15 15:37:51 +00007476 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007477 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007478 if (ObjCProperty)
7479 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7480 << ObjCProperty->getDeclName() << property_note_select;
7481 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007482 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007483 if (ObjCProperty)
7484 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7485 << ObjCProperty->getDeclName() << property_note_select;
7486 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007487 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007488 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7489 }
7490
Erik Pilkington8b352c42017-08-14 19:49:12 +00007491 S.Diag(NoteLocation, diag_available_here)
7492 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007493}
7494
7495static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
7496 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007497 assert(DD.Kind == DelayedDiagnostic::Availability &&
7498 "Expected an availability diagnostic here");
7499
Aaron Ballmanfb237522014-10-15 15:37:51 +00007500 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00007501 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007502 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007503 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
7504 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
7505 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00007506}
7507
John McCall2ec85372012-05-07 06:16:41 +00007508void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7509 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007510 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007511 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007512
John McCall2ec85372012-05-07 06:16:41 +00007513 // When delaying diagnostics to run in the context of a parsed
7514 // declaration, we only want to actually emit anything if parsing
7515 // succeeds.
7516 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007517
John McCall2ec85372012-05-07 06:16:41 +00007518 // We emit all the active diagnostics in this pool or any of its
7519 // parents. In general, we'll get one pool for the decl spec
7520 // and a child pool for each declarator; in a decl group like:
7521 // deprecated_typedef foo, *bar, baz();
7522 // only the declarator pops will be passed decls. This is correct;
7523 // we really do need to consider delayed diagnostics from the decl spec
7524 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007525 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007526 do {
Richard Smith5c9b3b72018-09-25 22:12:44 +00007527 bool AnyAccessFailures = false;
John McCall6347b682012-05-07 06:16:58 +00007528 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007529 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7530 // This const_cast is a bit lame. Really, Triggered should be mutable.
7531 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007532 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007533 continue;
7534
John McCallc1465822011-02-14 07:13:47 +00007535 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007536 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007537 // Don't bother giving deprecation/unavailable diagnostics if
7538 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007539 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00007540 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00007541 break;
7542
7543 case DelayedDiagnostic::Access:
Richard Smith5c9b3b72018-09-25 22:12:44 +00007544 // Only produce one access control diagnostic for a structured binding
7545 // declaration: we don't need to tell the user that all the fields are
7546 // inaccessible one at a time.
7547 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
7548 continue;
John McCall2ec85372012-05-07 06:16:41 +00007549 HandleDelayedAccessCheck(diag, decl);
Richard Smith5c9b3b72018-09-25 22:12:44 +00007550 if (diag.Triggered)
7551 AnyAccessFailures = true;
John McCall86121512010-01-27 03:50:35 +00007552 break;
John McCall31168b02011-06-15 23:02:42 +00007553
7554 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007555 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007556 break;
John McCall86121512010-01-27 03:50:35 +00007557 }
7558 }
John McCall2ec85372012-05-07 06:16:41 +00007559 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007560}
7561
John McCall6347b682012-05-07 06:16:58 +00007562/// Given a set of delayed diagnostics, re-emit them as if they had
7563/// been delayed in the current context instead of in the given pool.
7564/// Essentially, this just moves them to the current pool.
7565void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7566 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7567 assert(curPool && "re-emitting in undelayed context not supported");
7568 curPool->steal(pool);
7569}
7570
Erik Pilkington9f866a72017-07-18 20:32:07 +00007571static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
7572 const NamedDecl *ReferringDecl,
7573 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007574 StringRef Message,
7575 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007576 const ObjCInterfaceDecl *UnknownObjCClass,
7577 const ObjCPropertyDecl *ObjCProperty,
7578 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00007579 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007580 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
7581 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007582 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007583 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007584 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00007585 return;
7586 }
7587
Erik Pilkington9f866a72017-07-18 20:32:07 +00007588 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
7589 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007590 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007591 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00007592}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00007593
Erik Pilkington5cd57172016-08-16 17:44:11 +00007594namespace {
7595
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007596/// Returns true if the given statement can be a body-like child of \p Parent.
7597bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
7598 switch (Parent->getStmtClass()) {
7599 case Stmt::IfStmtClass:
7600 return cast<IfStmt>(Parent)->getThen() == S ||
7601 cast<IfStmt>(Parent)->getElse() == S;
7602 case Stmt::WhileStmtClass:
7603 return cast<WhileStmt>(Parent)->getBody() == S;
7604 case Stmt::DoStmtClass:
7605 return cast<DoStmt>(Parent)->getBody() == S;
7606 case Stmt::ForStmtClass:
7607 return cast<ForStmt>(Parent)->getBody() == S;
7608 case Stmt::CXXForRangeStmtClass:
7609 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
7610 case Stmt::ObjCForCollectionStmtClass:
7611 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
7612 case Stmt::CaseStmtClass:
7613 case Stmt::DefaultStmtClass:
7614 return cast<SwitchCase>(Parent)->getSubStmt() == S;
7615 default:
7616 return false;
7617 }
7618}
7619
7620class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
7621 const Stmt *Target;
7622
7623public:
7624 bool VisitStmt(Stmt *S) { return S != Target; }
7625
7626 /// Returns true if the given statement is present in the given declaration.
7627 static bool isContained(const Stmt *Target, const Decl *D) {
7628 StmtUSEFinder Visitor;
7629 Visitor.Target = Target;
7630 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
7631 }
7632};
7633
7634/// Traverses the AST and finds the last statement that used a given
7635/// declaration.
7636class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
7637 const Decl *D;
7638
7639public:
7640 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7641 if (DRE->getDecl() == D)
7642 return false;
7643 return true;
7644 }
7645
7646 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
7647 const CompoundStmt *Scope) {
7648 LastDeclUSEFinder Visitor;
7649 Visitor.D = D;
7650 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
7651 const Stmt *S = *I;
7652 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
7653 return S;
7654 }
7655 return nullptr;
7656 }
7657};
7658
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007659/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00007660///
7661/// This is done with a traversal of the AST of a function that makes reference
7662/// to a partially available declaration. Whenever we encounter an \c if of the
7663/// form: \c if(@available(...)), we use the version from the condition to visit
7664/// the then statement.
7665class DiagnoseUnguardedAvailability
7666 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
7667 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
7668
7669 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007670 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007671
7672 /// Stack of potentially nested 'if (@available(...))'s.
7673 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007674 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007675
Erik Pilkington42578572018-09-10 22:20:09 +00007676 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range,
7677 ObjCInterfaceDecl *ClassReceiver = nullptr);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007678
7679public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007680 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
7681 : SemaRef(SemaRef), Ctx(Ctx) {
7682 AvailabilityStack.push_back(
7683 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007684 }
7685
Alex Lorenz6f911122017-05-16 13:58:53 +00007686 bool TraverseDecl(Decl *D) {
7687 // Avoid visiting nested functions to prevent duplicate warnings.
7688 if (!D || isa<FunctionDecl>(D))
7689 return true;
7690 return Base::TraverseDecl(D);
7691 }
7692
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007693 bool TraverseStmt(Stmt *S) {
7694 if (!S)
7695 return true;
7696 StmtStack.push_back(S);
7697 bool Result = Base::TraverseStmt(S);
7698 StmtStack.pop_back();
7699 return Result;
7700 }
7701
Erik Pilkington5cd57172016-08-16 17:44:11 +00007702 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
7703
7704 bool TraverseIfStmt(IfStmt *If);
7705
Alex Lorenz6f911122017-05-16 13:58:53 +00007706 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
7707
Erik Pilkingtonba87c622017-08-18 20:20:56 +00007708 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
7709 // to any useful diagnostics.
7710 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
7711
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007712 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
7713 if (PRE->isClassReceiver())
7714 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
7715 return true;
7716 }
7717
Erik Pilkington5cd57172016-08-16 17:44:11 +00007718 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
Erik Pilkington42578572018-09-10 22:20:09 +00007719 if (ObjCMethodDecl *D = Msg->getMethodDecl()) {
7720 ObjCInterfaceDecl *ID = nullptr;
7721 QualType ReceiverTy = Msg->getClassReceiver();
7722 if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType())
7723 ID = ReceiverTy->getAsObjCInterfaceType()->getInterface();
7724
Erik Pilkington5cd57172016-08-16 17:44:11 +00007725 DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00007726 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID);
7727 }
Erik Pilkington5cd57172016-08-16 17:44:11 +00007728 return true;
7729 }
7730
7731 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7732 DiagnoseDeclAvailability(DRE->getDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007733 SourceRange(DRE->getBeginLoc(), DRE->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00007734 return true;
7735 }
7736
7737 bool VisitMemberExpr(MemberExpr *ME) {
7738 DiagnoseDeclAvailability(ME->getMemberDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007739 SourceRange(ME->getBeginLoc(), ME->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00007740 return true;
7741 }
7742
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007743 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007744 SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use)
Erik Pilkingtonfa983902018-10-30 20:31:30 +00007745 << (!SemaRef.getLangOpts().ObjC);
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007746 return true;
7747 }
7748
Erik Pilkington5cd57172016-08-16 17:44:11 +00007749 bool VisitTypeLoc(TypeLoc Ty);
7750};
7751
7752void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00007753 NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007754 AvailabilityResult Result;
7755 const NamedDecl *OffendingDecl;
7756 std::tie(Result, OffendingDecl) =
Erik Pilkington42578572018-09-10 22:20:09 +00007757 ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007758 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007759 // All other diagnostic kinds have already been handled in
7760 // DiagnoseAvailabilityOfDecl.
7761 if (Result != AR_NotYetIntroduced)
7762 return;
7763
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007764 const AvailabilityAttr *AA =
7765 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007766 VersionTuple Introduced = AA->getIntroduced();
7767
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007768 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007769 return;
7770
7771 // If the context of this function is less available than D, we should not
7772 // emit a diagnostic.
7773 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx))
7774 return;
7775
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007776 // We would like to emit the diagnostic even if -Wunguarded-availability is
7777 // not specified for deployment targets >= to iOS 11 or equivalent or
7778 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7779 // later.
7780 unsigned DiagKind =
7781 shouldDiagnoseAvailabilityByDefault(
7782 SemaRef.Context,
7783 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
7784 ? diag::warn_unguarded_availability_new
7785 : diag::warn_unguarded_availability;
7786
7787 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington5cd57172016-08-16 17:44:11 +00007788 << Range << D
7789 << AvailabilityAttr::getPrettyPlatformName(
7790 SemaRef.getASTContext().getTargetInfo().getPlatformName())
7791 << Introduced.getAsString();
7792
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007793 SemaRef.Diag(OffendingDecl->getLocation(),
7794 diag::note_availability_specified_here)
7795 << OffendingDecl << /* partial */ 3;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007796
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007797 auto FixitDiag =
7798 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
7799 << Range << D
Erik Pilkingtonfa983902018-10-30 20:31:30 +00007800 << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0
7801 : /*__builtin_available*/ 1);
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007802
7803 // Find the statement which should be enclosed in the if @available check.
7804 if (StmtStack.empty())
7805 return;
7806 const Stmt *StmtOfUse = StmtStack.back();
7807 const CompoundStmt *Scope = nullptr;
7808 for (const Stmt *S : llvm::reverse(StmtStack)) {
7809 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
7810 Scope = CS;
7811 break;
7812 }
7813 if (isBodyLikeChildStmt(StmtOfUse, S)) {
7814 // The declaration won't be seen outside of the statement, so we don't
7815 // have to wrap the uses of any declared variables in if (@available).
7816 // Therefore we can avoid setting Scope here.
7817 break;
7818 }
7819 StmtOfUse = S;
7820 }
7821 const Stmt *LastStmtOfUse = nullptr;
7822 if (isa<DeclStmt>(StmtOfUse) && Scope) {
7823 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
7824 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
7825 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
7826 break;
7827 }
7828 }
7829 }
7830
7831 const SourceManager &SM = SemaRef.getSourceManager();
7832 SourceLocation IfInsertionLoc =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007833 SM.getExpansionLoc(StmtOfUse->getBeginLoc());
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007834 SourceLocation StmtEndLoc =
7835 SM.getExpansionRange(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007836 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc())
Richard Smithb5f81712018-04-30 05:25:48 +00007837 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007838 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
7839 return;
7840
7841 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
7842 const char *ExtraIndentation = " ";
7843 std::string FixItString;
7844 llvm::raw_string_ostream FixItOS(FixItString);
Erik Pilkingtonfa983902018-10-30 20:31:30 +00007845 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available"
7846 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00007847 << "("
7848 << AvailabilityAttr::getPlatformNameSourceSpelling(
7849 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007850 << " " << Introduced.getAsString() << ", *)) {\n"
7851 << Indentation << ExtraIndentation;
7852 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
7853 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
7854 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
7855 /*SkipTrailingWhitespaceAndNewLine=*/false);
7856 if (ElseInsertionLoc.isInvalid())
7857 ElseInsertionLoc =
7858 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
7859 FixItOS.str().clear();
7860 FixItOS << "\n"
7861 << Indentation << "} else {\n"
7862 << Indentation << ExtraIndentation
7863 << "// Fallback on earlier versions\n"
7864 << Indentation << "}";
7865 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007866 }
7867}
7868
7869bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
7870 const Type *TyPtr = Ty.getTypePtr();
7871 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
7872
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007873 if (Range.isInvalid())
7874 return true;
7875
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007876 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007877 TagDecl *TD = TT->getDecl();
7878 DiagnoseDeclAvailability(TD, Range);
7879
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007880 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007881 TypedefNameDecl *D = TD->getDecl();
7882 DiagnoseDeclAvailability(D, Range);
7883
7884 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
7885 if (NamedDecl *D = ObjCO->getInterface())
7886 DiagnoseDeclAvailability(D, Range);
7887 }
7888
7889 return true;
7890}
7891
7892bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
7893 VersionTuple CondVersion;
7894 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
7895 CondVersion = E->getVersion();
7896
7897 // If we're using the '*' case here or if this check is redundant, then we
7898 // use the enclosing version to check both branches.
7899 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00007900 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007901 } else {
7902 // This isn't an availability checking 'if', we can just continue.
7903 return Base::TraverseIfStmt(If);
7904 }
7905
7906 AvailabilityStack.push_back(CondVersion);
7907 bool ShouldContinue = TraverseStmt(If->getThen());
7908 AvailabilityStack.pop_back();
7909
7910 return ShouldContinue && TraverseStmt(If->getElse());
7911}
7912
7913} // end anonymous namespace
7914
7915void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
7916 Stmt *Body = nullptr;
7917
7918 if (auto *FD = D->getAsFunction()) {
7919 // FIXME: We only examine the pattern decl for availability violations now,
7920 // but we should also examine instantiated templates.
7921 if (FD->isTemplateInstantiation())
7922 return;
7923
7924 Body = FD->getBody();
7925 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
7926 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00007927 else if (auto *BD = dyn_cast<BlockDecl>(D))
7928 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00007929
7930 assert(Body && "Need a body here!");
7931
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007932 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007933}
Erik Pilkington9f866a72017-07-18 20:32:07 +00007934
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007935void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
7936 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007937 const ObjCInterfaceDecl *UnknownObjCClass,
7938 bool ObjCPropertyAccess,
Erik Pilkington42578572018-09-10 22:20:09 +00007939 bool AvoidPartialAvailabilityChecks,
7940 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007941 std::string Message;
7942 AvailabilityResult Result;
7943 const NamedDecl* OffendingDecl;
7944 // See if this declaration is unavailable, deprecated, or partial.
Erik Pilkington42578572018-09-10 22:20:09 +00007945 std::tie(Result, OffendingDecl) =
7946 ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver);
Erik Pilkington9f866a72017-07-18 20:32:07 +00007947 if (Result == AR_Available)
7948 return;
7949
7950 if (Result == AR_NotYetIntroduced) {
7951 if (AvoidPartialAvailabilityChecks)
7952 return;
7953
7954 // We need to know the @available context in the current function to
7955 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
7956 // when we're done parsing the current function.
7957 if (getCurFunctionOrMethodDecl()) {
7958 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
7959 return;
7960 } else if (getCurBlock() || getCurLambda()) {
7961 getCurFunction()->HasPotentialAvailabilityViolations = true;
7962 return;
7963 }
7964 }
7965
7966 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007967 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007968 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
7969 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
7970 if (PDeclResult == Result)
7971 ObjCPDecl = PD;
7972 }
7973 }
7974
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007975 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007976 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
7977}