blob: 63501d92915ad0e9a1f55bd70adde0630dc7e0fa [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>
George Karpenkov1657f362018-11-30 02:18:37 +0000395static void handleSimpleAttribute(Sema &S, Decl *D, SourceRange SR,
396 unsigned SpellingIndex) {
397 D->addAttr(::new (S.Context) AttrType(SR, S.Context, SpellingIndex));
398}
399
400template <typename AttrType>
Erich Keanee891aa92018-07-13 15:07:47 +0000401static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) {
George Karpenkov1657f362018-11-30 02:18:37 +0000402 handleSimpleAttribute<AttrType>(S, D, AL.getRange(),
403 AL.getAttributeSpellingListIndex());
404}
405
406
407template <typename... DiagnosticArgs>
408static const Sema::SemaDiagnosticBuilder&
409appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr) {
410 return Bldr;
411}
412
413template <typename T, typename... DiagnosticArgs>
414static const Sema::SemaDiagnosticBuilder&
415appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr, T &&ExtraArg,
416 DiagnosticArgs &&... ExtraArgs) {
417 return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
418 std::forward<DiagnosticArgs>(ExtraArgs)...);
419}
420
421/// Add an attribute {@code AttrType} to declaration {@code D},
422/// provided the given {@code Check} function returns {@code true}
423/// on type of {@code D}.
424/// If check does not pass, emit diagnostic {@code DiagID},
425/// passing in all parameters specified in {@code ExtraArgs}.
426template <typename AttrType, typename... DiagnosticArgs>
427static void
428handleSimpleAttributeWithCheck(Sema &S, ValueDecl *D, SourceRange SR,
429 unsigned SpellingIndex,
430 llvm::function_ref<bool(QualType)> Check,
431 unsigned DiagID, DiagnosticArgs... ExtraArgs) {
432 if (!Check(D->getType())) {
433 Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
434 appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
435 return;
436 }
437 handleSimpleAttribute<AttrType>(S, D, SR, SpellingIndex);
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000438}
439
Justin Lebar3eaaf862016-01-13 01:07:35 +0000440template <typename AttrType>
441static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000442 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000443 handleSimpleAttribute<AttrType>(S, D, AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000444}
445
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000446/// Applies the given attribute to the Decl so long as the Decl doesn't
Justin Lebar3eaaf862016-01-13 01:07:35 +0000447/// already have one of the given incompatible attributes.
448template <typename AttrType, typename IncompatibleAttrType,
449 typename... IncompatibleAttrTypes>
450static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000451 const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000452 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
Justin Lebar3eaaf862016-01-13 01:07:35 +0000453 return;
454 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000455 AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000456}
457
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000458/// Check if the passed-in expression is of type int or bool.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000459static bool isIntOrBool(Expr *Exp) {
460 QualType QT = Exp->getType();
461 return QT->isBooleanType() || QT->isIntegerType();
462}
463
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000464
465// Check to see if the type is a smart pointer of some kind. We assume
466// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000467static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Trieu8d3fa392018-09-22 01:50:52 +0000468 auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record,
469 OverloadedOperatorKind Op) {
470 DeclContextLookupResult Result =
471 Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op));
472 return !Result.empty();
473 };
474
475 const RecordDecl *Record = RT->getDecl();
476 bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
477 bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
478 if (foundStarOperator && foundArrowOperator)
479 return true;
480
481 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
482 if (!CXXRecord)
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000483 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000484
Richard Trieu8d3fa392018-09-22 01:50:52 +0000485 for (auto BaseSpecifier : CXXRecord->bases()) {
486 if (!foundStarOperator)
487 foundStarOperator = IsOverloadedOperatorPresent(
488 BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
489 if (!foundArrowOperator)
490 foundArrowOperator = IsOverloadedOperatorPresent(
491 BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
492 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000493
Richard Trieu8d3fa392018-09-22 01:50:52 +0000494 if (foundStarOperator && foundArrowOperator)
495 return true;
496
497 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000498}
499
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000500/// Check if passed in Decl is a pointer type.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000501/// Note that this function may produce an error message.
502/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000503static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000504 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000505 const auto *VD = cast<ValueDecl>(D);
506 QualType QT = VD->getType();
Aaron Ballman553e6812013-12-26 14:54:11 +0000507 if (QT->isAnyPointerType())
508 return true;
509
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000510 if (const auto *RT = QT->getAs<RecordType>()) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000511 // If it's an incomplete type, it could be a smart pointer; skip it.
512 // (We don't want to force template instantiation if we can avoid it,
513 // since that would alter the order in which templates are instantiated.)
514 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000515 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000516
Aaron Ballman553e6812013-12-26 14:54:11 +0000517 if (threadSafetyCheckIsSmartPointer(S, RT))
518 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000519 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000520
Erich Keane44bacdf2018-08-09 13:21:32 +0000521 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000522 return false;
523}
524
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000525/// Checks that the passed in QualType either is of RecordType or points
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000526/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000527static const RecordType *getRecordType(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000528 if (const auto *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000529 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000530
531 // Now check if we point to record type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000532 if (const auto *PT = QT->getAs<PointerType>())
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000533 return PT->getPointeeType()->getAs<RecordType>();
534
Craig Topperc3ec1492014-05-26 06:22:03 +0000535 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000536}
537
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000538template <typename AttrType>
539static bool checkRecordDeclForAttr(const RecordDecl *RD) {
540 // Check if the record itself has the attribute.
541 if (RD->hasAttr<AttrType>())
542 return true;
543
544 // Else check if any base classes have the attribute.
545 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
546 CXXBasePaths BPaths(false, false);
547 if (CRD->lookupInBases(
548 [](const CXXBaseSpecifier *BS, CXXBasePath &) {
549 const auto &Ty = *BS->getType();
550 // If it's type-dependent, we assume it could have the attribute.
551 if (Ty.isDependentType())
552 return true;
553 return Ty.getAs<RecordType>()->getDecl()->hasAttr<AttrType>();
554 },
555 BPaths, true))
556 return true;
557 }
558 return false;
559}
560
Josh Gao55afa752017-08-11 07:54:35 +0000561static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000562 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000563
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000564 if (!RT)
565 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000566
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000567 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000568 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000569 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000570
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000571 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000572 // FIXME -- Check the type that the smart pointer points to.
573 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000574 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000575
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000576 return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl());
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000577}
578
Josh Gao55afa752017-08-11 07:54:35 +0000579static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000580 const auto *TD = Ty->getAs<TypedefType>();
581 if (!TD)
582 return false;
583
584 TypedefNameDecl *TN = TD->getDecl();
585 if (!TN)
586 return false;
587
Josh Gao55afa752017-08-11 07:54:35 +0000588 return TN->hasAttr<CapabilityAttr>();
Aaron Ballman76050722014-04-04 15:13:57 +0000589}
590
Josh Gaob40c1772017-08-08 19:44:35 +0000591static bool typeHasCapability(Sema &S, QualType Ty) {
Josh Gao55afa752017-08-11 07:54:35 +0000592 if (checkTypedefTypeForCapability(Ty))
593 return true;
Josh Gaob40c1772017-08-08 19:44:35 +0000594
Josh Gao55afa752017-08-11 07:54:35 +0000595 if (checkRecordTypeForCapability(S, Ty))
596 return true;
597
598 return false;
Josh Gaob40c1772017-08-08 19:44:35 +0000599}
600
Aaron Ballman76050722014-04-04 15:13:57 +0000601static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
602 // Capability expressions are simple expressions involving the boolean logic
603 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
604 // a DeclRefExpr is found, its type should be checked to determine whether it
605 // is a capability or not.
606
Yi Kong2d58d192017-12-14 22:24:45 +0000607 if (const auto *E = dyn_cast<CastExpr>(Ex))
Aaron Ballman76050722014-04-04 15:13:57 +0000608 return isCapabilityExpr(S, E->getSubExpr());
609 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
610 return isCapabilityExpr(S, E->getSubExpr());
611 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
Yi Kong2d58d192017-12-14 22:24:45 +0000612 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
613 E->getOpcode() == UO_Deref)
Aaron Ballman76050722014-04-04 15:13:57 +0000614 return isCapabilityExpr(S, E->getSubExpr());
615 return false;
616 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
617 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
618 return isCapabilityExpr(S, E->getLHS()) &&
619 isCapabilityExpr(S, E->getRHS());
620 return false;
621 }
622
Yi Kong2d58d192017-12-14 22:24:45 +0000623 return typeHasCapability(S, Ex->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000624}
625
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000626/// Checks that all attribute arguments, starting from Sidx, resolve to
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000627/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000628/// \param Sidx The attribute argument index to start checking with.
629/// \param ParamIdxOk Whether an argument can be indexing into a function
630/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000631static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000632 const ParsedAttr &AL,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000633 SmallVectorImpl<Expr *> &Args,
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000634 unsigned Sidx = 0,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000635 bool ParamIdxOk = false) {
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000636 if (Sidx == AL.getNumArgs()) {
637 // If we don't have any capability arguments, the attribute implicitly
638 // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
639 // a non-static method, and that the class is a (scoped) capability.
640 const auto *MD = dyn_cast<const CXXMethodDecl>(D);
641 if (MD && !MD->isStatic()) {
642 const CXXRecordDecl *RD = MD->getParent();
643 // FIXME -- need to check this again on template instantiation
644 if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
645 !checkRecordDeclForAttr<ScopedLockableAttr>(RD))
646 S.Diag(AL.getLoc(),
647 diag::warn_thread_attribute_not_on_capability_member)
648 << AL << MD->getParent();
649 } else {
650 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
651 << AL;
652 }
653 }
654
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000655 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
656 Expr *ArgExp = AL.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000657
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000658 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000659 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000660 Args.push_back(ArgExp);
661 continue;
662 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000663
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000664 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000665 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000666 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000667 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000668 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000669 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000670 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000671 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000672
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000673 // We allow constant strings to be used as a placeholder for expressions
674 // that are not valid C++ syntax, but warn that they are ignored.
Erich Keane44bacdf2018-08-09 13:21:32 +0000675 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000676 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000677 continue;
678 }
679
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000680 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000681
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000682 // A pointer to member expression of the form &MyClass::mu is treated
683 // specially -- we need to look at the type of the member.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000684 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000685 if (UOp->getOpcode() == UO_AddrOf)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000686 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000687 if (DRE->getDecl()->isCXXInstanceMember())
688 ArgTy = DRE->getDecl()->getType();
689
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000690 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000691 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000692
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000693 // Now check if we index into a record type function param.
Josh Gao55afa752017-08-11 07:54:35 +0000694 if(!RT && ParamIdxOk) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000695 const auto *FD = dyn_cast<FunctionDecl>(D);
696 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
Josh Gao55afa752017-08-11 07:54:35 +0000697 if(FD && IL) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000698 unsigned int NumParams = FD->getNumParams();
699 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000700 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
701 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000702 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
703 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +0000704 << AL << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000705 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000706 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000707 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000708 }
709 }
710
Aaron Ballman76050722014-04-04 15:13:57 +0000711 // If the type does not have a capability, see if the components of the
712 // expression have capabilities. This allows for writing C code where the
713 // capability may be on the type, and the expression is a capability
714 // boolean logic expression. Eg) requires_capability(A || B && !C)
715 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000716 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Erich Keane44bacdf2018-08-09 13:21:32 +0000717 << AL << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000718
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000719 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000720 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000721}
722
Chris Lattner58418ff2008-06-29 00:16:31 +0000723//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000724// Attribute Implementations
725//===----------------------------------------------------------------------===//
726
Erich Keanee891aa92018-07-13 15:07:47 +0000727static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000728 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000729 return;
730
Michael Han99315932013-01-24 16:46:58 +0000731 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000732 PtGuardedVarAttr(AL.getRange(), S.Context,
733 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000734}
735
Erich Keanee891aa92018-07-13 15:07:47 +0000736static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000737 Expr *&Arg) {
738 SmallVector<Expr *, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000739 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000740 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000741 unsigned Size = Args.size();
742 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000743 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000744
Michael Han3be3b442012-07-23 18:48:41 +0000745 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000746
Michael Han3be3b442012-07-23 18:48:41 +0000747 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000748}
749
Erich Keanee891aa92018-07-13 15:07:47 +0000750static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000751 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000752 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000753 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000754
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000755 D->addAttr(::new (S.Context) GuardedByAttr(
756 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000757}
758
Erich Keanee891aa92018-07-13 15:07:47 +0000759static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000760 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000761 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000762 return;
763
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000764 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000765 return;
766
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000767 D->addAttr(::new (S.Context) PtGuardedByAttr(
768 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000769}
770
Erich Keanee891aa92018-07-13 15:07:47 +0000771static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000772 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000773 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000774 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000775
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000776 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000777 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000778 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000779 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000780 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000781 }
782
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000783 // Check that all arguments are lockable objects.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000784 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000785 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000786 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000787
Michael Han3be3b442012-07-23 18:48:41 +0000788 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000789}
790
Erich Keanee891aa92018-07-13 15:07:47 +0000791static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000792 SmallVector<Expr *, 1> Args;
793 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000794 return;
795
796 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000797 D->addAttr(::new (S.Context) AcquiredAfterAttr(
798 AL.getRange(), S.Context, StartArg, Args.size(),
799 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000800}
801
Erich Keanee891aa92018-07-13 15:07:47 +0000802static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000803 SmallVector<Expr *, 1> Args;
804 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000805 return;
806
807 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000808 D->addAttr(::new (S.Context) AcquiredBeforeAttr(
809 AL.getRange(), S.Context, StartArg, Args.size(),
810 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000811}
812
Erich Keanee891aa92018-07-13 15:07:47 +0000813static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000814 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000815 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000816 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000817 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000818
Michael Han3be3b442012-07-23 18:48:41 +0000819 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000820}
821
Erich Keanee891aa92018-07-13 15:07:47 +0000822static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000823 SmallVector<Expr *, 1> Args;
824 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000825 return;
826
827 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000828 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000829 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000830 AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size,
831 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000832}
833
834static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000835 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000836 SmallVector<Expr *, 1> Args;
837 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000838 return;
839
840 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000841 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000842 D->addAttr(::new (S.Context) AssertExclusiveLockAttr(
843 AL.getRange(), S.Context, StartArg, Size,
844 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000845}
846
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000847/// Checks to be sure that the given parameter number is in bounds, and
Aaron Ballman836684a2018-02-25 20:40:06 +0000848/// is an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000849/// false.
850///
Aaron Ballman836684a2018-02-25 20:40:06 +0000851/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000852template <typename AttrInfo>
853static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Joel E. Denny81508102018-03-13 14:51:22 +0000854 const AttrInfo &AI, unsigned AttrArgNo) {
Aaron Ballman836684a2018-02-25 20:40:06 +0000855 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
856 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
Joel E. Denny81508102018-03-13 14:51:22 +0000857 ParamIdx Idx;
Aaron Ballman836684a2018-02-25 20:40:06 +0000858 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000859 Idx))
860 return false;
861
Joel E. Denny81508102018-03-13 14:51:22 +0000862 const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +0000863 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000864 SourceLocation SrcLoc = AttrArg->getBeginLoc();
Erich Keane623efd82017-03-30 21:48:55 +0000865 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +0000866 << AI << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000867 return false;
868 }
869 return true;
870}
871
Erich Keanee891aa92018-07-13 15:07:47 +0000872static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000873 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
874 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000875 return;
876
877 const auto *FD = cast<FunctionDecl>(D);
878 if (!FD->getReturnType()->isPointerType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000879 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
George Burgess IVe3763372016-12-22 02:50:20 +0000880 return;
881 }
882
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000883 const Expr *SizeExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +0000884 int SizeArgNoVal;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000885 // Parameter indices are 1-indexed, hence Index=1
Joel E. Denny81508102018-03-13 14:51:22 +0000886 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Index=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000887 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000888 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0))
George Burgess IVe3763372016-12-22 02:50:20 +0000889 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000890 ParamIdx SizeArgNo(SizeArgNoVal, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000891
Joel E. Denny81508102018-03-13 14:51:22 +0000892 ParamIdx NumberArgNo;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000893 if (AL.getNumArgs() == 2) {
894 const Expr *NumberExpr = AL.getArgAsExpr(1);
Joel E. Denny81508102018-03-13 14:51:22 +0000895 int Val;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000896 // Parameter indices are 1-based, hence Index=2
Joel E. Denny81508102018-03-13 14:51:22 +0000897 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Index=*/2))
George Burgess IVe3763372016-12-22 02:50:20 +0000898 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000899 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000900 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000901 NumberArgNo = ParamIdx(Val, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000902 }
903
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000904 D->addAttr(::new (S.Context)
905 AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo,
906 AL.getAttributeSpellingListIndex()));
George Burgess IVe3763372016-12-22 02:50:20 +0000907}
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000908
Erich Keanee891aa92018-07-13 15:07:47 +0000909static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000910 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000911 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000912 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000913
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000914 if (!isIntOrBool(AL.getArgAsExpr(0))) {
915 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000916 << AL << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000917 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000918 }
919
920 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000921 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000922
Michael Han3be3b442012-07-23 18:48:41 +0000923 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000924}
925
Michael Hana9171bc2012-08-03 17:40:43 +0000926static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000927 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000928 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000929 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000930 return;
931
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000932 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
933 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(),
934 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000935}
936
Michael Hana9171bc2012-08-03 17:40:43 +0000937static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000938 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000939 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000940 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000941 return;
942
Nico Weber462fd1e2015-01-07 23:50:05 +0000943 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000944 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(),
945 Args.size(), AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000946}
947
Erich Keanee891aa92018-07-13 15:07:47 +0000948static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000949 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000950 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000951 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000952 unsigned Size = Args.size();
953 if (Size == 0)
954 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000955
Michael Han99315932013-01-24 16:46:58 +0000956 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000957 LockReturnedAttr(AL.getRange(), S.Context, Args[0],
958 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000959}
960
Erich Keanee891aa92018-07-13 15:07:47 +0000961static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000962 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000963 return;
964
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000965 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000966 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000967 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000968 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000969 if (Size == 0)
970 return;
971 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000972
Michael Han99315932013-01-24 16:46:58 +0000973 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000974 LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size,
975 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000976}
977
Erich Keanee891aa92018-07-13 15:07:47 +0000978static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
George Burgess IV177399e2017-01-09 04:12:14 +0000979 Expr *&Cond, StringRef &Msg) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000980 Cond = AL.getArgAsExpr(0);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000981 if (!Cond->isTypeDependent()) {
982 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
983 if (Converted.isInvalid())
George Burgess IV177399e2017-01-09 04:12:14 +0000984 return false;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000985 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000986 }
987
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000988 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000989 return false;
990
991 if (Msg.empty())
992 Msg = "<no message provided>";
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000993
994 SmallVector<PartialDiagnosticAt, 8> Diags;
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000995 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000996 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
997 Diags)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000998 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
George Burgess IV0d546532016-11-10 21:47:12 +0000999 for (const PartialDiagnosticAt &PDiag : Diags)
1000 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +00001001 return false;
1002 }
1003 return true;
1004}
1005
Erich Keanee891aa92018-07-13 15:07:47 +00001006static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001007 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001008
1009 Expr *Cond;
1010 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001011 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001012 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001013 EnableIfAttr(AL.getRange(), S.Context, Cond, Msg,
1014 AL.getAttributeSpellingListIndex()));
George Burgess IV177399e2017-01-09 04:12:14 +00001015}
1016
1017namespace {
1018/// Determines if a given Expr references any of the given function's
1019/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
1020class ArgumentDependenceChecker
1021 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
1022#ifndef NDEBUG
1023 const CXXRecordDecl *ClassType;
1024#endif
1025 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
1026 bool Result;
1027
1028public:
1029 ArgumentDependenceChecker(const FunctionDecl *FD) {
1030#ifndef NDEBUG
1031 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1032 ClassType = MD->getParent();
1033 else
1034 ClassType = nullptr;
1035#endif
1036 Parms.insert(FD->param_begin(), FD->param_end());
1037 }
1038
1039 bool referencesArgs(Expr *E) {
1040 Result = false;
1041 TraverseStmt(E);
1042 return Result;
1043 }
1044
1045 bool VisitCXXThisExpr(CXXThisExpr *E) {
1046 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
1047 "`this` doesn't refer to the enclosing class?");
1048 Result = true;
1049 return false;
1050 }
1051
1052 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1053 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1054 if (Parms.count(PVD)) {
1055 Result = true;
1056 return false;
1057 }
1058 return true;
1059 }
1060};
1061}
1062
Erich Keanee891aa92018-07-13 15:07:47 +00001063static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001064 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001065
1066 Expr *Cond;
1067 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001068 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001069 return;
1070
1071 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001072 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +00001073 return;
1074
1075 DiagnoseIfAttr::DiagnosticType DiagType;
1076 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001077 S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
George Burgess IV177399e2017-01-09 04:12:14 +00001078 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001079 return;
1080 }
1081
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001082 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +00001083 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001084 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +00001085 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001086 AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent,
1087 cast<NamedDecl>(D), AL.getAttributeSpellingListIndex()));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001088}
1089
Erich Keanee891aa92018-07-13 15:07:47 +00001090static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001091 if (D->hasAttr<PassObjectSizeAttr>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001092 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001093 return;
1094 }
1095
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001096 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001097 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001098 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001099 return;
1100
1101 // pass_object_size's argument is passed in as the second argument of
1102 // __builtin_object_size. So, it has the same constraints as that second
1103 // argument; namely, it must be in the range [0, 3].
1104 if (Type > 3) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001105 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00001106 << AL << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001107 return;
1108 }
1109
1110 // pass_object_size is only supported on constant pointer parameters; as a
1111 // kindness to users, we allow the parameter to be non-const for declarations.
1112 // At this point, we have no clue if `D` belongs to a function declaration or
1113 // definition, so we defer the constness check until later.
1114 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001115 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001116 return;
1117 }
1118
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001119 D->addAttr(::new (S.Context) PassObjectSizeAttr(
1120 AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex()));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001121}
1122
Erich Keanee891aa92018-07-13 15:07:47 +00001123static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001124 ConsumableAttr::ConsumedState DefaultState;
1125
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001126 if (AL.isArgIdent(0)) {
1127 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001128 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1129 DefaultState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001130 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1131 << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001132 return;
1133 }
David Blaikie16f76d22013-09-06 01:28:43 +00001134 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001135 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001136 << AL << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001137 return;
1138 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001139
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001140 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001141 ConsumableAttr(AL.getRange(), S.Context, DefaultState,
1142 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001143}
1144
1145static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Erich Keanee891aa92018-07-13 15:07:47 +00001146 const ParsedAttr &AL) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001147 ASTContext &CurrContext = S.getASTContext();
1148 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001149
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001150 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1151 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001152 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001153 RD->getNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001154
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001155 return false;
1156 }
1157 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001158
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001159 return true;
1160}
1161
Erich Keanee891aa92018-07-13 15:07:47 +00001162static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001163 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001164 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001165
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001166 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001167 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001168
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001169 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001170 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001171 CallableWhenAttr::ConsumedState CallableState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001172
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001173 StringRef StateString;
1174 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001175 if (AL.isArgIdent(ArgIndex)) {
1176 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001177 StateString = Ident->Ident->getName();
1178 Loc = Ident->Loc;
1179 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001180 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001181 return;
1182 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001183
1184 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001185 CallableState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001186 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001187 return;
1188 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001189
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001190 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001191 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001192
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001193 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001194 CallableWhenAttr(AL.getRange(), S.Context, States.data(),
1195 States.size(), AL.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001196}
1197
Erich Keanee891aa92018-07-13 15:07:47 +00001198static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001199 ParamTypestateAttr::ConsumedState ParamState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001200
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001201 if (AL.isArgIdent(0)) {
1202 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001203 StringRef StateString = Ident->Ident->getName();
1204
1205 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1206 ParamState)) {
1207 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00001208 << AL << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001209 return;
1210 }
1211 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001212 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1213 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001214 return;
1215 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001216
DeLesley Hutchins69391772013-10-17 23:23:53 +00001217 // FIXME: This check is currently being done in the analysis. It can be
1218 // enabled here only after the parser propagates attributes at
1219 // template specialization definition, not declaration.
1220 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1221 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1222 //
1223 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001224 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001225 // ReturnType.getAsString();
1226 // return;
1227 //}
Fangrui Song6907ce22018-07-30 19:24:48 +00001228
DeLesley Hutchins69391772013-10-17 23:23:53 +00001229 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001230 ParamTypestateAttr(AL.getRange(), S.Context, ParamState,
1231 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001232}
1233
Erich Keanee891aa92018-07-13 15:07:47 +00001234static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001235 ReturnTypestateAttr::ConsumedState ReturnState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001236
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001237 if (AL.isArgIdent(0)) {
1238 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001239 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1240 ReturnState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001241 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1242 << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001243 return;
1244 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001245 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001246 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1247 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001248 return;
1249 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001250
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001251 // FIXME: This check is currently being done in the analysis. It can be
1252 // enabled here only after the parser propagates attributes at
1253 // template specialization definition, not declaration.
1254 //QualType ReturnType;
1255 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001256 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1257 // ReturnType = Param->getType();
1258 //
1259 //} else if (const CXXConstructorDecl *Constructor =
1260 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001261 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001262 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001263 //} else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001264 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001265 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1266 //}
1267 //
1268 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1269 //
1270 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1271 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1272 // ReturnType.getAsString();
1273 // return;
1274 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001275
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001276 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001277 ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState,
1278 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001279}
1280
Erich Keanee891aa92018-07-13 15:07:47 +00001281static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001282 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001283 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001284
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001285 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001286 if (AL.isArgIdent(0)) {
1287 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001288 StringRef Param = Ident->Ident->getName();
1289 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001290 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1291 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001292 return;
1293 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001294 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001295 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1296 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001297 return;
1298 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001299
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001300 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001301 SetTypestateAttr(AL.getRange(), S.Context, NewState,
1302 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001303}
1304
Erich Keanee891aa92018-07-13 15:07:47 +00001305static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001306 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001307 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001308
1309 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001310 if (AL.isArgIdent(0)) {
1311 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001312 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001313 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001314 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1315 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001316 return;
1317 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001318 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001319 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1320 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001321 return;
1322 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001323
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001324 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001325 TestTypestateAttr(AL.getRange(), S.Context, TestState,
1326 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001327}
1328
Erich Keanee891aa92018-07-13 15:07:47 +00001329static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001330 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001331 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001332}
1333
Erich Keanee891aa92018-07-13 15:07:47 +00001334static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001335 if (auto *TD = dyn_cast<TagDecl>(D))
1336 TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context,
1337 AL.getAttributeSpellingListIndex()));
1338 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001339 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1340 !FD->getType()->isIncompleteType() &&
1341 FD->isBitField() &&
1342 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001343
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001344 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1345 if (BitfieldByteAligned)
1346 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001347 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001348 << AL << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001349 else
1350 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001351 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001352 } else {
1353 // Report warning about changed offset in the newer compiler versions.
1354 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001355 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001356
1357 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001358 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001359 }
1360
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001361 } else
Erich Keane44bacdf2018-08-09 13:21:32 +00001362 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001363}
1364
Erich Keanee891aa92018-07-13 15:07:47 +00001365static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001366 // The IBOutlet/IBOutletCollection attributes only apply to instance
1367 // variables or properties of Objective-C classes. The outlet must also
1368 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001369 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001370 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001371 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001372 << AL << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001373 return false;
1374 }
1375 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001376 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001377 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001378 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001379 << AL << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001380 return false;
1381 }
1382 }
1383 else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001384 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001385 return false;
1386 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001387
Ted Kremenek7fd17232011-09-29 07:02:25 +00001388 return true;
1389}
1390
Erich Keanee891aa92018-07-13 15:07:47 +00001391static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001392 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001393 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001394
Michael Han99315932013-01-24 16:46:58 +00001395 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001396 IBOutletAttr(AL.getRange(), S.Context,
1397 AL.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001398}
1399
Erich Keanee891aa92018-07-13 15:07:47 +00001400static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001401
1402 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001403 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001404 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001405 return;
1406 }
1407
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001408 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001409 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001410
Richard Smithb1f9a282013-10-31 01:56:18 +00001411 ParsedType PT;
1412
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001413 if (AL.hasParsedType())
1414 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001415 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001416 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001417 S.getScopeForContext(D->getDeclContext()->getParent()));
1418 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001419 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001420 return;
1421 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001422 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001423
Craig Topperc3ec1492014-05-26 06:22:03 +00001424 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001425 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1426 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001427 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001428
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001429 // Diagnose use of non-object type in iboutletcollection attribute.
1430 // FIXME. Gnu attribute extension ignores use of builtin types in
1431 // attributes. So, __attribute__((iboutletcollection(char))) will be
1432 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001433 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001434 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001435 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1436 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001437 return;
1438 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001439
Michael Han99315932013-01-24 16:46:58 +00001440 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001441 IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc,
1442 AL.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001443}
1444
Hal Finkelee90a222014-09-26 05:04:30 +00001445bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1446 if (RefOkay) {
1447 if (T->isReferenceType())
1448 return true;
1449 } else {
1450 T = T.getNonReferenceType();
1451 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001452
Hal Finkelee90a222014-09-26 05:04:30 +00001453 // The nonnull attribute, and other similar attributes, can be applied to a
1454 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001455 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001456 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1457 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001458 for (const auto *I : UD->fields()) {
1459 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001460 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1461 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001462 }
1463 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001464 }
1465
1466 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001467}
1468
Erich Keanee891aa92018-07-13 15:07:47 +00001469static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001470 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001471 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001472 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001473 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001474 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001475 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001476 << AL << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001477 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001478 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001479 << AL << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001480 return false;
1481 }
1482 return true;
1483}
1484
Erich Keanee891aa92018-07-13 15:07:47 +00001485static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001486 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001487 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1488 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001489 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001490 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001491 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001492
1493 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001494 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1495 !attrNonNullArgCheck(
1496 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1497 Ex->getSourceRange(),
1498 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001499 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001500
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001501 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001502 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001503
1504 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001505 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1506 // check if the attribute came from a macro expansion or a template
1507 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001508 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001509 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001510 bool AnyPointers = isFunctionOrMethodVariadic(D);
1511 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1512 I != E && !AnyPointers; ++I) {
1513 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001514 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001515 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001516 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001517
Richard Smith588bd9b2014-08-27 04:59:42 +00001518 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001519 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001520 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001521
Joel E. Denny81508102018-03-13 14:51:22 +00001522 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001523 unsigned Size = NonNullArgs.size();
1524 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001525 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001526 NonNullAttr(AL.getRange(), S.Context, Start, Size,
1527 AL.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001528}
1529
Jordan Rosec9399072014-02-11 17:27:59 +00001530static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00001531 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001532 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001533 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001534 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001535 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001536 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001537 << D->getSourceRange();
1538 }
1539 return;
1540 }
1541
1542 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001543 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001544 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001545 return;
1546
1547 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001548 NonNullAttr(AL.getRange(), S.Context, nullptr, 0,
1549 AL.getAttributeSpellingListIndex()));
Jordan Rosec9399072014-02-11 17:27:59 +00001550}
1551
Erich Keanee891aa92018-07-13 15:07:47 +00001552static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001553 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001554 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001555 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001556 /* isReturnValue */ true))
1557 return;
1558
1559 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001560 ReturnsNonNullAttr(AL.getRange(), S.Context,
1561 AL.getAttributeSpellingListIndex()));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001562}
1563
Erich Keanee891aa92018-07-13 15:07:47 +00001564static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001565 if (D->isInvalidDecl())
1566 return;
1567
1568 // noescape only applies to pointer types.
1569 QualType T = cast<ParmVarDecl>(D)->getType();
1570 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001571 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001572 << AL << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001573 return;
1574 }
1575
1576 D->addAttr(::new (S.Context) NoEscapeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001577 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001578}
1579
Erich Keanee891aa92018-07-13 15:07:47 +00001580static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001581 Expr *E = AL.getArgAsExpr(0),
1582 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1583 S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE,
1584 AL.getAttributeSpellingListIndex());
Hal Finkelee90a222014-09-26 05:04:30 +00001585}
1586
Erich Keanee891aa92018-07-13 15:07:47 +00001587static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001588 S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0),
1589 AL.getAttributeSpellingListIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001590}
1591
Hal Finkelee90a222014-09-26 05:04:30 +00001592void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1593 Expr *OE, unsigned SpellingListIndex) {
1594 QualType ResultType = getFunctionOrMethodResultType(D);
1595 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1596
1597 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1598 SourceLocation AttrLoc = AttrRange.getBegin();
1599
1600 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1601 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1602 << &TmpAttr << AttrRange << SR;
1603 return;
1604 }
1605
1606 if (!E->isValueDependent()) {
1607 llvm::APSInt I(64);
1608 if (!E->isIntegerConstantExpr(I, Context)) {
1609 if (OE)
1610 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1611 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1612 << E->getSourceRange();
1613 else
1614 Diag(AttrLoc, diag::err_attribute_argument_type)
1615 << &TmpAttr << AANT_ArgumentIntegerConstant
1616 << E->getSourceRange();
1617 return;
1618 }
1619
1620 if (!I.isPowerOf2()) {
1621 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1622 << E->getSourceRange();
1623 return;
1624 }
1625 }
1626
1627 if (OE) {
1628 if (!OE->isValueDependent()) {
1629 llvm::APSInt I(64);
1630 if (!OE->isIntegerConstantExpr(I, Context)) {
1631 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1632 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1633 << OE->getSourceRange();
1634 return;
1635 }
1636 }
1637 }
1638
1639 D->addAttr(::new (Context)
1640 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1641}
1642
Erich Keane623efd82017-03-30 21:48:55 +00001643void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr,
1644 unsigned SpellingListIndex) {
1645 QualType ResultType = getFunctionOrMethodResultType(D);
1646
Joel E. Denny81508102018-03-13 14:51:22 +00001647 AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex);
Erich Keane623efd82017-03-30 21:48:55 +00001648 SourceLocation AttrLoc = AttrRange.getBegin();
1649
1650 if (!ResultType->isDependentType() &&
1651 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1652 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1653 << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D);
1654 return;
1655 }
1656
Joel E. Denny81508102018-03-13 14:51:22 +00001657 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001658 const auto *FuncDecl = cast<FunctionDecl>(D);
1659 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Joel E. Denny81508102018-03-13 14:51:22 +00001660 /*AttrArgNo=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001661 return;
1662
Joel E. Denny81508102018-03-13 14:51:22 +00001663 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001664 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001665 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001666 << &TmpAttr
1667 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001668 return;
1669 }
1670
Joel E. Denny81508102018-03-13 14:51:22 +00001671 D->addAttr(::new (Context)
1672 AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex));
Erich Keane623efd82017-03-30 21:48:55 +00001673}
1674
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001675/// Normalize the attribute, __foo__ becomes foo.
1676/// Returns true if normalization was applied.
1677static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001678 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1679 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001680 AttrName = AttrName.drop_front(2).drop_back(2);
1681 return true;
1682 }
1683 return false;
1684}
1685
Erich Keanee891aa92018-07-13 15:07:47 +00001686static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001687 // This attribute must be applied to a function declaration. The first
1688 // argument to the attribute must be an identifier, the name of the resource,
1689 // for example: malloc. The following arguments must be argument indexes, the
1690 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001691 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001692 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001693 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001694
Aaron Ballman00e99962013-08-31 01:11:41 +00001695 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001696 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001697 << AL << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001698 return;
1699 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001700
Richard Smith852e9ce2013-11-27 01:46:48 +00001701 // Figure out our Kind.
1702 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001703 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001704 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001705
Richard Smith852e9ce2013-11-27 01:46:48 +00001706 // Check arguments.
1707 switch (K) {
1708 case OwnershipAttr::Takes:
1709 case OwnershipAttr::Holds:
1710 if (AL.getNumArgs() < 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001711 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001712 return;
1713 }
1714 break;
1715 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001716 if (AL.getNumArgs() > 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001717 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001718 return;
1719 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001720 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001721 }
1722
Richard Smith852e9ce2013-11-27 01:46:48 +00001723 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001724
Richard Smith852e9ce2013-11-27 01:46:48 +00001725 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001726 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001727 Module = &S.PP.getIdentifierTable().get(ModuleName);
1728 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001729
Joel E. Denny81508102018-03-13 14:51:22 +00001730 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001731 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1732 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001733 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001734 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001735 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001736
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001737 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001738 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001739 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001740 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001741 case OwnershipAttr::Takes:
1742 case OwnershipAttr::Holds:
1743 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1744 Err = 0;
1745 break;
1746 case OwnershipAttr::Returns:
1747 if (!T->isIntegerType())
1748 Err = 1;
1749 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001750 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001751 if (-1 != Err) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001752 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1753 << Ex->getSourceRange();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001754 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001755 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001756
1757 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001758 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001759 // Cannot have two ownership attributes of different kinds for the same
1760 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001761 if (I->getOwnKind() != K && I->args_end() !=
1762 std::find(I->args_begin(), I->args_end(), Idx)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001763 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001764 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001765 } else if (K == OwnershipAttr::Returns &&
1766 I->getOwnKind() == OwnershipAttr::Returns) {
1767 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001768 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001769 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1770 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001771 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001772 if (I->args_size())
1773 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001774 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001775 return;
1776 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001777 }
1778 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001779 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001780 }
1781
Joel E. Denny81508102018-03-13 14:51:22 +00001782 ParamIdx *Start = OwnershipArgs.data();
1783 unsigned Size = OwnershipArgs.size();
1784 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001785 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001786 OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size,
1787 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001788}
1789
Erich Keanee891aa92018-07-13 15:07:47 +00001790static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001791 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001792 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001793 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001794 return;
1795 }
1796
1797 // gcc rejects
1798 // class c {
1799 // static int a __attribute__((weakref ("v2")));
1800 // static int b() __attribute__((weakref ("f3")));
1801 // };
1802 // and ignores the attributes of
1803 // void f(void) {
1804 // static int a __attribute__((weakref ("v2")));
1805 // }
1806 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001807 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001808 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001809 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1810 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001811 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001812 }
1813
1814 // The GCC manual says
1815 //
1816 // At present, a declaration to which `weakref' is attached can only
1817 // be `static'.
1818 //
1819 // It also says
1820 //
1821 // Without a TARGET,
1822 // given as an argument to `weakref' or to `alias', `weakref' is
1823 // equivalent to `weak'.
1824 //
1825 // gcc 4.4.1 will accept
1826 // int a7 __attribute__((weakref));
1827 // as
1828 // int a7 __attribute__((weak));
1829 // This looks like a bug in gcc. We reject that for now. We should revisit
1830 // it if this behaviour is actually used.
1831
Rafael Espindolac18086a2010-02-23 22:00:30 +00001832 // GCC rejects
1833 // static ((alias ("y"), weakref)).
1834 // Should we? How to check that weakref is before or after alias?
1835
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001836 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1837 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1838 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001839 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001840 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001841 // GCC will accept anything as the argument of weakref. Should we
1842 // check for an existing decl?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001843 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1844 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001845
Michael Han99315932013-01-24 16:46:58 +00001846 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001847 WeakRefAttr(AL.getRange(), S.Context,
1848 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001849}
1850
Erich Keanee891aa92018-07-13 15:07:47 +00001851static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001852 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001853 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001854 return;
1855
1856 // Aliases should be on declarations, not definitions.
1857 const auto *FD = cast<FunctionDecl>(D);
1858 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001859 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001860 return;
1861 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001862
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001863 D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str,
1864 AL.getAttributeSpellingListIndex()));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001865}
1866
Erich Keanee891aa92018-07-13 15:07:47 +00001867static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001868 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001869 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001870 return;
1871
Douglas Gregore8bbc122011-09-02 00:18:52 +00001872 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001873 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001874 return;
1875 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001876 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001877 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001878 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001879
David Majnemer2dc81462015-01-19 09:00:28 +00001880 // Aliases should be on declarations, not definitions.
1881 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1882 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001883 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001884 return;
1885 }
1886 } else {
1887 const auto *VD = cast<VarDecl>(D);
1888 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001889 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001890 return;
1891 }
1892 }
1893
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001894 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001895
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001896 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1897 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001898}
1899
Erich Keanee891aa92018-07-13 15:07:47 +00001900static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001901 StringRef Model;
1902 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001903 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001904 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001905 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001906
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001907 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001908 if (Model != "global-dynamic" && Model != "local-dynamic"
1909 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001910 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001911 return;
1912 }
1913
Michael Han99315932013-01-24 16:46:58 +00001914 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001915 TLSModelAttr(AL.getRange(), S.Context, Model,
1916 AL.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001917}
1918
Erich Keanee891aa92018-07-13 15:07:47 +00001919static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001920 QualType ResultType = getFunctionOrMethodResultType(D);
1921 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1922 D->addAttr(::new (S.Context) RestrictAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001923 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
David Majnemer631a90b2015-02-04 07:23:21 +00001924 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001925 }
1926
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001927 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001928 << AL << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001929}
1930
Erich Keane3efe0022018-07-20 14:13:28 +00001931static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1932 FunctionDecl *FD = cast<FunctionDecl>(D);
Erich Keane659c8712018-09-10 14:31:56 +00001933
1934 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1935 if (MD->getParent()->isLambda()) {
1936 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
1937 return;
1938 }
1939 }
1940
Erich Keane3efe0022018-07-20 14:13:28 +00001941 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1942 return;
1943
1944 SmallVector<IdentifierInfo *, 8> CPUs;
1945 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
1946 if (!AL.isArgIdent(ArgNo)) {
1947 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001948 << AL << AANT_ArgumentIdentifier;
Erich Keane3efe0022018-07-20 14:13:28 +00001949 return;
1950 }
1951
1952 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1953 StringRef CPUName = CPUArg->Ident->getName().trim();
1954
1955 if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1956 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1957 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1958 return;
1959 }
1960
1961 const TargetInfo &Target = S.Context.getTargetInfo();
1962 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1963 return Target.CPUSpecificManglingCharacter(CPUName) ==
1964 Target.CPUSpecificManglingCharacter(Cur->getName());
1965 })) {
1966 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1967 return;
1968 }
1969 CPUs.push_back(CPUArg->Ident);
1970 }
1971
1972 FD->setIsMultiVersion(true);
1973 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
1974 D->addAttr(::new (S.Context) CPUSpecificAttr(
1975 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
1976 AL.getAttributeSpellingListIndex()));
1977 else
1978 D->addAttr(::new (S.Context) CPUDispatchAttr(
1979 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
1980 AL.getAttributeSpellingListIndex()));
1981}
1982
Erich Keanee891aa92018-07-13 15:07:47 +00001983static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001984 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001985 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00001986 << AL << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001987 return;
1988 }
1989
Erich Keane44bacdf2018-08-09 13:21:32 +00001990 if (CommonAttr *CA = S.mergeCommonAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001991 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001992}
1993
Erich Keanee891aa92018-07-13 15:07:47 +00001994static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001995 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
Charles Davis0e379112016-08-08 21:19:08 +00001996 return;
1997
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001998 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001999 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
2000 const auto &Arch = Triple.getArch();
2001 if (Arch != llvm::Triple::x86 &&
2002 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002003 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
Erich Keane44bacdf2018-08-09 13:21:32 +00002004 << AL << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002005 return;
2006 }
2007 }
2008
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002009 D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context,
2010 AL.getAttributeSpellingListIndex()));
Charles Davis0e379112016-08-08 21:19:08 +00002011}
2012
Erich Keanee891aa92018-07-13 15:07:47 +00002013static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002014 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00002015
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002016 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002017 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002018 << Attrs << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002019 return;
2020 }
2021
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002022 D->addAttr(::new (S.Context) NoReturnAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00002023 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002024}
2025
Erich Keanee891aa92018-07-13 15:07:47 +00002026static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00002027 if (!S.getLangOpts().CFProtectionBranch)
2028 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
2029 else
2030 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00002031}
2032
Erich Keanee891aa92018-07-13 15:07:47 +00002033bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002034 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
2035 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002036 return true;
2037 }
2038
2039 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002040}
2041
Erich Keanee891aa92018-07-13 15:07:47 +00002042bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002043 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002044 if (!AL.existsInTarget(Context.getTargetInfo())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002045 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002046 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002047 return true;
2048 }
2049
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002050 return false;
2051}
2052
Erich Keanee891aa92018-07-13 15:07:47 +00002053static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2054
Ted Kremenek5295ce82010-08-19 00:51:58 +00002055 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2056 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00002057 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002058 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00002059 if (!VD || (!VD->getType()->isBlockPointerType() &&
2060 !VD->getType()->isFunctionPointerType())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002061 S.Diag(AL.getLoc(), AL.isCXX11Attribute()
2062 ? diag::err_attribute_wrong_decl_type
2063 : diag::warn_attribute_wrong_decl_type)
2064 << AL << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00002065 return;
2066 }
2067 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002068
Michael Han99315932013-01-24 16:46:58 +00002069 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002070 AnalyzerNoReturnAttr(AL.getRange(), S.Context,
2071 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002072}
2073
John Thompsoncdb847ba2010-08-09 21:53:52 +00002074// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00002075static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2076 /*
2077 Returning a Vector Class in Registers
2078
2079 According to the PPU ABI specifications, a class with a single member of
2080 vector type is returned in memory when used as the return value of a
2081 function.
2082 This results in inefficient code when implementing vector classes. To return
2083 the value in a single vector register, add the vecreturn attribute to the
2084 class definition. This attribute is also applicable to struct types.
2085
2086 Example:
2087
2088 struct Vector
2089 {
2090 __vector float xyzw;
2091 } __attribute__((vecreturn));
2092
2093 Vector Add(Vector lhs, Vector rhs)
2094 {
2095 Vector result;
2096 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2097 return result; // This will be returned in a register
2098 }
2099 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002100 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002101 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002102 return;
2103 }
2104
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002105 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002106 int count = 0;
2107
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002108 if (!isa<CXXRecordDecl>(R)) {
2109 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002110 return;
2111 }
2112
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002113 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2114 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002115 return;
2116 }
2117
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002118 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002119 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002120 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002121 return;
2122 }
2123 count++;
2124 }
2125
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002126 D->addAttr(::new (S.Context) VecReturnAttr(
2127 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002128}
2129
Richard Smithe233fbf2013-01-28 22:42:45 +00002130static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002131 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002132 if (isa<ParmVarDecl>(D)) {
2133 // [[carries_dependency]] can only be applied to a parameter if it is a
2134 // parameter of a function declaration or lambda.
2135 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002136 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002137 diag::err_carries_dependency_param_not_function_decl);
2138 return;
2139 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002140 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002141
2142 D->addAttr(::new (S.Context) CarriesDependencyAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002143 AL.getRange(), S.Context,
2144 AL.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002145}
2146
Erich Keanee891aa92018-07-13 15:07:47 +00002147static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002148 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002149
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002150 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002151 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002152 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Erich Keane44bacdf2018-08-09 13:21:32 +00002153 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002154
2155 D->addAttr(::new (S.Context) UnusedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002156 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002157}
2158
Erich Keanee891aa92018-07-13 15:07:47 +00002159static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002160 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002161 if (AL.getNumArgs() &&
2162 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002163 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002164
Michael Han99315932013-01-24 16:46:58 +00002165 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002166 ConstructorAttr(AL.getRange(), S.Context, priority,
2167 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002168}
2169
Erich Keanee891aa92018-07-13 15:07:47 +00002170static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002171 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002172 if (AL.getNumArgs() &&
2173 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002174 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002175
Michael Han99315932013-01-24 16:46:58 +00002176 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002177 DestructorAttr(AL.getRange(), S.Context, priority,
2178 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002179}
2180
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002181template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002182static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002183 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002184 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002185 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002186 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002187
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002188 D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str,
2189 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002190}
2191
Ted Kremenek438f8db2014-02-22 01:06:05 +00002192static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002193 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002194 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002195 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
Erich Keane44bacdf2018-08-09 13:21:32 +00002196 << AL << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002197 return;
2198 }
2199
Ted Kremenek28eace62013-11-23 01:01:34 +00002200 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002201 ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context,
2202 AL.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00002203}
2204
Jordy Rose740b0c22012-05-08 03:27:22 +00002205static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2206 IdentifierInfo *Platform,
2207 VersionTuple Introduced,
2208 VersionTuple Deprecated,
2209 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002210 StringRef PlatformName
2211 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2212 if (PlatformName.empty())
2213 PlatformName = Platform->getName();
2214
2215 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2216 // of these steps are needed).
2217 if (!Introduced.empty() && !Deprecated.empty() &&
2218 !(Introduced <= Deprecated)) {
2219 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2220 << 1 << PlatformName << Deprecated.getAsString()
2221 << 0 << Introduced.getAsString();
2222 return true;
2223 }
2224
2225 if (!Introduced.empty() && !Obsoleted.empty() &&
2226 !(Introduced <= Obsoleted)) {
2227 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2228 << 2 << PlatformName << Obsoleted.getAsString()
2229 << 0 << Introduced.getAsString();
2230 return true;
2231 }
2232
2233 if (!Deprecated.empty() && !Obsoleted.empty() &&
2234 !(Deprecated <= Obsoleted)) {
2235 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2236 << 2 << PlatformName << Obsoleted.getAsString()
2237 << 1 << Deprecated.getAsString();
2238 return true;
2239 }
2240
2241 return false;
2242}
2243
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002244/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002245///
2246/// If either version tuple is empty, then they are assumed to match. If
2247/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2248static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2249 bool BeforeIsOkay) {
2250 if (X.empty() || Y.empty())
2251 return true;
2252
2253 if (X == Y)
2254 return true;
2255
2256 if (BeforeIsOkay && X < Y)
2257 return true;
2258
2259 return false;
2260}
2261
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002262AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002263 IdentifierInfo *Platform,
Manman Ren719a8642016-05-06 21:04:01 +00002264 bool Implicit,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002265 VersionTuple Introduced,
2266 VersionTuple Deprecated,
2267 VersionTuple Obsoleted,
2268 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002269 StringRef Message,
Manman Rend8039df2016-02-22 04:47:24 +00002270 bool IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002271 StringRef Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002272 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00002273 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002274 VersionTuple MergedIntroduced = Introduced;
2275 VersionTuple MergedDeprecated = Deprecated;
2276 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002277 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002278 bool OverrideOrImpl = false;
2279 switch (AMK) {
2280 case AMK_None:
2281 case AMK_Redeclaration:
2282 OverrideOrImpl = false;
2283 break;
2284
2285 case AMK_Override:
2286 case AMK_ProtocolImplementation:
2287 OverrideOrImpl = true;
2288 break;
2289 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002290
Rafael Espindolac67f2232012-05-10 02:50:16 +00002291 if (D->hasAttrs()) {
2292 AttrVec &Attrs = D->getAttrs();
2293 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002294 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002295 if (!OldAA) {
2296 ++i;
2297 continue;
2298 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002299
Rafael Espindolac67f2232012-05-10 02:50:16 +00002300 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2301 if (OldPlatform != Platform) {
2302 ++i;
2303 continue;
2304 }
2305
Tim Northover7a73cc72015-10-30 16:30:49 +00002306 // If there is an existing availability attribute for this platform that
2307 // is explicit and the new one is implicit use the explicit one and
2308 // discard the new implicit attribute.
Manman Ren719a8642016-05-06 21:04:01 +00002309 if (!OldAA->isImplicit() && Implicit) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002310 return nullptr;
2311 }
2312
2313 // If there is an existing attribute for this platform that is implicit
2314 // and the new attribute is explicit then erase the old one and
2315 // continue processing the attributes.
Manman Ren719a8642016-05-06 21:04:01 +00002316 if (!Implicit && OldAA->isImplicit()) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002317 Attrs.erase(Attrs.begin() + i);
2318 --e;
2319 continue;
2320 }
2321
Rafael Espindolac67f2232012-05-10 02:50:16 +00002322 FoundAny = true;
2323 VersionTuple OldIntroduced = OldAA->getIntroduced();
2324 VersionTuple OldDeprecated = OldAA->getDeprecated();
2325 VersionTuple OldObsoleted = OldAA->getObsoleted();
2326 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002327
Douglas Gregord2a713e2015-09-30 21:27:42 +00002328 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2329 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2330 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002331 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002332 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2333 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002334 int Which = -1;
2335 VersionTuple FirstVersion;
2336 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002337 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002338 Which = 0;
2339 FirstVersion = OldIntroduced;
2340 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002341 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002342 Which = 1;
2343 FirstVersion = Deprecated;
2344 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002345 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002346 Which = 2;
2347 FirstVersion = Obsoleted;
2348 SecondVersion = OldObsoleted;
2349 }
2350
2351 if (Which == -1) {
2352 Diag(OldAA->getLocation(),
2353 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002354 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2355 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002356 } else {
2357 Diag(OldAA->getLocation(),
2358 diag::warn_mismatched_availability_override)
2359 << Which
2360 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002361 << FirstVersion.getAsString() << SecondVersion.getAsString()
2362 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002363 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002364 if (AMK == AMK_Override)
2365 Diag(Range.getBegin(), diag::note_overridden_method);
2366 else
2367 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002368 } else {
2369 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2370 Diag(Range.getBegin(), diag::note_previous_attribute);
2371 }
2372
Rafael Espindolac67f2232012-05-10 02:50:16 +00002373 Attrs.erase(Attrs.begin() + i);
2374 --e;
2375 continue;
2376 }
2377
2378 VersionTuple MergedIntroduced2 = MergedIntroduced;
2379 VersionTuple MergedDeprecated2 = MergedDeprecated;
2380 VersionTuple MergedObsoleted2 = MergedObsoleted;
2381
2382 if (MergedIntroduced2.empty())
2383 MergedIntroduced2 = OldIntroduced;
2384 if (MergedDeprecated2.empty())
2385 MergedDeprecated2 = OldDeprecated;
2386 if (MergedObsoleted2.empty())
2387 MergedObsoleted2 = OldObsoleted;
2388
2389 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2390 MergedIntroduced2, MergedDeprecated2,
2391 MergedObsoleted2)) {
2392 Attrs.erase(Attrs.begin() + i);
2393 --e;
2394 continue;
2395 }
2396
2397 MergedIntroduced = MergedIntroduced2;
2398 MergedDeprecated = MergedDeprecated2;
2399 MergedObsoleted = MergedObsoleted2;
2400 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002401 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002402 }
2403
2404 if (FoundAny &&
2405 MergedIntroduced == Introduced &&
2406 MergedDeprecated == Deprecated &&
2407 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002408 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002409
Douglas Gregord2a713e2015-09-30 21:27:42 +00002410 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002411 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002412 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002413 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002414 !OverrideOrImpl) {
Manman Ren719a8642016-05-06 21:04:01 +00002415 auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002416 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002417 Obsoleted, IsUnavailable, Message,
Manman Ren75bc6762016-03-21 17:30:55 +00002418 IsStrict, Replacement,
2419 AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002420 Avail->setImplicit(Implicit);
2421 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002422 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002423 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002424}
2425
Erich Keanee891aa92018-07-13 15:07:47 +00002426static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002427 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002428 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002429 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2430 unsigned Index = AL.getAttributeSpellingListIndex();
Fangrui Song6907ce22018-07-30 19:24:48 +00002431
Aaron Ballman00e99962013-08-31 01:11:41 +00002432 IdentifierInfo *II = Platform->Ident;
2433 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2434 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2435 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002436
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002437 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002438 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002439 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002440
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002441 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2442 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2443 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2444 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2445 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002446 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002447 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002448 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002449 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002450 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002451 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002452
Michael Wu260e9622018-11-12 02:44:33 +00002453 if (II->isStr("swift")) {
2454 if (Introduced.isValid() || Obsoleted.isValid() ||
2455 (!IsUnavailable && !Deprecated.isValid())) {
2456 S.Diag(AL.getLoc(),
2457 diag::warn_availability_swift_unavailable_deprecated_only);
2458 return;
2459 }
2460 }
2461
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002462 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II,
Manman Ren719a8642016-05-06 21:04:01 +00002463 false/*Implicit*/,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002464 Introduced.Version,
2465 Deprecated.Version,
2466 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002467 IsUnavailable, Str,
Manman Ren75bc6762016-03-21 17:30:55 +00002468 IsStrict, Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002469 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002470 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002471 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002472 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002473
2474 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2475 // matches before the start of the watchOS platform.
2476 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2477 IdentifierInfo *NewII = nullptr;
2478 if (II->getName() == "ios")
2479 NewII = &S.Context.Idents.get("watchos");
2480 else if (II->getName() == "ios_app_extension")
2481 NewII = &S.Context.Idents.get("watchos_app_extension");
2482
2483 if (NewII) {
2484 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2485 if (Version.empty())
2486 return Version;
2487 auto Major = Version.getMajor();
2488 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2489 if (NewMajor >= 2) {
2490 if (Version.getMinor().hasValue()) {
2491 if (Version.getSubminor().hasValue())
2492 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2493 Version.getSubminor().getValue());
2494 else
2495 return VersionTuple(NewMajor, Version.getMinor().getValue());
2496 }
2497 }
2498
2499 return VersionTuple(2, 0);
2500 };
2501
2502 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2503 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2504 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2505
2506 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002507 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002508 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002509 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002510 NewIntroduced,
2511 NewDeprecated,
2512 NewObsoleted,
2513 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002514 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002515 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002516 Sema::AMK_None,
2517 Index);
2518 if (NewAttr)
2519 D->addAttr(NewAttr);
2520 }
2521 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2522 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2523 // matches before the start of the tvOS platform.
2524 IdentifierInfo *NewII = nullptr;
2525 if (II->getName() == "ios")
2526 NewII = &S.Context.Idents.get("tvos");
2527 else if (II->getName() == "ios_app_extension")
2528 NewII = &S.Context.Idents.get("tvos_app_extension");
2529
2530 if (NewII) {
2531 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002532 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002533 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002534 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002535 Introduced.Version,
2536 Deprecated.Version,
2537 Obsoleted.Version,
2538 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002539 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002540 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002541 Sema::AMK_None,
2542 Index);
2543 if (NewAttr)
2544 D->addAttr(NewAttr);
2545 }
2546 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002547}
2548
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002549static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002550 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002551 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002552 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002553 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002554 "Invalid number of arguments in an external_source_symbol attribute");
2555
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002556 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002557 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002558 Language = SE->getString();
2559 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002560 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002561 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002562 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002563
2564 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002565 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2566 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002567}
2568
John McCalld041a9b2013-02-20 01:54:26 +00002569template <class T>
2570static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2571 typename T::VisibilityType value,
2572 unsigned attrSpellingListIndex) {
2573 T *existingAttr = D->getAttr<T>();
2574 if (existingAttr) {
2575 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2576 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002577 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002578 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2579 S.Diag(range.getBegin(), diag::note_previous_attribute);
2580 D->dropAttr<T>();
2581 }
2582 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2583}
2584
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002585VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002586 VisibilityAttr::VisibilityType Vis,
2587 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002588 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2589 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002590}
2591
John McCalld041a9b2013-02-20 01:54:26 +00002592TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2593 TypeVisibilityAttr::VisibilityType Vis,
2594 unsigned AttrSpellingListIndex) {
2595 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2596 AttrSpellingListIndex);
2597}
2598
Erich Keanee891aa92018-07-13 15:07:47 +00002599static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002600 bool isTypeVisibility) {
2601 // Visibility attributes don't mean anything on a typedef.
2602 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002603 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002604 return;
2605 }
2606
2607 // 'type_visibility' can only go on a type or namespace.
2608 if (isTypeVisibility &&
2609 !(isa<TagDecl>(D) ||
2610 isa<ObjCInterfaceDecl>(D) ||
2611 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002612 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002613 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002614 return;
2615 }
2616
Benjamin Kramer70370212013-09-09 15:08:57 +00002617 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002618 StringRef TypeStr;
2619 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002620 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002621 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002622
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002623 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002624 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002625 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2626 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002627 return;
2628 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002629
Aaron Ballman682ee422013-09-11 19:47:58 +00002630 // Complain about attempts to use protected visibility on targets
2631 // (like Darwin) that don't support it.
2632 if (type == VisibilityAttr::Protected &&
2633 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002634 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002635 type = VisibilityAttr::Default;
2636 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002637
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002638 unsigned Index = AL.getAttributeSpellingListIndex();
2639 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002640 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002641 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002642 (TypeVisibilityAttr::VisibilityType) type,
2643 Index);
2644 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002645 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002646 }
2647 if (newAttr)
2648 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002649}
2650
Erich Keanee891aa92018-07-13 15:07:47 +00002651static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002652 const auto *M = cast<ObjCMethodDecl>(D);
2653 if (!AL.isArgIdent(0)) {
2654 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002655 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002656 return;
2657 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002658
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002659 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002660 ObjCMethodFamilyAttr::FamilyKind F;
2661 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002662 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002663 return;
2664 }
2665
Alp Toker314cc812014-01-25 16:55:45 +00002666 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002667 !M->getReturnType()->isObjCObjectPointerType()) {
2668 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2669 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002670 // Ignore the attribute.
2671 return;
2672 }
2673
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002674 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2675 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002676}
2677
Erich Keanee891aa92018-07-13 15:07:47 +00002678static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002679 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002680 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002681 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002682 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2683 return;
2684 }
2685 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002686 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002687 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002688 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002689 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2690 return;
2691 }
2692 }
2693 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002694 // It is okay to include this attribute on properties, e.g.:
2695 //
2696 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2697 //
2698 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002699 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002700 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002701 }
Michael Han99315932013-01-24 16:46:58 +00002702 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002703 ObjCNSObjectAttr(AL.getRange(), S.Context,
2704 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002705}
2706
Erich Keanee891aa92018-07-13 15:07:47 +00002707static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002708 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002709 QualType T = TD->getUnderlyingType();
2710 if (!T->isObjCObjectPointerType()) {
2711 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2712 return;
2713 }
2714 } else {
2715 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2716 return;
2717 }
2718 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002719 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2720 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002721}
2722
Erich Keanee891aa92018-07-13 15:07:47 +00002723static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002724 if (!AL.isArgIdent(0)) {
2725 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002726 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002727 return;
2728 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002729
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002730 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002731 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002732 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002733 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002734 return;
2735 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002736
Michael Han99315932013-01-24 16:46:58 +00002737 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002738 BlocksAttr(AL.getRange(), S.Context, type,
2739 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002740}
2741
Erich Keanee891aa92018-07-13 15:07:47 +00002742static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002743 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002744 if (AL.getNumArgs() > 0) {
2745 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002746 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002747 if (E->isTypeDependent() || E->isValueDependent() ||
2748 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002749 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002750 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002751 return;
2752 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002753
John McCallb46f2872011-09-09 07:56:05 +00002754 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002755 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002756 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002757 return;
2758 }
John McCallb46f2872011-09-09 07:56:05 +00002759
2760 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002761 }
2762
Aaron Ballman18a78382013-11-21 00:28:23 +00002763 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002764 if (AL.getNumArgs() > 1) {
2765 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002766 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002767 if (E->isTypeDependent() || E->isValueDependent() ||
2768 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002769 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002770 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002771 return;
2772 }
2773 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002774
John McCallb46f2872011-09-09 07:56:05 +00002775 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002776 // FIXME: This error message could be improved, it would be nice
2777 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002778 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002779 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002780 return;
2781 }
2782 }
2783
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002784 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002785 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002786 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002787 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002788 return;
2789 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002790
Chris Lattner9363e312009-03-17 23:03:47 +00002791 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002792 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002793 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002794 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002795 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002796 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002797 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002798 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002799 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002800 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002801 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002802 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002803 return;
2804 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002805 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002806 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002807 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002808 const FunctionType *FT = Ty->isFunctionPointerType()
2809 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002810 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002811 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002812 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002813 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002814 return;
2815 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002816 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002817 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002818 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002819 return;
2820 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002821 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002822 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002823 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002824 return;
2825 }
Michael Han99315932013-01-24 16:46:58 +00002826 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002827 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2828 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002829}
2830
Erich Keanee891aa92018-07-13 15:07:47 +00002831static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002832 if (D->getFunctionType() &&
2833 D->getFunctionType()->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002834 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002835 return;
2836 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002837 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002838 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002839 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002840 return;
2841 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002842
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002843 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002844 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002845 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2846 !AL.getScopeName())
Erich Keane44bacdf2018-08-09 13:21:32 +00002847 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballmane7964782016-03-07 22:44:55 +00002848
Fangrui Song6907ce22018-07-30 19:24:48 +00002849 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002850 WarnUnusedResultAttr(AL.getRange(), S.Context,
2851 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002852}
2853
Erich Keanee891aa92018-07-13 15:07:47 +00002854static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002855 // weak_import only applies to variable & function declarations.
2856 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002857 if (!D->canBeWeakImported(isDef)) {
2858 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002859 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002860 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002861 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002862 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002863 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002864 // Nothing to warn about here.
2865 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002866 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002867 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002868
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002869 return;
2870 }
2871
Michael Han99315932013-01-24 16:46:58 +00002872 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002873 WeakImportAttr(AL.getRange(), S.Context,
2874 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002875}
2876
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002877// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002878template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002879static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002880 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002881 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002882 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002883 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2884 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002885 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002886 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002887 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002888 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002889 return;
2890 }
2891 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002892
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002893 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2894 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2895 Existing->getYDim() == WGSize[1] &&
2896 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002897 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002898
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002899 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002900 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002901 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002902}
2903
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002904// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002905static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002906 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002907 const Expr *E = AL.getArgAsExpr(0);
2908 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002909 return;
2910 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002911 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002912 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002913 return;
2914 }
2915
2916 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2917 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2918 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002919 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002920
2921 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002922 AL.getRange(), S.Context, SGSize,
2923 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002924}
2925
Erich Keanee891aa92018-07-13 15:07:47 +00002926static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002927 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002928 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002929 return;
2930 }
2931
Craig Topperc3ec1492014-05-26 06:22:03 +00002932 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002933 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002934 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002935
2936 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2937 (ParmType->isBooleanType() ||
2938 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002939 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002940 << ParmType;
2941 return;
2942 }
2943
Aaron Ballmana9e05402013-12-02 22:16:55 +00002944 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002945 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002946 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002947 return;
2948 }
2949 }
2950
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002951 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002952 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002953 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002954}
2955
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002956SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002957 StringRef Name,
2958 unsigned AttrSpellingListIndex) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002959 // Explicit or partial specializations do not inherit
2960 // the section attribute from the primary template.
2961 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2962 if (AttrSpellingListIndex == SectionAttr::Declspec_allocate &&
2963 FD->isFunctionTemplateSpecialization())
2964 return nullptr;
2965 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002966 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2967 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002968 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002969 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2970 << 1 /*section*/;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002971 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002972 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002973 }
Michael Han99315932013-01-24 16:46:58 +00002974 return ::new (Context) SectionAttr(Range, Context, Name,
2975 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002976}
2977
Reid Kleckner2a133222015-03-04 23:39:17 +00002978bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2979 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2980 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002981 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002982 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002983 return false;
2984 }
2985 return true;
2986}
2987
Erich Keanee891aa92018-07-13 15:07:47 +00002988static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002989 // Make sure that there is a string literal as the sections's single
2990 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002991 StringRef Str;
2992 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002993 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002994 return;
Mike Stump11289f42009-09-09 15:08:12 +00002995
Reid Kleckner2a133222015-03-04 23:39:17 +00002996 if (!S.checkSectionName(LiteralLoc, Str))
2997 return;
2998
Chris Lattner30ba6742009-08-10 19:03:04 +00002999 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003000 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00003001 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003002 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00003003 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00003004 return;
3005 }
Mike Stump11289f42009-09-09 15:08:12 +00003006
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003007 unsigned Index = AL.getAttributeSpellingListIndex();
3008 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003009 if (NewAttr)
3010 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00003011}
3012
Erich Keane7963e8b2018-07-18 20:04:48 +00003013static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) {
3014 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
3015 if (!Error.empty()) {
3016 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
3017 << 0 /*'code-seg'*/;
3018 return false;
3019 }
3020 return true;
3021}
3022
3023CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range,
3024 StringRef Name,
3025 unsigned AttrSpellingListIndex) {
3026 // Explicit or partial specializations do not inherit
3027 // the code_seg attribute from the primary template.
3028 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3029 if (FD->isFunctionTemplateSpecialization())
3030 return nullptr;
3031 }
3032 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3033 if (ExistingAttr->getName() == Name)
3034 return nullptr;
3035 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3036 << 0 /*codeseg*/;
3037 Diag(Range.getBegin(), diag::note_previous_attribute);
3038 return nullptr;
3039 }
3040 return ::new (Context) CodeSegAttr(Range, Context, Name,
3041 AttrSpellingListIndex);
3042}
3043
3044static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3045 StringRef Str;
3046 SourceLocation LiteralLoc;
3047 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3048 return;
3049 if (!checkCodeSegName(S, LiteralLoc, Str))
3050 return;
3051 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3052 if (!ExistingAttr->isImplicit()) {
3053 S.Diag(AL.getLoc(),
3054 ExistingAttr->getName() == Str
3055 ? diag::warn_duplicate_codeseg_attribute
3056 : diag::err_conflicting_codeseg_attribute);
3057 return;
3058 }
3059 D->dropAttr<CodeSegAttr>();
3060 }
3061 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str,
3062 AL.getAttributeSpellingListIndex()))
3063 D->addAttr(CSA);
3064}
3065
Erich Keane57e15cd2017-07-19 22:06:33 +00003066// Check for things we'd like to warn about. Multiversioning issues are
3067// handled later in the process, once we know how many exist.
3068bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3069 enum FirstParam { Unsupported, Duplicate };
3070 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00003071 for (auto Str : {"tune=", "fpmath="})
3072 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00003073 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3074 << Unsupported << None << Str;
3075
3076 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
3077
3078 if (!ParsedAttrs.Architecture.empty() &&
3079 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3080 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3081 << Unsupported << Architecture << ParsedAttrs.Architecture;
3082
3083 if (ParsedAttrs.DuplicateArchitecture)
3084 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3085 << Duplicate << None << "arch=";
3086
3087 for (const auto &Feature : ParsedAttrs.Features) {
3088 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3089 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3090 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3091 << Unsupported << None << CurFeature;
3092 }
3093
Erich Keane29636aa2018-02-16 17:31:59 +00003094 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003095}
3096
Erich Keanee891aa92018-07-13 15:07:47 +00003097static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003098 StringRef Str;
3099 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003100 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003101 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003102 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003103
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003104 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00003105 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003106 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00003107 D->addAttr(NewAttr);
3108}
3109
Erich Keanee891aa92018-07-13 15:07:47 +00003110static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003111 Expr *E = AL.getArgAsExpr(0);
3112 uint32_t VecWidth;
3113 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3114 AL.setInvalid();
3115 return;
3116 }
3117
3118 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3119 if (Existing && Existing->getVectorWidth() != VecWidth) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003120 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Craig Topper74c10e32018-07-09 19:00:16 +00003121 return;
3122 }
3123
3124 D->addAttr(::new (S.Context)
3125 MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth,
3126 AL.getAttributeSpellingListIndex()));
3127}
3128
Erich Keanee891aa92018-07-13 15:07:47 +00003129static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003130 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003131 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003132 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003133 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003134
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003135 // gcc only allows for simple identifiers. Since we support more than gcc, we
3136 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003137 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003138 if (DRE->hasQualifier())
3139 S.Diag(Loc, diag::warn_cleanup_ext);
3140 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3141 NI = DRE->getNameInfo();
3142 if (!FD) {
3143 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3144 << NI.getName();
3145 return;
3146 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003147 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003148 if (ULE->hasExplicitTemplateArgs())
3149 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003150 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3151 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003152 if (!FD) {
3153 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3154 << NI.getName();
3155 if (ULE->getType() == S.Context.OverloadTy)
3156 S.NoteAllOverloadCandidates(ULE);
3157 return;
3158 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003159 } else {
3160 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003161 return;
3162 }
3163
Anders Carlssond277d792009-01-31 01:16:18 +00003164 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003165 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3166 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003167 return;
3168 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003169
Anders Carlsson723f55d2009-02-07 23:16:50 +00003170 // We're currently more strict than GCC about what function types we accept.
3171 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003172 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003173 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003174 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3175 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003176 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3177 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003178 return;
3179 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003180
Michael Han99315932013-01-24 16:46:58 +00003181 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003182 CleanupAttr(AL.getRange(), S.Context, FD,
3183 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003184}
3185
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003186static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003187 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003188 if (!AL.isArgIdent(0)) {
3189 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003190 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003191 return;
3192 }
3193
3194 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003195 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003196 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3197 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003198 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003199 return;
3200 }
3201
3202 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003203 AL.getRange(), S.Context, ExtensibilityKind,
3204 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003205}
3206
Mike Stumpd3bb5572009-07-24 19:02:52 +00003207/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003208/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003209static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003210 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003211 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003212 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003213 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003214
Eric Christopherb64963e2015-08-13 21:34:35 +00003215 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003216 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003217
Eric Christopherb64963e2015-08-13 21:34:35 +00003218 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3219 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003220 !isCFStringType(Ty, S.Context) &&
3221 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003222 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003223 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003224 << "a string type" << IdxExpr->getSourceRange()
3225 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003226 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003227 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003228 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003229 if (!isNSStringType(Ty, S.Context) &&
3230 !isCFStringType(Ty, S.Context) &&
3231 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003232 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003233 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003234 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003235 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003236 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003237 }
3238
Joel E. Denny81508102018-03-13 14:51:22 +00003239 D->addAttr(::new (S.Context) FormatArgAttr(
3240 AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003241}
3242
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003243enum FormatAttrKind {
3244 CFStringFormat,
3245 NSStringFormat,
3246 StrftimeFormat,
3247 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003248 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003249 InvalidFormat
3250};
3251
3252/// getFormatAttrKind - Map from format attribute names to supported format
3253/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003254static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003255 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003256 // Check for formats that get handled specially.
3257 .Case("NSString", NSStringFormat)
3258 .Case("CFString", CFStringFormat)
3259 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003260
Mehdi Amini06d367c2016-10-24 20:39:34 +00003261 // Otherwise, check for supported formats.
3262 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3263 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3264 .Case("kprintf", SupportedFormat) // OpenBSD.
3265 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3266 .Case("os_trace", SupportedFormat)
3267 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003268
Mehdi Amini06d367c2016-10-24 20:39:34 +00003269 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3270 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003271}
3272
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003273/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003274/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003275static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003276 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003277 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003278 return;
3279 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003280
Aaron Ballman4a611152013-11-27 16:34:09 +00003281 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003282 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3283 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003284 return;
3285 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003286 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003287 if (S.Context.getAsArrayType(T))
3288 T = S.Context.getBaseElementType(T);
3289 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003290 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3291 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003292 return;
3293 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003294
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003295 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003296 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003297 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3298 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003299 return;
3300 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003301
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003302 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003303 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003304 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003305 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003306 return;
3307 }
Michael Han99315932013-01-24 16:46:58 +00003308 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003309 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3310 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003311}
3312
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003313FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3314 IdentifierInfo *Format, int FormatIdx,
3315 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003316 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003317 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003318 for (auto *F : D->specific_attrs<FormatAttr>()) {
3319 if (F->getType() == Format &&
3320 F->getFormatIdx() == FormatIdx &&
3321 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003322 // If we don't have a valid location for this attribute, adopt the
3323 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003324 if (F->getLocation().isInvalid())
3325 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003326 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003327 }
3328 }
3329
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003330 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3331 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003332}
3333
Mike Stumpd3bb5572009-07-24 19:02:52 +00003334/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003335/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003336static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003337 if (!AL.isArgIdent(0)) {
3338 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003339 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003340 return;
3341 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003342
Chandler Carruth743682b2010-11-16 08:35:43 +00003343 // In C++ the implicit 'this' function parameter also counts, and they are
3344 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003345 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003346 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003347
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003348 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003349 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003350
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003351 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003352 // If we've modified the string name, we need a new identifier for it.
3353 II = &S.Context.Idents.get(Format);
3354 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003355
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003356 // Check for supported formats.
3357 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003358
Chris Lattner12161d32010-03-22 21:08:50 +00003359 if (Kind == IgnoredFormat)
3360 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003361
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003362 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003363 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003364 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003365 return;
3366 }
3367
3368 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003369 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003370 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003371 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003372 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003373
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003374 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003375 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003376 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003377 return;
3378 }
3379
3380 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003381 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003382
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003383 if (HasImplicitThisParam) {
3384 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003385 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003386 diag::err_format_attribute_implicit_this_format_string)
3387 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003388 return;
3389 }
3390 ArgIdx--;
3391 }
Mike Stump11289f42009-09-09 15:08:12 +00003392
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003393 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003394 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003395
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003396 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003397 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003398 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003399 << "a CFString" << IdxExpr->getSourceRange()
3400 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003401 return;
3402 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003403 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003404 // FIXME: do we need to check if the type is NSString*? What are the
3405 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003406 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003407 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003408 << "an NSString" << IdxExpr->getSourceRange()
3409 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003410 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003411 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003412 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003413 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003414 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003415 << "a string type" << IdxExpr->getSourceRange()
3416 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003417 return;
3418 }
3419
3420 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003421 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003422 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003423 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003424 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003425
3426 // check if the function is variadic if the 3rd argument non-zero
3427 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003428 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003429 ++NumArgs; // +1 for ...
3430 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003431 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003432 return;
3433 }
3434 }
3435
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003436 // strftime requires FirstArg to be 0 because it doesn't read from any
3437 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003438 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003439 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003440 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003441 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003442 return;
3443 }
3444 // if 0 it disables parameter checking (to use with e.g. va_list)
3445 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003446 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003447 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003448 return;
3449 }
3450
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003451 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003452 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003453 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003454 if (NewAttr)
3455 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003456}
3457
Erich Keanee891aa92018-07-13 15:07:47 +00003458static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003459 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003460 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003461 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003462 if (TD && TD->getUnderlyingType()->isUnionType())
3463 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3464 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003465 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003466
3467 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003468 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3469 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003470 return;
3471 }
3472
John McCallf937c022011-10-07 06:10:15 +00003473 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003474 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003475 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003476 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003477 return;
3478 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003479
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003480 RecordDecl::field_iterator Field = RD->field_begin(),
3481 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003482 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003483 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003484 return;
3485 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003486
David Blaikie40ed2972012-06-06 20:45:41 +00003487 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003488 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003489 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003490 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003491 diag::warn_transparent_union_attribute_floating)
3492 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003493 return;
3494 }
3495
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003496 if (FirstType->isIncompleteType())
3497 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003498 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3499 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3500 for (; Field != FieldEnd; ++Field) {
3501 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003502 if (FieldType->isIncompleteType())
3503 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003504 // FIXME: this isn't fully correct; we also need to test whether the
3505 // members of the union would all have the same calling convention as the
3506 // first member of the union. Checking just the size and alignment isn't
3507 // sufficient (consider structs passed on the stack instead of in registers
3508 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003509 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003510 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003511 // Warn if we drop the attribute.
3512 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003513 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003514 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003515 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003516 diag::warn_transparent_union_attribute_field_size_align)
3517 << isSize << Field->getDeclName() << FieldBits;
3518 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003519 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003520 diag::note_transparent_union_first_field_size_align)
3521 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003522 return;
3523 }
3524 }
3525
Michael Han99315932013-01-24 16:46:58 +00003526 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003527 TransparentUnionAttr(AL.getRange(), S.Context,
3528 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003529}
3530
Erich Keanee891aa92018-07-13 15:07:47 +00003531static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003532 // Make sure that there is a string literal as the annotation's single
3533 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003534 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003535 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003536 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003537
3538 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003539 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3540 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003541 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003542 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003543
Michael Han99315932013-01-24 16:46:58 +00003544 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003545 AnnotateAttr(AL.getRange(), S.Context, Str,
3546 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003547}
3548
Erich Keanee891aa92018-07-13 15:07:47 +00003549static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003550 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3551 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003552}
3553
3554void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3555 unsigned SpellingListIndex) {
3556 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3557 SourceLocation AttrLoc = AttrRange.getBegin();
3558
3559 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003560 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003561 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003562 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003563 T = VD->getType();
3564 else
3565 llvm_unreachable("Unknown decl type for align_value");
3566
3567 if (!T->isDependentType() && !T->isAnyPointerType() &&
3568 !T->isReferenceType() && !T->isMemberPointerType()) {
3569 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3570 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3571 return;
3572 }
3573
3574 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003575 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003576 ExprResult ICE
3577 = VerifyIntegerConstantExpression(E, &Alignment,
3578 diag::err_align_value_attribute_argument_not_int,
3579 /*AllowFold*/ false);
3580 if (ICE.isInvalid())
3581 return;
3582
3583 if (!Alignment.isPowerOf2()) {
3584 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3585 << E->getSourceRange();
3586 return;
3587 }
3588
3589 D->addAttr(::new (Context)
3590 AlignValueAttr(AttrRange, Context, ICE.get(),
3591 SpellingListIndex));
3592 return;
3593 }
3594
3595 // Save dependent expressions in the AST to be instantiated.
3596 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003597}
3598
Erich Keanee891aa92018-07-13 15:07:47 +00003599static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003600 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003601 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003602 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003603 return;
3604 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003605
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003606 if (AL.getNumArgs() == 0) {
3607 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3608 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003609 return;
3610 }
3611
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003612 Expr *E = AL.getArgAsExpr(0);
3613 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3614 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003615 diag::err_pack_expansion_without_parameter_packs);
3616 return;
3617 }
3618
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003619 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003620 return;
3621
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003622 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3623 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003624}
3625
3626void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003627 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003628 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3629 SourceLocation AttrLoc = AttrRange.getBegin();
3630
Richard Smith1dba27c2013-01-29 09:02:09 +00003631 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003632 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003633 // C++11 [dcl.align]p1:
3634 // An alignment-specifier may be applied to a variable or to a class
3635 // data member, but it shall not be applied to a bit-field, a function
3636 // parameter, the formal parameter of a catch clause, or a variable
3637 // declared with the register storage class specifier. An
3638 // alignment-specifier may also be applied to the declaration of a class
3639 // or enumeration type.
3640 // C11 6.7.5/2:
3641 // An alignment attribute shall not be specified in a declaration of
3642 // a typedef, or a bit-field, or a function, or a parameter, or an
3643 // object declared with the register storage-class specifier.
3644 int DiagKind = -1;
3645 if (isa<ParmVarDecl>(D)) {
3646 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003647 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003648 if (VD->getStorageClass() == SC_Register)
3649 DiagKind = 1;
3650 if (VD->isExceptionVariable())
3651 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003652 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003653 if (FD->isBitField())
3654 DiagKind = 3;
3655 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003656 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003657 << (TmpAttr.isC11() ? ExpectedVariableOrField
3658 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003659 return;
3660 }
3661 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003662 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003663 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003664 return;
3665 }
3666 }
3667
Richard Smith90ae9672018-06-20 23:36:55 +00003668 if (E->isValueDependent()) {
3669 // We can't support a dependent alignment on a non-dependent type,
3670 // because we have no way to model that a type is "alignment-dependent"
3671 // but not dependent in any other way.
3672 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3673 if (!TND->getUnderlyingType()->isDependentType()) {
3674 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3675 << E->getSourceRange();
3676 return;
3677 }
3678 }
3679
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003680 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003681 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3682 AA->setPackExpansion(IsPackExpansion);
3683 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003684 return;
3685 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003686
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003687 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003688 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003689 ExprResult ICE
3690 = VerifyIntegerConstantExpression(E, &Alignment,
3691 diag::err_aligned_attribute_argument_not_int,
3692 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003693 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003694 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003695
David Majnemer0be6bd02015-07-26 09:02:21 +00003696 uint64_t AlignVal = Alignment.getZExtValue();
3697
Richard Smith848e1f12013-02-01 08:12:08 +00003698 // C++11 [dcl.align]p2:
3699 // -- if the constant expression evaluates to zero, the alignment
3700 // specifier shall have no effect
3701 // C11 6.7.5p6:
3702 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003703 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003704 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003705 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3706 << E->getSourceRange();
3707 return;
3708 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003709 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003710
David Majnemerabecae72014-02-12 20:36:10 +00003711 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003712 unsigned MaxValidAlignment =
3713 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3714 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003715 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003716 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3717 << E->getSourceRange();
3718 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003719 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003720
David Majnemer0be6bd02015-07-26 09:02:21 +00003721 if (Context.getTargetInfo().isTLSSupported()) {
3722 unsigned MaxTLSAlign =
3723 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3724 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003725 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003726 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3727 VD->getTLSKind() != VarDecl::TLS_None) {
3728 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3729 << (unsigned)AlignVal << VD << MaxTLSAlign;
3730 return;
3731 }
3732 }
3733
Richard Smith44c247f2013-02-22 08:32:16 +00003734 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003735 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003736 AA->setPackExpansion(IsPackExpansion);
3737 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003738}
3739
Michael Hanaf02bbe2013-02-01 01:19:17 +00003740void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003741 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003742 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003743 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003744 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3745 SpellingListIndex);
3746 AA->setPackExpansion(IsPackExpansion);
3747 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003748}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003749
Richard Smith848e1f12013-02-01 08:12:08 +00003750void Sema::CheckAlignasUnderalignment(Decl *D) {
3751 assert(D->hasAttrs() && "no attributes on decl");
3752
David Majnemer475b25e2015-01-21 10:54:38 +00003753 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003754 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003755 UnderlyingTy = DiagTy = VD->getType();
3756 } else {
3757 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003758 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003759 UnderlyingTy = ED->getIntegerType();
3760 }
3761 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003762 return;
3763
3764 // C++11 [dcl.align]p5, C11 6.7.5/4:
3765 // The combined effect of all alignment attributes in a declaration shall
3766 // not specify an alignment that is less strict than the alignment that
3767 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003768 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003769 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003770 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003771 if (I->isAlignmentDependent())
3772 return;
3773 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003774 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003775 Align = std::max(Align, I->getAlignment(Context));
3776 }
3777
3778 if (AlignasAttr && Align) {
3779 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003780 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003781 if (NaturalAlign > RequestedAlign)
3782 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003783 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003784 }
3785}
3786
David Majnemer2c4e00a2014-01-29 22:07:36 +00003787bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003788 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003789 MSInheritanceAttr::Spelling SemanticSpelling) {
3790 assert(RD->hasDefinition() && "RD has no definition!");
3791
David Majnemer98c9ee22014-02-07 00:43:07 +00003792 // We may not have seen base specifiers or any virtual methods yet. We will
3793 // have to wait until the record is defined to catch any mismatches.
3794 if (!RD->getDefinition()->isCompleteDefinition())
3795 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003796
David Majnemer98c9ee22014-02-07 00:43:07 +00003797 // The unspecified model never matches what a definition could need.
3798 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3799 return false;
3800
David Majnemer4bb09802014-02-10 19:50:15 +00003801 if (BestCase) {
3802 if (RD->calculateInheritanceModel() == SemanticSpelling)
3803 return false;
3804 } else {
3805 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3806 return false;
3807 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003808
3809 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3810 << 0 /*definition*/;
3811 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3812 << RD->getNameAsString();
3813 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003814}
3815
Alexey Bataevf278eb12015-11-19 10:13:11 +00003816/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3817/// attribute.
3818static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3819 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003820 IntegerMode = true;
3821 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003822 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003823 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003824 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003825 case 'Q':
3826 DestWidth = 8;
3827 break;
3828 case 'H':
3829 DestWidth = 16;
3830 break;
3831 case 'S':
3832 DestWidth = 32;
3833 break;
3834 case 'D':
3835 DestWidth = 64;
3836 break;
3837 case 'X':
3838 DestWidth = 96;
3839 break;
3840 case 'T':
3841 DestWidth = 128;
3842 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003843 }
3844 if (Str[1] == 'F') {
3845 IntegerMode = false;
3846 } else if (Str[1] == 'C') {
3847 IntegerMode = false;
3848 ComplexMode = true;
3849 } else if (Str[1] != 'I') {
3850 DestWidth = 0;
3851 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003852 break;
3853 case 4:
3854 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3855 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003856 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003857 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003858 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003859 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003860 break;
3861 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003862 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003863 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003864 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003865 case 11:
3866 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003867 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003868 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003869 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003870}
3871
3872/// handleModeAttr - This attribute modifies the width of a decl with primitive
3873/// type.
3874///
3875/// Despite what would be logical, the mode attribute is a decl attribute, not a
3876/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3877/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00003878static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003879 // This attribute isn't documented, but glibc uses it. It changes
3880 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003881 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003882 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
3883 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003884 return;
3885 }
3886
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003887 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003888
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003889 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003890}
3891
3892void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3893 unsigned SpellingListIndex, bool InInstantiation) {
3894 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003895 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003896 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003897
3898 unsigned DestWidth = 0;
3899 bool IntegerMode = true;
3900 bool ComplexMode = false;
3901 llvm::APInt VectorSize(64, 0);
3902 if (Str.size() >= 4 && Str[0] == 'V') {
3903 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3904 size_t StrSize = Str.size();
3905 size_t VectorStringLength = 0;
3906 while ((VectorStringLength + 1) < StrSize &&
3907 isdigit(Str[VectorStringLength + 1]))
3908 ++VectorStringLength;
3909 if (VectorStringLength &&
3910 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3911 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003912 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003913 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003914 // Avoid duplicate warning from template instantiation.
3915 if (!InInstantiation)
3916 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003917 } else {
3918 VectorSize = 0;
3919 }
3920 }
3921
3922 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003923 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3924
3925 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3926 // and friends, at least with glibc.
3927 // FIXME: Make sure floating-point mappings are accurate
3928 // FIXME: Support XF and TF types
3929 if (!DestWidth) {
3930 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3931 return;
3932 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003933
3934 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003935 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003936 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003937 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003938 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3939 // Try to get type from enum declaration, default to int.
3940 OldTy = ED->getIntegerType();
3941 if (OldTy.isNull())
3942 OldTy = Context.IntTy;
3943 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003944 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003945
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003946 if (OldTy->isDependentType()) {
3947 D->addAttr(::new (Context)
3948 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3949 return;
3950 }
3951
Alexey Bataev326057d2015-06-19 07:46:21 +00003952 // Base type can also be a vector type (see PR17453).
3953 // Distinguish between base type and base element type.
3954 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003955 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00003956 OldElemTy = VT->getElementType();
3957
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003958 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3959 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3960 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3961 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3962 VectorSize.getBoolValue()) {
3963 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3964 return;
3965 }
3966 bool IntegralOrAnyEnumType =
3967 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3968
3969 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3970 !IntegralOrAnyEnumType)
3971 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003972 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003973 if (!IntegralOrAnyEnumType)
3974 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003975 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003976 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003977 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003978 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003979 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003980 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003981 }
3982
Alexey Bataev326057d2015-06-19 07:46:21 +00003983 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003984
3985 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003986 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3987 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003988 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003989 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003990
Alexey Bataev326057d2015-06-19 07:46:21 +00003991 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003992 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003993 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003994 }
3995
Eli Friedman4735374e2009-03-03 06:41:03 +00003996 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003997 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003998 }
3999
4000 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004001 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004002 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
4003 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004004 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004005 // Complex machine mode does not support base vector types.
4006 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004007 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004008 return;
4009 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004010 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00004011 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004012 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004013 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004014 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00004015 }
4016
4017 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004018 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004019 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004020 }
4021
4022 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004023 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004024 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004025 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004026 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004027 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004028 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004029
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004030 D->addAttr(::new (Context)
4031 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00004032}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004033
Erich Keanee891aa92018-07-13 15:07:47 +00004034static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Han99315932013-01-24 16:46:58 +00004035 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004036 NoDebugAttr(AL.getRange(), S.Context,
4037 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00004038}
4039
Paul Robinson30e41fb2014-12-15 18:57:28 +00004040AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00004041 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00004042 unsigned AttrSpellingListIndex) {
4043 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004044 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00004045 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4046 return nullptr;
4047 }
4048
4049 if (D->hasAttr<AlwaysInlineAttr>())
4050 return nullptr;
4051
4052 return ::new (Context) AlwaysInlineAttr(Range, Context,
4053 AttrSpellingListIndex);
4054}
4055
Erich Keane44bacdf2018-08-09 13:21:32 +00004056CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
4057 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004058 return nullptr;
4059
Erich Keane44bacdf2018-08-09 13:21:32 +00004060 return ::new (Context)
4061 CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004062}
4063
Erich Keane44bacdf2018-08-09 13:21:32 +00004064CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
4065 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
4066 return nullptr;
4067
4068 return ::new (Context)
4069 CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex());
4070}
4071
4072InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4073 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004074 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004075 // Attribute applies to Var but not any subclass of it (like ParmVar,
4076 // ImplicitParm or VarTemplateSpecialization).
4077 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004078 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4079 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4080 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004081 return nullptr;
4082 }
4083 // Attribute does not apply to non-static local variables.
4084 if (VD->hasLocalStorage()) {
4085 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4086 return nullptr;
4087 }
4088 }
4089
Erich Keane44bacdf2018-08-09 13:21:32 +00004090 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
4091 return nullptr;
4092
4093 return ::new (Context) InternalLinkageAttr(
4094 AL.getRange(), Context, AL.getAttributeSpellingListIndex());
4095}
4096InternalLinkageAttr *
4097Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4098 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4099 // Attribute applies to Var but not any subclass of it (like ParmVar,
4100 // ImplicitParm or VarTemplateSpecialization).
4101 if (VD->getKind() != Decl::Var) {
4102 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4103 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4104 : ExpectedVariableOrFunction);
4105 return nullptr;
4106 }
4107 // Attribute does not apply to non-static local variables.
4108 if (VD->hasLocalStorage()) {
4109 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4110 return nullptr;
4111 }
4112 }
4113
4114 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004115 return nullptr;
4116
4117 return ::new (Context)
Erich Keane44bacdf2018-08-09 13:21:32 +00004118 InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004119}
4120
Paul Robinson30e41fb2014-12-15 18:57:28 +00004121MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
4122 unsigned AttrSpellingListIndex) {
4123 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4124 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
4125 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4126 return nullptr;
4127 }
4128
4129 if (D->hasAttr<MinSizeAttr>())
4130 return nullptr;
4131
4132 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
4133}
4134
4135OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
4136 unsigned AttrSpellingListIndex) {
4137 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4138 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4139 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4140 D->dropAttr<AlwaysInlineAttr>();
4141 }
4142 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4143 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4144 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4145 D->dropAttr<MinSizeAttr>();
4146 }
4147
4148 if (D->hasAttr<OptimizeNoneAttr>())
4149 return nullptr;
4150
4151 return ::new (Context) OptimizeNoneAttr(Range, Context,
4152 AttrSpellingListIndex);
4153}
4154
Erich Keanee891aa92018-07-13 15:07:47 +00004155static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004156 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004157 return;
4158
Paul Robinson080b1f32015-01-13 18:34:56 +00004159 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004160 D, AL.getRange(), AL.getName(),
4161 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004162 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004163}
4164
Erich Keanee891aa92018-07-13 15:07:47 +00004165static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004166 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004167 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004168 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004169}
4170
Erich Keanee891aa92018-07-13 15:07:47 +00004171static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004172 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004173 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004174 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004175}
4176
Erich Keanee891aa92018-07-13 15:07:47 +00004177static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004178 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004179 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004180 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004181 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004182 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004183 return;
4184 }
4185 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004186 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004187}
4188
Erich Keanee891aa92018-07-13 15:07:47 +00004189static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004190 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004191 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004192 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004193 // extern __shared__ is only allowed on arrays with no length (e.g.
4194 // "int x[]").
Yaxun Liu97670892018-10-02 17:48:54 +00004195 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004196 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004197 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004198 return;
4199 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004200 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004201 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004202 << S.CurrentCUDATarget())
4203 return;
Justin Lebar10411012016-09-30 23:57:30 +00004204 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004205 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004206}
4207
Erich Keanee891aa92018-07-13 15:07:47 +00004208static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004209 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4210 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004211 return;
4212 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004213 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004214 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004215 SourceRange RTRange = FD->getReturnTypeSourceRange();
4216 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004217 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004218 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4219 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004220 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004221 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004222 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4223 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004224 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004225 << Method;
4226 return;
4227 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004228 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004229 }
4230 // Only warn for "inline" when compiling for host, to cut down on noise.
4231 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004232 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004233
Aaron Ballman3aff6332013-12-02 19:30:36 +00004234 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004235 CUDAGlobalAttr(AL.getRange(), S.Context,
4236 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004237}
4238
Erich Keanee891aa92018-07-13 15:07:47 +00004239static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004240 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004241 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004242 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004243 return;
4244 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004245
Michael Han99315932013-01-24 16:46:58 +00004246 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004247 GNUInlineAttr(AL.getRange(), S.Context,
4248 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004249}
4250
Erich Keanee891aa92018-07-13 15:07:47 +00004251static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004252 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004253
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004254 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004255 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4256 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004257 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004258 return;
4259
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004260 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004261 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004262 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004263 return;
4264 }
4265
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004266 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004267 case ParsedAttr::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004268 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004269 FastCallAttr(AL.getRange(), S.Context,
4270 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004271 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004272 case ParsedAttr::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004273 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004274 StdCallAttr(AL.getRange(), S.Context,
4275 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004276 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004277 case ParsedAttr::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004278 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004279 ThisCallAttr(AL.getRange(), S.Context,
4280 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004281 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004282 case ParsedAttr::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004283 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004284 CDeclAttr(AL.getRange(), S.Context,
4285 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004286 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004287 case ParsedAttr::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004288 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004289 PascalAttr(AL.getRange(), S.Context,
4290 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004291 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004292 case ParsedAttr::AT_SwiftCall:
John McCall477f2bb2016-03-03 06:39:32 +00004293 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004294 SwiftCallAttr(AL.getRange(), S.Context,
4295 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004296 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004297 case ParsedAttr::AT_VectorCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004298 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004299 VectorCallAttr(AL.getRange(), S.Context,
4300 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004301 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004302 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004303 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004304 MSABIAttr(AL.getRange(), S.Context,
4305 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004306 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004307 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004308 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004309 SysVABIAttr(AL.getRange(), S.Context,
4310 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004311 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004312 case ParsedAttr::AT_RegCall:
Erich Keane757d3172016-11-02 18:29:35 +00004313 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004314 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004315 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004316 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004317 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004318 switch (CC) {
4319 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004320 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004321 break;
4322 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004323 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004324 break;
4325 default:
4326 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004327 }
4328
Michael Han99315932013-01-24 16:46:58 +00004329 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004330 PcsAttr(AL.getRange(), S.Context, PCS,
4331 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004332 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004333 }
Sander de Smalen44a22532018-11-26 16:38:37 +00004334 case ParsedAttr::AT_AArch64VectorPcs:
4335 D->addAttr(::new(S.Context)
4336 AArch64VectorPcsAttr(AL.getRange(), S.Context,
4337 AL.getAttributeSpellingListIndex()));
4338 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004339 case ParsedAttr::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004340 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004341 IntelOclBiccAttr(AL.getRange(), S.Context,
4342 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004343 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004344 case ParsedAttr::AT_PreserveMost:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004345 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004346 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004347 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004348 case ParsedAttr::AT_PreserveAll:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004349 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004350 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004351 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004352 default:
4353 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004354 }
4355}
4356
Erich Keanee891aa92018-07-13 15:07:47 +00004357static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004358 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004359 return;
4360
4361 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004362 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004363 StringRef RuleName;
4364
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004365 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004366 return;
4367
4368 // FIXME: Warn if the rule name is unknown. This is tricky because only
4369 // clang-tidy knows about available rules.
4370 DiagnosticIdentifiers.push_back(RuleName);
4371 }
4372 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004373 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4374 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004375}
4376
Erich Keanee891aa92018-07-13 15:07:47 +00004377bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004378 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004379 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004380 return true;
4381
Erich Keaneb11ebc52017-09-27 03:20:13 +00004382 if (Attrs.hasProcessingCache()) {
4383 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004384 return false;
4385 }
4386
Erich Keanee891aa92018-07-13 15:07:47 +00004387 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004388 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4389 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004390 return true;
4391 }
4392
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004393 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004394 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004395 case ParsedAttr::AT_CDecl:
4396 CC = CC_C;
4397 break;
4398 case ParsedAttr::AT_FastCall:
4399 CC = CC_X86FastCall;
4400 break;
4401 case ParsedAttr::AT_StdCall:
4402 CC = CC_X86StdCall;
4403 break;
4404 case ParsedAttr::AT_ThisCall:
4405 CC = CC_X86ThisCall;
4406 break;
4407 case ParsedAttr::AT_Pascal:
4408 CC = CC_X86Pascal;
4409 break;
4410 case ParsedAttr::AT_SwiftCall:
4411 CC = CC_Swift;
4412 break;
4413 case ParsedAttr::AT_VectorCall:
4414 CC = CC_X86VectorCall;
4415 break;
Sander de Smalen44a22532018-11-26 16:38:37 +00004416 case ParsedAttr::AT_AArch64VectorPcs:
4417 CC = CC_AArch64VectorCall;
4418 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004419 case ParsedAttr::AT_RegCall:
4420 CC = CC_X86RegCall;
4421 break;
4422 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004423 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004424 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004425 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004426 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004427 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4428 CC_C;
4429 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004430 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004431 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004432 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4433 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004434 return true;
4435 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004436 if (StrRef == "aapcs") {
4437 CC = CC_AAPCS;
4438 break;
4439 } else if (StrRef == "aapcs-vfp") {
4440 CC = CC_AAPCS_VFP;
4441 break;
4442 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004443
Erich Keaneb11ebc52017-09-27 03:20:13 +00004444 Attrs.setInvalid();
4445 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004446 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004447 }
Erich Keanee891aa92018-07-13 15:07:47 +00004448 case ParsedAttr::AT_IntelOclBicc:
4449 CC = CC_IntelOclBicc;
4450 break;
4451 case ParsedAttr::AT_PreserveMost:
4452 CC = CC_PreserveMost;
4453 break;
4454 case ParsedAttr::AT_PreserveAll:
4455 CC = CC_PreserveAll;
4456 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004457 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004458 }
4459
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004460 const TargetInfo &TI = Context.getTargetInfo();
4461 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004462 if (A != TargetInfo::CCCR_OK) {
4463 if (A == TargetInfo::CCCR_Warning)
Erich Keane44bacdf2018-08-09 13:21:32 +00004464 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004465
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004466 // This convention is not valid for the target. Use the default function or
4467 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004468 bool IsCXXMethod = false, IsVariadic = false;
4469 if (FD) {
4470 IsCXXMethod = FD->isCXXInstanceMember();
4471 IsVariadic = FD->isVariadic();
4472 }
4473 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004474 }
4475
Erich Keaneb11ebc52017-09-27 03:20:13 +00004476 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004477 return false;
4478}
4479
John McCall477f2bb2016-03-03 06:39:32 +00004480/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004481static bool isValidSwiftContextType(QualType Ty) {
4482 if (!Ty->hasPointerRepresentation())
4483 return Ty->isDependentType();
4484 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004485}
4486
4487/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004488static bool isValidSwiftIndirectResultType(QualType Ty) {
4489 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4490 Ty = PtrType->getPointeeType();
4491 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4492 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004493 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004494 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004495 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004496 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004497}
4498
4499/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004500static bool isValidSwiftErrorResultType(QualType Ty) {
4501 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4502 Ty = PtrType->getPointeeType();
4503 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4504 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004505 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004506 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004507 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004508 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004509 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004510 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004511}
4512
Erich Keanee891aa92018-07-13 15:07:47 +00004513static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004514 ParameterABI Abi) {
4515 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4516 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004517}
4518
4519void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4520 unsigned spellingIndex) {
4521
4522 QualType type = cast<ParmVarDecl>(D)->getType();
4523
4524 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4525 if (existingAttr->getABI() != abi) {
4526 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4527 << getParameterABISpelling(abi) << existingAttr;
4528 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4529 return;
4530 }
4531 }
4532
4533 switch (abi) {
4534 case ParameterABI::Ordinary:
4535 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4536
4537 case ParameterABI::SwiftContext:
4538 if (!isValidSwiftContextType(type)) {
4539 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4540 << getParameterABISpelling(abi)
4541 << /*pointer to pointer */ 0 << type;
4542 }
4543 D->addAttr(::new (Context)
4544 SwiftContextAttr(range, Context, spellingIndex));
4545 return;
4546
4547 case ParameterABI::SwiftErrorResult:
4548 if (!isValidSwiftErrorResultType(type)) {
4549 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4550 << getParameterABISpelling(abi)
4551 << /*pointer to pointer */ 1 << type;
4552 }
4553 D->addAttr(::new (Context)
4554 SwiftErrorResultAttr(range, Context, spellingIndex));
4555 return;
4556
4557 case ParameterABI::SwiftIndirectResult:
4558 if (!isValidSwiftIndirectResultType(type)) {
4559 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4560 << getParameterABISpelling(abi)
4561 << /*pointer*/ 0 << type;
4562 }
4563 D->addAttr(::new (Context)
4564 SwiftIndirectResultAttr(range, Context, spellingIndex));
4565 return;
4566 }
4567 llvm_unreachable("bad parameter ABI attribute");
4568}
4569
John McCall3882ace2011-01-05 12:14:39 +00004570/// Checks a regparm attribute, returning true if it is ill-formed and
4571/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004572bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004573 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004574 return true;
4575
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004576 if (!checkAttributeNumArgs(*this, AL, 1)) {
4577 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004578 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004579 }
Eli Friedman7044b762009-03-27 21:06:47 +00004580
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004581 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004582 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4583 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4584 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004585 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004586 }
4587
Douglas Gregore8bbc122011-09-02 00:18:52 +00004588 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004589 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004590 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004591 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004592 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004593 }
4594
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004595 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004596 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004597 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004598 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004599 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004600 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004601 }
4602
John McCall3882ace2011-01-05 12:14:39 +00004603 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004604}
4605
Artem Belevichbcec9da2016-06-06 22:54:57 +00004606// Checks whether an argument of launch_bounds attribute is
4607// acceptable, performs implicit conversion to Rvalue, and returns
4608// non-nullptr Expr result on success. Otherwise, it returns nullptr
4609// and may output an error.
4610static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004611 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004612 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004613 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004614 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004615
4616 // Accept template arguments for now as they depend on something else.
4617 // We'll get to check them when they eventually get instantiated.
4618 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004619 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004620
4621 llvm::APSInt I(64);
4622 if (!E->isIntegerConstantExpr(I, S.Context)) {
4623 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004624 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004625 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004626 }
4627 // Make sure we can fit it in 32 bits.
4628 if (!I.isIntN(32)) {
4629 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4630 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004631 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004632 }
4633 if (I < 0)
4634 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004635 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004636
Artem Belevichbcec9da2016-06-06 22:54:57 +00004637 // We may need to perform implicit conversion of the argument.
4638 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4639 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4640 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4641 assert(!ValArg.isInvalid() &&
4642 "Unexpected PerformCopyInitialization() failure.");
4643
4644 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004645}
4646
4647void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4648 Expr *MinBlocks, unsigned SpellingListIndex) {
4649 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4650 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004651 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4652 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004653 return;
4654
Artem Belevichbcec9da2016-06-06 22:54:57 +00004655 if (MinBlocks) {
4656 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4657 if (MinBlocks == nullptr)
4658 return;
4659 }
Artem Belevich7093e402015-04-21 22:55:54 +00004660
4661 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4662 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4663}
4664
Erich Keanee891aa92018-07-13 15:07:47 +00004665static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004666 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4667 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004668 return;
4669
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004670 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4671 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4672 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004673}
4674
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004675static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004676 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004677 if (!AL.isArgIdent(0)) {
4678 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004679 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004680 return;
4681 }
Joel E. Denny81508102018-03-13 14:51:22 +00004682
4683 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004684 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004685 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004686 return;
4687
Joel E. Denny81508102018-03-13 14:51:22 +00004688 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004689 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004690 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004691 return;
4692
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004693 bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004694 if (IsPointer) {
4695 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004696 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4697 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4698 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004699 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004700 }
4701
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004702 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
4703 AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx,
4704 TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004705}
4706
4707static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004708 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004709 if (!AL.isArgIdent(0)) {
4710 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004711 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004712 return;
4713 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004714
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004715 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004716 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004717
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004718 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004719 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004720 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004721 return;
4722 }
4723
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004724 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004725 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004726 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004727 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004728
Michael Han99315932013-01-24 16:46:58 +00004729 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004730 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004731 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004732 AL.getLayoutCompatible(),
4733 AL.getMustBeNull(),
4734 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004735}
4736
Erich Keanee891aa92018-07-13 15:07:47 +00004737static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004738 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004739
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004740 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004741 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004742 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004743 return;
4744
Joel E. Denny81508102018-03-13 14:51:22 +00004745 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
4746 D->addAttr(::new (S.Context) XRayLogArgsAttr(
4747 AL.getRange(), S.Context, ArgCount.getSourceIndex(),
4748 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004749}
4750
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004751//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004752// Checker-specific attribute handlers.
4753//===----------------------------------------------------------------------===//
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004754static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4755 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004756}
4757
George Karpenkov1657f362018-11-30 02:18:37 +00004758static bool isValidSubjectOfNSAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004759 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004760 QT->isObjCNSObjectType();
John McCalled433932011-01-25 03:31:58 +00004761}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004762
George Karpenkov1657f362018-11-30 02:18:37 +00004763static bool isValidSubjectOfCFAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004764 return QT->isDependentType() || QT->isPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004765 isValidSubjectOfNSAttribute(QT);
John McCalled433932011-01-25 03:31:58 +00004766}
4767
George Karpenkov1657f362018-11-30 02:18:37 +00004768static bool isValidSubjectOfOSAttribute(QualType QT) {
4769 return QT->isDependentType() || QT->isPointerType();
John McCall3b5a8f52016-03-03 00:10:03 +00004770}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004771
George Karpenkov1657f362018-11-30 02:18:37 +00004772void Sema::AddXConsumedAttr(Decl *D, SourceRange SR, unsigned SpellingIndex,
4773 RetainOwnershipKind K,
4774 bool IsTemplateInstantiation) {
4775 ValueDecl *VD = cast<ValueDecl>(D);
4776 switch (K) {
4777 case RetainOwnershipKind::OS:
4778 handleSimpleAttributeWithCheck<OSConsumedAttr>(
4779 *this, VD, SR, SpellingIndex, &isValidSubjectOfOSAttribute,
4780 diag::warn_ns_attribute_wrong_parameter_type,
4781 /*ExtraArgs=*/SR, "os_consumed", /*pointers*/ 1);
4782 return;
4783 case RetainOwnershipKind::NS:
4784 handleSimpleAttributeWithCheck<NSConsumedAttr>(
4785 *this, VD, SR, SpellingIndex, &isValidSubjectOfNSAttribute,
John McCall3b5a8f52016-03-03 00:10:03 +00004786
George Karpenkov1657f362018-11-30 02:18:37 +00004787 // These attributes are normally just advisory, but in ARC, ns_consumed
4788 // is significant. Allow non-dependent code to contain inappropriate
4789 // attributes even in ARC, but require template instantiations to be
4790 // set up correctly.
4791 ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount)
4792 ? diag::err_ns_attribute_wrong_parameter_type
4793 : diag::warn_ns_attribute_wrong_parameter_type),
4794 /*ExtraArgs=*/SR, "ns_consumed", /*objc pointers*/ 0);
4795 return;
4796 case RetainOwnershipKind::CF:
4797 handleSimpleAttributeWithCheck<CFConsumedAttr>(
4798 *this, VD, SR, SpellingIndex,
4799 &isValidSubjectOfCFAttribute,
4800 diag::warn_ns_attribute_wrong_parameter_type,
4801 /*ExtraArgs=*/SR, "cf_consumed", /*pointers*/1);
John McCalled433932011-01-25 03:31:58 +00004802 return;
4803 }
George Karpenkov1657f362018-11-30 02:18:37 +00004804}
John McCalled433932011-01-25 03:31:58 +00004805
George Karpenkov1657f362018-11-30 02:18:37 +00004806static Sema::RetainOwnershipKind
4807parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) {
4808 switch (AL.getKind()) {
4809 case ParsedAttr::AT_CFConsumed:
4810 return Sema::RetainOwnershipKind::CF;
4811 case ParsedAttr::AT_OSConsumed:
4812 return Sema::RetainOwnershipKind::OS;
4813 case ParsedAttr::AT_NSConsumed:
4814 return Sema::RetainOwnershipKind::NS;
4815 default:
4816 llvm_unreachable("Wrong argument supplied");
4817 }
John McCalled433932011-01-25 03:31:58 +00004818}
4819
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004820bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4821 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004822 return false;
4823
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004824 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4825 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004826 return true;
4827}
4828
George Karpenkov1657f362018-11-30 02:18:37 +00004829static void handleXReturnsXRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004830 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004831 QualType ReturnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004832
George Karpenkov1657f362018-11-30 02:18:37 +00004833 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004834 ReturnType = MD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00004835 } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
4836 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) {
John McCall31168b02011-06-15 23:02:42 +00004837 return; // ignore: was handled as a type attribute
George Karpenkov1657f362018-11-30 02:18:37 +00004838 } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004839 ReturnType = PD->getType();
George Karpenkov1657f362018-11-30 02:18:37 +00004840 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004841 ReturnType = FD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00004842 } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
4843 // Attributes on parameters are used for out-parameters,
4844 // passed as pointers-to-pointers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004845 ReturnType = Param->getType()->getPointeeType();
4846 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004847 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov1657f362018-11-30 02:18:37 +00004848 << AL << /*pointer-to-CF-pointer*/ 2 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004849 return;
4850 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004851 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00004852 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004853 } else {
4854 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004855 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004856 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004857 case ParsedAttr::AT_NSReturnsRetained:
4858 case ParsedAttr::AT_NSReturnsAutoreleased:
4859 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004860 case ParsedAttr::AT_OSReturnsRetained:
4861 case ParsedAttr::AT_OSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004862 ExpectedDeclKind = ExpectedFunctionOrMethod;
4863 break;
4864
Erich Keanee891aa92018-07-13 15:07:47 +00004865 case ParsedAttr::AT_CFReturnsRetained:
4866 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004867 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4868 break;
4869 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004870 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004871 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004872 return;
4873 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004874
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004875 bool TypeOK;
4876 bool Cf;
4877 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004878 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004879 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004880 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
4881 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004882 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004883
4884 case ParsedAttr::AT_NSReturnsAutoreleased:
4885 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004886 TypeOK = isValidSubjectOfNSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004887 Cf = false;
John McCalled433932011-01-25 03:31:58 +00004888 break;
4889
Erich Keanee891aa92018-07-13 15:07:47 +00004890 case ParsedAttr::AT_CFReturnsRetained:
4891 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004892 TypeOK = isValidSubjectOfCFAttribute(ReturnType);
4893 Cf = true;
4894 break;
4895
4896 case ParsedAttr::AT_OSReturnsRetained:
4897 case ParsedAttr::AT_OSReturnsNotRetained:
4898 TypeOK = isValidSubjectOfOSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004899 Cf = true;
John McCalled433932011-01-25 03:31:58 +00004900 break;
4901 }
4902
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004903 if (!TypeOK) {
4904 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00004905 return;
4906
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004907 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004908 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004909 << AL << /*pointer-to-CF*/ 2 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004910 } else {
4911 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4912 enum : unsigned {
4913 Function,
4914 Method,
4915 Property
4916 } SubjectKind = Function;
4917 if (isa<ObjCMethodDecl>(D))
4918 SubjectKind = Method;
4919 else if (isa<ObjCPropertyDecl>(D))
4920 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004921 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004922 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004923 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004924 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004925 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004926
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004927 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004928 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004929 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004930 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00004931 handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL);
John McCalled433932011-01-25 03:31:58 +00004932 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004933 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004934 handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00004935 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004936 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004937 handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00004938 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004939 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004940 handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004941 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004942 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004943 handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL);
4944 return;
4945 case ParsedAttr::AT_OSReturnsRetained:
4946 handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL);
4947 return;
4948 case ParsedAttr::AT_OSReturnsNotRetained:
4949 handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004950 return;
4951 };
4952}
4953
John McCallcf166702011-07-22 08:53:00 +00004954static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004955 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004956 const int EP_ObjCMethod = 1;
4957 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00004958
Erich Keaneb11ebc52017-09-27 03:20:13 +00004959 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004960 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004961 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004962 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004963 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004964 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004965
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004966 if (!resultType->isReferenceType() &&
4967 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004968 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004969 << SourceRange(loc) << Attrs
4970 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
4971 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004972
4973 // Drop the attribute.
4974 return;
4975 }
4976
Nico Weber462fd1e2015-01-07 23:50:05 +00004977 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00004978 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004979}
4980
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004981static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004982 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004983 const auto *Method = cast<ObjCMethodDecl>(D);
4984
4985 const DeclContext *DC = Method->getDeclContext();
4986 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004987 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00004988 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004989 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4990 return;
4991 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004992 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004993 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00004994 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004995 return;
4996 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004997
4998 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
4999 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005000}
5001
Erich Keanee891aa92018-07-13 15:07:47 +00005002static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005003 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00005004
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005005 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005006 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005007 return;
5008 }
John McCall28592582015-02-01 22:34:06 +00005009
5010 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005011 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00005012 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005013 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00005014 return;
5015 }
5016
5017 // Only allow 'cv void *'.
5018 QualType T = TD->getUnderlyingType();
5019 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005020 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00005021 return;
5022 }
5023 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005024
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005025 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005026 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
5027 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005028}
5029
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005030static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005031 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005032 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00005033
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005034 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005035 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005036 return;
5037 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005038
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005039 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005040 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
5041 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005042}
5043
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005044static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005045 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005046 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005047 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005048 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005049 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005050 return;
5051 }
5052 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005053 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005054 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005055 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005056 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005057 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005058 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005059 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005060}
5061
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005062static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005063 const ParsedAttr &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005064 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005065 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005066 IFace = CatDecl->getClassInterface();
5067 else
5068 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005069
5070 if (!IFace)
5071 return;
5072
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00005073 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00005074 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005075 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
5076 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005077}
5078
Erich Keanee891aa92018-07-13 15:07:47 +00005079static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005080 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005081 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005082 return;
5083 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005084 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005085 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005086 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005087}
5088
Nico Webera6916892016-06-10 18:53:04 +00005089// When a user wants to use objc_boxable with a union or struct
5090// but they don't have access to the declaration (legacy/third-party code)
5091// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00005092// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00005093static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00005094 bool notify = false;
5095
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005096 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00005097 if (RD && RD->getDefinition()) {
5098 RD = RD->getDefinition();
5099 notify = true;
5100 }
5101
5102 if (RD) {
5103 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005104 ObjCBoxableAttr(AL.getRange(), S.Context,
5105 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00005106 RD->addAttr(BoxableAttr);
5107 if (notify) {
5108 // we need to notify ASTReader/ASTWriter about
5109 // modification of existing declaration
5110 if (ASTMutationListener *L = S.getASTMutationListener())
5111 L->AddedAttributeToRecord(BoxableAttr, RD);
5112 }
5113 }
5114}
5115
Erich Keanee891aa92018-07-13 15:07:47 +00005116static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005117 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005118
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005119 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005120 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005121}
5122
Chandler Carruthedc2c642011-07-02 00:01:44 +00005123static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005124 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005125 const auto *VD = cast<ValueDecl>(D);
5126 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005127
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005128 if (!QT->isDependentType() &&
5129 !QT->isObjCLifetimeType()) {
5130 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5131 << QT;
John McCall31168b02011-06-15 23:02:42 +00005132 return;
5133 }
5134
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005135 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005136
5137 // If we have no lifetime yet, check the lifetime we're presumably
5138 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005139 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5140 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005141
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005142 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005143 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005144 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005145 "didn't infer lifetime for non-dependent type?");
5146 break;
5147
5148 case Qualifiers::OCL_Weak: // meaningful
5149 case Qualifiers::OCL_Strong: // meaningful
5150 break;
5151
5152 case Qualifiers::OCL_ExplicitNone:
5153 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005154 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5155 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005156 break;
5157 }
5158
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005159 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005160 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
5161 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00005162}
5163
Francois Picheta83957a2010-12-19 06:50:37 +00005164//===----------------------------------------------------------------------===//
5165// Microsoft specific attribute handlers.
5166//===----------------------------------------------------------------------===//
5167
Nico Weber88f5ed92016-09-13 18:55:26 +00005168UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
5169 unsigned AttrSpellingListIndex, StringRef Uuid) {
5170 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005171 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005172 return nullptr;
5173 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5174 Diag(Range.getBegin(), diag::note_previous_uuid);
5175 D->dropAttr<UuidAttr>();
5176 }
5177
5178 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5179}
5180
Erich Keanee891aa92018-07-13 15:07:47 +00005181static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005182 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005183 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005184 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005185 return;
5186 }
5187
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005188 StringRef StrRef;
5189 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005190 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005191 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005192
David Majnemer89085342013-08-09 08:56:20 +00005193 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5194 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005195 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5196 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005197
Reid Kleckner140c4a72013-05-17 14:04:52 +00005198 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005199 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005200 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005201 return;
5202 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005203
David Majnemer89085342013-08-09 08:56:20 +00005204 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005205 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005206 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005207 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005208 return;
5209 }
David Majnemer89085342013-08-09 08:56:20 +00005210 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005211 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005212 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005213 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005214 }
Francois Picheta83957a2010-12-19 06:50:37 +00005215
Nico Weber469891e2017-05-05 17:05:56 +00005216 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5217 // the only thing in the [] list, the [] too), and add an insertion of
5218 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5219 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005220 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005221 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005222 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5223 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005224
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005225 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5226 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005227 if (UA)
5228 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005229}
5230
Erich Keanee891aa92018-07-13 15:07:47 +00005231static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005232 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005233 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005234 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005235 return;
5236 }
5237 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005238 D, AL.getRange(), /*BestCase=*/true,
5239 AL.getAttributeSpellingListIndex(),
5240 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005241 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005242 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005243 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5244 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005245}
5246
Erich Keanee891aa92018-07-13 15:07:47 +00005247static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005248 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005249 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005250 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005251 return;
5252 }
5253 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005254 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005255 return;
5256 }
5257 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005258 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005259 return;
5260 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005261 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5262 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005263}
5264
Erich Keanee891aa92018-07-13 15:07:47 +00005265static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005266 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005267 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005268 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005269 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005270 return;
5271 Tags.push_back(Tag);
5272 }
5273
5274 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5275 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005276 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005277 return;
5278 }
5279 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005280 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005281 return;
5282 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005283 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005284 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005285 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005286 return;
5287
5288 // Store tags sorted and without duplicates.
Fangrui Song55fab262018-09-26 22:16:28 +00005289 llvm::sort(Tags);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005290 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5291
5292 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005293 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5294 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005295}
5296
Erich Keanee891aa92018-07-13 15:07:47 +00005297static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005298 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005299 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005300 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005301 return;
5302 }
5303
5304 StringRef Str;
5305 SourceLocation ArgLoc;
5306
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005307 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005308 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005309 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005310 return;
5311
5312 ARMInterruptAttr::InterruptType Kind;
5313 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005314 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5315 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005316 return;
5317 }
5318
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005319 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005320 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005321 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005322}
5323
Erich Keanee891aa92018-07-13 15:07:47 +00005324static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005325 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005326 return;
5327
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005328 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005329 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5330 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005331 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005332 }
5333
5334 // FIXME: Check for decl - it should be void ()(void).
5335
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005336 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005337 llvm::APSInt NumParams(32);
5338 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005339 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005340 << AL << AANT_ArgumentIntegerConstant
5341 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005342 return;
5343 }
5344
5345 unsigned Num = NumParams.getLimitedValue(255);
5346 if ((Num & 1) || Num > 30) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005347 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005348 << AL << (int)NumParams.getSExtValue()
5349 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005350 return;
5351 }
5352
Aaron Ballman36a53502014-01-16 13:03:14 +00005353 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005354 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5355 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005356 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005357}
5358
Erich Keanee891aa92018-07-13 15:07:47 +00005359static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005360 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005361 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005362 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005363 return;
5364 }
5365
5366 StringRef Str;
5367 SourceLocation ArgLoc;
5368
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005369 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005370 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005371 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005372 return;
5373
5374 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5375 // a) Must be a function.
5376 // b) Must have no parameters.
5377 // c) Must have the 'void' return type.
5378 // d) Cannot have the 'mips16' attribute, as that instruction set
5379 // lacks the 'eret' instruction.
5380 // e) The attribute itself must either have no argument or one of the
5381 // valid interrupt types, see [MipsInterruptDocs].
5382
5383 if (!isFunctionOrMethod(D)) {
5384 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5385 << "'interrupt'" << ExpectedFunctionOrMethod;
5386 return;
5387 }
5388
5389 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5390 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5391 << 0;
5392 return;
5393 }
5394
5395 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5396 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5397 << 1;
5398 return;
5399 }
5400
Erich Keane44bacdf2018-08-09 13:21:32 +00005401 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005402 return;
5403
5404 MipsInterruptAttr::InterruptType Kind;
5405 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005406 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005407 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005408 return;
5409 }
5410
5411 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005412 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005413}
5414
Erich Keanee891aa92018-07-13 15:07:47 +00005415static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005416 // Semantic checks for a function with the 'interrupt' attribute.
5417 // a) Must be a function.
5418 // b) Must have the 'void' return type.
5419 // c) Must take 1 or 2 arguments.
5420 // d) The 1st argument must be a pointer.
5421 // e) The 2nd argument (if any) must be an unsigned integer.
5422 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5423 CXXMethodDecl::isStaticOverloadedOperator(
5424 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005425 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005426 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005427 return;
5428 }
5429 // Interrupt handler must have void return type.
5430 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5431 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5432 diag::err_anyx86_interrupt_attribute)
5433 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5434 ? 0
5435 : 1)
5436 << 0;
5437 return;
5438 }
5439 // Interrupt handler must have 1 or 2 parameters.
5440 unsigned NumParams = getFunctionOrMethodNumParams(D);
5441 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005442 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005443 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5444 ? 0
5445 : 1)
5446 << 1;
5447 return;
5448 }
5449 // The first argument must be a pointer.
5450 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5451 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5452 diag::err_anyx86_interrupt_attribute)
5453 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5454 ? 0
5455 : 1)
5456 << 2;
5457 return;
5458 }
5459 // The second argument, if present, must be an unsigned integer.
5460 unsigned TypeSize =
5461 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5462 ? 64
5463 : 32;
5464 if (NumParams == 2 &&
5465 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5466 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5467 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5468 diag::err_anyx86_interrupt_attribute)
5469 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5470 ? 0
5471 : 1)
5472 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5473 return;
5474 }
5475 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005476 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005477 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5478}
5479
Erich Keanee891aa92018-07-13 15:07:47 +00005480static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005481 if (!isFunctionOrMethod(D)) {
5482 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5483 << "'interrupt'" << ExpectedFunction;
5484 return;
5485 }
5486
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005487 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005488 return;
5489
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005490 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005491}
5492
Erich Keanee891aa92018-07-13 15:07:47 +00005493static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005494 if (!isFunctionOrMethod(D)) {
5495 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5496 << "'signal'" << ExpectedFunction;
5497 return;
5498 }
5499
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005500 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005501 return;
5502
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005503 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005504}
5505
Ana Pazos1eee1b72018-07-26 17:37:45 +00005506
5507static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5508 const ParsedAttr &AL) {
5509 // Warn about repeated attributes.
5510 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5511 S.Diag(AL.getRange().getBegin(),
5512 diag::warn_riscv_repeated_interrupt_attribute);
5513 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5514 return;
5515 }
5516
5517 // Check the attribute argument. Argument is optional.
5518 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5519 return;
5520
5521 StringRef Str;
5522 SourceLocation ArgLoc;
5523
5524 // 'machine'is the default interrupt mode.
5525 if (AL.getNumArgs() == 0)
5526 Str = "machine";
5527 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5528 return;
5529
5530 // Semantic checks for a function with the 'interrupt' attribute:
5531 // - Must be a function.
5532 // - Must have no parameters.
5533 // - Must have the 'void' return type.
5534 // - The attribute itself must either have no argument or one of the
5535 // valid interrupt types, see [RISCVInterruptDocs].
5536
5537 if (D->getFunctionType() == nullptr) {
5538 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5539 << "'interrupt'" << ExpectedFunction;
5540 return;
5541 }
5542
5543 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5544 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 0;
5545 return;
5546 }
5547
5548 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5549 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 1;
5550 return;
5551 }
5552
5553 RISCVInterruptAttr::InterruptType Kind;
5554 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005555 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5556 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005557 return;
5558 }
5559
5560 D->addAttr(::new (S.Context) RISCVInterruptAttr(
5561 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
5562}
5563
Erich Keanee891aa92018-07-13 15:07:47 +00005564static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005565 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005566 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5567 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005568 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005569 break;
5570 case llvm::Triple::mipsel:
5571 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005572 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005573 break;
5574 case llvm::Triple::x86:
5575 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005576 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005577 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005578 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005579 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005580 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005581 case llvm::Triple::riscv32:
5582 case llvm::Triple::riscv64:
5583 handleRISCVInterruptAttr(S, D, AL);
5584 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005585 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005586 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005587 break;
5588 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005589}
5590
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005591static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005592 const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005593 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005594 Expr *MinExpr = AL.getArgAsExpr(0);
5595 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005596 return;
5597
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005598 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005599 Expr *MaxExpr = AL.getArgAsExpr(1);
5600 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005601 return;
5602
5603 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005604 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005605 return;
5606 }
5607 if (Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005608 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005609 return;
5610 }
5611
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005612 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005613 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5614 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005615}
5616
Erich Keanee891aa92018-07-13 15:07:47 +00005617static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005618 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005619 Expr *MinExpr = AL.getArgAsExpr(0);
5620 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005621 return;
5622
5623 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005624 if (AL.getNumArgs() == 2) {
5625 Expr *MaxExpr = AL.getArgAsExpr(1);
5626 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005627 return;
5628 }
5629
5630 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005631 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005632 return;
5633 }
5634 if (Max != 0 && Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005635 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005636 return;
5637 }
5638
5639 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005640 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5641 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005642}
5643
Erich Keanee891aa92018-07-13 15:07:47 +00005644static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005645 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005646 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5647 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005648 return;
5649
5650 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005651 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5652 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005653}
5654
Erich Keanee891aa92018-07-13 15:07:47 +00005655static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005656 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005657 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5658 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005659 return;
5660
5661 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005662 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5663 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005664}
5665
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005666static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005667 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005668 // If we try to apply it to a function pointer, don't warn, but don't
5669 // do anything, either. It doesn't matter anyway, because there's nothing
5670 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005671 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005672 if (VD && VD->getType()->isFunctionPointerType())
5673 return;
5674 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005675 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005676 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5677 TD->getUnderlyingType()->isFunctionType()))
5678 return;
5679 // Attribute can only be applied to function types.
5680 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005681 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005682 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005683 return;
5684 }
5685
Aaron Ballman36a53502014-01-16 13:03:14 +00005686 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005687 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5688 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005689}
5690
Erich Keanee891aa92018-07-13 15:07:47 +00005691static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005692 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005693 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5694 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005695 return;
5696
5697 // TODO: Investigate what happens with the next major version of MSVC.
Reid Kleckner1a94d872018-12-17 23:16:43 +00005698 if (Version != LangOptions::MSVC2015 / 100) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005699 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005700 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005701 return;
5702 }
5703
Reid Kleckner1a94d872018-12-17 23:16:43 +00005704 // The attribute expects a "major" version number like 19, but new versions of
5705 // MSVC have moved to updating the "minor", or less significant numbers, so we
5706 // have to multiply by 100 now.
5707 Version *= 100;
5708
David Majnemercd3ebfe2016-05-23 17:16:12 +00005709 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005710 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5711 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005712}
5713
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005714DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5715 unsigned AttrSpellingListIndex) {
5716 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005717 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005718 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005719 }
5720
5721 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005722 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005723
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005724 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005725}
5726
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005727DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5728 unsigned AttrSpellingListIndex) {
5729 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005730 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005731 D->dropAttr<DLLImportAttr>();
5732 }
5733
5734 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005735 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005736
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005737 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005738}
5739
Erich Keanee891aa92018-07-13 15:07:47 +00005740static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005741 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5742 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005743 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00005744 return;
5745 }
5746
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005747 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00005748 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005749 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5750 // MinGW doesn't allow dllimport on inline functions.
5751 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00005752 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005753 return;
5754 }
5755 }
5756
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005757 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005758 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5759 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005760 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00005761 return;
5762 }
5763 }
5764
Hans Wennborge82f19c2014-06-24 23:57:05 +00005765 unsigned Index = A.getAttributeSpellingListIndex();
Erich Keanee891aa92018-07-13 15:07:47 +00005766 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Hans Wennborge82f19c2014-06-24 23:57:05 +00005767 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5768 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005769 if (NewAttr)
5770 D->addAttr(NewAttr);
5771}
5772
David Majnemer2c4e00a2014-01-29 22:07:36 +00005773MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00005774Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00005775 unsigned AttrSpellingListIndex,
5776 MSInheritanceAttr::Spelling SemanticSpelling) {
5777 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
5778 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00005779 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005780 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
5781 << 1 /*previous declaration*/;
5782 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
5783 D->dropAttr<MSInheritanceAttr>();
5784 }
5785
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005786 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005787 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00005788 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
5789 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005790 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005791 }
5792 } else {
5793 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
5794 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5795 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005796 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005797 }
5798 if (RD->getDescribedClassTemplate()) {
5799 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5800 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005801 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005802 }
5803 }
5804
5805 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00005806 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005807}
5808
Erich Keanee891aa92018-07-13 15:07:47 +00005809static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005810 // The capability attributes take a single string parameter for the name of
5811 // the capability they represent. The lockable attribute does not take any
5812 // parameters. However, semantically, both attributes represent the same
5813 // concept, and so they use the same semantic attribute. Eventually, the
5814 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00005815 //
Alp Toker958027b2014-07-14 19:42:55 +00005816 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00005817 // literal will be considered a "mutex."
5818 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005819 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00005820 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005821 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005822 return;
5823
Aaron Ballman6c810072014-03-05 21:47:13 +00005824 // Currently, there are only two names allowed for a capability: role and
5825 // mutex (case insensitive). Diagnose other capability names.
5826 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
5827 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
5828
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005829 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
5830 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005831}
5832
Erich Keanee891aa92018-07-13 15:07:47 +00005833static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00005834 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005835 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00005836 return;
5837
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005838 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00005839 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005840 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005841}
5842
5843static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005844 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005845 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005846 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005847 return;
5848
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005849 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005850 S.Context,
5851 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005852 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005853}
5854
5855static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005856 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005857 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005858 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005859 return;
5860
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005861 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005862 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005863 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005864 Args.data(),
5865 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005866 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005867}
5868
5869static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005870 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005871 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005872 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005873 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005874
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005875 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005876 AL.getRange(), S.Context, Args.data(), Args.size(),
5877 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005878}
5879
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005880static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005881 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005882 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005883 return;
5884
5885 // check that all arguments are lockable objects
5886 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005887 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005888 if (Args.empty())
5889 return;
5890
5891 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005892 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
5893 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005894
5895 D->addAttr(RCA);
5896}
5897
Erich Keanee891aa92018-07-13 15:07:47 +00005898static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005899 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005900 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005901 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00005902 // Do not want to attach the attribute to the namespace because that will
5903 // cause confusing diagnostic reports for uses of declarations within the
5904 // namespace.
5905 return;
5906 }
5907 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005908
Manman Renc7890fe2016-03-16 18:50:49 +00005909 // Handle the cases where the attribute has a text message.
5910 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005911 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
5912 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00005913 return;
5914
5915 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005916 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
5917 checkAttributeAtMostNumArgs(S, AL, 1);
5918 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
Sander de Smalen44a22532018-11-26 16:38:37 +00005919 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
5920 return;
5921
5922 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
5923 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
5924
5925 D->addAttr(::new (S.Context)
5926 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005927 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005928}
5929
5930static bool isGlobalVar(const Decl *D) {
5931 if (const auto *S = dyn_cast<VarDecl>(D))
5932 return S->hasGlobalStorage();
5933 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00005934}
5935
Erich Keanee891aa92018-07-13 15:07:47 +00005936static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005937 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00005938 return;
5939
Benjamin Kramer1b582012016-02-13 18:11:49 +00005940 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00005941
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005942 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00005943 StringRef SanitizerName;
5944 SourceLocation LiteralLoc;
5945
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005946 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00005947 return;
5948
5949 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
5950 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005951 else if (isGlobalVar(D) && SanitizerName != "address")
5952 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005953 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00005954 Sanitizers.push_back(SanitizerName);
5955 }
5956
5957 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005958 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
5959 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005960}
5961
5962static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005963 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005964 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00005965 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005966 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
5967 .Case("no_address_safety_analysis", "address")
5968 .Case("no_sanitize_address", "address")
5969 .Case("no_sanitize_thread", "thread")
5970 .Case("no_sanitize_memory", "memory");
5971 if (isGlobalVar(D) && SanitizerName != "address")
5972 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005973 << AL << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00005974 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005975 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
5976 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005977}
5978
Erich Keanee891aa92018-07-13 15:07:47 +00005979static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005980 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005981 D->addAttr(Internal);
5982}
5983
Erich Keanee891aa92018-07-13 15:07:47 +00005984static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005985 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005986 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00005987 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005988 else
Erich Keane44bacdf2018-08-09 13:21:32 +00005989 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
5990 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005991}
5992
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005993/// Handles semantic checking for features that are common to all attributes,
5994/// such as checking whether a parameter was properly specified, or the correct
5995/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005996static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005997 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005998 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00005999 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006000 //
6001 // We also bail on unknown and ignored attributes because those are handled
6002 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00006003 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006004 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006005 // Check whether the attribute requires specific language extensions to be
6006 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006007 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00006008 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00006009 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006010 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00006011 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006012 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00006013 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006014
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006015 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006016 // If there are no optional arguments, then checking for the argument count
6017 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006018 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006019 return true;
6020 } else {
6021 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006022 if (AL.getMinArgs() &&
6023 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006024 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006025 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
6026 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006027 return true;
6028 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00006029
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006030 if (S.CheckAttrTarget(AL))
6031 return true;
6032
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006033 return false;
6034}
6035
Erich Keanee891aa92018-07-13 15:07:47 +00006036static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006037 if (D->isInvalidDecl())
6038 return;
6039
6040 // Check if there is only one access qualifier.
6041 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006042 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
6043 AL.getSemanticSpelling()) {
6044 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
6045 << AL.getName()->getName() << AL.getRange();
6046 } else {
6047 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
6048 << D->getSourceRange();
6049 D->setInvalidDecl(true);
6050 return;
6051 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00006052 }
6053
6054 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
6055 // image object can be read and written.
6056 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
6057 // object. Using the read_write (or __read_write) qualifier with the pipe
6058 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006059 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006060 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006061 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006062 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006063 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00006064 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00006065 D->setInvalidDecl(true);
6066 return;
6067 }
6068 }
6069 }
6070
6071 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006072 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00006073}
6074
Erik Pilkington5a559e62018-08-21 17:24:06 +00006075static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006076 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00006077 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
6078 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
6079 return;
6080 }
6081
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006082 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00006083 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006084 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00006085 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00006086}
6087
JF Bastien14daa202018-12-18 05:12:21 +00006088static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6089 assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic &&
6090 "uninitialized is only valid on automatic duration variables");
6091 unsigned Index = AL.getAttributeSpellingListIndex();
6092 D->addAttr(::new (S.Context)
6093 UninitializedAttr(AL.getLoc(), S.Context, Index));
6094}
6095
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00006096//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006097// Top Level Sema Entry Points
6098//===----------------------------------------------------------------------===//
6099
Richard Smithf8a75c32013-08-29 00:47:48 +00006100/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
6101/// the attribute applies to decls. If the attribute is a type attribute, just
6102/// silently ignore it if a GNU attribute.
6103static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006104 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00006105 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00006106 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00006107 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00006108
Richard Smithf8a75c32013-08-29 00:47:48 +00006109 // Ignore C++11 attributes on declarator chunks: they appertain to the type
6110 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006111 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00006112 return;
6113
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006114 // Unknown attributes are automatically warned on. Target-specific attributes
6115 // which do not apply to the current target architecture are treated as
6116 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006117 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006118 !AL.existsInTarget(S.Context.getTargetInfo())) {
Simon Pilgrimfc0ff612018-12-17 12:17:37 +00006119 S.Diag(AL.getLoc(),
6120 AL.isDeclspecAttribute()
6121 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
6122 : (unsigned)diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006123 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006124 return;
6125 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006126
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006127 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006128 return;
6129
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006130 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006131 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006132 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00006133 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006134 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00006135 break;
6136 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006137 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00006138 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006139 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006140 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006141 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006142 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006143 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006144 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006145 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006146 case ParsedAttr::AT_DLLExport:
6147 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006148 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006149 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006150 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006151 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006152 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006153 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006154 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006155 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006156 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006157 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006158 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006159 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006160 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006161 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006162 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006163 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006164 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006165 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006166 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006167 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006168 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006169 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006170 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006171 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006172 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006173 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006174 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006175 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006176 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006177 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006178 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006179 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006180 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006181 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006182 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006183 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006184 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006185 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006186 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006187 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006188 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006189 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006190 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006191 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006192 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006193 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006194 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006195 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006196 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006197 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006198 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006199 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006200 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006201 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006202 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006203 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006204 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006205 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006206 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006207 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006208 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006209 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006210 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006211 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006212 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006213 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006214 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006215 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006216 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006217 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006218 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006219 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006220 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006221 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006222 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006223 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006224 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006225 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006226 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006227 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006228 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006229 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006230 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006231 case ParsedAttr::AT_CPUDispatch:
6232 case ParsedAttr::AT_CPUSpecific:
6233 handleCPUSpecificAttr(S, D, AL);
6234 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006235 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006236 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006237 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006238 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006239 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006240 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006241 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006242 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006243 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006244 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006245 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006246 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006247 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006248 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006249 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006250 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006251 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006252 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006253 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006254 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006255 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006256 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006257 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006258 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006259 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006260 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006261 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006262 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006263 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006264 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006265 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006266 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006267 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006268 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006269 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006270 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006271 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006272 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006273 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006274 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006275 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006276 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006277 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006278 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006279 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006280 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006281 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006282 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006283 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006284 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006285 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006286 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006287 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006288 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006289 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006290 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006291 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006292 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006293 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006294 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006295 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006296 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006297 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006298 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006299 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006300 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006301 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006302 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006303 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006304 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006305 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006306 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006307 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006308 case ParsedAttr::AT_LifetimeBound:
6309 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6310 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006311 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006312 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006313 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006314 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006315 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006316 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006317 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006318 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006319 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006320 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006321 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006322 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006323 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006324 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006325 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006326 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006327 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6328 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006329 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006330 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006331 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006332 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006333 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006334 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006335 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006336 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006337 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006338 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006339 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006340 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006341 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006342 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006343 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006344 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006345 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006346 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006347 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006348 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006349 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006350 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006351 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006352 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006353 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006354 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006355 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006356 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006357 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006358 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006359 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006360 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006361 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006362 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006363 handleNoCfCheckAttr(S, D, AL);
6364 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006365 case ParsedAttr::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006366 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006367 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006368 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006369 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006370 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006371 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006372 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006373 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006374 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006375 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006376 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006377 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006378 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006379 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006380 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006381 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006382 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006383 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006384 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006385 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006386 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006387 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006388 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006389 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006390 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006391 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006392 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006393 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006394 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006395 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006396 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006397 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006398 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006399 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006400 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006401 case ParsedAttr::AT_ObjCRuntimeVisible:
6402 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6403 break;
6404 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006405 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006406 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006407 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006408 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6409 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006410 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006411 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006412 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6413 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006414 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006415 case ParsedAttr::AT_CFConsumed:
6416 case ParsedAttr::AT_NSConsumed:
George Karpenkov1657f362018-11-30 02:18:37 +00006417 case ParsedAttr::AT_OSConsumed:
6418 S.AddXConsumedAttr(D, AL.getRange(), AL.getAttributeSpellingListIndex(),
6419 parsedAttrToRetainOwnershipKind(AL),
6420 /*IsTemplateInstantiation=*/false);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006421 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006422 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006423 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006424 break;
George Karpenkovda2c77f2018-12-06 22:06:59 +00006425 case ParsedAttr::AT_OSConsumesThis:
6426 handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL);
6427 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006428 case ParsedAttr::AT_NSReturnsAutoreleased:
6429 case ParsedAttr::AT_NSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006430 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006431 case ParsedAttr::AT_CFReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006432 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006433 case ParsedAttr::AT_OSReturnsNotRetained:
6434 case ParsedAttr::AT_OSReturnsRetained:
6435 handleXReturnsXRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006436 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006437 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006438 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006439 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006440 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006441 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006442 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006443 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006444 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006445 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006446 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006447 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006448 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006449 case ParsedAttr::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006450 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006451 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006452 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006453 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006454 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006455 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006456 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006457 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006458 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006459 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006460 break;
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006461 case ParsedAttr::AT_SpeculativeLoadHardening:
6462 handleSimpleAttribute<SpeculativeLoadHardeningAttr>(S, D, AL);
6463 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006464 case ParsedAttr::AT_CodeSeg:
6465 handleCodeSegAttr(S, D, AL);
6466 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006467 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006468 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006469 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006470 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006471 handleMinVectorWidthAttr(S, D, AL);
6472 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006473 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006474 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006475 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006476 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006477 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006478 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006479 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006480 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006481 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006482 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006483 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006484 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006485 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006486 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006487 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006488 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006489 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006490 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006491 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006492 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006493 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006494 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006495 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006496 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006497 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006498 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6499 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006500 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006501 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00006502 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
6503 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006504 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006505 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006506 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006507 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006508 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006509 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006510 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006511 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006512 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006513 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006514 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006515 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006516 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006517 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006518 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006519 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006520 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006521 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006522 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006523 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006524 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006525 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006526 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006527 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006528 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006529 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006530 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006531 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006532 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006533 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006534 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006535 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006536 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006537 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006538 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006539 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006540 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006541 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006542 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006543 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006544 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006545 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006546 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006547 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006548 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006549 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006550 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006551 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006552 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006553 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006554 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006555 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006556 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006557 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006558 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006559 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006560 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006561 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006562 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006563 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006564 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006565 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006566 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006567 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006568 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006569 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006570 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006571 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006572 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006573 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006574 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00006575 // Interacts with -fstack-protector options.
6576 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
6577 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006578 case ParsedAttr::AT_StdCall:
6579 case ParsedAttr::AT_CDecl:
6580 case ParsedAttr::AT_FastCall:
6581 case ParsedAttr::AT_ThisCall:
6582 case ParsedAttr::AT_Pascal:
6583 case ParsedAttr::AT_RegCall:
6584 case ParsedAttr::AT_SwiftCall:
6585 case ParsedAttr::AT_VectorCall:
6586 case ParsedAttr::AT_MSABI:
6587 case ParsedAttr::AT_SysVABI:
6588 case ParsedAttr::AT_Pcs:
6589 case ParsedAttr::AT_IntelOclBicc:
6590 case ParsedAttr::AT_PreserveMost:
6591 case ParsedAttr::AT_PreserveAll:
Sander de Smalen44a22532018-11-26 16:38:37 +00006592 case ParsedAttr::AT_AArch64VectorPcs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006593 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006594 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006595 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006596 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006597 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006598 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006599 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006600 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006601 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006602 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006603 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006604 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006605 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006606 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006607 case ParsedAttr::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006608 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006609 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006610 case ParsedAttr::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006611 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006612 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006613 case ParsedAttr::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006614 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006615 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006616 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006617 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006618 break;
Louis Dionned2695792018-10-04 15:49:42 +00006619 case ParsedAttr::AT_ExcludeFromExplicitInstantiation:
6620 handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL);
6621 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006622 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006623 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006624 break;
John McCall8d32c052012-05-22 21:28:12 +00006625
6626 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006627 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006628 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006629 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006630 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006631 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006632 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006633 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006634 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006635 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006636 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006637 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00006638 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006639 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006640 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00006641 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006642 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006643 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00006644 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006645 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006646 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006647 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006648 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006649 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006650 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006651 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006652 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006653 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00006654
Erich Keanee891aa92018-07-13 15:07:47 +00006655 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006656 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006657 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006658
6659 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006660 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006661 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006662 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006663 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006664 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006665 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006666 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006667 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006668 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006669 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006670 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006671 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006672 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006673 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006674 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006675 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006676 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00006677 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006678 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006679 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00006680 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006681 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006682 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00006683 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006684 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006685 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006686 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006687 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006688 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006689 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006690 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006691 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006692 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006693 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006694 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006695 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006696 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006697 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006698 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006699 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006700 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006701 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006702 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006703 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006704 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006705 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006706 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006707 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006708
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006709 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006710 case ParsedAttr::AT_Capability:
6711 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006712 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006713 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006714 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006715 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006716 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006717
Erich Keanee891aa92018-07-13 15:07:47 +00006718 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006719 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006720 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006721 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006722 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006723 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006724 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006725 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006726 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006727 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006728 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006729 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006730
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00006731 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006732 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006733 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006734 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006735 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006736 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006737 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006738 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006739 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006740 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006741 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006742 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006743 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006744 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006745 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00006746 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006747 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006748 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006749 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006750 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006751 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006752 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006753 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006754 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006755 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006756
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006757 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006758 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006759 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006760 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006761 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006762 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006763 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006764 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006765 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006766 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006767 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006768 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00006769 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006770 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006771 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006772 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006773 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006774 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006775 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006776 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00006777
6778 // Move semantics attribute.
6779 case ParsedAttr::AT_Reinitializes:
6780 handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
6781 break;
Erik Pilkington5a559e62018-08-21 17:24:06 +00006782
6783 case ParsedAttr::AT_AlwaysDestroy:
6784 case ParsedAttr::AT_NoDestroy:
6785 handleDestroyAttr(S, D, AL);
6786 break;
JF Bastien14daa202018-12-18 05:12:21 +00006787
6788 case ParsedAttr::AT_Uninitialized:
6789 handleUninitializedAttr(S, D, AL);
6790 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006791 }
6792}
6793
6794/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
6795/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00006796void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00006797 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00006798 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00006799 if (AttrList.empty())
6800 return;
6801
Erich Keanee891aa92018-07-13 15:07:47 +00006802 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00006803 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00006804
Joey Gouly2cd9db12013-12-13 16:15:28 +00006805 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00006806 // GCC accepts
6807 // static int a9 __attribute__((weakref));
6808 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00006809 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00006810 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
6811 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00006812 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00006813 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006814 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006815
Aaron Ballmanbe243a72014-12-04 22:45:31 +00006816 // FIXME: We should be able to handle this in TableGen as well. It would be
6817 // good to have a way to specify "these attributes must appear as a group",
6818 // for these. Additionally, it would be good to have a way to specify "these
6819 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00006820 if (!D->hasAttr<OpenCLKernelAttr>()) {
6821 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006822 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006823 // FIXME: This emits a different error message than
6824 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00006825 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006826 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006827 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006828 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006829 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006830 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006831 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006832 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006833 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006834 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
6835 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00006836 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
6837 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
6838 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6839 << A << ExpectedKernelFunction;
6840 D->setInvalidDecl();
6841 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
6842 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6843 << A << ExpectedKernelFunction;
6844 D->setInvalidDecl();
6845 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
6846 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6847 << A << ExpectedKernelFunction;
6848 D->setInvalidDecl();
6849 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
6850 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6851 << A << ExpectedKernelFunction;
6852 D->setInvalidDecl();
6853 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006854 }
6855 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006856}
6857
Hiroshi Inoue939d9322017-06-30 05:40:31 +00006858// Helper for delayed processing TransparentUnion attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006859void Sema::ProcessDeclAttributeDelayed(Decl *D,
6860 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006861 for (const ParsedAttr &AL : AttrList)
6862 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00006863 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00006864 break;
6865 }
6866}
6867
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006868// Annotation attributes are the only attributes allowed after an access
6869// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00006870bool Sema::ProcessAccessDeclAttributeList(
6871 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006872 for (const ParsedAttr &AL : AttrList) {
6873 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00006874 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006875 } else {
Erich Keanec480f302018-07-12 21:09:05 +00006876 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006877 return true;
6878 }
6879 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006880 return false;
6881}
6882
John McCall42856de2011-10-01 05:17:03 +00006883/// checkUnusedDeclAttributes - Check a list of attributes to see if it
6884/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00006885static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00006886 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00006887 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006888 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
6889 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00006890 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00006891 continue;
John McCall42856de2011-10-01 05:17:03 +00006892
Erich Keanee891aa92018-07-13 15:07:47 +00006893 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00006894 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006895 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006896 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00006897 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
6898 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006899 }
6900 }
6901}
6902
6903/// checkUnusedDeclAttributes - Given a declarator which is not being
6904/// used to build a declaration, complain about any decl attributes
6905/// which might be lying around on it.
6906void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00006907 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00006908 ::checkUnusedDeclAttributes(*this, D.getAttributes());
6909 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
6910 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
6911}
6912
Ryan Flynn7d470f32009-07-30 03:15:39 +00006913/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00006914/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00006915NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
6916 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00006917 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00006918 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006919 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00006920 FunctionDecl *NewFD;
6921 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00006922 // FIXME: Mangling?
6923 // FIXME: Is the qualifier info correct?
6924 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00006925 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
6926 Loc, Loc, DeclarationName(II),
6927 FD->getType(), FD->getTypeSourceInfo(),
6928 SC_None, false/*isInlineSpecified*/,
6929 FD->hasPrototype(),
6930 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006931 NewD = NewFD;
6932
6933 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00006934 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00006935
6936 // Fake up parameter variables; they are declared as if this were
6937 // a typedef.
6938 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006939 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00006940 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00006941 for (const auto &AI : FT->param_types()) {
6942 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006943 Param->setScopeInfo(0, Params.size());
6944 Params.push_back(Param);
6945 }
David Blaikie9c70e042011-09-21 18:16:56 +00006946 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00006947 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006948 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00006949 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00006950 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00006951 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00006952 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006953 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00006954 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00006955 }
6956 return NewD;
6957}
6958
James Dennett634962f2012-06-14 21:40:34 +00006959/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00006960/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00006961void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00006962 if (W.getUsed()) return; // only do this once
6963 W.setUsed(true);
6964 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
6965 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00006966 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00006967 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
6968 W.getLocation()));
6969 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00006970 WeakTopLevelDecl.push_back(NewD);
6971 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
6972 // to insert Decl at TU scope, sorry.
6973 DeclContext *SavedContext = CurContext;
6974 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00006975 NewD->setDeclContext(CurContext);
6976 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00006977 PushOnScopeChains(NewD, S);
6978 CurContext = SavedContext;
6979 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00006980 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00006981 }
6982}
6983
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006984void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
6985 // It's valid to "forward-declare" #pragma weak, in which case we
6986 // have to do this.
6987 LoadExternalWeakUndeclaredIdentifiers();
6988 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006989 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006990 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006991 if (VD->isExternC())
6992 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006993 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006994 if (FD->isExternC())
6995 ND = FD;
6996 if (ND) {
6997 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00006998 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006999 if (I != WeakUndeclaredIdentifiers.end()) {
7000 WeakInfo W = I->second;
7001 DeclApplyPragmaWeak(S, ND, W);
7002 WeakUndeclaredIdentifiers[Id] = W;
7003 }
7004 }
7005 }
7006 }
7007}
7008
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007009/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
7010/// it, apply them to D. This is a bit tricky because PD can have attributes
7011/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00007012void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007013 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00007014 if (!PD.getDeclSpec().getAttributes().empty())
7015 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00007016
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007017 // Walk the declarator structure, applying decl attributes that were in a type
7018 // position to the decl itself. This handles cases like:
7019 // int *__attr__(x)** D;
7020 // when X is a decl attribute.
7021 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00007022 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
7023 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00007024
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007025 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00007026 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00007027
7028 // Apply additional attributes specified by '#pragma clang attribute'.
7029 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007030}
John McCall28a6aea2009-11-04 02:18:39 +00007031
John McCall31168b02011-06-15 23:02:42 +00007032/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00007033/// If so, it'll still be annotated with an attribute that makes it
7034/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007035static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00007036 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00007037 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00007038 // Private ivars are always okay. Unfortunately, people don't
7039 // always properly make their ivars private, even in system headers.
7040 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007041 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
7042 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00007043 return false;
7044
John McCallc6af8c62015-10-28 05:03:19 +00007045 // Silently accept unsupported uses of __weak in both user and system
7046 // declarations when it's been disabled, for ease of integration with
7047 // -fno-objc-arc files. We do have to take some care against attempts
7048 // to define such things; for now, we've only done that for ivars
7049 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007050 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00007051 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
7052 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
7053 reason = UnavailableAttr::IR_ForbiddenWeak;
7054 return true;
7055 }
John McCallb61e14e2015-10-27 04:54:50 +00007056 }
7057
John McCallc6af8c62015-10-28 05:03:19 +00007058 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007059 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00007060 // Currently, all the failures dealt with this way are due to ARC
7061 // restrictions.
7062 reason = UnavailableAttr::IR_ARCForbiddenType;
7063 return true;
John McCallb61e14e2015-10-27 04:54:50 +00007064 }
7065
7066 return false;
John McCall31168b02011-06-15 23:02:42 +00007067}
7068
7069/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007070static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
7071 Decl *D) {
7072 auto Reason = UnavailableAttr::IR_None;
7073 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
7074 assert(Reason && "didn't set reason?");
7075 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00007076 return;
7077 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00007078 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007079 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00007080 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007081 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007082 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007083 DD.getForbiddenTypeDiagnostic() ==
7084 diag::err_arc_array_param_no_ownership) {
7085 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007086 return;
7087 }
7088 }
John McCall31168b02011-06-15 23:02:42 +00007089
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007090 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
7091 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
7092 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00007093}
7094
Manman Ren45b1ab12016-05-06 19:57:16 +00007095static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
7096 const Decl *D) {
7097 // Check each AvailabilityAttr to find the one for this platform.
7098 for (const auto *A : D->attrs()) {
7099 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
7100 // FIXME: this is copied from CheckAvailability. We should try to
7101 // de-duplicate.
7102
7103 // Check if this is an App Extension "platform", and if so chop off
7104 // the suffix for matching with the actual platform.
7105 StringRef ActualPlatform = Avail->getPlatform()->getName();
7106 StringRef RealizedPlatform = ActualPlatform;
7107 if (Context.getLangOpts().AppExt) {
7108 size_t suffix = RealizedPlatform.rfind("_app_extension");
7109 if (suffix != StringRef::npos)
7110 RealizedPlatform = RealizedPlatform.slice(0, suffix);
7111 }
7112
7113 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
7114
7115 // Match the platform name.
7116 if (RealizedPlatform == TargetPlatform)
7117 return Avail;
7118 }
7119 }
7120 return nullptr;
7121}
7122
Erik Pilkington9f866a72017-07-18 20:32:07 +00007123/// The diagnostic we should emit for \c D, and the declaration that
7124/// originated it, or \c AR_Available.
7125///
7126/// \param D The declaration to check.
7127/// \param Message If non-null, this will be populated with the message from
7128/// the availability attribute that is selected.
Erik Pilkington42578572018-09-10 22:20:09 +00007129/// \param ClassReceiver If we're checking the the method of a class message
7130/// send, the class. Otherwise nullptr.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007131static std::pair<AvailabilityResult, const NamedDecl *>
Erik Pilkington42578572018-09-10 22:20:09 +00007132ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D,
7133 std::string *Message,
7134 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007135 AvailabilityResult Result = D->getAvailability(Message);
7136
7137 // For typedefs, if the typedef declaration appears available look
7138 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007139 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007140 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007141 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007142 D = TT->getDecl();
7143 Result = D->getAvailability(Message);
7144 continue;
7145 }
7146 }
7147 break;
7148 }
7149
7150 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007151 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007152 if (IDecl->getDefinition()) {
7153 D = IDecl->getDefinition();
7154 Result = D->getAvailability(Message);
7155 }
7156 }
7157
7158 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7159 if (Result == AR_Available) {
7160 const DeclContext *DC = ECD->getDeclContext();
7161 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
7162 Result = TheEnumDecl->getAvailability(Message);
7163 D = TheEnumDecl;
7164 }
7165 }
7166
Erik Pilkington42578572018-09-10 22:20:09 +00007167 // For +new, infer availability from -init.
7168 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7169 if (S.NSAPIObj && ClassReceiver) {
7170 ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod(
7171 S.NSAPIObj->getInitSelector());
7172 if (Init && Result == AR_Available && MD->isClassMethod() &&
7173 MD->getSelector() == S.NSAPIObj->getNewSelector() &&
7174 MD->definedInNSObject(S.getASTContext())) {
7175 Result = Init->getAvailability(Message);
7176 D = Init;
7177 }
7178 }
7179 }
7180
Erik Pilkington9f866a72017-07-18 20:32:07 +00007181 return {Result, D};
7182}
7183
7184
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007185/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007186/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
7187/// in a deprecated context, but not the other way around.
7188static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
7189 VersionTuple DeclVersion,
7190 Decl *Ctx) {
7191 assert(K != AR_Available && "Expected an unavailable declaration here!");
7192
7193 // Checks if we should emit the availability diagnostic in the context of C.
7194 auto CheckContext = [&](const Decl *C) {
7195 if (K == AR_NotYetIntroduced) {
7196 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
7197 if (AA->getIntroduced() >= DeclVersion)
7198 return true;
7199 } else if (K == AR_Deprecated)
7200 if (C->isDeprecated())
7201 return true;
7202
7203 if (C->isUnavailable())
7204 return true;
7205 return false;
7206 };
7207
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007208 do {
7209 if (CheckContext(Ctx))
7210 return false;
7211
7212 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00007213 // Unless it is "+load" method.
7214 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
7215 if (MethodD->isClassMethod() &&
7216 MethodD->getSelector().getAsString() == "load")
7217 return true;
7218
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007219 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007220 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
7221 if (CheckContext(Interface))
7222 return false;
7223 }
7224 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007225 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007226 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
7227 if (CheckContext(Interface))
7228 return false;
7229 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
7230
7231 return true;
7232}
7233
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007234static bool
7235shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
7236 const VersionTuple &DeploymentVersion,
7237 const VersionTuple &DeclVersion) {
7238 const auto &Triple = Context.getTargetInfo().getTriple();
7239 VersionTuple ForceAvailabilityFromVersion;
7240 switch (Triple.getOS()) {
7241 case llvm::Triple::IOS:
7242 case llvm::Triple::TvOS:
7243 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7244 break;
7245 case llvm::Triple::WatchOS:
7246 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7247 break;
7248 case llvm::Triple::Darwin:
7249 case llvm::Triple::MacOSX:
7250 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7251 break;
7252 default:
7253 // New targets should always warn about availability.
7254 return Triple.getVendor() == llvm::Triple::Apple;
7255 }
7256 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7257 DeclVersion >= ForceAvailabilityFromVersion;
7258}
7259
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007260static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7261 for (Decl *Ctx = OrigCtx; Ctx;
7262 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7263 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7264 return cast<NamedDecl>(Ctx);
7265 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7266 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7267 return Imp->getClassInterface();
7268 return CD;
7269 }
7270 }
7271
7272 return dyn_cast<NamedDecl>(OrigCtx);
7273}
7274
Alex Lorenz727c21e2017-07-26 13:58:02 +00007275namespace {
7276
7277struct AttributeInsertion {
7278 StringRef Prefix;
7279 SourceLocation Loc;
7280 StringRef Suffix;
7281
7282 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007283 return {" ", D->getEndLoc(), ""};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007284 }
7285 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7286 return {" ", Loc, ""};
7287 }
7288 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007289 return {"", D->getBeginLoc(), "\n"};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007290 }
7291};
7292
7293} // end anonymous namespace
7294
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007295/// Tries to parse a string as ObjC method name.
7296///
7297/// \param Name The string to parse. Expected to originate from availability
7298/// attribute argument.
7299/// \param SlotNames The vector that will be populated with slot names. In case
7300/// of unsuccessful parsing can contain invalid data.
7301/// \returns A number of method parameters if parsing was successful, None
7302/// otherwise.
7303static Optional<unsigned>
7304tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
7305 const LangOptions &LangOpts) {
7306 // Accept replacements starting with - or + as valid ObjC method names.
7307 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
7308 Name = Name.drop_front(1);
7309 if (Name.empty())
7310 return None;
7311 Name.split(SlotNames, ':');
7312 unsigned NumParams;
7313 if (Name.back() == ':') {
7314 // Remove an empty string at the end that doesn't represent any slot.
7315 SlotNames.pop_back();
7316 NumParams = SlotNames.size();
7317 } else {
7318 if (SlotNames.size() != 1)
7319 // Not a valid method name, just a colon-separated string.
7320 return None;
7321 NumParams = 0;
7322 }
7323 // Verify all slot names are valid.
7324 bool AllowDollar = LangOpts.DollarIdents;
7325 for (StringRef S : SlotNames) {
7326 if (S.empty())
7327 continue;
7328 if (!isValidIdentifier(S, AllowDollar))
7329 return None;
7330 }
7331 return NumParams;
7332}
7333
Alex Lorenz727c21e2017-07-26 13:58:02 +00007334/// Returns a source location in which it's appropriate to insert a new
7335/// attribute for the given declaration \D.
7336static Optional<AttributeInsertion>
7337createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7338 const LangOptions &LangOpts) {
7339 if (isa<ObjCPropertyDecl>(D))
7340 return AttributeInsertion::createInsertionAfter(D);
7341 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7342 if (MD->hasBody())
7343 return None;
7344 return AttributeInsertion::createInsertionAfter(D);
7345 }
7346 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7347 SourceLocation Loc =
7348 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7349 if (Loc.isInvalid())
7350 return None;
7351 // Insert after the 'struct'/whatever keyword.
7352 return AttributeInsertion::createInsertionAfter(Loc);
7353 }
7354 return AttributeInsertion::createInsertionBefore(D);
7355}
7356
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007357/// Actually emit an availability diagnostic for a reference to an unavailable
7358/// decl.
7359///
7360/// \param Ctx The context that the reference occurred in
7361/// \param ReferringDecl The exact declaration that was referenced.
7362/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007363/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007364/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7365/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7366/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007367static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007368 Decl *Ctx, const NamedDecl *ReferringDecl,
7369 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007370 StringRef Message,
7371 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007372 const ObjCInterfaceDecl *UnknownObjCClass,
7373 const ObjCPropertyDecl *ObjCProperty,
7374 bool ObjCPropertyAccess) {
7375 // Diagnostics for deprecated or unavailable.
7376 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007377 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007378 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007379
7380 // Matches 'diag::note_property_attribute' options.
7381 unsigned property_note_select;
7382
7383 // Matches diag::note_availability_specified_here.
7384 unsigned available_here_select_kind;
7385
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007386 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007387 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007388 DeclVersion = AA->getIntroduced();
7389
7390 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx))
7391 return;
7392
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007393 SourceLocation Loc = Locs.front();
7394
Erik Pilkington8b352c42017-08-14 19:49:12 +00007395 // The declaration can have multiple availability attributes, we are looking
7396 // at one of them.
7397 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7398 if (A && A->isInherited()) {
7399 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7400 Redecl = Redecl->getPreviousDecl()) {
7401 const AvailabilityAttr *AForRedecl =
7402 getAttrForPlatform(S.Context, Redecl);
7403 if (AForRedecl && !AForRedecl->isInherited()) {
7404 // If D is a declaration with inherited attributes, the note should
7405 // point to the declaration with actual attributes.
7406 NoteLocation = Redecl->getLocation();
7407 break;
7408 }
7409 }
7410 }
7411
Aaron Ballmanfb237522014-10-15 15:37:51 +00007412 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007413 case AR_NotYetIntroduced: {
7414 // We would like to emit the diagnostic even if -Wunguarded-availability is
7415 // not specified for deployment targets >= to iOS 11 or equivalent or
7416 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7417 // later.
7418 const AvailabilityAttr *AA =
7419 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7420 VersionTuple Introduced = AA->getIntroduced();
7421
7422 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7423 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7424 Introduced);
7425 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7426 : diag::warn_unguarded_availability;
7427
7428 S.Diag(Loc, Warning)
7429 << OffendingDecl
7430 << AvailabilityAttr::getPrettyPlatformName(
7431 S.getASTContext().getTargetInfo().getPlatformName())
7432 << Introduced.getAsString();
7433
7434 S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here)
7435 << OffendingDecl << /* partial */ 3;
7436
7437 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007438 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007439 if (TD->getDeclName().isEmpty()) {
7440 S.Diag(TD->getLocation(),
7441 diag::note_decl_unguarded_availability_silence)
7442 << /*Anonymous*/ 1 << TD->getKindName();
7443 return;
7444 }
7445 auto FixitNoteDiag =
7446 S.Diag(Enclosing->getLocation(),
7447 diag::note_decl_unguarded_availability_silence)
7448 << /*Named*/ 0 << Enclosing;
7449 // Don't offer a fixit for declarations with availability attributes.
7450 if (Enclosing->hasAttr<AvailabilityAttr>())
7451 return;
7452 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7453 return;
7454 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7455 Enclosing, S.getSourceManager(), S.getLangOpts());
7456 if (!Insertion)
7457 return;
7458 std::string PlatformName =
7459 AvailabilityAttr::getPlatformNameSourceSpelling(
7460 S.getASTContext().getTargetInfo().getPlatformName())
7461 .lower();
7462 std::string Introduced =
7463 OffendingDecl->getVersionIntroduced().getAsString();
7464 FixitNoteDiag << FixItHint::CreateInsertion(
7465 Insertion->Loc,
7466 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7467 "(" + Introduced + "))" + Insertion->Suffix)
7468 .str());
7469 }
7470 return;
7471 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007472 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007473 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7474 : diag::warn_property_method_deprecated;
7475 diag_message = diag::warn_deprecated_message;
7476 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7477 property_note_select = /* deprecated */ 0;
7478 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007479 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7480 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007481 break;
7482
Erik Pilkington796a3e22016-08-05 22:59:03 +00007483 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007484 diag = !ObjCPropertyAccess ? diag::err_unavailable
7485 : diag::err_property_method_unavailable;
7486 diag_message = diag::err_unavailable_message;
7487 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7488 property_note_select = /* unavailable */ 1;
7489 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007490
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007491 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7492 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007493 // Most of these failures are due to extra restrictions in ARC;
7494 // reflect that in the primary diagnostic when applicable.
7495 auto flagARCError = [&] {
7496 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007497 S.getSourceManager().isInSystemHeader(
7498 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007499 diag = diag::err_unavailable_in_arc;
7500 };
7501
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007502 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007503 case UnavailableAttr::IR_None: break;
7504
7505 case UnavailableAttr::IR_ARCForbiddenType:
7506 flagARCError();
7507 diag_available_here = diag::note_arc_forbidden_type;
7508 break;
7509
7510 case UnavailableAttr::IR_ForbiddenWeak:
7511 if (S.getLangOpts().ObjCWeakRuntime)
7512 diag_available_here = diag::note_arc_weak_disabled;
7513 else
7514 diag_available_here = diag::note_arc_weak_no_runtime;
7515 break;
7516
7517 case UnavailableAttr::IR_ARCForbiddenConversion:
7518 flagARCError();
7519 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7520 break;
7521
7522 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7523 flagARCError();
7524 diag_available_here = diag::note_arc_init_returns_unrelated;
7525 break;
7526
7527 case UnavailableAttr::IR_ARCFieldWithOwnership:
7528 flagARCError();
7529 diag_available_here = diag::note_arc_field_with_ownership;
7530 break;
7531 }
7532 }
John McCallb61e14e2015-10-27 04:54:50 +00007533 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007534 break;
7535
Erik Pilkington796a3e22016-08-05 22:59:03 +00007536 case AR_Available:
7537 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007538 }
7539
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007540 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007541 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007542 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007543 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7544 Replacement = AL->getReplacement();
7545 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7546 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007547
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007548 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00007549 if (!Replacement.empty())
7550 UseRange =
7551 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007552 if (UseRange.isValid()) {
7553 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
7554 Selector Sel = MethodDecl->getSelector();
7555 SmallVector<StringRef, 12> SelectorSlotNames;
7556 Optional<unsigned> NumParams = tryParseObjCMethodName(
7557 Replacement, SelectorSlotNames, S.getLangOpts());
7558 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
7559 assert(SelectorSlotNames.size() == Locs.size());
7560 for (unsigned I = 0; I < Locs.size(); ++I) {
7561 if (!Sel.getNameForSlot(I).empty()) {
7562 CharSourceRange NameRange = CharSourceRange::getCharRange(
7563 Locs[I], S.getLocForEndOfToken(Locs[I]));
7564 FixIts.push_back(FixItHint::CreateReplacement(
7565 NameRange, SelectorSlotNames[I]));
7566 } else
7567 FixIts.push_back(
7568 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
7569 }
7570 } else
7571 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7572 } else
7573 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7574 }
Manman Renc7890fe2016-03-16 18:50:49 +00007575 }
7576
Aaron Ballmanfb237522014-10-15 15:37:51 +00007577 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007578 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007579 if (ObjCProperty)
7580 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7581 << ObjCProperty->getDeclName() << property_note_select;
7582 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007583 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007584 if (ObjCProperty)
7585 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7586 << ObjCProperty->getDeclName() << property_note_select;
7587 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007588 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007589 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7590 }
7591
Erik Pilkington8b352c42017-08-14 19:49:12 +00007592 S.Diag(NoteLocation, diag_available_here)
7593 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007594}
7595
7596static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
7597 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007598 assert(DD.Kind == DelayedDiagnostic::Availability &&
7599 "Expected an availability diagnostic here");
7600
Aaron Ballmanfb237522014-10-15 15:37:51 +00007601 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00007602 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007603 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007604 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
7605 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
7606 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00007607}
7608
John McCall2ec85372012-05-07 06:16:41 +00007609void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7610 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007611 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007612 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007613
John McCall2ec85372012-05-07 06:16:41 +00007614 // When delaying diagnostics to run in the context of a parsed
7615 // declaration, we only want to actually emit anything if parsing
7616 // succeeds.
7617 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007618
John McCall2ec85372012-05-07 06:16:41 +00007619 // We emit all the active diagnostics in this pool or any of its
7620 // parents. In general, we'll get one pool for the decl spec
7621 // and a child pool for each declarator; in a decl group like:
7622 // deprecated_typedef foo, *bar, baz();
7623 // only the declarator pops will be passed decls. This is correct;
7624 // we really do need to consider delayed diagnostics from the decl spec
7625 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007626 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007627 do {
Richard Smith5c9b3b72018-09-25 22:12:44 +00007628 bool AnyAccessFailures = false;
John McCall6347b682012-05-07 06:16:58 +00007629 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007630 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7631 // This const_cast is a bit lame. Really, Triggered should be mutable.
7632 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007633 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007634 continue;
7635
John McCallc1465822011-02-14 07:13:47 +00007636 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007637 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007638 // Don't bother giving deprecation/unavailable diagnostics if
7639 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007640 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00007641 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00007642 break;
7643
7644 case DelayedDiagnostic::Access:
Richard Smith5c9b3b72018-09-25 22:12:44 +00007645 // Only produce one access control diagnostic for a structured binding
7646 // declaration: we don't need to tell the user that all the fields are
7647 // inaccessible one at a time.
7648 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
7649 continue;
John McCall2ec85372012-05-07 06:16:41 +00007650 HandleDelayedAccessCheck(diag, decl);
Richard Smith5c9b3b72018-09-25 22:12:44 +00007651 if (diag.Triggered)
7652 AnyAccessFailures = true;
John McCall86121512010-01-27 03:50:35 +00007653 break;
John McCall31168b02011-06-15 23:02:42 +00007654
7655 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007656 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007657 break;
John McCall86121512010-01-27 03:50:35 +00007658 }
7659 }
John McCall2ec85372012-05-07 06:16:41 +00007660 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007661}
7662
John McCall6347b682012-05-07 06:16:58 +00007663/// Given a set of delayed diagnostics, re-emit them as if they had
7664/// been delayed in the current context instead of in the given pool.
7665/// Essentially, this just moves them to the current pool.
7666void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7667 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7668 assert(curPool && "re-emitting in undelayed context not supported");
7669 curPool->steal(pool);
7670}
7671
Erik Pilkington9f866a72017-07-18 20:32:07 +00007672static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
7673 const NamedDecl *ReferringDecl,
7674 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007675 StringRef Message,
7676 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007677 const ObjCInterfaceDecl *UnknownObjCClass,
7678 const ObjCPropertyDecl *ObjCProperty,
7679 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00007680 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007681 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
7682 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007683 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007684 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007685 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00007686 return;
7687 }
7688
Erik Pilkington9f866a72017-07-18 20:32:07 +00007689 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
7690 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007691 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007692 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00007693}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00007694
Erik Pilkington5cd57172016-08-16 17:44:11 +00007695namespace {
7696
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007697/// Returns true if the given statement can be a body-like child of \p Parent.
7698bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
7699 switch (Parent->getStmtClass()) {
7700 case Stmt::IfStmtClass:
7701 return cast<IfStmt>(Parent)->getThen() == S ||
7702 cast<IfStmt>(Parent)->getElse() == S;
7703 case Stmt::WhileStmtClass:
7704 return cast<WhileStmt>(Parent)->getBody() == S;
7705 case Stmt::DoStmtClass:
7706 return cast<DoStmt>(Parent)->getBody() == S;
7707 case Stmt::ForStmtClass:
7708 return cast<ForStmt>(Parent)->getBody() == S;
7709 case Stmt::CXXForRangeStmtClass:
7710 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
7711 case Stmt::ObjCForCollectionStmtClass:
7712 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
7713 case Stmt::CaseStmtClass:
7714 case Stmt::DefaultStmtClass:
7715 return cast<SwitchCase>(Parent)->getSubStmt() == S;
7716 default:
7717 return false;
7718 }
7719}
7720
7721class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
7722 const Stmt *Target;
7723
7724public:
7725 bool VisitStmt(Stmt *S) { return S != Target; }
7726
7727 /// Returns true if the given statement is present in the given declaration.
7728 static bool isContained(const Stmt *Target, const Decl *D) {
7729 StmtUSEFinder Visitor;
7730 Visitor.Target = Target;
7731 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
7732 }
7733};
7734
7735/// Traverses the AST and finds the last statement that used a given
7736/// declaration.
7737class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
7738 const Decl *D;
7739
7740public:
7741 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7742 if (DRE->getDecl() == D)
7743 return false;
7744 return true;
7745 }
7746
7747 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
7748 const CompoundStmt *Scope) {
7749 LastDeclUSEFinder Visitor;
7750 Visitor.D = D;
7751 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
7752 const Stmt *S = *I;
7753 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
7754 return S;
7755 }
7756 return nullptr;
7757 }
7758};
7759
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007760/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00007761///
7762/// This is done with a traversal of the AST of a function that makes reference
7763/// to a partially available declaration. Whenever we encounter an \c if of the
7764/// form: \c if(@available(...)), we use the version from the condition to visit
7765/// the then statement.
7766class DiagnoseUnguardedAvailability
7767 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
7768 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
7769
7770 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007771 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007772
7773 /// Stack of potentially nested 'if (@available(...))'s.
7774 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007775 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007776
Erik Pilkington42578572018-09-10 22:20:09 +00007777 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range,
7778 ObjCInterfaceDecl *ClassReceiver = nullptr);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007779
7780public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007781 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
7782 : SemaRef(SemaRef), Ctx(Ctx) {
7783 AvailabilityStack.push_back(
7784 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007785 }
7786
Alex Lorenz6f911122017-05-16 13:58:53 +00007787 bool TraverseDecl(Decl *D) {
7788 // Avoid visiting nested functions to prevent duplicate warnings.
7789 if (!D || isa<FunctionDecl>(D))
7790 return true;
7791 return Base::TraverseDecl(D);
7792 }
7793
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007794 bool TraverseStmt(Stmt *S) {
7795 if (!S)
7796 return true;
7797 StmtStack.push_back(S);
7798 bool Result = Base::TraverseStmt(S);
7799 StmtStack.pop_back();
7800 return Result;
7801 }
7802
Erik Pilkington5cd57172016-08-16 17:44:11 +00007803 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
7804
7805 bool TraverseIfStmt(IfStmt *If);
7806
Alex Lorenz6f911122017-05-16 13:58:53 +00007807 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
7808
Erik Pilkingtonba87c622017-08-18 20:20:56 +00007809 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
7810 // to any useful diagnostics.
7811 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
7812
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007813 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
7814 if (PRE->isClassReceiver())
7815 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
7816 return true;
7817 }
7818
Erik Pilkington5cd57172016-08-16 17:44:11 +00007819 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
Erik Pilkington42578572018-09-10 22:20:09 +00007820 if (ObjCMethodDecl *D = Msg->getMethodDecl()) {
7821 ObjCInterfaceDecl *ID = nullptr;
7822 QualType ReceiverTy = Msg->getClassReceiver();
7823 if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType())
7824 ID = ReceiverTy->getAsObjCInterfaceType()->getInterface();
7825
Erik Pilkington5cd57172016-08-16 17:44:11 +00007826 DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00007827 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID);
7828 }
Erik Pilkington5cd57172016-08-16 17:44:11 +00007829 return true;
7830 }
7831
7832 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7833 DiagnoseDeclAvailability(DRE->getDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007834 SourceRange(DRE->getBeginLoc(), DRE->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00007835 return true;
7836 }
7837
7838 bool VisitMemberExpr(MemberExpr *ME) {
7839 DiagnoseDeclAvailability(ME->getMemberDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007840 SourceRange(ME->getBeginLoc(), ME->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00007841 return true;
7842 }
7843
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007844 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007845 SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use)
Erik Pilkingtonfa983902018-10-30 20:31:30 +00007846 << (!SemaRef.getLangOpts().ObjC);
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007847 return true;
7848 }
7849
Erik Pilkington5cd57172016-08-16 17:44:11 +00007850 bool VisitTypeLoc(TypeLoc Ty);
7851};
7852
7853void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00007854 NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007855 AvailabilityResult Result;
7856 const NamedDecl *OffendingDecl;
7857 std::tie(Result, OffendingDecl) =
Erik Pilkington42578572018-09-10 22:20:09 +00007858 ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007859 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007860 // All other diagnostic kinds have already been handled in
7861 // DiagnoseAvailabilityOfDecl.
7862 if (Result != AR_NotYetIntroduced)
7863 return;
7864
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007865 const AvailabilityAttr *AA =
7866 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007867 VersionTuple Introduced = AA->getIntroduced();
7868
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007869 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007870 return;
7871
7872 // If the context of this function is less available than D, we should not
7873 // emit a diagnostic.
7874 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx))
7875 return;
7876
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007877 // We would like to emit the diagnostic even if -Wunguarded-availability is
7878 // not specified for deployment targets >= to iOS 11 or equivalent or
7879 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7880 // later.
7881 unsigned DiagKind =
7882 shouldDiagnoseAvailabilityByDefault(
7883 SemaRef.Context,
7884 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
7885 ? diag::warn_unguarded_availability_new
7886 : diag::warn_unguarded_availability;
7887
7888 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington5cd57172016-08-16 17:44:11 +00007889 << Range << D
7890 << AvailabilityAttr::getPrettyPlatformName(
7891 SemaRef.getASTContext().getTargetInfo().getPlatformName())
7892 << Introduced.getAsString();
7893
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007894 SemaRef.Diag(OffendingDecl->getLocation(),
7895 diag::note_availability_specified_here)
7896 << OffendingDecl << /* partial */ 3;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007897
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007898 auto FixitDiag =
7899 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
7900 << Range << D
Erik Pilkingtonfa983902018-10-30 20:31:30 +00007901 << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0
7902 : /*__builtin_available*/ 1);
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007903
7904 // Find the statement which should be enclosed in the if @available check.
7905 if (StmtStack.empty())
7906 return;
7907 const Stmt *StmtOfUse = StmtStack.back();
7908 const CompoundStmt *Scope = nullptr;
7909 for (const Stmt *S : llvm::reverse(StmtStack)) {
7910 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
7911 Scope = CS;
7912 break;
7913 }
7914 if (isBodyLikeChildStmt(StmtOfUse, S)) {
7915 // The declaration won't be seen outside of the statement, so we don't
7916 // have to wrap the uses of any declared variables in if (@available).
7917 // Therefore we can avoid setting Scope here.
7918 break;
7919 }
7920 StmtOfUse = S;
7921 }
7922 const Stmt *LastStmtOfUse = nullptr;
7923 if (isa<DeclStmt>(StmtOfUse) && Scope) {
7924 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
7925 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
7926 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
7927 break;
7928 }
7929 }
7930 }
7931
7932 const SourceManager &SM = SemaRef.getSourceManager();
7933 SourceLocation IfInsertionLoc =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007934 SM.getExpansionLoc(StmtOfUse->getBeginLoc());
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007935 SourceLocation StmtEndLoc =
7936 SM.getExpansionRange(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007937 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc())
Richard Smithb5f81712018-04-30 05:25:48 +00007938 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007939 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
7940 return;
7941
7942 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
7943 const char *ExtraIndentation = " ";
7944 std::string FixItString;
7945 llvm::raw_string_ostream FixItOS(FixItString);
Erik Pilkingtonfa983902018-10-30 20:31:30 +00007946 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available"
7947 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00007948 << "("
7949 << AvailabilityAttr::getPlatformNameSourceSpelling(
7950 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007951 << " " << Introduced.getAsString() << ", *)) {\n"
7952 << Indentation << ExtraIndentation;
7953 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
7954 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
7955 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
7956 /*SkipTrailingWhitespaceAndNewLine=*/false);
7957 if (ElseInsertionLoc.isInvalid())
7958 ElseInsertionLoc =
7959 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
7960 FixItOS.str().clear();
7961 FixItOS << "\n"
7962 << Indentation << "} else {\n"
7963 << Indentation << ExtraIndentation
7964 << "// Fallback on earlier versions\n"
7965 << Indentation << "}";
7966 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007967 }
7968}
7969
7970bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
7971 const Type *TyPtr = Ty.getTypePtr();
7972 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
7973
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007974 if (Range.isInvalid())
7975 return true;
7976
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007977 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007978 TagDecl *TD = TT->getDecl();
7979 DiagnoseDeclAvailability(TD, Range);
7980
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007981 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007982 TypedefNameDecl *D = TD->getDecl();
7983 DiagnoseDeclAvailability(D, Range);
7984
7985 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
7986 if (NamedDecl *D = ObjCO->getInterface())
7987 DiagnoseDeclAvailability(D, Range);
7988 }
7989
7990 return true;
7991}
7992
7993bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
7994 VersionTuple CondVersion;
7995 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
7996 CondVersion = E->getVersion();
7997
7998 // If we're using the '*' case here or if this check is redundant, then we
7999 // use the enclosing version to check both branches.
8000 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00008001 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008002 } else {
8003 // This isn't an availability checking 'if', we can just continue.
8004 return Base::TraverseIfStmt(If);
8005 }
8006
8007 AvailabilityStack.push_back(CondVersion);
8008 bool ShouldContinue = TraverseStmt(If->getThen());
8009 AvailabilityStack.pop_back();
8010
8011 return ShouldContinue && TraverseStmt(If->getElse());
8012}
8013
8014} // end anonymous namespace
8015
8016void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
8017 Stmt *Body = nullptr;
8018
8019 if (auto *FD = D->getAsFunction()) {
8020 // FIXME: We only examine the pattern decl for availability violations now,
8021 // but we should also examine instantiated templates.
8022 if (FD->isTemplateInstantiation())
8023 return;
8024
8025 Body = FD->getBody();
8026 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
8027 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00008028 else if (auto *BD = dyn_cast<BlockDecl>(D))
8029 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008030
8031 assert(Body && "Need a body here!");
8032
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008033 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008034}
Erik Pilkington9f866a72017-07-18 20:32:07 +00008035
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008036void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
8037 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008038 const ObjCInterfaceDecl *UnknownObjCClass,
8039 bool ObjCPropertyAccess,
Erik Pilkington42578572018-09-10 22:20:09 +00008040 bool AvoidPartialAvailabilityChecks,
8041 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008042 std::string Message;
8043 AvailabilityResult Result;
8044 const NamedDecl* OffendingDecl;
8045 // See if this declaration is unavailable, deprecated, or partial.
Erik Pilkington42578572018-09-10 22:20:09 +00008046 std::tie(Result, OffendingDecl) =
8047 ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver);
Erik Pilkington9f866a72017-07-18 20:32:07 +00008048 if (Result == AR_Available)
8049 return;
8050
8051 if (Result == AR_NotYetIntroduced) {
8052 if (AvoidPartialAvailabilityChecks)
8053 return;
8054
8055 // We need to know the @available context in the current function to
8056 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
8057 // when we're done parsing the current function.
8058 if (getCurFunctionOrMethodDecl()) {
8059 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
8060 return;
8061 } else if (getCurBlock() || getCurLambda()) {
8062 getCurFunction()->HasPotentialAvailabilityViolations = true;
8063 return;
8064 }
8065 }
8066
8067 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008068 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008069 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
8070 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
8071 if (PDeclResult == Result)
8072 ObjCPDecl = PD;
8073 }
8074 }
8075
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008076 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008077 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
8078}