blob: 864c930136e4f3ba8a270d24774211f1c58ba376 [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
Erik Pilkington1e368822019-01-04 18:33:06 +000096static const ParmVarDecl *getFunctionOrMethodParam(const Decl *D,
97 unsigned Idx) {
98 if (const auto *FD = dyn_cast<FunctionDecl>(D))
99 return FD->getParamDecl(Idx);
100 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
101 return MD->getParamDecl(Idx);
102 if (const auto *BD = dyn_cast<BlockDecl>(D))
103 return BD->getParamDecl(Idx);
104 return nullptr;
105}
106
Alp Toker601b22c2014-01-21 23:35:24 +0000107static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000108 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +0000109 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000110 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000111 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000112
Alp Toker03376dc2014-07-07 09:02:20 +0000113 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000114}
115
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000116static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
Erik Pilkington1e368822019-01-04 18:33:06 +0000117 if (auto *PVD = getFunctionOrMethodParam(D, Idx))
118 return PVD->getSourceRange();
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000119 return SourceRange();
120}
121
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000123 if (const FunctionType *FnTy = D->getFunctionType())
George Burgess IV00f70bd2018-03-01 05:43:23 +0000124 return FnTy->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000125 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000126}
127
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000128static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
129 if (const auto *FD = dyn_cast<FunctionDecl>(D))
130 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000131 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000132 return MD->getReturnTypeSourceRange();
133 return SourceRange();
134}
135
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000137 if (const FunctionType *FnTy = D->getFunctionType())
138 return cast<FunctionProtoType>(FnTy)->isVariadic();
139 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballmane13d0092014-08-01 17:02:34 +0000140 return BD->isVariadic();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000141 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000142}
143
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144static bool isInstanceMethod(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000145 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000146 return MethodDecl->isInstance();
147 return false;
148}
149
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000150static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000151 const auto *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000152 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000153 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000154
John McCall96fa4842010-05-17 21:00:27 +0000155 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
156 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000157 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000158
John McCall96fa4842010-05-17 21:00:27 +0000159 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000160
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000161 // FIXME: Should we walk the chain of classes?
162 return ClsName == &Ctx.Idents.get("NSString") ||
163 ClsName == &Ctx.Idents.get("NSMutableString");
164}
165
Daniel Dunbar980c6692008-09-26 03:32:58 +0000166static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000167 const auto *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000168 if (!PT)
169 return false;
170
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000171 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000172 if (!RT)
173 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000174
Daniel Dunbar980c6692008-09-26 03:32:58 +0000175 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000176 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000177 return false;
178
179 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
180}
181
Erich Keanee891aa92018-07-13 15:07:47 +0000182static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
Richard Smithb87c4652013-10-31 21:23:20 +0000183 // FIXME: Include the type in the argument list.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000184 return AL.getNumArgs() + AL.hasParsedType();
Richard Smithb87c4652013-10-31 21:23:20 +0000185}
186
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000187template <typename Compare>
Erich Keanee891aa92018-07-13 15:07:47 +0000188static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000189 unsigned Num, unsigned Diag,
190 Compare Comp) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000191 if (Comp(getNumAttributeArgs(AL), Num)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000192 S.Diag(AL.getLoc(), Diag) << AL << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000193 return false;
194 }
195
196 return true;
197}
198
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000199/// Check if the attribute has exactly as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000200/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000201static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000202 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000203 diag::err_attribute_wrong_number_arguments,
204 std::not_equal_to<unsigned>());
205}
206
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000207/// Check if the attribute has at least as many args as Num. May
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000208/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000209static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL,
Richard Smithb87c4652013-10-31 21:23:20 +0000210 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000211 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000212 diag::err_attribute_too_few_arguments,
213 std::less<unsigned>());
214}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000215
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000216/// Check if the attribute has at most as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000217/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000218static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL,
219 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000220 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000221 diag::err_attribute_too_many_arguments,
222 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000223}
224
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000225/// A helper function to provide Attribute Location for the Attr types
Erich Keanee891aa92018-07-13 15:07:47 +0000226/// AND the ParsedAttr.
Erich Keane623efd82017-03-30 21:48:55 +0000227template <typename AttrInfo>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000228static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value,
Erich Keane623efd82017-03-30 21:48:55 +0000229 SourceLocation>::type
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000230getAttrLoc(const AttrInfo &AL) {
231 return AL.getLocation();
Erich Keane623efd82017-03-30 21:48:55 +0000232}
Erich Keanee891aa92018-07-13 15:07:47 +0000233static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); }
Erich Keane623efd82017-03-30 21:48:55 +0000234
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000235/// If Expr is a valid integer constant, get the value of the integer
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000236/// expression and return success or failure. May output an error.
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000237///
238/// Negative argument is implicitly converted to unsigned, unless
239/// \p StrictlyUnsigned is true.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000240template <typename AttrInfo>
241static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000242 uint32_t &Val, unsigned Idx = UINT_MAX,
243 bool StrictlyUnsigned = false) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000244 llvm::APSInt I(32);
245 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
246 !Expr->isIntegerConstantExpr(I, S.Context)) {
247 if (Idx != UINT_MAX)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000248 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000249 << AI << Idx << AANT_ArgumentIntegerConstant
250 << Expr->getSourceRange();
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000251 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000252 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000253 << AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange();
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000254 return false;
255 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000256
257 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000258 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
259 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000260 return false;
261 }
262
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000263 if (StrictlyUnsigned && I.isSigned() && I.isNegative()) {
Andrew Savonichev1a5623482018-09-17 10:39:46 +0000264 S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
265 << AI << /*non-negative*/ 1;
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000266 return false;
267 }
268
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000269 Val = (uint32_t)I.getZExtValue();
270 return true;
271}
272
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000273/// Wrapper around checkUInt32Argument, with an extra check to be sure
George Burgess IVe3763372016-12-22 02:50:20 +0000274/// that the result will fit into a regular (signed) int. All args have the same
275/// purpose as they do in checkUInt32Argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000276template <typename AttrInfo>
277static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000278 int &Val, unsigned Idx = UINT_MAX) {
George Burgess IVe3763372016-12-22 02:50:20 +0000279 uint32_t UVal;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000280 if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
George Burgess IVe3763372016-12-22 02:50:20 +0000281 return false;
282
George Burgess IVa8049572016-12-22 19:00:31 +0000283 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
George Burgess IVe3763372016-12-22 02:50:20 +0000284 llvm::APSInt I(32); // for toString
285 I = UVal;
286 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
287 << I.toString(10, false) << 32 << /* Unsigned */ 0;
288 return false;
289 }
290
291 Val = UVal;
292 return true;
293}
294
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000295/// Diagnose mutually exclusive attributes when present on a given
Aaron Ballmanfb763042013-12-02 18:05:46 +0000296/// declaration. Returns true if diagnosed.
297template <typename AttrTy>
Erich Keane44bacdf2018-08-09 13:21:32 +0000298static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000299 if (const auto *A = D->getAttr<AttrTy>()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000300 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
301 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
302 return true;
303 }
304 return false;
305}
306
307template <typename AttrTy>
308static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
309 if (const auto *A = D->getAttr<AttrTy>()) {
310 S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000311 << A;
312 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000313 return true;
314 }
315 return false;
316}
317
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000318/// Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000319/// instance method D. May output an error.
320///
321/// \returns true if IdxExpr is a valid index.
Erich Keane623efd82017-03-30 21:48:55 +0000322template <typename AttrInfo>
323static bool checkFunctionOrMethodParameterIndex(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000324 Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
Joel E. Denny81508102018-03-13 14:51:22 +0000325 const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
David Majnemer06864812015-04-07 06:01:53 +0000326 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000327
328 // In C++ the implicit 'this' function parameter also counts.
329 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000330 bool HP = hasFunctionProto(D);
331 bool HasImplicitThisParam = isInstanceMethod(D);
332 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000333 unsigned NumParams =
334 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000335
336 llvm::APSInt IdxInt;
337 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
338 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000339 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000340 << &AI << AttrArgNum << AANT_ArgumentIntegerConstant
341 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000342 return false;
343 }
344
Joel E. Denny81508102018-03-13 14:51:22 +0000345 unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX);
346 if (IdxSource < 1 || (!IV && IdxSource > NumParams)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000347 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +0000348 << &AI << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000349 return false;
350 }
Joel E. Denny81508102018-03-13 14:51:22 +0000351 if (HasImplicitThisParam && !CanIndexImplicitThis) {
352 if (IdxSource == 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000353 S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
354 << &AI << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000355 return false;
356 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000357 }
358
Joel E. Denny81508102018-03-13 14:51:22 +0000359 Idx = ParamIdx(IdxSource, D);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000360 return true;
361}
362
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000363/// Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000364/// If not emit an error and return false. If the argument is an identifier it
365/// will emit an error with a fixit hint and treat it as if it was a string
366/// literal.
Erich Keanee891aa92018-07-13 15:07:47 +0000367bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
368 StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000369 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000370 // Look for identifiers. If we have one emit a hint to fix it to a literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000371 if (AL.isArgIdent(ArgNum)) {
372 IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000373 Diag(Loc->Loc, diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000374 << AL << AANT_ArgumentString
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000375 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000376 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000377 Str = Loc->Ident->getName();
378 if (ArgLocation)
379 *ArgLocation = Loc->Loc;
380 return true;
381 }
382
383 // Now check for an actual string literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000384 Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
385 const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000386 if (ArgLocation)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000387 *ArgLocation = ArgExpr->getBeginLoc();
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000388
389 if (!Literal || !Literal->isAscii()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000390 Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000391 << AL << AANT_ArgumentString;
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000392 return false;
393 }
394
395 Str = Literal->getString();
396 return true;
397}
398
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000399/// Applies the given attribute to the Decl without performing any
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000400/// additional semantic checking.
401template <typename AttrType>
George Karpenkov1657f362018-11-30 02:18:37 +0000402static void handleSimpleAttribute(Sema &S, Decl *D, SourceRange SR,
403 unsigned SpellingIndex) {
404 D->addAttr(::new (S.Context) AttrType(SR, S.Context, SpellingIndex));
405}
406
407template <typename AttrType>
Erich Keanee891aa92018-07-13 15:07:47 +0000408static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) {
George Karpenkov1657f362018-11-30 02:18:37 +0000409 handleSimpleAttribute<AttrType>(S, D, AL.getRange(),
410 AL.getAttributeSpellingListIndex());
411}
412
413
414template <typename... DiagnosticArgs>
415static const Sema::SemaDiagnosticBuilder&
416appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr) {
417 return Bldr;
418}
419
420template <typename T, typename... DiagnosticArgs>
421static const Sema::SemaDiagnosticBuilder&
422appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr, T &&ExtraArg,
423 DiagnosticArgs &&... ExtraArgs) {
424 return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
425 std::forward<DiagnosticArgs>(ExtraArgs)...);
426}
427
428/// Add an attribute {@code AttrType} to declaration {@code D},
429/// provided the given {@code Check} function returns {@code true}
430/// on type of {@code D}.
431/// If check does not pass, emit diagnostic {@code DiagID},
432/// passing in all parameters specified in {@code ExtraArgs}.
433template <typename AttrType, typename... DiagnosticArgs>
434static void
435handleSimpleAttributeWithCheck(Sema &S, ValueDecl *D, SourceRange SR,
436 unsigned SpellingIndex,
437 llvm::function_ref<bool(QualType)> Check,
438 unsigned DiagID, DiagnosticArgs... ExtraArgs) {
439 if (!Check(D->getType())) {
440 Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
441 appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
442 return;
443 }
444 handleSimpleAttribute<AttrType>(S, D, SR, SpellingIndex);
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000445}
446
Justin Lebar3eaaf862016-01-13 01:07:35 +0000447template <typename AttrType>
448static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000449 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000450 handleSimpleAttribute<AttrType>(S, D, AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000451}
452
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000453/// Applies the given attribute to the Decl so long as the Decl doesn't
Justin Lebar3eaaf862016-01-13 01:07:35 +0000454/// already have one of the given incompatible attributes.
455template <typename AttrType, typename IncompatibleAttrType,
456 typename... IncompatibleAttrTypes>
457static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000458 const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000459 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
Justin Lebar3eaaf862016-01-13 01:07:35 +0000460 return;
461 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000462 AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000463}
464
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000465/// Check if the passed-in expression is of type int or bool.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000466static bool isIntOrBool(Expr *Exp) {
467 QualType QT = Exp->getType();
468 return QT->isBooleanType() || QT->isIntegerType();
469}
470
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000471
472// Check to see if the type is a smart pointer of some kind. We assume
473// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000474static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Trieu8d3fa392018-09-22 01:50:52 +0000475 auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record,
476 OverloadedOperatorKind Op) {
477 DeclContextLookupResult Result =
478 Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op));
479 return !Result.empty();
480 };
481
482 const RecordDecl *Record = RT->getDecl();
483 bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
484 bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
485 if (foundStarOperator && foundArrowOperator)
486 return true;
487
488 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
489 if (!CXXRecord)
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000490 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000491
Richard Trieu8d3fa392018-09-22 01:50:52 +0000492 for (auto BaseSpecifier : CXXRecord->bases()) {
493 if (!foundStarOperator)
494 foundStarOperator = IsOverloadedOperatorPresent(
495 BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
496 if (!foundArrowOperator)
497 foundArrowOperator = IsOverloadedOperatorPresent(
498 BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
499 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000500
Richard Trieu8d3fa392018-09-22 01:50:52 +0000501 if (foundStarOperator && foundArrowOperator)
502 return true;
503
504 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000505}
506
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000507/// Check if passed in Decl is a pointer type.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000508/// Note that this function may produce an error message.
509/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000510static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000511 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000512 const auto *VD = cast<ValueDecl>(D);
513 QualType QT = VD->getType();
Aaron Ballman553e6812013-12-26 14:54:11 +0000514 if (QT->isAnyPointerType())
515 return true;
516
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000517 if (const auto *RT = QT->getAs<RecordType>()) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000518 // If it's an incomplete type, it could be a smart pointer; skip it.
519 // (We don't want to force template instantiation if we can avoid it,
520 // since that would alter the order in which templates are instantiated.)
521 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000522 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000523
Aaron Ballman553e6812013-12-26 14:54:11 +0000524 if (threadSafetyCheckIsSmartPointer(S, RT))
525 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000526 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000527
Erich Keane44bacdf2018-08-09 13:21:32 +0000528 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000529 return false;
530}
531
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000532/// Checks that the passed in QualType either is of RecordType or points
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000533/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000534static const RecordType *getRecordType(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000535 if (const auto *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000536 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000537
538 // Now check if we point to record type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000539 if (const auto *PT = QT->getAs<PointerType>())
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000540 return PT->getPointeeType()->getAs<RecordType>();
541
Craig Topperc3ec1492014-05-26 06:22:03 +0000542 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000543}
544
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000545template <typename AttrType>
546static bool checkRecordDeclForAttr(const RecordDecl *RD) {
547 // Check if the record itself has the attribute.
548 if (RD->hasAttr<AttrType>())
549 return true;
550
551 // Else check if any base classes have the attribute.
552 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
553 CXXBasePaths BPaths(false, false);
554 if (CRD->lookupInBases(
555 [](const CXXBaseSpecifier *BS, CXXBasePath &) {
556 const auto &Ty = *BS->getType();
557 // If it's type-dependent, we assume it could have the attribute.
558 if (Ty.isDependentType())
559 return true;
560 return Ty.getAs<RecordType>()->getDecl()->hasAttr<AttrType>();
561 },
562 BPaths, true))
563 return true;
564 }
565 return false;
566}
567
Josh Gao55afa752017-08-11 07:54:35 +0000568static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000569 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000570
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000571 if (!RT)
572 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000573
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000574 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000575 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000576 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000577
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000578 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000579 // FIXME -- Check the type that the smart pointer points to.
580 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000581 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000582
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000583 return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl());
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000584}
585
Josh Gao55afa752017-08-11 07:54:35 +0000586static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000587 const auto *TD = Ty->getAs<TypedefType>();
588 if (!TD)
589 return false;
590
591 TypedefNameDecl *TN = TD->getDecl();
592 if (!TN)
593 return false;
594
Josh Gao55afa752017-08-11 07:54:35 +0000595 return TN->hasAttr<CapabilityAttr>();
Aaron Ballman76050722014-04-04 15:13:57 +0000596}
597
Josh Gaob40c1772017-08-08 19:44:35 +0000598static bool typeHasCapability(Sema &S, QualType Ty) {
Josh Gao55afa752017-08-11 07:54:35 +0000599 if (checkTypedefTypeForCapability(Ty))
600 return true;
Josh Gaob40c1772017-08-08 19:44:35 +0000601
Josh Gao55afa752017-08-11 07:54:35 +0000602 if (checkRecordTypeForCapability(S, Ty))
603 return true;
604
605 return false;
Josh Gaob40c1772017-08-08 19:44:35 +0000606}
607
Aaron Ballman76050722014-04-04 15:13:57 +0000608static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
609 // Capability expressions are simple expressions involving the boolean logic
610 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
611 // a DeclRefExpr is found, its type should be checked to determine whether it
612 // is a capability or not.
613
Yi Kong2d58d192017-12-14 22:24:45 +0000614 if (const auto *E = dyn_cast<CastExpr>(Ex))
Aaron Ballman76050722014-04-04 15:13:57 +0000615 return isCapabilityExpr(S, E->getSubExpr());
616 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
617 return isCapabilityExpr(S, E->getSubExpr());
618 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
Yi Kong2d58d192017-12-14 22:24:45 +0000619 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
620 E->getOpcode() == UO_Deref)
Aaron Ballman76050722014-04-04 15:13:57 +0000621 return isCapabilityExpr(S, E->getSubExpr());
622 return false;
623 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
624 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
625 return isCapabilityExpr(S, E->getLHS()) &&
626 isCapabilityExpr(S, E->getRHS());
627 return false;
628 }
629
Yi Kong2d58d192017-12-14 22:24:45 +0000630 return typeHasCapability(S, Ex->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000631}
632
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000633/// Checks that all attribute arguments, starting from Sidx, resolve to
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000634/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000635/// \param Sidx The attribute argument index to start checking with.
636/// \param ParamIdxOk Whether an argument can be indexing into a function
637/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000638static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000639 const ParsedAttr &AL,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000640 SmallVectorImpl<Expr *> &Args,
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000641 unsigned Sidx = 0,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000642 bool ParamIdxOk = false) {
Aaron Puchert7ba1ab72018-09-20 00:39:27 +0000643 if (Sidx == AL.getNumArgs()) {
644 // If we don't have any capability arguments, the attribute implicitly
645 // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
646 // a non-static method, and that the class is a (scoped) capability.
647 const auto *MD = dyn_cast<const CXXMethodDecl>(D);
648 if (MD && !MD->isStatic()) {
649 const CXXRecordDecl *RD = MD->getParent();
650 // FIXME -- need to check this again on template instantiation
651 if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
652 !checkRecordDeclForAttr<ScopedLockableAttr>(RD))
653 S.Diag(AL.getLoc(),
654 diag::warn_thread_attribute_not_on_capability_member)
655 << AL << MD->getParent();
656 } else {
657 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
658 << AL;
659 }
660 }
661
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000662 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
663 Expr *ArgExp = AL.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000664
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000665 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000666 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000667 Args.push_back(ArgExp);
668 continue;
669 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000670
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000671 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000672 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000673 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000674 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000675 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000676 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000677 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000678 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000679
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000680 // We allow constant strings to be used as a placeholder for expressions
681 // that are not valid C++ syntax, but warn that they are ignored.
Erich Keane44bacdf2018-08-09 13:21:32 +0000682 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000683 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000684 continue;
685 }
686
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000687 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000688
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000689 // A pointer to member expression of the form &MyClass::mu is treated
690 // specially -- we need to look at the type of the member.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000691 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000692 if (UOp->getOpcode() == UO_AddrOf)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000693 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000694 if (DRE->getDecl()->isCXXInstanceMember())
695 ArgTy = DRE->getDecl()->getType();
696
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000697 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000698 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000699
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000700 // Now check if we index into a record type function param.
Josh Gao55afa752017-08-11 07:54:35 +0000701 if(!RT && ParamIdxOk) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000702 const auto *FD = dyn_cast<FunctionDecl>(D);
703 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
Josh Gao55afa752017-08-11 07:54:35 +0000704 if(FD && IL) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000705 unsigned int NumParams = FD->getNumParams();
706 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000707 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
708 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000709 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
710 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +0000711 << AL << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000712 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000713 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000714 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000715 }
716 }
717
Aaron Ballman76050722014-04-04 15:13:57 +0000718 // If the type does not have a capability, see if the components of the
719 // expression have capabilities. This allows for writing C code where the
720 // capability may be on the type, and the expression is a capability
721 // boolean logic expression. Eg) requires_capability(A || B && !C)
722 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000723 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Erich Keane44bacdf2018-08-09 13:21:32 +0000724 << AL << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000725
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000726 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000727 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000728}
729
Chris Lattner58418ff2008-06-29 00:16:31 +0000730//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000731// Attribute Implementations
732//===----------------------------------------------------------------------===//
733
Erich Keanee891aa92018-07-13 15:07:47 +0000734static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000735 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000736 return;
737
Michael Han99315932013-01-24 16:46:58 +0000738 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000739 PtGuardedVarAttr(AL.getRange(), S.Context,
740 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000741}
742
Erich Keanee891aa92018-07-13 15:07:47 +0000743static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000744 Expr *&Arg) {
745 SmallVector<Expr *, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000746 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000747 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000748 unsigned Size = Args.size();
749 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000750 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000751
Michael Han3be3b442012-07-23 18:48:41 +0000752 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000753
Michael Han3be3b442012-07-23 18:48:41 +0000754 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000755}
756
Erich Keanee891aa92018-07-13 15:07:47 +0000757static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000758 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000759 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000760 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000761
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000762 D->addAttr(::new (S.Context) GuardedByAttr(
763 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000764}
765
Erich Keanee891aa92018-07-13 15:07:47 +0000766static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000767 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000768 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000769 return;
770
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000771 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000772 return;
773
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000774 D->addAttr(::new (S.Context) PtGuardedByAttr(
775 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000776}
777
Erich Keanee891aa92018-07-13 15:07:47 +0000778static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000779 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000780 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000781 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000782
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000783 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000784 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000785 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000786 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000787 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000788 }
789
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000790 // Check that all arguments are lockable objects.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000791 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000792 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000793 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000794
Michael Han3be3b442012-07-23 18:48:41 +0000795 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000796}
797
Erich Keanee891aa92018-07-13 15:07:47 +0000798static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000799 SmallVector<Expr *, 1> Args;
800 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000801 return;
802
803 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000804 D->addAttr(::new (S.Context) AcquiredAfterAttr(
805 AL.getRange(), S.Context, StartArg, Args.size(),
806 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000807}
808
Erich Keanee891aa92018-07-13 15:07:47 +0000809static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000810 SmallVector<Expr *, 1> Args;
811 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000812 return;
813
814 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000815 D->addAttr(::new (S.Context) AcquiredBeforeAttr(
816 AL.getRange(), S.Context, StartArg, Args.size(),
817 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000818}
819
Erich Keanee891aa92018-07-13 15:07:47 +0000820static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000821 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000822 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000823 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000824 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000825
Michael Han3be3b442012-07-23 18:48:41 +0000826 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000827}
828
Erich Keanee891aa92018-07-13 15:07:47 +0000829static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000830 SmallVector<Expr *, 1> Args;
831 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000832 return;
833
834 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000835 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000836 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000837 AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size,
838 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000839}
840
841static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000842 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000843 SmallVector<Expr *, 1> Args;
844 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000845 return;
846
847 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000848 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000849 D->addAttr(::new (S.Context) AssertExclusiveLockAttr(
850 AL.getRange(), S.Context, StartArg, Size,
851 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000852}
853
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000854/// Checks to be sure that the given parameter number is in bounds, and
Aaron Ballman836684a2018-02-25 20:40:06 +0000855/// is an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000856/// false.
857///
Aaron Ballman836684a2018-02-25 20:40:06 +0000858/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000859template <typename AttrInfo>
860static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Joel E. Denny81508102018-03-13 14:51:22 +0000861 const AttrInfo &AI, unsigned AttrArgNo) {
Aaron Ballman836684a2018-02-25 20:40:06 +0000862 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
863 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
Joel E. Denny81508102018-03-13 14:51:22 +0000864 ParamIdx Idx;
Aaron Ballman836684a2018-02-25 20:40:06 +0000865 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000866 Idx))
867 return false;
868
Joel E. Denny81508102018-03-13 14:51:22 +0000869 const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +0000870 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000871 SourceLocation SrcLoc = AttrArg->getBeginLoc();
Erich Keane623efd82017-03-30 21:48:55 +0000872 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +0000873 << AI << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000874 return false;
875 }
876 return true;
877}
878
Erich Keanee891aa92018-07-13 15:07:47 +0000879static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000880 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
881 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000882 return;
883
884 const auto *FD = cast<FunctionDecl>(D);
885 if (!FD->getReturnType()->isPointerType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000886 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
George Burgess IVe3763372016-12-22 02:50:20 +0000887 return;
888 }
889
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000890 const Expr *SizeExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +0000891 int SizeArgNoVal;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000892 // Parameter indices are 1-indexed, hence Index=1
Joel E. Denny81508102018-03-13 14:51:22 +0000893 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Index=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000894 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000895 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0))
George Burgess IVe3763372016-12-22 02:50:20 +0000896 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000897 ParamIdx SizeArgNo(SizeArgNoVal, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000898
Joel E. Denny81508102018-03-13 14:51:22 +0000899 ParamIdx NumberArgNo;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000900 if (AL.getNumArgs() == 2) {
901 const Expr *NumberExpr = AL.getArgAsExpr(1);
Joel E. Denny81508102018-03-13 14:51:22 +0000902 int Val;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000903 // Parameter indices are 1-based, hence Index=2
Joel E. Denny81508102018-03-13 14:51:22 +0000904 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Index=*/2))
George Burgess IVe3763372016-12-22 02:50:20 +0000905 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000906 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000907 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000908 NumberArgNo = ParamIdx(Val, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000909 }
910
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000911 D->addAttr(::new (S.Context)
912 AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo,
913 AL.getAttributeSpellingListIndex()));
George Burgess IVe3763372016-12-22 02:50:20 +0000914}
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000915
Erich Keanee891aa92018-07-13 15:07:47 +0000916static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000917 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000918 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000919 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000920
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000921 if (!isIntOrBool(AL.getArgAsExpr(0))) {
922 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000923 << AL << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000924 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000925 }
926
927 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000928 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000929
Michael Han3be3b442012-07-23 18:48:41 +0000930 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000931}
932
Michael Hana9171bc2012-08-03 17:40:43 +0000933static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000934 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000935 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000936 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000937 return;
938
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000939 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
940 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(),
941 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000942}
943
Michael Hana9171bc2012-08-03 17:40:43 +0000944static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000945 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000946 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000947 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000948 return;
949
Nico Weber462fd1e2015-01-07 23:50:05 +0000950 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000951 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(),
952 Args.size(), AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000953}
954
Erich Keanee891aa92018-07-13 15:07:47 +0000955static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000956 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000957 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000958 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000959 unsigned Size = Args.size();
960 if (Size == 0)
961 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000962
Michael Han99315932013-01-24 16:46:58 +0000963 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000964 LockReturnedAttr(AL.getRange(), S.Context, Args[0],
965 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000966}
967
Erich Keanee891aa92018-07-13 15:07:47 +0000968static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000969 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000970 return;
971
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000972 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000973 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000974 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000975 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000976 if (Size == 0)
977 return;
978 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000979
Michael Han99315932013-01-24 16:46:58 +0000980 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000981 LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size,
982 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000983}
984
Erich Keanee891aa92018-07-13 15:07:47 +0000985static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
George Burgess IV177399e2017-01-09 04:12:14 +0000986 Expr *&Cond, StringRef &Msg) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000987 Cond = AL.getArgAsExpr(0);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000988 if (!Cond->isTypeDependent()) {
989 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
990 if (Converted.isInvalid())
George Burgess IV177399e2017-01-09 04:12:14 +0000991 return false;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000992 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000993 }
994
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000995 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000996 return false;
997
998 if (Msg.empty())
999 Msg = "<no message provided>";
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001000
1001 SmallVector<PartialDiagnosticAt, 8> Diags;
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001002 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001003 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
1004 Diags)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001005 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
George Burgess IV0d546532016-11-10 21:47:12 +00001006 for (const PartialDiagnosticAt &PDiag : Diags)
1007 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +00001008 return false;
1009 }
1010 return true;
1011}
1012
Erich Keanee891aa92018-07-13 15:07:47 +00001013static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001014 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001015
1016 Expr *Cond;
1017 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001018 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001019 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001020 EnableIfAttr(AL.getRange(), S.Context, Cond, Msg,
1021 AL.getAttributeSpellingListIndex()));
George Burgess IV177399e2017-01-09 04:12:14 +00001022}
1023
1024namespace {
1025/// Determines if a given Expr references any of the given function's
1026/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
1027class ArgumentDependenceChecker
1028 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
1029#ifndef NDEBUG
1030 const CXXRecordDecl *ClassType;
1031#endif
1032 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
1033 bool Result;
1034
1035public:
1036 ArgumentDependenceChecker(const FunctionDecl *FD) {
1037#ifndef NDEBUG
1038 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1039 ClassType = MD->getParent();
1040 else
1041 ClassType = nullptr;
1042#endif
1043 Parms.insert(FD->param_begin(), FD->param_end());
1044 }
1045
1046 bool referencesArgs(Expr *E) {
1047 Result = false;
1048 TraverseStmt(E);
1049 return Result;
1050 }
1051
1052 bool VisitCXXThisExpr(CXXThisExpr *E) {
1053 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
1054 "`this` doesn't refer to the enclosing class?");
1055 Result = true;
1056 return false;
1057 }
1058
1059 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1060 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1061 if (Parms.count(PVD)) {
1062 Result = true;
1063 return false;
1064 }
1065 return true;
1066 }
1067};
1068}
1069
Erich Keanee891aa92018-07-13 15:07:47 +00001070static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001071 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001072
1073 Expr *Cond;
1074 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001075 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001076 return;
1077
1078 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001079 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +00001080 return;
1081
1082 DiagnoseIfAttr::DiagnosticType DiagType;
1083 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001084 S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
George Burgess IV177399e2017-01-09 04:12:14 +00001085 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001086 return;
1087 }
1088
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001089 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +00001090 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001091 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +00001092 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001093 AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent,
1094 cast<NamedDecl>(D), AL.getAttributeSpellingListIndex()));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001095}
1096
Erich Keanee891aa92018-07-13 15:07:47 +00001097static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001098 if (D->hasAttr<PassObjectSizeAttr>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001099 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001100 return;
1101 }
1102
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001103 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001104 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001105 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001106 return;
1107
1108 // pass_object_size's argument is passed in as the second argument of
1109 // __builtin_object_size. So, it has the same constraints as that second
1110 // argument; namely, it must be in the range [0, 3].
1111 if (Type > 3) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001112 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00001113 << AL << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001114 return;
1115 }
1116
1117 // pass_object_size is only supported on constant pointer parameters; as a
1118 // kindness to users, we allow the parameter to be non-const for declarations.
1119 // At this point, we have no clue if `D` belongs to a function declaration or
1120 // definition, so we defer the constness check until later.
1121 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001122 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001123 return;
1124 }
1125
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001126 D->addAttr(::new (S.Context) PassObjectSizeAttr(
1127 AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex()));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001128}
1129
Erich Keanee891aa92018-07-13 15:07:47 +00001130static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001131 ConsumableAttr::ConsumedState DefaultState;
1132
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001133 if (AL.isArgIdent(0)) {
1134 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001135 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1136 DefaultState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001137 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1138 << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001139 return;
1140 }
David Blaikie16f76d22013-09-06 01:28:43 +00001141 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001142 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001143 << AL << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001144 return;
1145 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001146
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001147 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001148 ConsumableAttr(AL.getRange(), S.Context, DefaultState,
1149 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001150}
1151
1152static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Erich Keanee891aa92018-07-13 15:07:47 +00001153 const ParsedAttr &AL) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001154 ASTContext &CurrContext = S.getASTContext();
1155 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001156
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001157 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1158 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001159 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001160 RD->getNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001161
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001162 return false;
1163 }
1164 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001165
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001166 return true;
1167}
1168
Erich Keanee891aa92018-07-13 15:07:47 +00001169static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001170 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001171 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001172
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001173 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001174 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001175
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001176 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001177 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001178 CallableWhenAttr::ConsumedState CallableState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001179
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001180 StringRef StateString;
1181 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001182 if (AL.isArgIdent(ArgIndex)) {
1183 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001184 StateString = Ident->Ident->getName();
1185 Loc = Ident->Loc;
1186 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001187 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001188 return;
1189 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001190
1191 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001192 CallableState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001193 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001194 return;
1195 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001196
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001197 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001198 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001199
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001200 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001201 CallableWhenAttr(AL.getRange(), S.Context, States.data(),
1202 States.size(), AL.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001203}
1204
Erich Keanee891aa92018-07-13 15:07:47 +00001205static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001206 ParamTypestateAttr::ConsumedState ParamState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001207
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001208 if (AL.isArgIdent(0)) {
1209 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001210 StringRef StateString = Ident->Ident->getName();
1211
1212 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1213 ParamState)) {
1214 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00001215 << AL << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001216 return;
1217 }
1218 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001219 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1220 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001221 return;
1222 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001223
DeLesley Hutchins69391772013-10-17 23:23:53 +00001224 // FIXME: This check is currently being done in the analysis. It can be
1225 // enabled here only after the parser propagates attributes at
1226 // template specialization definition, not declaration.
1227 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1228 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1229 //
1230 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001231 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001232 // ReturnType.getAsString();
1233 // return;
1234 //}
Fangrui Song6907ce22018-07-30 19:24:48 +00001235
DeLesley Hutchins69391772013-10-17 23:23:53 +00001236 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001237 ParamTypestateAttr(AL.getRange(), S.Context, ParamState,
1238 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001239}
1240
Erich Keanee891aa92018-07-13 15:07:47 +00001241static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001242 ReturnTypestateAttr::ConsumedState ReturnState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001243
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001244 if (AL.isArgIdent(0)) {
1245 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001246 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1247 ReturnState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001248 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1249 << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001250 return;
1251 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001252 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001253 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1254 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001255 return;
1256 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001257
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001258 // FIXME: This check is currently being done in the analysis. It can be
1259 // enabled here only after the parser propagates attributes at
1260 // template specialization definition, not declaration.
1261 //QualType ReturnType;
1262 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001263 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1264 // ReturnType = Param->getType();
1265 //
1266 //} else if (const CXXConstructorDecl *Constructor =
1267 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001268 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001269 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001270 //} else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001271 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001272 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1273 //}
1274 //
1275 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1276 //
1277 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1278 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1279 // ReturnType.getAsString();
1280 // return;
1281 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001282
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001283 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001284 ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState,
1285 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001286}
1287
Erich Keanee891aa92018-07-13 15:07:47 +00001288static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001289 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001290 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001291
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001292 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001293 if (AL.isArgIdent(0)) {
1294 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001295 StringRef Param = Ident->Ident->getName();
1296 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001297 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1298 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001299 return;
1300 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001301 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001302 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1303 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001304 return;
1305 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001306
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001307 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001308 SetTypestateAttr(AL.getRange(), S.Context, NewState,
1309 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001310}
1311
Erich Keanee891aa92018-07-13 15:07:47 +00001312static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001313 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001314 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001315
1316 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001317 if (AL.isArgIdent(0)) {
1318 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001319 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001320 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001321 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1322 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001323 return;
1324 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001325 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001326 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1327 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001328 return;
1329 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001330
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001331 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001332 TestTypestateAttr(AL.getRange(), S.Context, TestState,
1333 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001334}
1335
Erich Keanee891aa92018-07-13 15:07:47 +00001336static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001337 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001338 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001339}
1340
Erich Keanee891aa92018-07-13 15:07:47 +00001341static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001342 if (auto *TD = dyn_cast<TagDecl>(D))
1343 TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context,
1344 AL.getAttributeSpellingListIndex()));
1345 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001346 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1347 !FD->getType()->isIncompleteType() &&
1348 FD->isBitField() &&
1349 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001350
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001351 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1352 if (BitfieldByteAligned)
1353 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001354 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001355 << AL << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001356 else
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 } else {
1360 // Report warning about changed offset in the newer compiler versions.
1361 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001362 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001363
1364 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001365 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001366 }
1367
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001368 } else
Erich Keane44bacdf2018-08-09 13:21:32 +00001369 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001370}
1371
Erich Keanee891aa92018-07-13 15:07:47 +00001372static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001373 // The IBOutlet/IBOutletCollection attributes only apply to instance
1374 // variables or properties of Objective-C classes. The outlet must also
1375 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001376 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001377 if (!VD->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 << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001380 return false;
1381 }
1382 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001383 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001384 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001385 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001386 << AL << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001387 return false;
1388 }
1389 }
1390 else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001391 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001392 return false;
1393 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001394
Ted Kremenek7fd17232011-09-29 07:02:25 +00001395 return true;
1396}
1397
Erich Keanee891aa92018-07-13 15:07:47 +00001398static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001399 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001400 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001401
Michael Han99315932013-01-24 16:46:58 +00001402 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001403 IBOutletAttr(AL.getRange(), S.Context,
1404 AL.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001405}
1406
Erich Keanee891aa92018-07-13 15:07:47 +00001407static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001408
1409 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001410 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001411 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001412 return;
1413 }
1414
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001415 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001416 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001417
Richard Smithb1f9a282013-10-31 01:56:18 +00001418 ParsedType PT;
1419
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001420 if (AL.hasParsedType())
1421 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001422 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001423 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001424 S.getScopeForContext(D->getDeclContext()->getParent()));
1425 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001426 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001427 return;
1428 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001429 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001430
Craig Topperc3ec1492014-05-26 06:22:03 +00001431 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001432 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1433 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001434 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001435
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001436 // Diagnose use of non-object type in iboutletcollection attribute.
1437 // FIXME. Gnu attribute extension ignores use of builtin types in
1438 // attributes. So, __attribute__((iboutletcollection(char))) will be
1439 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001440 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001441 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001442 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1443 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001444 return;
1445 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001446
Michael Han99315932013-01-24 16:46:58 +00001447 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001448 IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc,
1449 AL.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001450}
1451
Hal Finkelee90a222014-09-26 05:04:30 +00001452bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1453 if (RefOkay) {
1454 if (T->isReferenceType())
1455 return true;
1456 } else {
1457 T = T.getNonReferenceType();
1458 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001459
Hal Finkelee90a222014-09-26 05:04:30 +00001460 // The nonnull attribute, and other similar attributes, can be applied to a
1461 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001462 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001463 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1464 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001465 for (const auto *I : UD->fields()) {
1466 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001467 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1468 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001469 }
1470 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001471 }
1472
1473 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001474}
1475
Erich Keanee891aa92018-07-13 15:07:47 +00001476static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001477 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001478 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001479 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001480 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001481 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001482 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001483 << AL << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001484 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001485 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001486 << AL << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001487 return false;
1488 }
1489 return true;
1490}
1491
Erich Keanee891aa92018-07-13 15:07:47 +00001492static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001493 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001494 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1495 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001496 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001497 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001498 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001499
1500 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001501 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1502 !attrNonNullArgCheck(
1503 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1504 Ex->getSourceRange(),
1505 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001506 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001507
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001508 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001509 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001510
1511 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001512 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1513 // check if the attribute came from a macro expansion or a template
1514 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001515 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001516 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001517 bool AnyPointers = isFunctionOrMethodVariadic(D);
1518 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1519 I != E && !AnyPointers; ++I) {
1520 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001521 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001522 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001523 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001524
Richard Smith588bd9b2014-08-27 04:59:42 +00001525 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001526 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001527 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001528
Joel E. Denny81508102018-03-13 14:51:22 +00001529 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001530 unsigned Size = NonNullArgs.size();
1531 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001532 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001533 NonNullAttr(AL.getRange(), S.Context, Start, Size,
1534 AL.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001535}
1536
Jordan Rosec9399072014-02-11 17:27:59 +00001537static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00001538 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001539 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001540 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001541 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001542 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001543 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001544 << D->getSourceRange();
1545 }
1546 return;
1547 }
1548
1549 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001550 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001551 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001552 return;
1553
1554 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001555 NonNullAttr(AL.getRange(), S.Context, nullptr, 0,
1556 AL.getAttributeSpellingListIndex()));
Jordan Rosec9399072014-02-11 17:27:59 +00001557}
1558
Erich Keanee891aa92018-07-13 15:07:47 +00001559static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001560 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001561 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001562 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001563 /* isReturnValue */ true))
1564 return;
1565
1566 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001567 ReturnsNonNullAttr(AL.getRange(), S.Context,
1568 AL.getAttributeSpellingListIndex()));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001569}
1570
Erich Keanee891aa92018-07-13 15:07:47 +00001571static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001572 if (D->isInvalidDecl())
1573 return;
1574
1575 // noescape only applies to pointer types.
1576 QualType T = cast<ParmVarDecl>(D)->getType();
1577 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001578 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001579 << AL << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001580 return;
1581 }
1582
1583 D->addAttr(::new (S.Context) NoEscapeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001584 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001585}
1586
Erich Keanee891aa92018-07-13 15:07:47 +00001587static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001588 Expr *E = AL.getArgAsExpr(0),
1589 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1590 S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE,
1591 AL.getAttributeSpellingListIndex());
Hal Finkelee90a222014-09-26 05:04:30 +00001592}
1593
Erich Keanee891aa92018-07-13 15:07:47 +00001594static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001595 S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0),
1596 AL.getAttributeSpellingListIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001597}
1598
Hal Finkelee90a222014-09-26 05:04:30 +00001599void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1600 Expr *OE, unsigned SpellingListIndex) {
1601 QualType ResultType = getFunctionOrMethodResultType(D);
1602 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1603
1604 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1605 SourceLocation AttrLoc = AttrRange.getBegin();
1606
1607 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1608 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1609 << &TmpAttr << AttrRange << SR;
1610 return;
1611 }
1612
1613 if (!E->isValueDependent()) {
1614 llvm::APSInt I(64);
1615 if (!E->isIntegerConstantExpr(I, Context)) {
1616 if (OE)
1617 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1618 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1619 << E->getSourceRange();
1620 else
1621 Diag(AttrLoc, diag::err_attribute_argument_type)
1622 << &TmpAttr << AANT_ArgumentIntegerConstant
1623 << E->getSourceRange();
1624 return;
1625 }
1626
1627 if (!I.isPowerOf2()) {
1628 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1629 << E->getSourceRange();
1630 return;
1631 }
1632 }
1633
1634 if (OE) {
1635 if (!OE->isValueDependent()) {
1636 llvm::APSInt I(64);
1637 if (!OE->isIntegerConstantExpr(I, Context)) {
1638 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1639 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1640 << OE->getSourceRange();
1641 return;
1642 }
1643 }
1644 }
1645
1646 D->addAttr(::new (Context)
1647 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1648}
1649
Erich Keane623efd82017-03-30 21:48:55 +00001650void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr,
1651 unsigned SpellingListIndex) {
1652 QualType ResultType = getFunctionOrMethodResultType(D);
1653
Joel E. Denny81508102018-03-13 14:51:22 +00001654 AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex);
Erich Keane623efd82017-03-30 21:48:55 +00001655 SourceLocation AttrLoc = AttrRange.getBegin();
1656
1657 if (!ResultType->isDependentType() &&
1658 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1659 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1660 << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D);
1661 return;
1662 }
1663
Joel E. Denny81508102018-03-13 14:51:22 +00001664 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001665 const auto *FuncDecl = cast<FunctionDecl>(D);
1666 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Joel E. Denny81508102018-03-13 14:51:22 +00001667 /*AttrArgNo=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001668 return;
1669
Joel E. Denny81508102018-03-13 14:51:22 +00001670 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001671 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001672 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001673 << &TmpAttr
1674 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001675 return;
1676 }
1677
Joel E. Denny81508102018-03-13 14:51:22 +00001678 D->addAttr(::new (Context)
1679 AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex));
Erich Keane623efd82017-03-30 21:48:55 +00001680}
1681
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001682/// Normalize the attribute, __foo__ becomes foo.
1683/// Returns true if normalization was applied.
1684static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001685 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1686 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001687 AttrName = AttrName.drop_front(2).drop_back(2);
1688 return true;
1689 }
1690 return false;
1691}
1692
Erich Keanee891aa92018-07-13 15:07:47 +00001693static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001694 // This attribute must be applied to a function declaration. The first
1695 // argument to the attribute must be an identifier, the name of the resource,
1696 // for example: malloc. The following arguments must be argument indexes, the
1697 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001698 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001699 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001700 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001701
Aaron Ballman00e99962013-08-31 01:11:41 +00001702 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001703 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001704 << AL << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001705 return;
1706 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001707
Richard Smith852e9ce2013-11-27 01:46:48 +00001708 // Figure out our Kind.
1709 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001710 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001711 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001712
Richard Smith852e9ce2013-11-27 01:46:48 +00001713 // Check arguments.
1714 switch (K) {
1715 case OwnershipAttr::Takes:
1716 case OwnershipAttr::Holds:
1717 if (AL.getNumArgs() < 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001718 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001719 return;
1720 }
1721 break;
1722 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001723 if (AL.getNumArgs() > 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001724 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001725 return;
1726 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001727 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001728 }
1729
Richard Smith852e9ce2013-11-27 01:46:48 +00001730 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001731
Richard Smith852e9ce2013-11-27 01:46:48 +00001732 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001733 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001734 Module = &S.PP.getIdentifierTable().get(ModuleName);
1735 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001736
Joel E. Denny81508102018-03-13 14:51:22 +00001737 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001738 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1739 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001740 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001741 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001742 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001743
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001744 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001745 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001746 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001747 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001748 case OwnershipAttr::Takes:
1749 case OwnershipAttr::Holds:
1750 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1751 Err = 0;
1752 break;
1753 case OwnershipAttr::Returns:
1754 if (!T->isIntegerType())
1755 Err = 1;
1756 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001757 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001758 if (-1 != Err) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001759 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1760 << Ex->getSourceRange();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001761 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001762 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001763
1764 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001765 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001766 // Cannot have two ownership attributes of different kinds for the same
1767 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001768 if (I->getOwnKind() != K && I->args_end() !=
1769 std::find(I->args_begin(), I->args_end(), Idx)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001770 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001771 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001772 } else if (K == OwnershipAttr::Returns &&
1773 I->getOwnKind() == OwnershipAttr::Returns) {
1774 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001775 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001776 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1777 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001778 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001779 if (I->args_size())
1780 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001781 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001782 return;
1783 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001784 }
1785 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001786 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001787 }
1788
Joel E. Denny81508102018-03-13 14:51:22 +00001789 ParamIdx *Start = OwnershipArgs.data();
1790 unsigned Size = OwnershipArgs.size();
1791 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001792 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001793 OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size,
1794 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001795}
1796
Erich Keanee891aa92018-07-13 15:07:47 +00001797static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001798 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001799 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001800 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001801 return;
1802 }
1803
1804 // gcc rejects
1805 // class c {
1806 // static int a __attribute__((weakref ("v2")));
1807 // static int b() __attribute__((weakref ("f3")));
1808 // };
1809 // and ignores the attributes of
1810 // void f(void) {
1811 // static int a __attribute__((weakref ("v2")));
1812 // }
1813 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001814 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001815 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001816 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1817 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001818 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001819 }
1820
1821 // The GCC manual says
1822 //
1823 // At present, a declaration to which `weakref' is attached can only
1824 // be `static'.
1825 //
1826 // It also says
1827 //
1828 // Without a TARGET,
1829 // given as an argument to `weakref' or to `alias', `weakref' is
1830 // equivalent to `weak'.
1831 //
1832 // gcc 4.4.1 will accept
1833 // int a7 __attribute__((weakref));
1834 // as
1835 // int a7 __attribute__((weak));
1836 // This looks like a bug in gcc. We reject that for now. We should revisit
1837 // it if this behaviour is actually used.
1838
Rafael Espindolac18086a2010-02-23 22:00:30 +00001839 // GCC rejects
1840 // static ((alias ("y"), weakref)).
1841 // Should we? How to check that weakref is before or after alias?
1842
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001843 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1844 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1845 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001846 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001847 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001848 // GCC will accept anything as the argument of weakref. Should we
1849 // check for an existing decl?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001850 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1851 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001852
Michael Han99315932013-01-24 16:46:58 +00001853 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001854 WeakRefAttr(AL.getRange(), S.Context,
1855 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001856}
1857
Erich Keanee891aa92018-07-13 15:07:47 +00001858static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001859 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001860 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001861 return;
1862
1863 // Aliases should be on declarations, not definitions.
1864 const auto *FD = cast<FunctionDecl>(D);
1865 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001866 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001867 return;
1868 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001869
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001870 D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str,
1871 AL.getAttributeSpellingListIndex()));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001872}
1873
Erich Keanee891aa92018-07-13 15:07:47 +00001874static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001875 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001876 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001877 return;
1878
Douglas Gregore8bbc122011-09-02 00:18:52 +00001879 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001880 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001881 return;
1882 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001883 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001884 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001885 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001886
David Majnemer2dc81462015-01-19 09:00:28 +00001887 // Aliases should be on declarations, not definitions.
1888 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1889 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001890 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001891 return;
1892 }
1893 } else {
1894 const auto *VD = cast<VarDecl>(D);
1895 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001896 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001897 return;
1898 }
1899 }
1900
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001901 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001902
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001903 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1904 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001905}
1906
Erich Keanee891aa92018-07-13 15:07:47 +00001907static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001908 StringRef Model;
1909 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001910 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001911 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001912 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001913
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001914 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001915 if (Model != "global-dynamic" && Model != "local-dynamic"
1916 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001917 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001918 return;
1919 }
1920
Michael Han99315932013-01-24 16:46:58 +00001921 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001922 TLSModelAttr(AL.getRange(), S.Context, Model,
1923 AL.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001924}
1925
Erich Keanee891aa92018-07-13 15:07:47 +00001926static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001927 QualType ResultType = getFunctionOrMethodResultType(D);
1928 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1929 D->addAttr(::new (S.Context) RestrictAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001930 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
David Majnemer631a90b2015-02-04 07:23:21 +00001931 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001932 }
1933
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001934 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001935 << AL << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001936}
1937
Erich Keane3efe0022018-07-20 14:13:28 +00001938static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1939 FunctionDecl *FD = cast<FunctionDecl>(D);
Erich Keane659c8712018-09-10 14:31:56 +00001940
1941 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1942 if (MD->getParent()->isLambda()) {
1943 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
1944 return;
1945 }
1946 }
1947
Erich Keane3efe0022018-07-20 14:13:28 +00001948 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1949 return;
1950
1951 SmallVector<IdentifierInfo *, 8> CPUs;
1952 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
1953 if (!AL.isArgIdent(ArgNo)) {
1954 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001955 << AL << AANT_ArgumentIdentifier;
Erich Keane3efe0022018-07-20 14:13:28 +00001956 return;
1957 }
1958
1959 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1960 StringRef CPUName = CPUArg->Ident->getName().trim();
1961
1962 if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1963 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1964 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1965 return;
1966 }
1967
1968 const TargetInfo &Target = S.Context.getTargetInfo();
1969 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1970 return Target.CPUSpecificManglingCharacter(CPUName) ==
1971 Target.CPUSpecificManglingCharacter(Cur->getName());
1972 })) {
1973 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1974 return;
1975 }
1976 CPUs.push_back(CPUArg->Ident);
1977 }
1978
1979 FD->setIsMultiVersion(true);
1980 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
1981 D->addAttr(::new (S.Context) CPUSpecificAttr(
1982 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
1983 AL.getAttributeSpellingListIndex()));
1984 else
1985 D->addAttr(::new (S.Context) CPUDispatchAttr(
1986 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
1987 AL.getAttributeSpellingListIndex()));
1988}
1989
Erich Keanee891aa92018-07-13 15:07:47 +00001990static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001991 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001992 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00001993 << AL << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001994 return;
1995 }
1996
Erich Keane44bacdf2018-08-09 13:21:32 +00001997 if (CommonAttr *CA = S.mergeCommonAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001998 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001999}
2000
Erich Keanee891aa92018-07-13 15:07:47 +00002001static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002002 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
Charles Davis0e379112016-08-08 21:19:08 +00002003 return;
2004
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002005 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002006 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
2007 const auto &Arch = Triple.getArch();
2008 if (Arch != llvm::Triple::x86 &&
2009 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002010 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
Erich Keane44bacdf2018-08-09 13:21:32 +00002011 << AL << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00002012 return;
2013 }
2014 }
2015
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002016 D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context,
2017 AL.getAttributeSpellingListIndex()));
Charles Davis0e379112016-08-08 21:19:08 +00002018}
2019
Erich Keanee891aa92018-07-13 15:07:47 +00002020static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002021 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00002022
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002023 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002024 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002025 << Attrs << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002026 return;
2027 }
2028
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002029 D->addAttr(::new (S.Context) NoReturnAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00002030 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002031}
2032
Erich Keanee891aa92018-07-13 15:07:47 +00002033static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00002034 if (!S.getLangOpts().CFProtectionBranch)
2035 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
2036 else
2037 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00002038}
2039
Erich Keanee891aa92018-07-13 15:07:47 +00002040bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00002041 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
2042 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002043 return true;
2044 }
2045
2046 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002047}
2048
Erich Keanee891aa92018-07-13 15:07:47 +00002049bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002050 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002051 if (!AL.existsInTarget(Context.getTargetInfo())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002052 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002053 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002054 return true;
2055 }
2056
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00002057 return false;
2058}
2059
Erich Keanee891aa92018-07-13 15:07:47 +00002060static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2061
Ted Kremenek5295ce82010-08-19 00:51:58 +00002062 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2063 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00002064 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002065 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00002066 if (!VD || (!VD->getType()->isBlockPointerType() &&
2067 !VD->getType()->isFunctionPointerType())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002068 S.Diag(AL.getLoc(), AL.isCXX11Attribute()
2069 ? diag::err_attribute_wrong_decl_type
2070 : diag::warn_attribute_wrong_decl_type)
2071 << AL << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00002072 return;
2073 }
2074 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002075
Michael Han99315932013-01-24 16:46:58 +00002076 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002077 AnalyzerNoReturnAttr(AL.getRange(), S.Context,
2078 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002079}
2080
John Thompsoncdb847ba2010-08-09 21:53:52 +00002081// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00002082static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2083 /*
2084 Returning a Vector Class in Registers
2085
2086 According to the PPU ABI specifications, a class with a single member of
2087 vector type is returned in memory when used as the return value of a
2088 function.
2089 This results in inefficient code when implementing vector classes. To return
2090 the value in a single vector register, add the vecreturn attribute to the
2091 class definition. This attribute is also applicable to struct types.
2092
2093 Example:
2094
2095 struct Vector
2096 {
2097 __vector float xyzw;
2098 } __attribute__((vecreturn));
2099
2100 Vector Add(Vector lhs, Vector rhs)
2101 {
2102 Vector result;
2103 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2104 return result; // This will be returned in a register
2105 }
2106 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002107 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002108 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002109 return;
2110 }
2111
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002112 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002113 int count = 0;
2114
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002115 if (!isa<CXXRecordDecl>(R)) {
2116 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002117 return;
2118 }
2119
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002120 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2121 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002122 return;
2123 }
2124
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002125 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002126 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002127 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002128 return;
2129 }
2130 count++;
2131 }
2132
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002133 D->addAttr(::new (S.Context) VecReturnAttr(
2134 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002135}
2136
Richard Smithe233fbf2013-01-28 22:42:45 +00002137static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002138 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002139 if (isa<ParmVarDecl>(D)) {
2140 // [[carries_dependency]] can only be applied to a parameter if it is a
2141 // parameter of a function declaration or lambda.
2142 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002143 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002144 diag::err_carries_dependency_param_not_function_decl);
2145 return;
2146 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002147 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002148
2149 D->addAttr(::new (S.Context) CarriesDependencyAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002150 AL.getRange(), S.Context,
2151 AL.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002152}
2153
Erich Keanee891aa92018-07-13 15:07:47 +00002154static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002155 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002156
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002157 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002158 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002159 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Erich Keane44bacdf2018-08-09 13:21:32 +00002160 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002161
2162 D->addAttr(::new (S.Context) UnusedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002163 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002164}
2165
Erich Keanee891aa92018-07-13 15:07:47 +00002166static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002167 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002168 if (AL.getNumArgs() &&
2169 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002170 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002171
Michael Han99315932013-01-24 16:46:58 +00002172 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002173 ConstructorAttr(AL.getRange(), S.Context, priority,
2174 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002175}
2176
Erich Keanee891aa92018-07-13 15:07:47 +00002177static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002178 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002179 if (AL.getNumArgs() &&
2180 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002181 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002182
Michael Han99315932013-01-24 16:46:58 +00002183 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002184 DestructorAttr(AL.getRange(), S.Context, priority,
2185 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002186}
2187
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002188template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002189static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002190 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002191 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002192 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002193 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002194
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002195 D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str,
2196 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002197}
2198
Ted Kremenek438f8db2014-02-22 01:06:05 +00002199static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002200 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002201 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002202 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
Erich Keane44bacdf2018-08-09 13:21:32 +00002203 << AL << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002204 return;
2205 }
2206
Ted Kremenek28eace62013-11-23 01:01:34 +00002207 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002208 ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context,
2209 AL.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00002210}
2211
Jordy Rose740b0c22012-05-08 03:27:22 +00002212static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2213 IdentifierInfo *Platform,
2214 VersionTuple Introduced,
2215 VersionTuple Deprecated,
2216 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002217 StringRef PlatformName
2218 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2219 if (PlatformName.empty())
2220 PlatformName = Platform->getName();
2221
2222 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2223 // of these steps are needed).
2224 if (!Introduced.empty() && !Deprecated.empty() &&
2225 !(Introduced <= Deprecated)) {
2226 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2227 << 1 << PlatformName << Deprecated.getAsString()
2228 << 0 << Introduced.getAsString();
2229 return true;
2230 }
2231
2232 if (!Introduced.empty() && !Obsoleted.empty() &&
2233 !(Introduced <= Obsoleted)) {
2234 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2235 << 2 << PlatformName << Obsoleted.getAsString()
2236 << 0 << Introduced.getAsString();
2237 return true;
2238 }
2239
2240 if (!Deprecated.empty() && !Obsoleted.empty() &&
2241 !(Deprecated <= Obsoleted)) {
2242 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2243 << 2 << PlatformName << Obsoleted.getAsString()
2244 << 1 << Deprecated.getAsString();
2245 return true;
2246 }
2247
2248 return false;
2249}
2250
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002251/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002252///
2253/// If either version tuple is empty, then they are assumed to match. If
2254/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2255static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2256 bool BeforeIsOkay) {
2257 if (X.empty() || Y.empty())
2258 return true;
2259
2260 if (X == Y)
2261 return true;
2262
2263 if (BeforeIsOkay && X < Y)
2264 return true;
2265
2266 return false;
2267}
2268
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002269AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002270 IdentifierInfo *Platform,
Manman Ren719a8642016-05-06 21:04:01 +00002271 bool Implicit,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002272 VersionTuple Introduced,
2273 VersionTuple Deprecated,
2274 VersionTuple Obsoleted,
2275 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002276 StringRef Message,
Manman Rend8039df2016-02-22 04:47:24 +00002277 bool IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002278 StringRef Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002279 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00002280 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002281 VersionTuple MergedIntroduced = Introduced;
2282 VersionTuple MergedDeprecated = Deprecated;
2283 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002284 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002285 bool OverrideOrImpl = false;
2286 switch (AMK) {
2287 case AMK_None:
2288 case AMK_Redeclaration:
2289 OverrideOrImpl = false;
2290 break;
2291
2292 case AMK_Override:
2293 case AMK_ProtocolImplementation:
2294 OverrideOrImpl = true;
2295 break;
2296 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002297
Rafael Espindolac67f2232012-05-10 02:50:16 +00002298 if (D->hasAttrs()) {
2299 AttrVec &Attrs = D->getAttrs();
2300 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002301 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002302 if (!OldAA) {
2303 ++i;
2304 continue;
2305 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002306
Rafael Espindolac67f2232012-05-10 02:50:16 +00002307 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2308 if (OldPlatform != Platform) {
2309 ++i;
2310 continue;
2311 }
2312
Tim Northover7a73cc72015-10-30 16:30:49 +00002313 // If there is an existing availability attribute for this platform that
2314 // is explicit and the new one is implicit use the explicit one and
2315 // discard the new implicit attribute.
Manman Ren719a8642016-05-06 21:04:01 +00002316 if (!OldAA->isImplicit() && Implicit) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002317 return nullptr;
2318 }
2319
2320 // If there is an existing attribute for this platform that is implicit
2321 // and the new attribute is explicit then erase the old one and
2322 // continue processing the attributes.
Manman Ren719a8642016-05-06 21:04:01 +00002323 if (!Implicit && OldAA->isImplicit()) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002324 Attrs.erase(Attrs.begin() + i);
2325 --e;
2326 continue;
2327 }
2328
Rafael Espindolac67f2232012-05-10 02:50:16 +00002329 FoundAny = true;
2330 VersionTuple OldIntroduced = OldAA->getIntroduced();
2331 VersionTuple OldDeprecated = OldAA->getDeprecated();
2332 VersionTuple OldObsoleted = OldAA->getObsoleted();
2333 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002334
Douglas Gregord2a713e2015-09-30 21:27:42 +00002335 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2336 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2337 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002338 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002339 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2340 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002341 int Which = -1;
2342 VersionTuple FirstVersion;
2343 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002344 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002345 Which = 0;
2346 FirstVersion = OldIntroduced;
2347 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002348 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002349 Which = 1;
2350 FirstVersion = Deprecated;
2351 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002352 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002353 Which = 2;
2354 FirstVersion = Obsoleted;
2355 SecondVersion = OldObsoleted;
2356 }
2357
2358 if (Which == -1) {
2359 Diag(OldAA->getLocation(),
2360 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002361 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2362 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002363 } else {
2364 Diag(OldAA->getLocation(),
2365 diag::warn_mismatched_availability_override)
2366 << Which
2367 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002368 << FirstVersion.getAsString() << SecondVersion.getAsString()
2369 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002370 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002371 if (AMK == AMK_Override)
2372 Diag(Range.getBegin(), diag::note_overridden_method);
2373 else
2374 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002375 } else {
2376 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2377 Diag(Range.getBegin(), diag::note_previous_attribute);
2378 }
2379
Rafael Espindolac67f2232012-05-10 02:50:16 +00002380 Attrs.erase(Attrs.begin() + i);
2381 --e;
2382 continue;
2383 }
2384
2385 VersionTuple MergedIntroduced2 = MergedIntroduced;
2386 VersionTuple MergedDeprecated2 = MergedDeprecated;
2387 VersionTuple MergedObsoleted2 = MergedObsoleted;
2388
2389 if (MergedIntroduced2.empty())
2390 MergedIntroduced2 = OldIntroduced;
2391 if (MergedDeprecated2.empty())
2392 MergedDeprecated2 = OldDeprecated;
2393 if (MergedObsoleted2.empty())
2394 MergedObsoleted2 = OldObsoleted;
2395
2396 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2397 MergedIntroduced2, MergedDeprecated2,
2398 MergedObsoleted2)) {
2399 Attrs.erase(Attrs.begin() + i);
2400 --e;
2401 continue;
2402 }
2403
2404 MergedIntroduced = MergedIntroduced2;
2405 MergedDeprecated = MergedDeprecated2;
2406 MergedObsoleted = MergedObsoleted2;
2407 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002408 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002409 }
2410
2411 if (FoundAny &&
2412 MergedIntroduced == Introduced &&
2413 MergedDeprecated == Deprecated &&
2414 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002415 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002416
Douglas Gregord2a713e2015-09-30 21:27:42 +00002417 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002418 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002419 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002420 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002421 !OverrideOrImpl) {
Manman Ren719a8642016-05-06 21:04:01 +00002422 auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002423 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002424 Obsoleted, IsUnavailable, Message,
Manman Ren75bc6762016-03-21 17:30:55 +00002425 IsStrict, Replacement,
2426 AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002427 Avail->setImplicit(Implicit);
2428 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002429 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002430 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002431}
2432
Erich Keanee891aa92018-07-13 15:07:47 +00002433static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002434 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002435 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002436 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2437 unsigned Index = AL.getAttributeSpellingListIndex();
Fangrui Song6907ce22018-07-30 19:24:48 +00002438
Aaron Ballman00e99962013-08-31 01:11:41 +00002439 IdentifierInfo *II = Platform->Ident;
2440 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2441 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2442 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002443
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002444 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002445 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002446 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002447
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002448 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2449 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2450 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2451 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2452 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002453 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002454 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002455 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002456 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002457 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002458 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002459
Michael Wu260e9622018-11-12 02:44:33 +00002460 if (II->isStr("swift")) {
2461 if (Introduced.isValid() || Obsoleted.isValid() ||
2462 (!IsUnavailable && !Deprecated.isValid())) {
2463 S.Diag(AL.getLoc(),
2464 diag::warn_availability_swift_unavailable_deprecated_only);
2465 return;
2466 }
2467 }
2468
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002469 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II,
Manman Ren719a8642016-05-06 21:04:01 +00002470 false/*Implicit*/,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002471 Introduced.Version,
2472 Deprecated.Version,
2473 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002474 IsUnavailable, Str,
Manman Ren75bc6762016-03-21 17:30:55 +00002475 IsStrict, Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002476 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002477 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002478 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002479 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002480
2481 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2482 // matches before the start of the watchOS platform.
2483 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2484 IdentifierInfo *NewII = nullptr;
2485 if (II->getName() == "ios")
2486 NewII = &S.Context.Idents.get("watchos");
2487 else if (II->getName() == "ios_app_extension")
2488 NewII = &S.Context.Idents.get("watchos_app_extension");
2489
2490 if (NewII) {
2491 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2492 if (Version.empty())
2493 return Version;
2494 auto Major = Version.getMajor();
2495 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2496 if (NewMajor >= 2) {
2497 if (Version.getMinor().hasValue()) {
2498 if (Version.getSubminor().hasValue())
2499 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2500 Version.getSubminor().getValue());
2501 else
2502 return VersionTuple(NewMajor, Version.getMinor().getValue());
2503 }
2504 }
2505
2506 return VersionTuple(2, 0);
2507 };
2508
2509 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2510 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2511 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2512
2513 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002514 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002515 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002516 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002517 NewIntroduced,
2518 NewDeprecated,
2519 NewObsoleted,
2520 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002521 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002522 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002523 Sema::AMK_None,
2524 Index);
2525 if (NewAttr)
2526 D->addAttr(NewAttr);
2527 }
2528 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2529 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2530 // matches before the start of the tvOS platform.
2531 IdentifierInfo *NewII = nullptr;
2532 if (II->getName() == "ios")
2533 NewII = &S.Context.Idents.get("tvos");
2534 else if (II->getName() == "ios_app_extension")
2535 NewII = &S.Context.Idents.get("tvos_app_extension");
2536
2537 if (NewII) {
2538 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002539 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002540 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002541 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002542 Introduced.Version,
2543 Deprecated.Version,
2544 Obsoleted.Version,
2545 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002546 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002547 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002548 Sema::AMK_None,
2549 Index);
2550 if (NewAttr)
2551 D->addAttr(NewAttr);
2552 }
2553 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002554}
2555
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002556static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002557 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002558 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002559 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002560 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002561 "Invalid number of arguments in an external_source_symbol attribute");
2562
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002563 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002564 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002565 Language = SE->getString();
2566 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002567 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002568 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002569 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002570
2571 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002572 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2573 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002574}
2575
John McCalld041a9b2013-02-20 01:54:26 +00002576template <class T>
2577static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2578 typename T::VisibilityType value,
2579 unsigned attrSpellingListIndex) {
2580 T *existingAttr = D->getAttr<T>();
2581 if (existingAttr) {
2582 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2583 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002584 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002585 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2586 S.Diag(range.getBegin(), diag::note_previous_attribute);
2587 D->dropAttr<T>();
2588 }
2589 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2590}
2591
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002592VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002593 VisibilityAttr::VisibilityType Vis,
2594 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002595 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2596 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002597}
2598
John McCalld041a9b2013-02-20 01:54:26 +00002599TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2600 TypeVisibilityAttr::VisibilityType Vis,
2601 unsigned AttrSpellingListIndex) {
2602 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2603 AttrSpellingListIndex);
2604}
2605
Erich Keanee891aa92018-07-13 15:07:47 +00002606static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002607 bool isTypeVisibility) {
2608 // Visibility attributes don't mean anything on a typedef.
2609 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002610 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002611 return;
2612 }
2613
2614 // 'type_visibility' can only go on a type or namespace.
2615 if (isTypeVisibility &&
2616 !(isa<TagDecl>(D) ||
2617 isa<ObjCInterfaceDecl>(D) ||
2618 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002619 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002620 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002621 return;
2622 }
2623
Benjamin Kramer70370212013-09-09 15:08:57 +00002624 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002625 StringRef TypeStr;
2626 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002627 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002629
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002630 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002631 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002632 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2633 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002634 return;
2635 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002636
Aaron Ballman682ee422013-09-11 19:47:58 +00002637 // Complain about attempts to use protected visibility on targets
2638 // (like Darwin) that don't support it.
2639 if (type == VisibilityAttr::Protected &&
2640 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002641 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002642 type = VisibilityAttr::Default;
2643 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002644
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002645 unsigned Index = AL.getAttributeSpellingListIndex();
2646 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002647 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002648 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002649 (TypeVisibilityAttr::VisibilityType) type,
2650 Index);
2651 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002652 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002653 }
2654 if (newAttr)
2655 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002656}
2657
Erich Keanee891aa92018-07-13 15:07:47 +00002658static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002659 const auto *M = cast<ObjCMethodDecl>(D);
2660 if (!AL.isArgIdent(0)) {
2661 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002662 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002663 return;
2664 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002665
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002666 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002667 ObjCMethodFamilyAttr::FamilyKind F;
2668 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002669 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002670 return;
2671 }
2672
Alp Toker314cc812014-01-25 16:55:45 +00002673 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002674 !M->getReturnType()->isObjCObjectPointerType()) {
2675 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2676 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002677 // Ignore the attribute.
2678 return;
2679 }
2680
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002681 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2682 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002683}
2684
Erich Keanee891aa92018-07-13 15:07:47 +00002685static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002686 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002687 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002688 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002689 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2690 return;
2691 }
2692 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002693 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002694 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002695 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002696 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2697 return;
2698 }
2699 }
2700 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002701 // It is okay to include this attribute on properties, e.g.:
2702 //
2703 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2704 //
2705 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002706 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002707 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002708 }
Michael Han99315932013-01-24 16:46:58 +00002709 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002710 ObjCNSObjectAttr(AL.getRange(), S.Context,
2711 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002712}
2713
Erich Keanee891aa92018-07-13 15:07:47 +00002714static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002715 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002716 QualType T = TD->getUnderlyingType();
2717 if (!T->isObjCObjectPointerType()) {
2718 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2719 return;
2720 }
2721 } else {
2722 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2723 return;
2724 }
2725 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002726 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2727 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002728}
2729
Erich Keanee891aa92018-07-13 15:07:47 +00002730static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002731 if (!AL.isArgIdent(0)) {
2732 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002733 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002734 return;
2735 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002736
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002737 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002738 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002739 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002740 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002741 return;
2742 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002743
Michael Han99315932013-01-24 16:46:58 +00002744 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002745 BlocksAttr(AL.getRange(), S.Context, type,
2746 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002747}
2748
Erich Keanee891aa92018-07-13 15:07:47 +00002749static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002750 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002751 if (AL.getNumArgs() > 0) {
2752 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002753 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002754 if (E->isTypeDependent() || E->isValueDependent() ||
2755 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002756 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002757 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002758 return;
2759 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002760
John McCallb46f2872011-09-09 07:56:05 +00002761 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002762 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002763 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002764 return;
2765 }
John McCallb46f2872011-09-09 07:56:05 +00002766
2767 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002768 }
2769
Aaron Ballman18a78382013-11-21 00:28:23 +00002770 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002771 if (AL.getNumArgs() > 1) {
2772 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002773 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002774 if (E->isTypeDependent() || E->isValueDependent() ||
2775 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002776 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002777 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002778 return;
2779 }
2780 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002781
John McCallb46f2872011-09-09 07:56:05 +00002782 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002783 // FIXME: This error message could be improved, it would be nice
2784 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002785 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002786 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002787 return;
2788 }
2789 }
2790
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002791 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002792 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002793 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002794 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002795 return;
2796 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002797
Chris Lattner9363e312009-03-17 23:03:47 +00002798 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002799 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002800 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002801 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002802 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002803 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002804 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002805 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002806 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002807 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002808 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002809 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002810 return;
2811 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002812 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002813 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002814 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002815 const FunctionType *FT = Ty->isFunctionPointerType()
2816 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002817 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002818 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002819 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002820 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002821 return;
2822 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002823 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002824 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002825 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002826 return;
2827 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002828 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002829 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002830 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002831 return;
2832 }
Michael Han99315932013-01-24 16:46:58 +00002833 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002834 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2835 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002836}
2837
Erich Keanee891aa92018-07-13 15:07:47 +00002838static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002839 if (D->getFunctionType() &&
2840 D->getFunctionType()->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002841 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002842 return;
2843 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002844 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002845 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002846 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002847 return;
2848 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002849
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002850 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002851 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002852 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2853 !AL.getScopeName())
Erich Keane44bacdf2018-08-09 13:21:32 +00002854 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballmane7964782016-03-07 22:44:55 +00002855
Fangrui Song6907ce22018-07-30 19:24:48 +00002856 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002857 WarnUnusedResultAttr(AL.getRange(), S.Context,
2858 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002859}
2860
Erich Keanee891aa92018-07-13 15:07:47 +00002861static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002862 // weak_import only applies to variable & function declarations.
2863 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002864 if (!D->canBeWeakImported(isDef)) {
2865 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002866 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002867 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002868 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002869 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002870 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002871 // Nothing to warn about here.
2872 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002873 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002874 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002875
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002876 return;
2877 }
2878
Michael Han99315932013-01-24 16:46:58 +00002879 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002880 WeakImportAttr(AL.getRange(), S.Context,
2881 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002882}
2883
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002884// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002885template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002886static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002887 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002888 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002889 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002890 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2891 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002892 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002893 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002894 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002895 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002896 return;
2897 }
2898 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002899
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002900 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2901 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2902 Existing->getYDim() == WGSize[1] &&
2903 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002904 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002905
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002906 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002907 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002908 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002909}
2910
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002911// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002912static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002913 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002914 const Expr *E = AL.getArgAsExpr(0);
2915 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002916 return;
2917 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002918 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002919 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002920 return;
2921 }
2922
2923 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2924 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2925 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002926 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002927
2928 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002929 AL.getRange(), S.Context, SGSize,
2930 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002931}
2932
Erich Keanee891aa92018-07-13 15:07:47 +00002933static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002934 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002935 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002936 return;
2937 }
2938
Craig Topperc3ec1492014-05-26 06:22:03 +00002939 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002940 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002941 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002942
2943 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2944 (ParmType->isBooleanType() ||
2945 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002946 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002947 << ParmType;
2948 return;
2949 }
2950
Aaron Ballmana9e05402013-12-02 22:16:55 +00002951 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002952 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002953 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002954 return;
2955 }
2956 }
2957
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002958 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002959 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002960 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002961}
2962
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002963SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002964 StringRef Name,
2965 unsigned AttrSpellingListIndex) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002966 // Explicit or partial specializations do not inherit
2967 // the section attribute from the primary template.
2968 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2969 if (AttrSpellingListIndex == SectionAttr::Declspec_allocate &&
2970 FD->isFunctionTemplateSpecialization())
2971 return nullptr;
2972 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002973 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2974 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002975 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002976 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2977 << 1 /*section*/;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002978 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002979 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002980 }
Michael Han99315932013-01-24 16:46:58 +00002981 return ::new (Context) SectionAttr(Range, Context, Name,
2982 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002983}
2984
Reid Kleckner2a133222015-03-04 23:39:17 +00002985bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2986 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2987 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002988 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002989 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002990 return false;
2991 }
2992 return true;
2993}
2994
Erich Keanee891aa92018-07-13 15:07:47 +00002995static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002996 // Make sure that there is a string literal as the sections's single
2997 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002998 StringRef Str;
2999 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003000 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00003001 return;
Mike Stump11289f42009-09-09 15:08:12 +00003002
Reid Kleckner2a133222015-03-04 23:39:17 +00003003 if (!S.checkSectionName(LiteralLoc, Str))
3004 return;
3005
Chris Lattner30ba6742009-08-10 19:03:04 +00003006 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003007 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00003008 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003009 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00003010 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00003011 return;
3012 }
Mike Stump11289f42009-09-09 15:08:12 +00003013
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003014 unsigned Index = AL.getAttributeSpellingListIndex();
3015 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003016 if (NewAttr)
3017 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00003018}
3019
Erich Keane7963e8b2018-07-18 20:04:48 +00003020static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) {
3021 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
3022 if (!Error.empty()) {
3023 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
3024 << 0 /*'code-seg'*/;
3025 return false;
3026 }
3027 return true;
3028}
3029
3030CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range,
3031 StringRef Name,
3032 unsigned AttrSpellingListIndex) {
3033 // Explicit or partial specializations do not inherit
3034 // the code_seg attribute from the primary template.
3035 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3036 if (FD->isFunctionTemplateSpecialization())
3037 return nullptr;
3038 }
3039 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3040 if (ExistingAttr->getName() == Name)
3041 return nullptr;
3042 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3043 << 0 /*codeseg*/;
3044 Diag(Range.getBegin(), diag::note_previous_attribute);
3045 return nullptr;
3046 }
3047 return ::new (Context) CodeSegAttr(Range, Context, Name,
3048 AttrSpellingListIndex);
3049}
3050
3051static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3052 StringRef Str;
3053 SourceLocation LiteralLoc;
3054 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3055 return;
3056 if (!checkCodeSegName(S, LiteralLoc, Str))
3057 return;
3058 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3059 if (!ExistingAttr->isImplicit()) {
3060 S.Diag(AL.getLoc(),
3061 ExistingAttr->getName() == Str
3062 ? diag::warn_duplicate_codeseg_attribute
3063 : diag::err_conflicting_codeseg_attribute);
3064 return;
3065 }
3066 D->dropAttr<CodeSegAttr>();
3067 }
3068 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str,
3069 AL.getAttributeSpellingListIndex()))
3070 D->addAttr(CSA);
3071}
3072
Erich Keane57e15cd2017-07-19 22:06:33 +00003073// Check for things we'd like to warn about. Multiversioning issues are
3074// handled later in the process, once we know how many exist.
3075bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3076 enum FirstParam { Unsupported, Duplicate };
3077 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00003078 for (auto Str : {"tune=", "fpmath="})
3079 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00003080 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3081 << Unsupported << None << Str;
3082
3083 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
3084
3085 if (!ParsedAttrs.Architecture.empty() &&
3086 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3087 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3088 << Unsupported << Architecture << ParsedAttrs.Architecture;
3089
3090 if (ParsedAttrs.DuplicateArchitecture)
3091 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3092 << Duplicate << None << "arch=";
3093
3094 for (const auto &Feature : ParsedAttrs.Features) {
3095 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3096 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3097 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3098 << Unsupported << None << CurFeature;
3099 }
3100
Erich Keane29636aa2018-02-16 17:31:59 +00003101 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003102}
3103
Erich Keanee891aa92018-07-13 15:07:47 +00003104static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003105 StringRef Str;
3106 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003107 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003108 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003109 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003110
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003111 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00003112 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003113 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00003114 D->addAttr(NewAttr);
3115}
3116
Erich Keanee891aa92018-07-13 15:07:47 +00003117static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003118 Expr *E = AL.getArgAsExpr(0);
3119 uint32_t VecWidth;
3120 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3121 AL.setInvalid();
3122 return;
3123 }
3124
3125 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3126 if (Existing && Existing->getVectorWidth() != VecWidth) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003127 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Craig Topper74c10e32018-07-09 19:00:16 +00003128 return;
3129 }
3130
3131 D->addAttr(::new (S.Context)
3132 MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth,
3133 AL.getAttributeSpellingListIndex()));
3134}
3135
Erich Keanee891aa92018-07-13 15:07:47 +00003136static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003137 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003138 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003139 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003140 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003141
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003142 // gcc only allows for simple identifiers. Since we support more than gcc, we
3143 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003144 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003145 if (DRE->hasQualifier())
3146 S.Diag(Loc, diag::warn_cleanup_ext);
3147 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3148 NI = DRE->getNameInfo();
3149 if (!FD) {
3150 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3151 << NI.getName();
3152 return;
3153 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003154 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003155 if (ULE->hasExplicitTemplateArgs())
3156 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003157 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3158 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003159 if (!FD) {
3160 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3161 << NI.getName();
3162 if (ULE->getType() == S.Context.OverloadTy)
3163 S.NoteAllOverloadCandidates(ULE);
3164 return;
3165 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003166 } else {
3167 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003168 return;
3169 }
3170
Anders Carlssond277d792009-01-31 01:16:18 +00003171 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003172 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3173 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003174 return;
3175 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003176
Anders Carlsson723f55d2009-02-07 23:16:50 +00003177 // We're currently more strict than GCC about what function types we accept.
3178 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003179 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003180 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003181 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3182 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003183 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3184 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003185 return;
3186 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003187
Michael Han99315932013-01-24 16:46:58 +00003188 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003189 CleanupAttr(AL.getRange(), S.Context, FD,
3190 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003191}
3192
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003193static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003194 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003195 if (!AL.isArgIdent(0)) {
3196 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003197 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003198 return;
3199 }
3200
3201 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003202 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003203 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3204 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003205 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003206 return;
3207 }
3208
3209 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003210 AL.getRange(), S.Context, ExtensibilityKind,
3211 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003212}
3213
Mike Stumpd3bb5572009-07-24 19:02:52 +00003214/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003215/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003216static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003217 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003218 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003219 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003220 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003221
Eric Christopherb64963e2015-08-13 21:34:35 +00003222 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003223 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003224
Eric Christopherb64963e2015-08-13 21:34:35 +00003225 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3226 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003227 !isCFStringType(Ty, S.Context) &&
3228 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003229 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003230 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003231 << "a string type" << IdxExpr->getSourceRange()
3232 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003233 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003234 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003235 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003236 if (!isNSStringType(Ty, S.Context) &&
3237 !isCFStringType(Ty, S.Context) &&
3238 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003239 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003240 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003241 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003242 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003243 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003244 }
3245
Joel E. Denny81508102018-03-13 14:51:22 +00003246 D->addAttr(::new (S.Context) FormatArgAttr(
3247 AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003248}
3249
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003250enum FormatAttrKind {
3251 CFStringFormat,
3252 NSStringFormat,
3253 StrftimeFormat,
3254 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003255 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003256 InvalidFormat
3257};
3258
3259/// getFormatAttrKind - Map from format attribute names to supported format
3260/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003261static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003262 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003263 // Check for formats that get handled specially.
3264 .Case("NSString", NSStringFormat)
3265 .Case("CFString", CFStringFormat)
3266 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003267
Mehdi Amini06d367c2016-10-24 20:39:34 +00003268 // Otherwise, check for supported formats.
3269 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3270 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3271 .Case("kprintf", SupportedFormat) // OpenBSD.
3272 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3273 .Case("os_trace", SupportedFormat)
3274 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003275
Mehdi Amini06d367c2016-10-24 20:39:34 +00003276 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3277 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003278}
3279
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003280/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003281/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003282static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003283 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003284 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003285 return;
3286 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003287
Aaron Ballman4a611152013-11-27 16:34:09 +00003288 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003289 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3290 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003291 return;
3292 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003293 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003294 if (S.Context.getAsArrayType(T))
3295 T = S.Context.getBaseElementType(T);
3296 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003297 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3298 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003299 return;
3300 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003301
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003302 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003303 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003304 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3305 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003306 return;
3307 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003308
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003309 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003310 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003311 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003312 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003313 return;
3314 }
Michael Han99315932013-01-24 16:46:58 +00003315 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003316 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3317 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003318}
3319
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003320FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3321 IdentifierInfo *Format, int FormatIdx,
3322 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003323 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003324 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003325 for (auto *F : D->specific_attrs<FormatAttr>()) {
3326 if (F->getType() == Format &&
3327 F->getFormatIdx() == FormatIdx &&
3328 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003329 // If we don't have a valid location for this attribute, adopt the
3330 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003331 if (F->getLocation().isInvalid())
3332 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003333 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003334 }
3335 }
3336
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003337 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3338 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003339}
3340
Mike Stumpd3bb5572009-07-24 19:02:52 +00003341/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003342/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003343static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003344 if (!AL.isArgIdent(0)) {
3345 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003346 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003347 return;
3348 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003349
Chandler Carruth743682b2010-11-16 08:35:43 +00003350 // In C++ the implicit 'this' function parameter also counts, and they are
3351 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003352 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003353 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003354
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003355 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003356 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003357
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003358 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003359 // If we've modified the string name, we need a new identifier for it.
3360 II = &S.Context.Idents.get(Format);
3361 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003362
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003363 // Check for supported formats.
3364 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003365
Chris Lattner12161d32010-03-22 21:08:50 +00003366 if (Kind == IgnoredFormat)
3367 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003368
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003369 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003370 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003371 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003372 return;
3373 }
3374
3375 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003376 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003377 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003378 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003379 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003380
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003381 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003382 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003383 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003384 return;
3385 }
3386
3387 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003388 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003389
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003390 if (HasImplicitThisParam) {
3391 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003392 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003393 diag::err_format_attribute_implicit_this_format_string)
3394 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003395 return;
3396 }
3397 ArgIdx--;
3398 }
Mike Stump11289f42009-09-09 15:08:12 +00003399
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003400 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003401 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003402
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003403 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003404 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003405 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003406 << "a CFString" << IdxExpr->getSourceRange()
3407 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003408 return;
3409 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003410 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003411 // FIXME: do we need to check if the type is NSString*? What are the
3412 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003413 if (!isNSStringType(Ty, S.Context)) {
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 << "an NSString" << IdxExpr->getSourceRange()
3416 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003417 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003418 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003419 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003420 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003421 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003422 << "a string type" << IdxExpr->getSourceRange()
3423 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003424 return;
3425 }
3426
3427 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003428 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003429 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003430 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003431 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003432
3433 // check if the function is variadic if the 3rd argument non-zero
3434 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003435 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003436 ++NumArgs; // +1 for ...
3437 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003438 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003439 return;
3440 }
3441 }
3442
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003443 // strftime requires FirstArg to be 0 because it doesn't read from any
3444 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003445 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003446 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003447 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003448 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003449 return;
3450 }
3451 // if 0 it disables parameter checking (to use with e.g. va_list)
3452 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003453 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003454 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003455 return;
3456 }
3457
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003458 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003459 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003460 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003461 if (NewAttr)
3462 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003463}
3464
Erich Keanee891aa92018-07-13 15:07:47 +00003465static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003466 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003467 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003468 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003469 if (TD && TD->getUnderlyingType()->isUnionType())
3470 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3471 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003472 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003473
3474 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003475 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3476 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003477 return;
3478 }
3479
John McCallf937c022011-10-07 06:10:15 +00003480 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003481 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003482 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003483 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003484 return;
3485 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003486
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003487 RecordDecl::field_iterator Field = RD->field_begin(),
3488 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003489 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003490 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003491 return;
3492 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003493
David Blaikie40ed2972012-06-06 20:45:41 +00003494 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003495 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003496 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003497 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003498 diag::warn_transparent_union_attribute_floating)
3499 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003500 return;
3501 }
3502
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003503 if (FirstType->isIncompleteType())
3504 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003505 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3506 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3507 for (; Field != FieldEnd; ++Field) {
3508 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003509 if (FieldType->isIncompleteType())
3510 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003511 // FIXME: this isn't fully correct; we also need to test whether the
3512 // members of the union would all have the same calling convention as the
3513 // first member of the union. Checking just the size and alignment isn't
3514 // sufficient (consider structs passed on the stack instead of in registers
3515 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003516 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003517 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003518 // Warn if we drop the attribute.
3519 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003520 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003521 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003522 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003523 diag::warn_transparent_union_attribute_field_size_align)
3524 << isSize << Field->getDeclName() << FieldBits;
3525 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003526 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003527 diag::note_transparent_union_first_field_size_align)
3528 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003529 return;
3530 }
3531 }
3532
Michael Han99315932013-01-24 16:46:58 +00003533 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003534 TransparentUnionAttr(AL.getRange(), S.Context,
3535 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003536}
3537
Erich Keanee891aa92018-07-13 15:07:47 +00003538static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003539 // Make sure that there is a string literal as the annotation's single
3540 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003541 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003542 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003543 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003544
3545 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003546 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3547 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003548 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003549 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003550
Michael Han99315932013-01-24 16:46:58 +00003551 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003552 AnnotateAttr(AL.getRange(), S.Context, Str,
3553 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003554}
3555
Erich Keanee891aa92018-07-13 15:07:47 +00003556static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003557 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3558 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003559}
3560
3561void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3562 unsigned SpellingListIndex) {
3563 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3564 SourceLocation AttrLoc = AttrRange.getBegin();
3565
3566 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003567 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003568 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003569 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003570 T = VD->getType();
3571 else
3572 llvm_unreachable("Unknown decl type for align_value");
3573
3574 if (!T->isDependentType() && !T->isAnyPointerType() &&
3575 !T->isReferenceType() && !T->isMemberPointerType()) {
3576 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3577 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3578 return;
3579 }
3580
3581 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003582 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003583 ExprResult ICE
3584 = VerifyIntegerConstantExpression(E, &Alignment,
3585 diag::err_align_value_attribute_argument_not_int,
3586 /*AllowFold*/ false);
3587 if (ICE.isInvalid())
3588 return;
3589
3590 if (!Alignment.isPowerOf2()) {
3591 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3592 << E->getSourceRange();
3593 return;
3594 }
3595
3596 D->addAttr(::new (Context)
3597 AlignValueAttr(AttrRange, Context, ICE.get(),
3598 SpellingListIndex));
3599 return;
3600 }
3601
3602 // Save dependent expressions in the AST to be instantiated.
3603 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003604}
3605
Erich Keanee891aa92018-07-13 15:07:47 +00003606static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003607 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003608 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003609 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003610 return;
3611 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003612
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003613 if (AL.getNumArgs() == 0) {
3614 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3615 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003616 return;
3617 }
3618
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003619 Expr *E = AL.getArgAsExpr(0);
3620 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3621 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003622 diag::err_pack_expansion_without_parameter_packs);
3623 return;
3624 }
3625
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003626 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003627 return;
3628
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003629 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3630 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003631}
3632
3633void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003634 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003635 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3636 SourceLocation AttrLoc = AttrRange.getBegin();
3637
Richard Smith1dba27c2013-01-29 09:02:09 +00003638 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003639 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003640 // C++11 [dcl.align]p1:
3641 // An alignment-specifier may be applied to a variable or to a class
3642 // data member, but it shall not be applied to a bit-field, a function
3643 // parameter, the formal parameter of a catch clause, or a variable
3644 // declared with the register storage class specifier. An
3645 // alignment-specifier may also be applied to the declaration of a class
3646 // or enumeration type.
3647 // C11 6.7.5/2:
3648 // An alignment attribute shall not be specified in a declaration of
3649 // a typedef, or a bit-field, or a function, or a parameter, or an
3650 // object declared with the register storage-class specifier.
3651 int DiagKind = -1;
3652 if (isa<ParmVarDecl>(D)) {
3653 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003654 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003655 if (VD->getStorageClass() == SC_Register)
3656 DiagKind = 1;
3657 if (VD->isExceptionVariable())
3658 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003659 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003660 if (FD->isBitField())
3661 DiagKind = 3;
3662 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003663 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003664 << (TmpAttr.isC11() ? ExpectedVariableOrField
3665 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003666 return;
3667 }
3668 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003669 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003670 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003671 return;
3672 }
3673 }
3674
Richard Smith90ae9672018-06-20 23:36:55 +00003675 if (E->isValueDependent()) {
3676 // We can't support a dependent alignment on a non-dependent type,
3677 // because we have no way to model that a type is "alignment-dependent"
3678 // but not dependent in any other way.
3679 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3680 if (!TND->getUnderlyingType()->isDependentType()) {
3681 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3682 << E->getSourceRange();
3683 return;
3684 }
3685 }
3686
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003687 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003688 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3689 AA->setPackExpansion(IsPackExpansion);
3690 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003691 return;
3692 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003693
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003694 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003695 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003696 ExprResult ICE
3697 = VerifyIntegerConstantExpression(E, &Alignment,
3698 diag::err_aligned_attribute_argument_not_int,
3699 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003700 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003701 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003702
David Majnemer0be6bd02015-07-26 09:02:21 +00003703 uint64_t AlignVal = Alignment.getZExtValue();
3704
Richard Smith848e1f12013-02-01 08:12:08 +00003705 // C++11 [dcl.align]p2:
3706 // -- if the constant expression evaluates to zero, the alignment
3707 // specifier shall have no effect
3708 // C11 6.7.5p6:
3709 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003710 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003711 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003712 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3713 << E->getSourceRange();
3714 return;
3715 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003716 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003717
David Majnemerabecae72014-02-12 20:36:10 +00003718 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003719 unsigned MaxValidAlignment =
3720 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3721 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003722 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003723 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3724 << E->getSourceRange();
3725 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003726 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003727
David Majnemer0be6bd02015-07-26 09:02:21 +00003728 if (Context.getTargetInfo().isTLSSupported()) {
3729 unsigned MaxTLSAlign =
3730 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3731 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003732 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003733 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3734 VD->getTLSKind() != VarDecl::TLS_None) {
3735 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3736 << (unsigned)AlignVal << VD << MaxTLSAlign;
3737 return;
3738 }
3739 }
3740
Richard Smith44c247f2013-02-22 08:32:16 +00003741 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003742 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003743 AA->setPackExpansion(IsPackExpansion);
3744 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003745}
3746
Michael Hanaf02bbe2013-02-01 01:19:17 +00003747void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003748 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003749 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003750 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003751 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3752 SpellingListIndex);
3753 AA->setPackExpansion(IsPackExpansion);
3754 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003755}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003756
Richard Smith848e1f12013-02-01 08:12:08 +00003757void Sema::CheckAlignasUnderalignment(Decl *D) {
3758 assert(D->hasAttrs() && "no attributes on decl");
3759
David Majnemer475b25e2015-01-21 10:54:38 +00003760 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003761 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003762 UnderlyingTy = DiagTy = VD->getType();
3763 } else {
3764 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003765 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003766 UnderlyingTy = ED->getIntegerType();
3767 }
3768 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003769 return;
3770
3771 // C++11 [dcl.align]p5, C11 6.7.5/4:
3772 // The combined effect of all alignment attributes in a declaration shall
3773 // not specify an alignment that is less strict than the alignment that
3774 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003775 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003776 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003777 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003778 if (I->isAlignmentDependent())
3779 return;
3780 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003781 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003782 Align = std::max(Align, I->getAlignment(Context));
3783 }
3784
3785 if (AlignasAttr && Align) {
3786 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003787 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003788 if (NaturalAlign > RequestedAlign)
3789 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003790 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003791 }
3792}
3793
David Majnemer2c4e00a2014-01-29 22:07:36 +00003794bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003795 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003796 MSInheritanceAttr::Spelling SemanticSpelling) {
3797 assert(RD->hasDefinition() && "RD has no definition!");
3798
David Majnemer98c9ee22014-02-07 00:43:07 +00003799 // We may not have seen base specifiers or any virtual methods yet. We will
3800 // have to wait until the record is defined to catch any mismatches.
3801 if (!RD->getDefinition()->isCompleteDefinition())
3802 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003803
David Majnemer98c9ee22014-02-07 00:43:07 +00003804 // The unspecified model never matches what a definition could need.
3805 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3806 return false;
3807
David Majnemer4bb09802014-02-10 19:50:15 +00003808 if (BestCase) {
3809 if (RD->calculateInheritanceModel() == SemanticSpelling)
3810 return false;
3811 } else {
3812 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3813 return false;
3814 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003815
3816 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3817 << 0 /*definition*/;
3818 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3819 << RD->getNameAsString();
3820 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003821}
3822
Alexey Bataevf278eb12015-11-19 10:13:11 +00003823/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3824/// attribute.
3825static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3826 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003827 IntegerMode = true;
3828 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003829 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003830 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003831 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003832 case 'Q':
3833 DestWidth = 8;
3834 break;
3835 case 'H':
3836 DestWidth = 16;
3837 break;
3838 case 'S':
3839 DestWidth = 32;
3840 break;
3841 case 'D':
3842 DestWidth = 64;
3843 break;
3844 case 'X':
3845 DestWidth = 96;
3846 break;
3847 case 'T':
3848 DestWidth = 128;
3849 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003850 }
3851 if (Str[1] == 'F') {
3852 IntegerMode = false;
3853 } else if (Str[1] == 'C') {
3854 IntegerMode = false;
3855 ComplexMode = true;
3856 } else if (Str[1] != 'I') {
3857 DestWidth = 0;
3858 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003859 break;
3860 case 4:
3861 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3862 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003863 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003864 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003865 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003866 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003867 break;
3868 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003869 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003870 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003871 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003872 case 11:
3873 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003874 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003875 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003876 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003877}
3878
3879/// handleModeAttr - This attribute modifies the width of a decl with primitive
3880/// type.
3881///
3882/// Despite what would be logical, the mode attribute is a decl attribute, not a
3883/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3884/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00003885static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003886 // This attribute isn't documented, but glibc uses it. It changes
3887 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003888 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003889 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
3890 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003891 return;
3892 }
3893
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003894 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003895
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003896 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003897}
3898
3899void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3900 unsigned SpellingListIndex, bool InInstantiation) {
3901 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003902 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003903 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003904
3905 unsigned DestWidth = 0;
3906 bool IntegerMode = true;
3907 bool ComplexMode = false;
3908 llvm::APInt VectorSize(64, 0);
3909 if (Str.size() >= 4 && Str[0] == 'V') {
3910 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3911 size_t StrSize = Str.size();
3912 size_t VectorStringLength = 0;
3913 while ((VectorStringLength + 1) < StrSize &&
3914 isdigit(Str[VectorStringLength + 1]))
3915 ++VectorStringLength;
3916 if (VectorStringLength &&
3917 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3918 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003919 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003920 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003921 // Avoid duplicate warning from template instantiation.
3922 if (!InInstantiation)
3923 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003924 } else {
3925 VectorSize = 0;
3926 }
3927 }
3928
3929 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003930 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3931
3932 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3933 // and friends, at least with glibc.
3934 // FIXME: Make sure floating-point mappings are accurate
3935 // FIXME: Support XF and TF types
3936 if (!DestWidth) {
3937 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3938 return;
3939 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003940
3941 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003942 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003943 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003944 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003945 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3946 // Try to get type from enum declaration, default to int.
3947 OldTy = ED->getIntegerType();
3948 if (OldTy.isNull())
3949 OldTy = Context.IntTy;
3950 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003951 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003952
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003953 if (OldTy->isDependentType()) {
3954 D->addAttr(::new (Context)
3955 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3956 return;
3957 }
3958
Alexey Bataev326057d2015-06-19 07:46:21 +00003959 // Base type can also be a vector type (see PR17453).
3960 // Distinguish between base type and base element type.
3961 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003962 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00003963 OldElemTy = VT->getElementType();
3964
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003965 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3966 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3967 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3968 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3969 VectorSize.getBoolValue()) {
3970 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3971 return;
3972 }
3973 bool IntegralOrAnyEnumType =
3974 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3975
3976 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3977 !IntegralOrAnyEnumType)
3978 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003979 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003980 if (!IntegralOrAnyEnumType)
3981 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003982 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003983 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003984 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003985 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003986 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003987 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003988 }
3989
Alexey Bataev326057d2015-06-19 07:46:21 +00003990 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003991
3992 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003993 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3994 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003995 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003996 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003997
Alexey Bataev326057d2015-06-19 07:46:21 +00003998 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003999 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004000 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004001 }
4002
Eli Friedman4735374e2009-03-03 06:41:03 +00004003 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004004 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004005 }
4006
4007 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00004008 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004009 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
4010 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004011 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00004012 // Complex machine mode does not support base vector types.
4013 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004014 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004015 return;
4016 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004017 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00004018 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004019 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00004020 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004021 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00004022 }
4023
4024 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004025 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00004026 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00004027 }
4028
4029 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004030 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004031 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004032 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004033 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004034 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00004035 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00004036
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00004037 D->addAttr(::new (Context)
4038 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00004039}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004040
Erich Keanee891aa92018-07-13 15:07:47 +00004041static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Han99315932013-01-24 16:46:58 +00004042 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004043 NoDebugAttr(AL.getRange(), S.Context,
4044 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00004045}
4046
Paul Robinson30e41fb2014-12-15 18:57:28 +00004047AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00004048 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00004049 unsigned AttrSpellingListIndex) {
4050 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004051 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00004052 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4053 return nullptr;
4054 }
4055
4056 if (D->hasAttr<AlwaysInlineAttr>())
4057 return nullptr;
4058
4059 return ::new (Context) AlwaysInlineAttr(Range, Context,
4060 AttrSpellingListIndex);
4061}
4062
Erich Keane44bacdf2018-08-09 13:21:32 +00004063CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
4064 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004065 return nullptr;
4066
Erich Keane44bacdf2018-08-09 13:21:32 +00004067 return ::new (Context)
4068 CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004069}
4070
Erich Keane44bacdf2018-08-09 13:21:32 +00004071CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
4072 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
4073 return nullptr;
4074
4075 return ::new (Context)
4076 CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex());
4077}
4078
4079InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4080 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004081 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004082 // Attribute applies to Var but not any subclass of it (like ParmVar,
4083 // ImplicitParm or VarTemplateSpecialization).
4084 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004085 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4086 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4087 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004088 return nullptr;
4089 }
4090 // Attribute does not apply to non-static local variables.
4091 if (VD->hasLocalStorage()) {
4092 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4093 return nullptr;
4094 }
4095 }
4096
Erich Keane44bacdf2018-08-09 13:21:32 +00004097 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
4098 return nullptr;
4099
4100 return ::new (Context) InternalLinkageAttr(
4101 AL.getRange(), Context, AL.getAttributeSpellingListIndex());
4102}
4103InternalLinkageAttr *
4104Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4105 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4106 // Attribute applies to Var but not any subclass of it (like ParmVar,
4107 // ImplicitParm or VarTemplateSpecialization).
4108 if (VD->getKind() != Decl::Var) {
4109 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4110 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4111 : ExpectedVariableOrFunction);
4112 return nullptr;
4113 }
4114 // Attribute does not apply to non-static local variables.
4115 if (VD->hasLocalStorage()) {
4116 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4117 return nullptr;
4118 }
4119 }
4120
4121 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004122 return nullptr;
4123
4124 return ::new (Context)
Erich Keane44bacdf2018-08-09 13:21:32 +00004125 InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004126}
4127
Paul Robinson30e41fb2014-12-15 18:57:28 +00004128MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
4129 unsigned AttrSpellingListIndex) {
4130 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4131 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
4132 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4133 return nullptr;
4134 }
4135
4136 if (D->hasAttr<MinSizeAttr>())
4137 return nullptr;
4138
4139 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
4140}
4141
4142OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
4143 unsigned AttrSpellingListIndex) {
4144 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4145 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4146 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4147 D->dropAttr<AlwaysInlineAttr>();
4148 }
4149 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4150 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4151 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4152 D->dropAttr<MinSizeAttr>();
4153 }
4154
4155 if (D->hasAttr<OptimizeNoneAttr>())
4156 return nullptr;
4157
4158 return ::new (Context) OptimizeNoneAttr(Range, Context,
4159 AttrSpellingListIndex);
4160}
4161
Erich Keanee891aa92018-07-13 15:07:47 +00004162static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004163 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004164 return;
4165
Paul Robinson080b1f32015-01-13 18:34:56 +00004166 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004167 D, AL.getRange(), AL.getName(),
4168 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004169 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004170}
4171
Erich Keanee891aa92018-07-13 15:07:47 +00004172static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004173 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004174 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004175 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004176}
4177
Erich Keanee891aa92018-07-13 15:07:47 +00004178static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004179 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004180 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004181 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004182}
4183
Erich Keanee891aa92018-07-13 15:07:47 +00004184static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004185 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004186 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004187 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004188 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004189 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004190 return;
4191 }
4192 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004193 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004194}
4195
Erich Keanee891aa92018-07-13 15:07:47 +00004196static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004197 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004198 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004199 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004200 // extern __shared__ is only allowed on arrays with no length (e.g.
4201 // "int x[]").
Yaxun Liu97670892018-10-02 17:48:54 +00004202 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004203 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004204 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004205 return;
4206 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004207 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004208 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004209 << S.CurrentCUDATarget())
4210 return;
Justin Lebar10411012016-09-30 23:57:30 +00004211 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004212 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004213}
4214
Erich Keanee891aa92018-07-13 15:07:47 +00004215static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004216 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4217 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004218 return;
4219 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004220 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004221 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004222 SourceRange RTRange = FD->getReturnTypeSourceRange();
4223 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004224 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004225 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4226 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004227 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004228 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004229 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4230 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004231 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004232 << Method;
4233 return;
4234 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004235 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004236 }
4237 // Only warn for "inline" when compiling for host, to cut down on noise.
4238 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004239 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004240
Aaron Ballman3aff6332013-12-02 19:30:36 +00004241 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004242 CUDAGlobalAttr(AL.getRange(), S.Context,
4243 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004244}
4245
Erich Keanee891aa92018-07-13 15:07:47 +00004246static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004247 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004248 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004249 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004250 return;
4251 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004252
Michael Han99315932013-01-24 16:46:58 +00004253 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004254 GNUInlineAttr(AL.getRange(), S.Context,
4255 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004256}
4257
Erich Keanee891aa92018-07-13 15:07:47 +00004258static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004259 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004260
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004261 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004262 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4263 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004264 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004265 return;
4266
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004267 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004268 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004269 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004270 return;
4271 }
4272
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004273 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004274 case ParsedAttr::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004275 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004276 FastCallAttr(AL.getRange(), S.Context,
4277 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004278 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004279 case ParsedAttr::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004280 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004281 StdCallAttr(AL.getRange(), S.Context,
4282 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004283 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004284 case ParsedAttr::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004285 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004286 ThisCallAttr(AL.getRange(), S.Context,
4287 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004288 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004289 case ParsedAttr::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004290 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004291 CDeclAttr(AL.getRange(), S.Context,
4292 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004293 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004294 case ParsedAttr::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004295 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004296 PascalAttr(AL.getRange(), S.Context,
4297 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004298 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004299 case ParsedAttr::AT_SwiftCall:
John McCall477f2bb2016-03-03 06:39:32 +00004300 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004301 SwiftCallAttr(AL.getRange(), S.Context,
4302 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004303 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004304 case ParsedAttr::AT_VectorCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004305 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004306 VectorCallAttr(AL.getRange(), S.Context,
4307 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004308 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004309 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004310 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004311 MSABIAttr(AL.getRange(), S.Context,
4312 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004313 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004314 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004315 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004316 SysVABIAttr(AL.getRange(), S.Context,
4317 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004318 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004319 case ParsedAttr::AT_RegCall:
Erich Keane757d3172016-11-02 18:29:35 +00004320 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004321 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004322 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004323 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004324 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004325 switch (CC) {
4326 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004327 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004328 break;
4329 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004330 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004331 break;
4332 default:
4333 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004334 }
4335
Michael Han99315932013-01-24 16:46:58 +00004336 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004337 PcsAttr(AL.getRange(), S.Context, PCS,
4338 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004339 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004340 }
Sander de Smalen44a22532018-11-26 16:38:37 +00004341 case ParsedAttr::AT_AArch64VectorPcs:
4342 D->addAttr(::new(S.Context)
4343 AArch64VectorPcsAttr(AL.getRange(), S.Context,
4344 AL.getAttributeSpellingListIndex()));
4345 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004346 case ParsedAttr::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004347 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004348 IntelOclBiccAttr(AL.getRange(), S.Context,
4349 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004350 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004351 case ParsedAttr::AT_PreserveMost:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004352 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004353 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004354 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004355 case ParsedAttr::AT_PreserveAll:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004356 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004357 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004358 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004359 default:
4360 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004361 }
4362}
4363
Erich Keanee891aa92018-07-13 15:07:47 +00004364static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004365 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004366 return;
4367
4368 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004369 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004370 StringRef RuleName;
4371
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004372 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004373 return;
4374
4375 // FIXME: Warn if the rule name is unknown. This is tricky because only
4376 // clang-tidy knows about available rules.
4377 DiagnosticIdentifiers.push_back(RuleName);
4378 }
4379 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004380 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4381 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004382}
4383
Erich Keanee891aa92018-07-13 15:07:47 +00004384bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004385 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004386 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004387 return true;
4388
Erich Keaneb11ebc52017-09-27 03:20:13 +00004389 if (Attrs.hasProcessingCache()) {
4390 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004391 return false;
4392 }
4393
Erich Keanee891aa92018-07-13 15:07:47 +00004394 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004395 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4396 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004397 return true;
4398 }
4399
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004400 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004401 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004402 case ParsedAttr::AT_CDecl:
4403 CC = CC_C;
4404 break;
4405 case ParsedAttr::AT_FastCall:
4406 CC = CC_X86FastCall;
4407 break;
4408 case ParsedAttr::AT_StdCall:
4409 CC = CC_X86StdCall;
4410 break;
4411 case ParsedAttr::AT_ThisCall:
4412 CC = CC_X86ThisCall;
4413 break;
4414 case ParsedAttr::AT_Pascal:
4415 CC = CC_X86Pascal;
4416 break;
4417 case ParsedAttr::AT_SwiftCall:
4418 CC = CC_Swift;
4419 break;
4420 case ParsedAttr::AT_VectorCall:
4421 CC = CC_X86VectorCall;
4422 break;
Sander de Smalen44a22532018-11-26 16:38:37 +00004423 case ParsedAttr::AT_AArch64VectorPcs:
4424 CC = CC_AArch64VectorCall;
4425 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004426 case ParsedAttr::AT_RegCall:
4427 CC = CC_X86RegCall;
4428 break;
4429 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004430 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004431 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004432 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004433 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004434 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4435 CC_C;
4436 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004437 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004438 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004439 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4440 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004441 return true;
4442 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004443 if (StrRef == "aapcs") {
4444 CC = CC_AAPCS;
4445 break;
4446 } else if (StrRef == "aapcs-vfp") {
4447 CC = CC_AAPCS_VFP;
4448 break;
4449 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004450
Erich Keaneb11ebc52017-09-27 03:20:13 +00004451 Attrs.setInvalid();
4452 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004453 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004454 }
Erich Keanee891aa92018-07-13 15:07:47 +00004455 case ParsedAttr::AT_IntelOclBicc:
4456 CC = CC_IntelOclBicc;
4457 break;
4458 case ParsedAttr::AT_PreserveMost:
4459 CC = CC_PreserveMost;
4460 break;
4461 case ParsedAttr::AT_PreserveAll:
4462 CC = CC_PreserveAll;
4463 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004464 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004465 }
4466
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004467 const TargetInfo &TI = Context.getTargetInfo();
4468 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004469 if (A != TargetInfo::CCCR_OK) {
4470 if (A == TargetInfo::CCCR_Warning)
Erich Keane44bacdf2018-08-09 13:21:32 +00004471 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004472
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004473 // This convention is not valid for the target. Use the default function or
4474 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004475 bool IsCXXMethod = false, IsVariadic = false;
4476 if (FD) {
4477 IsCXXMethod = FD->isCXXInstanceMember();
4478 IsVariadic = FD->isVariadic();
4479 }
4480 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004481 }
4482
Erich Keaneb11ebc52017-09-27 03:20:13 +00004483 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004484 return false;
4485}
4486
John McCall477f2bb2016-03-03 06:39:32 +00004487/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004488static bool isValidSwiftContextType(QualType Ty) {
4489 if (!Ty->hasPointerRepresentation())
4490 return Ty->isDependentType();
4491 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004492}
4493
4494/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004495static bool isValidSwiftIndirectResultType(QualType Ty) {
4496 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4497 Ty = PtrType->getPointeeType();
4498 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4499 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004500 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004501 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004502 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004503 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004504}
4505
4506/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004507static bool isValidSwiftErrorResultType(QualType Ty) {
4508 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4509 Ty = PtrType->getPointeeType();
4510 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4511 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004512 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004513 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004514 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004515 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004516 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004517 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004518}
4519
Erich Keanee891aa92018-07-13 15:07:47 +00004520static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004521 ParameterABI Abi) {
4522 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4523 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004524}
4525
4526void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4527 unsigned spellingIndex) {
4528
4529 QualType type = cast<ParmVarDecl>(D)->getType();
4530
4531 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4532 if (existingAttr->getABI() != abi) {
4533 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4534 << getParameterABISpelling(abi) << existingAttr;
4535 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4536 return;
4537 }
4538 }
4539
4540 switch (abi) {
4541 case ParameterABI::Ordinary:
4542 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4543
4544 case ParameterABI::SwiftContext:
4545 if (!isValidSwiftContextType(type)) {
4546 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4547 << getParameterABISpelling(abi)
4548 << /*pointer to pointer */ 0 << type;
4549 }
4550 D->addAttr(::new (Context)
4551 SwiftContextAttr(range, Context, spellingIndex));
4552 return;
4553
4554 case ParameterABI::SwiftErrorResult:
4555 if (!isValidSwiftErrorResultType(type)) {
4556 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4557 << getParameterABISpelling(abi)
4558 << /*pointer to pointer */ 1 << type;
4559 }
4560 D->addAttr(::new (Context)
4561 SwiftErrorResultAttr(range, Context, spellingIndex));
4562 return;
4563
4564 case ParameterABI::SwiftIndirectResult:
4565 if (!isValidSwiftIndirectResultType(type)) {
4566 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4567 << getParameterABISpelling(abi)
4568 << /*pointer*/ 0 << type;
4569 }
4570 D->addAttr(::new (Context)
4571 SwiftIndirectResultAttr(range, Context, spellingIndex));
4572 return;
4573 }
4574 llvm_unreachable("bad parameter ABI attribute");
4575}
4576
John McCall3882ace2011-01-05 12:14:39 +00004577/// Checks a regparm attribute, returning true if it is ill-formed and
4578/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004579bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004580 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004581 return true;
4582
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004583 if (!checkAttributeNumArgs(*this, AL, 1)) {
4584 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004585 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004586 }
Eli Friedman7044b762009-03-27 21:06:47 +00004587
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004588 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004589 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4590 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4591 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004592 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004593 }
4594
Douglas Gregore8bbc122011-09-02 00:18:52 +00004595 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004596 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004597 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004598 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004599 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004600 }
4601
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004602 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004603 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004604 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004605 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004606 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004607 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004608 }
4609
John McCall3882ace2011-01-05 12:14:39 +00004610 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004611}
4612
Artem Belevichbcec9da2016-06-06 22:54:57 +00004613// Checks whether an argument of launch_bounds attribute is
4614// acceptable, performs implicit conversion to Rvalue, and returns
4615// non-nullptr Expr result on success. Otherwise, it returns nullptr
4616// and may output an error.
4617static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004618 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004619 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004620 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004621 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004622
4623 // Accept template arguments for now as they depend on something else.
4624 // We'll get to check them when they eventually get instantiated.
4625 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004626 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004627
4628 llvm::APSInt I(64);
4629 if (!E->isIntegerConstantExpr(I, S.Context)) {
4630 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004631 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004632 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004633 }
4634 // Make sure we can fit it in 32 bits.
4635 if (!I.isIntN(32)) {
4636 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4637 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004638 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004639 }
4640 if (I < 0)
4641 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004642 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004643
Artem Belevichbcec9da2016-06-06 22:54:57 +00004644 // We may need to perform implicit conversion of the argument.
4645 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4646 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4647 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4648 assert(!ValArg.isInvalid() &&
4649 "Unexpected PerformCopyInitialization() failure.");
4650
4651 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004652}
4653
4654void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4655 Expr *MinBlocks, unsigned SpellingListIndex) {
4656 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4657 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004658 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4659 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004660 return;
4661
Artem Belevichbcec9da2016-06-06 22:54:57 +00004662 if (MinBlocks) {
4663 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4664 if (MinBlocks == nullptr)
4665 return;
4666 }
Artem Belevich7093e402015-04-21 22:55:54 +00004667
4668 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4669 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4670}
4671
Erich Keanee891aa92018-07-13 15:07:47 +00004672static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004673 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4674 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004675 return;
4676
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004677 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4678 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4679 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004680}
4681
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004682static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004683 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004684 if (!AL.isArgIdent(0)) {
4685 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004686 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004687 return;
4688 }
Joel E. Denny81508102018-03-13 14:51:22 +00004689
4690 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004691 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004692 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004693 return;
4694
Joel E. Denny81508102018-03-13 14:51:22 +00004695 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004696 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004697 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004698 return;
4699
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004700 bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004701 if (IsPointer) {
4702 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004703 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4704 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4705 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004706 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004707 }
4708
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004709 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
4710 AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx,
4711 TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004712}
4713
4714static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004715 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004716 if (!AL.isArgIdent(0)) {
4717 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004718 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004719 return;
4720 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004721
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004722 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004723 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004724
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004725 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004726 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004727 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004728 return;
4729 }
4730
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004731 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004732 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004733 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004734 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004735
Michael Han99315932013-01-24 16:46:58 +00004736 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004737 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004738 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004739 AL.getLayoutCompatible(),
4740 AL.getMustBeNull(),
4741 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004742}
4743
Erich Keanee891aa92018-07-13 15:07:47 +00004744static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004745 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004746
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004747 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004748 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004749 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004750 return;
4751
Joel E. Denny81508102018-03-13 14:51:22 +00004752 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
4753 D->addAttr(::new (S.Context) XRayLogArgsAttr(
4754 AL.getRange(), S.Context, ArgCount.getSourceIndex(),
4755 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004756}
4757
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004758//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004759// Checker-specific attribute handlers.
4760//===----------------------------------------------------------------------===//
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004761static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4762 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004763}
4764
George Karpenkov1657f362018-11-30 02:18:37 +00004765static bool isValidSubjectOfNSAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004766 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004767 QT->isObjCNSObjectType();
John McCalled433932011-01-25 03:31:58 +00004768}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004769
George Karpenkov1657f362018-11-30 02:18:37 +00004770static bool isValidSubjectOfCFAttribute(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004771 return QT->isDependentType() || QT->isPointerType() ||
George Karpenkov1657f362018-11-30 02:18:37 +00004772 isValidSubjectOfNSAttribute(QT);
John McCalled433932011-01-25 03:31:58 +00004773}
4774
George Karpenkov1657f362018-11-30 02:18:37 +00004775static bool isValidSubjectOfOSAttribute(QualType QT) {
4776 return QT->isDependentType() || QT->isPointerType();
John McCall3b5a8f52016-03-03 00:10:03 +00004777}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004778
George Karpenkov1657f362018-11-30 02:18:37 +00004779void Sema::AddXConsumedAttr(Decl *D, SourceRange SR, unsigned SpellingIndex,
4780 RetainOwnershipKind K,
4781 bool IsTemplateInstantiation) {
4782 ValueDecl *VD = cast<ValueDecl>(D);
4783 switch (K) {
4784 case RetainOwnershipKind::OS:
4785 handleSimpleAttributeWithCheck<OSConsumedAttr>(
4786 *this, VD, SR, SpellingIndex, &isValidSubjectOfOSAttribute,
4787 diag::warn_ns_attribute_wrong_parameter_type,
4788 /*ExtraArgs=*/SR, "os_consumed", /*pointers*/ 1);
4789 return;
4790 case RetainOwnershipKind::NS:
4791 handleSimpleAttributeWithCheck<NSConsumedAttr>(
4792 *this, VD, SR, SpellingIndex, &isValidSubjectOfNSAttribute,
John McCall3b5a8f52016-03-03 00:10:03 +00004793
George Karpenkov1657f362018-11-30 02:18:37 +00004794 // These attributes are normally just advisory, but in ARC, ns_consumed
4795 // is significant. Allow non-dependent code to contain inappropriate
4796 // attributes even in ARC, but require template instantiations to be
4797 // set up correctly.
4798 ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount)
4799 ? diag::err_ns_attribute_wrong_parameter_type
4800 : diag::warn_ns_attribute_wrong_parameter_type),
4801 /*ExtraArgs=*/SR, "ns_consumed", /*objc pointers*/ 0);
4802 return;
4803 case RetainOwnershipKind::CF:
4804 handleSimpleAttributeWithCheck<CFConsumedAttr>(
4805 *this, VD, SR, SpellingIndex,
4806 &isValidSubjectOfCFAttribute,
4807 diag::warn_ns_attribute_wrong_parameter_type,
4808 /*ExtraArgs=*/SR, "cf_consumed", /*pointers*/1);
John McCalled433932011-01-25 03:31:58 +00004809 return;
4810 }
George Karpenkov1657f362018-11-30 02:18:37 +00004811}
John McCalled433932011-01-25 03:31:58 +00004812
George Karpenkov1657f362018-11-30 02:18:37 +00004813static Sema::RetainOwnershipKind
4814parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) {
4815 switch (AL.getKind()) {
4816 case ParsedAttr::AT_CFConsumed:
4817 return Sema::RetainOwnershipKind::CF;
4818 case ParsedAttr::AT_OSConsumed:
4819 return Sema::RetainOwnershipKind::OS;
4820 case ParsedAttr::AT_NSConsumed:
4821 return Sema::RetainOwnershipKind::NS;
4822 default:
4823 llvm_unreachable("Wrong argument supplied");
4824 }
John McCalled433932011-01-25 03:31:58 +00004825}
4826
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004827bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4828 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004829 return false;
4830
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004831 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4832 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004833 return true;
4834}
4835
George Karpenkov1657f362018-11-30 02:18:37 +00004836static void handleXReturnsXRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004837 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004838 QualType ReturnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004839
George Karpenkov1657f362018-11-30 02:18:37 +00004840 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004841 ReturnType = MD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00004842 } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
4843 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) {
John McCall31168b02011-06-15 23:02:42 +00004844 return; // ignore: was handled as a type attribute
George Karpenkov1657f362018-11-30 02:18:37 +00004845 } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004846 ReturnType = PD->getType();
George Karpenkov1657f362018-11-30 02:18:37 +00004847 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004848 ReturnType = FD->getReturnType();
George Karpenkov1657f362018-11-30 02:18:37 +00004849 } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
4850 // Attributes on parameters are used for out-parameters,
4851 // passed as pointers-to-pointers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004852 ReturnType = Param->getType()->getPointeeType();
4853 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004854 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
George Karpenkov1657f362018-11-30 02:18:37 +00004855 << AL << /*pointer-to-CF-pointer*/ 2 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004856 return;
4857 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004858 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00004859 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004860 } else {
4861 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004862 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004863 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004864 case ParsedAttr::AT_NSReturnsRetained:
4865 case ParsedAttr::AT_NSReturnsAutoreleased:
4866 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004867 case ParsedAttr::AT_OSReturnsRetained:
4868 case ParsedAttr::AT_OSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004869 ExpectedDeclKind = ExpectedFunctionOrMethod;
4870 break;
4871
Erich Keanee891aa92018-07-13 15:07:47 +00004872 case ParsedAttr::AT_CFReturnsRetained:
4873 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004874 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4875 break;
4876 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004877 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004878 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004879 return;
4880 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004881
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004882 bool TypeOK;
4883 bool Cf;
4884 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004885 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004886 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004887 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
4888 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004889 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004890
4891 case ParsedAttr::AT_NSReturnsAutoreleased:
4892 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004893 TypeOK = isValidSubjectOfNSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004894 Cf = false;
John McCalled433932011-01-25 03:31:58 +00004895 break;
4896
Erich Keanee891aa92018-07-13 15:07:47 +00004897 case ParsedAttr::AT_CFReturnsRetained:
4898 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004899 TypeOK = isValidSubjectOfCFAttribute(ReturnType);
4900 Cf = true;
4901 break;
4902
4903 case ParsedAttr::AT_OSReturnsRetained:
4904 case ParsedAttr::AT_OSReturnsNotRetained:
4905 TypeOK = isValidSubjectOfOSAttribute(ReturnType);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004906 Cf = true;
John McCalled433932011-01-25 03:31:58 +00004907 break;
4908 }
4909
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004910 if (!TypeOK) {
4911 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00004912 return;
4913
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004914 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004915 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004916 << AL << /*pointer-to-CF*/ 2 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004917 } else {
4918 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4919 enum : unsigned {
4920 Function,
4921 Method,
4922 Property
4923 } SubjectKind = Function;
4924 if (isa<ObjCMethodDecl>(D))
4925 SubjectKind = Method;
4926 else if (isa<ObjCPropertyDecl>(D))
4927 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004928 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004929 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004930 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004931 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004932 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004933
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004934 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004935 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004936 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004937 case ParsedAttr::AT_NSReturnsAutoreleased:
George Karpenkov1657f362018-11-30 02:18:37 +00004938 handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL);
John McCalled433932011-01-25 03:31:58 +00004939 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004940 case ParsedAttr::AT_CFReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004941 handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00004942 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004943 case ParsedAttr::AT_NSReturnsNotRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004944 handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenekd9c66632010-02-18 00:05:45 +00004945 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004946 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004947 handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004948 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004949 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00004950 handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL);
4951 return;
4952 case ParsedAttr::AT_OSReturnsRetained:
4953 handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL);
4954 return;
4955 case ParsedAttr::AT_OSReturnsNotRetained:
4956 handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004957 return;
4958 };
4959}
4960
John McCallcf166702011-07-22 08:53:00 +00004961static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004962 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004963 const int EP_ObjCMethod = 1;
4964 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00004965
Erich Keaneb11ebc52017-09-27 03:20:13 +00004966 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004967 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004968 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004969 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004970 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004971 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004972
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004973 if (!resultType->isReferenceType() &&
4974 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004975 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004976 << SourceRange(loc) << Attrs
4977 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
4978 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004979
4980 // Drop the attribute.
4981 return;
4982 }
4983
Nico Weber462fd1e2015-01-07 23:50:05 +00004984 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00004985 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004986}
4987
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004988static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004989 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004990 const auto *Method = cast<ObjCMethodDecl>(D);
4991
4992 const DeclContext *DC = Method->getDeclContext();
4993 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004994 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00004995 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004996 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4997 return;
4998 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004999 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005000 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00005001 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005002 return;
5003 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005004
5005 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
5006 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005007}
5008
Erich Keanee891aa92018-07-13 15:07:47 +00005009static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005010 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00005011
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005012 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005013 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005014 return;
5015 }
John McCall28592582015-02-01 22:34:06 +00005016
5017 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005018 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00005019 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005020 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00005021 return;
5022 }
5023
5024 // Only allow 'cv void *'.
5025 QualType T = TD->getUnderlyingType();
5026 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005027 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00005028 return;
5029 }
5030 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005031
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005032 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005033 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
5034 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005035}
5036
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005037static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005038 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005039 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00005040
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005041 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005042 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005043 return;
5044 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005045
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005046 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005047 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
5048 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005049}
5050
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005051static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005052 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005053 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005054 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005055 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005056 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005057 return;
5058 }
5059 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005060 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005061 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005062 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005063 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005064 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005065 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005066 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005067}
5068
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005069static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005070 const ParsedAttr &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005071 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005072 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00005073 IFace = CatDecl->getClassInterface();
5074 else
5075 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005076
5077 if (!IFace)
5078 return;
5079
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00005080 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00005081 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005082 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
5083 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005084}
5085
Erich Keanee891aa92018-07-13 15:07:47 +00005086static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005087 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005088 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005089 return;
5090 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005091 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00005092 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005093 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005094}
5095
Nico Webera6916892016-06-10 18:53:04 +00005096// When a user wants to use objc_boxable with a union or struct
5097// but they don't have access to the declaration (legacy/third-party code)
5098// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00005099// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00005100static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00005101 bool notify = false;
5102
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005103 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00005104 if (RD && RD->getDefinition()) {
5105 RD = RD->getDefinition();
5106 notify = true;
5107 }
5108
5109 if (RD) {
5110 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005111 ObjCBoxableAttr(AL.getRange(), S.Context,
5112 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00005113 RD->addAttr(BoxableAttr);
5114 if (notify) {
5115 // we need to notify ASTReader/ASTWriter about
5116 // modification of existing declaration
5117 if (ASTMutationListener *L = S.getASTMutationListener())
5118 L->AddedAttributeToRecord(BoxableAttr, RD);
5119 }
5120 }
5121}
5122
Erich Keanee891aa92018-07-13 15:07:47 +00005123static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005124 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005125
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005126 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005127 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005128}
5129
Chandler Carruthedc2c642011-07-02 00:01:44 +00005130static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005131 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005132 const auto *VD = cast<ValueDecl>(D);
5133 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005134
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005135 if (!QT->isDependentType() &&
5136 !QT->isObjCLifetimeType()) {
5137 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5138 << QT;
John McCall31168b02011-06-15 23:02:42 +00005139 return;
5140 }
5141
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005142 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005143
5144 // If we have no lifetime yet, check the lifetime we're presumably
5145 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005146 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5147 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005148
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005149 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005150 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005151 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005152 "didn't infer lifetime for non-dependent type?");
5153 break;
5154
5155 case Qualifiers::OCL_Weak: // meaningful
5156 case Qualifiers::OCL_Strong: // meaningful
5157 break;
5158
5159 case Qualifiers::OCL_ExplicitNone:
5160 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005161 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5162 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005163 break;
5164 }
5165
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005166 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005167 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
5168 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00005169}
5170
Francois Picheta83957a2010-12-19 06:50:37 +00005171//===----------------------------------------------------------------------===//
5172// Microsoft specific attribute handlers.
5173//===----------------------------------------------------------------------===//
5174
Nico Weber88f5ed92016-09-13 18:55:26 +00005175UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
5176 unsigned AttrSpellingListIndex, StringRef Uuid) {
5177 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005178 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005179 return nullptr;
5180 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5181 Diag(Range.getBegin(), diag::note_previous_uuid);
5182 D->dropAttr<UuidAttr>();
5183 }
5184
5185 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5186}
5187
Erich Keanee891aa92018-07-13 15:07:47 +00005188static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005189 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005190 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005191 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005192 return;
5193 }
5194
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005195 StringRef StrRef;
5196 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005197 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005198 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005199
David Majnemer89085342013-08-09 08:56:20 +00005200 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5201 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005202 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5203 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005204
Reid Kleckner140c4a72013-05-17 14:04:52 +00005205 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005206 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005207 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005208 return;
5209 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005210
David Majnemer89085342013-08-09 08:56:20 +00005211 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005212 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005213 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005214 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005215 return;
5216 }
David Majnemer89085342013-08-09 08:56:20 +00005217 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005218 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005219 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005220 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005221 }
Francois Picheta83957a2010-12-19 06:50:37 +00005222
Nico Weber469891e2017-05-05 17:05:56 +00005223 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5224 // the only thing in the [] list, the [] too), and add an insertion of
5225 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5226 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005227 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005228 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005229 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5230 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005231
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005232 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5233 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005234 if (UA)
5235 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005236}
5237
Erich Keanee891aa92018-07-13 15:07:47 +00005238static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005239 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005240 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005241 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005242 return;
5243 }
5244 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005245 D, AL.getRange(), /*BestCase=*/true,
5246 AL.getAttributeSpellingListIndex(),
5247 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005248 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005249 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005250 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5251 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005252}
5253
Erich Keanee891aa92018-07-13 15:07:47 +00005254static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005255 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005256 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005257 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005258 return;
5259 }
5260 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005261 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005262 return;
5263 }
5264 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005265 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005266 return;
5267 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005268 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5269 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005270}
5271
Erich Keanee891aa92018-07-13 15:07:47 +00005272static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005273 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005274 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005275 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005276 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005277 return;
5278 Tags.push_back(Tag);
5279 }
5280
5281 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5282 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005283 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005284 return;
5285 }
5286 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005287 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005288 return;
5289 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005290 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005291 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005292 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005293 return;
5294
5295 // Store tags sorted and without duplicates.
Fangrui Song55fab262018-09-26 22:16:28 +00005296 llvm::sort(Tags);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005297 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5298
5299 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005300 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5301 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005302}
5303
Erich Keanee891aa92018-07-13 15:07:47 +00005304static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005305 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005306 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005307 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005308 return;
5309 }
5310
5311 StringRef Str;
5312 SourceLocation ArgLoc;
5313
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005314 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005315 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005316 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005317 return;
5318
5319 ARMInterruptAttr::InterruptType Kind;
5320 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005321 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5322 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005323 return;
5324 }
5325
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005326 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005327 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005328 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005329}
5330
Erich Keanee891aa92018-07-13 15:07:47 +00005331static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005332 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005333 return;
5334
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005335 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005336 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5337 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005338 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005339 }
5340
5341 // FIXME: Check for decl - it should be void ()(void).
5342
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005343 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005344 llvm::APSInt NumParams(32);
5345 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005346 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005347 << AL << AANT_ArgumentIntegerConstant
5348 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005349 return;
5350 }
5351
5352 unsigned Num = NumParams.getLimitedValue(255);
5353 if ((Num & 1) || Num > 30) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005354 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005355 << AL << (int)NumParams.getSExtValue()
5356 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005357 return;
5358 }
5359
Aaron Ballman36a53502014-01-16 13:03:14 +00005360 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005361 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5362 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005363 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005364}
5365
Erich Keanee891aa92018-07-13 15:07:47 +00005366static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005367 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005368 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005369 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005370 return;
5371 }
5372
5373 StringRef Str;
5374 SourceLocation ArgLoc;
5375
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005376 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005377 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005378 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005379 return;
5380
5381 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5382 // a) Must be a function.
5383 // b) Must have no parameters.
5384 // c) Must have the 'void' return type.
5385 // d) Cannot have the 'mips16' attribute, as that instruction set
5386 // lacks the 'eret' instruction.
5387 // e) The attribute itself must either have no argument or one of the
5388 // valid interrupt types, see [MipsInterruptDocs].
5389
5390 if (!isFunctionOrMethod(D)) {
5391 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5392 << "'interrupt'" << ExpectedFunctionOrMethod;
5393 return;
5394 }
5395
5396 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5397 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5398 << 0;
5399 return;
5400 }
5401
5402 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5403 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5404 << 1;
5405 return;
5406 }
5407
Erich Keane44bacdf2018-08-09 13:21:32 +00005408 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005409 return;
5410
5411 MipsInterruptAttr::InterruptType Kind;
5412 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005413 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005414 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005415 return;
5416 }
5417
5418 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005419 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005420}
5421
Erich Keanee891aa92018-07-13 15:07:47 +00005422static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005423 // Semantic checks for a function with the 'interrupt' attribute.
5424 // a) Must be a function.
5425 // b) Must have the 'void' return type.
5426 // c) Must take 1 or 2 arguments.
5427 // d) The 1st argument must be a pointer.
5428 // e) The 2nd argument (if any) must be an unsigned integer.
5429 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5430 CXXMethodDecl::isStaticOverloadedOperator(
5431 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005432 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005433 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005434 return;
5435 }
5436 // Interrupt handler must have void return type.
5437 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5438 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5439 diag::err_anyx86_interrupt_attribute)
5440 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5441 ? 0
5442 : 1)
5443 << 0;
5444 return;
5445 }
5446 // Interrupt handler must have 1 or 2 parameters.
5447 unsigned NumParams = getFunctionOrMethodNumParams(D);
5448 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005449 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005450 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5451 ? 0
5452 : 1)
5453 << 1;
5454 return;
5455 }
5456 // The first argument must be a pointer.
5457 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5458 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5459 diag::err_anyx86_interrupt_attribute)
5460 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5461 ? 0
5462 : 1)
5463 << 2;
5464 return;
5465 }
5466 // The second argument, if present, must be an unsigned integer.
5467 unsigned TypeSize =
5468 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5469 ? 64
5470 : 32;
5471 if (NumParams == 2 &&
5472 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5473 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5474 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5475 diag::err_anyx86_interrupt_attribute)
5476 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5477 ? 0
5478 : 1)
5479 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5480 return;
5481 }
5482 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005483 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005484 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5485}
5486
Erich Keanee891aa92018-07-13 15:07:47 +00005487static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005488 if (!isFunctionOrMethod(D)) {
5489 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5490 << "'interrupt'" << ExpectedFunction;
5491 return;
5492 }
5493
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005494 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005495 return;
5496
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005497 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005498}
5499
Erich Keanee891aa92018-07-13 15:07:47 +00005500static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005501 if (!isFunctionOrMethod(D)) {
5502 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5503 << "'signal'" << ExpectedFunction;
5504 return;
5505 }
5506
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005507 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005508 return;
5509
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005510 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005511}
5512
Ana Pazos1eee1b72018-07-26 17:37:45 +00005513
5514static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5515 const ParsedAttr &AL) {
5516 // Warn about repeated attributes.
5517 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5518 S.Diag(AL.getRange().getBegin(),
5519 diag::warn_riscv_repeated_interrupt_attribute);
5520 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5521 return;
5522 }
5523
5524 // Check the attribute argument. Argument is optional.
5525 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5526 return;
5527
5528 StringRef Str;
5529 SourceLocation ArgLoc;
5530
5531 // 'machine'is the default interrupt mode.
5532 if (AL.getNumArgs() == 0)
5533 Str = "machine";
5534 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5535 return;
5536
5537 // Semantic checks for a function with the 'interrupt' attribute:
5538 // - Must be a function.
5539 // - Must have no parameters.
5540 // - Must have the 'void' return type.
5541 // - The attribute itself must either have no argument or one of the
5542 // valid interrupt types, see [RISCVInterruptDocs].
5543
5544 if (D->getFunctionType() == nullptr) {
5545 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5546 << "'interrupt'" << ExpectedFunction;
5547 return;
5548 }
5549
5550 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5551 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 0;
5552 return;
5553 }
5554
5555 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5556 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 1;
5557 return;
5558 }
5559
5560 RISCVInterruptAttr::InterruptType Kind;
5561 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005562 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5563 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005564 return;
5565 }
5566
5567 D->addAttr(::new (S.Context) RISCVInterruptAttr(
5568 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
5569}
5570
Erich Keanee891aa92018-07-13 15:07:47 +00005571static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005572 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005573 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5574 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005575 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005576 break;
5577 case llvm::Triple::mipsel:
5578 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005579 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005580 break;
5581 case llvm::Triple::x86:
5582 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005583 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005584 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005585 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005586 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005587 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005588 case llvm::Triple::riscv32:
5589 case llvm::Triple::riscv64:
5590 handleRISCVInterruptAttr(S, D, AL);
5591 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005592 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005593 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005594 break;
5595 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005596}
5597
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005598static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005599 const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005600 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005601 Expr *MinExpr = AL.getArgAsExpr(0);
5602 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005603 return;
5604
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005605 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005606 Expr *MaxExpr = AL.getArgAsExpr(1);
5607 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005608 return;
5609
5610 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005611 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005612 return;
5613 }
5614 if (Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005615 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005616 return;
5617 }
5618
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005619 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005620 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5621 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005622}
5623
Erich Keanee891aa92018-07-13 15:07:47 +00005624static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005625 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005626 Expr *MinExpr = AL.getArgAsExpr(0);
5627 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005628 return;
5629
5630 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005631 if (AL.getNumArgs() == 2) {
5632 Expr *MaxExpr = AL.getArgAsExpr(1);
5633 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005634 return;
5635 }
5636
5637 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005638 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005639 return;
5640 }
5641 if (Max != 0 && Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005642 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005643 return;
5644 }
5645
5646 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005647 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5648 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005649}
5650
Erich Keanee891aa92018-07-13 15:07:47 +00005651static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005652 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005653 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5654 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005655 return;
5656
5657 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005658 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5659 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005660}
5661
Erich Keanee891aa92018-07-13 15:07:47 +00005662static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005663 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005664 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5665 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005666 return;
5667
5668 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005669 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5670 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005671}
5672
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005673static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005674 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005675 // If we try to apply it to a function pointer, don't warn, but don't
5676 // do anything, either. It doesn't matter anyway, because there's nothing
5677 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005678 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005679 if (VD && VD->getType()->isFunctionPointerType())
5680 return;
5681 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005682 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005683 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5684 TD->getUnderlyingType()->isFunctionType()))
5685 return;
5686 // Attribute can only be applied to function types.
5687 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005688 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005689 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005690 return;
5691 }
5692
Aaron Ballman36a53502014-01-16 13:03:14 +00005693 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005694 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5695 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005696}
5697
Erich Keanee891aa92018-07-13 15:07:47 +00005698static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005699 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005700 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5701 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005702 return;
5703
5704 // TODO: Investigate what happens with the next major version of MSVC.
Reid Kleckner1a94d872018-12-17 23:16:43 +00005705 if (Version != LangOptions::MSVC2015 / 100) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005706 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005707 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005708 return;
5709 }
5710
Reid Kleckner1a94d872018-12-17 23:16:43 +00005711 // The attribute expects a "major" version number like 19, but new versions of
5712 // MSVC have moved to updating the "minor", or less significant numbers, so we
5713 // have to multiply by 100 now.
5714 Version *= 100;
5715
David Majnemercd3ebfe2016-05-23 17:16:12 +00005716 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005717 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5718 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005719}
5720
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005721DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5722 unsigned AttrSpellingListIndex) {
5723 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005724 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005725 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005726 }
5727
5728 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005729 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005730
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005731 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005732}
5733
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005734DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5735 unsigned AttrSpellingListIndex) {
5736 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005737 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005738 D->dropAttr<DLLImportAttr>();
5739 }
5740
5741 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005742 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005743
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005744 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005745}
5746
Erich Keanee891aa92018-07-13 15:07:47 +00005747static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005748 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5749 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005750 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00005751 return;
5752 }
5753
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005754 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00005755 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005756 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5757 // MinGW doesn't allow dllimport on inline functions.
5758 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00005759 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005760 return;
5761 }
5762 }
5763
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005764 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005765 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5766 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005767 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00005768 return;
5769 }
5770 }
5771
Hans Wennborge82f19c2014-06-24 23:57:05 +00005772 unsigned Index = A.getAttributeSpellingListIndex();
Erich Keanee891aa92018-07-13 15:07:47 +00005773 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Hans Wennborge82f19c2014-06-24 23:57:05 +00005774 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5775 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005776 if (NewAttr)
5777 D->addAttr(NewAttr);
5778}
5779
David Majnemer2c4e00a2014-01-29 22:07:36 +00005780MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00005781Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00005782 unsigned AttrSpellingListIndex,
5783 MSInheritanceAttr::Spelling SemanticSpelling) {
5784 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
5785 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00005786 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005787 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
5788 << 1 /*previous declaration*/;
5789 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
5790 D->dropAttr<MSInheritanceAttr>();
5791 }
5792
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005793 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005794 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00005795 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
5796 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005797 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005798 }
5799 } else {
5800 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
5801 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5802 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005803 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005804 }
5805 if (RD->getDescribedClassTemplate()) {
5806 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5807 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005808 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005809 }
5810 }
5811
5812 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00005813 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005814}
5815
Erich Keanee891aa92018-07-13 15:07:47 +00005816static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005817 // The capability attributes take a single string parameter for the name of
5818 // the capability they represent. The lockable attribute does not take any
5819 // parameters. However, semantically, both attributes represent the same
5820 // concept, and so they use the same semantic attribute. Eventually, the
5821 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00005822 //
Alp Toker958027b2014-07-14 19:42:55 +00005823 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00005824 // literal will be considered a "mutex."
5825 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005826 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00005827 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005828 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005829 return;
5830
Aaron Ballman6c810072014-03-05 21:47:13 +00005831 // Currently, there are only two names allowed for a capability: role and
5832 // mutex (case insensitive). Diagnose other capability names.
5833 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
5834 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
5835
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005836 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
5837 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005838}
5839
Erich Keanee891aa92018-07-13 15:07:47 +00005840static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00005841 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005842 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00005843 return;
5844
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005845 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00005846 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005847 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005848}
5849
5850static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005851 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005852 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005853 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005854 return;
5855
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005856 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005857 S.Context,
5858 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005859 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005860}
5861
5862static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005863 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005864 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005865 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005866 return;
5867
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005868 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005869 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005870 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005871 Args.data(),
5872 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005873 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005874}
5875
5876static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005877 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005878 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005879 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005880 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005881
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005882 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005883 AL.getRange(), S.Context, Args.data(), Args.size(),
5884 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005885}
5886
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005887static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005888 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005889 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005890 return;
5891
5892 // check that all arguments are lockable objects
5893 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005894 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005895 if (Args.empty())
5896 return;
5897
5898 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005899 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
5900 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005901
5902 D->addAttr(RCA);
5903}
5904
Erich Keanee891aa92018-07-13 15:07:47 +00005905static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005906 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005907 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005908 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00005909 // Do not want to attach the attribute to the namespace because that will
5910 // cause confusing diagnostic reports for uses of declarations within the
5911 // namespace.
5912 return;
5913 }
5914 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005915
Manman Renc7890fe2016-03-16 18:50:49 +00005916 // Handle the cases where the attribute has a text message.
5917 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005918 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
5919 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00005920 return;
5921
5922 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005923 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
5924 checkAttributeAtMostNumArgs(S, AL, 1);
5925 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
Sander de Smalen44a22532018-11-26 16:38:37 +00005926 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
5927 return;
5928
5929 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
5930 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
5931
5932 D->addAttr(::new (S.Context)
5933 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005934 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005935}
5936
5937static bool isGlobalVar(const Decl *D) {
5938 if (const auto *S = dyn_cast<VarDecl>(D))
5939 return S->hasGlobalStorage();
5940 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00005941}
5942
Erich Keanee891aa92018-07-13 15:07:47 +00005943static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005944 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00005945 return;
5946
Benjamin Kramer1b582012016-02-13 18:11:49 +00005947 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00005948
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005949 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00005950 StringRef SanitizerName;
5951 SourceLocation LiteralLoc;
5952
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005953 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00005954 return;
5955
5956 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
5957 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005958 else if (isGlobalVar(D) && SanitizerName != "address")
5959 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005960 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00005961 Sanitizers.push_back(SanitizerName);
5962 }
5963
5964 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005965 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
5966 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005967}
5968
5969static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005970 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005971 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00005972 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005973 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
5974 .Case("no_address_safety_analysis", "address")
5975 .Case("no_sanitize_address", "address")
5976 .Case("no_sanitize_thread", "thread")
5977 .Case("no_sanitize_memory", "memory");
5978 if (isGlobalVar(D) && SanitizerName != "address")
5979 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005980 << AL << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00005981 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005982 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
5983 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005984}
5985
Erich Keanee891aa92018-07-13 15:07:47 +00005986static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005987 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005988 D->addAttr(Internal);
5989}
5990
Erich Keanee891aa92018-07-13 15:07:47 +00005991static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005992 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005993 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00005994 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005995 else
Erich Keane44bacdf2018-08-09 13:21:32 +00005996 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
5997 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005998}
5999
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006000/// Handles semantic checking for features that are common to all attributes,
6001/// such as checking whether a parameter was properly specified, or the correct
6002/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006003static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006004 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006005 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00006006 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006007 //
6008 // We also bail on unknown and ignored attributes because those are handled
6009 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00006010 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006011 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006012 // Check whether the attribute requires specific language extensions to be
6013 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006014 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00006015 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00006016 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006017 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00006018 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006019 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00006020 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006021
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006022 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006023 // If there are no optional arguments, then checking for the argument count
6024 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006025 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006026 return true;
6027 } else {
6028 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006029 if (AL.getMinArgs() &&
6030 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006031 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006032 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
6033 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00006034 return true;
6035 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00006036
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006037 if (S.CheckAttrTarget(AL))
6038 return true;
6039
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006040 return false;
6041}
6042
Erich Keanee891aa92018-07-13 15:07:47 +00006043static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006044 if (D->isInvalidDecl())
6045 return;
6046
6047 // Check if there is only one access qualifier.
6048 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00006049 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
6050 AL.getSemanticSpelling()) {
6051 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
6052 << AL.getName()->getName() << AL.getRange();
6053 } else {
6054 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
6055 << D->getSourceRange();
6056 D->setInvalidDecl(true);
6057 return;
6058 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00006059 }
6060
6061 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
6062 // image object can be read and written.
6063 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
6064 // object. Using the read_write (or __read_write) qualifier with the pipe
6065 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006066 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006067 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006068 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00006069 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006070 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00006071 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00006072 D->setInvalidDecl(true);
6073 return;
6074 }
6075 }
6076 }
6077
6078 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006079 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00006080}
6081
Erik Pilkington5a559e62018-08-21 17:24:06 +00006082static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006083 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00006084 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
6085 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
6086 return;
6087 }
6088
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006089 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00006090 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00006091 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00006092 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00006093}
6094
JF Bastien14daa202018-12-18 05:12:21 +00006095static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6096 assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic &&
6097 "uninitialized is only valid on automatic duration variables");
6098 unsigned Index = AL.getAttributeSpellingListIndex();
6099 D->addAttr(::new (S.Context)
6100 UninitializedAttr(AL.getLoc(), S.Context, Index));
6101}
6102
Erik Pilkington1e368822019-01-04 18:33:06 +00006103static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD,
6104 bool DiagnoseFailure) {
6105 QualType Ty = VD->getType();
6106 if (!Ty->isObjCRetainableType()) {
6107 if (DiagnoseFailure) {
6108 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6109 << 0;
6110 }
6111 return false;
6112 }
6113
6114 Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime();
6115
6116 // Sema::inferObjCARCLifetime must run after processing decl attributes
6117 // (because __block lowers to an attribute), so if the lifetime hasn't been
6118 // explicitly specified, infer it locally now.
6119 if (LifetimeQual == Qualifiers::OCL_None)
6120 LifetimeQual = Ty->getObjCARCImplicitLifetime();
6121
6122 // The attributes only really makes sense for __strong variables; ignore any
6123 // attempts to annotate a parameter with any other lifetime qualifier.
6124 if (LifetimeQual != Qualifiers::OCL_Strong) {
6125 if (DiagnoseFailure) {
6126 S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6127 << 1;
6128 }
6129 return false;
6130 }
6131
6132 // Tampering with the type of a VarDecl here is a bit of a hack, but we need
6133 // to ensure that the variable is 'const' so that we can error on
6134 // modification, which can otherwise over-release.
6135 VD->setType(Ty.withConst());
6136 VD->setARCPseudoStrong(true);
6137 return true;
6138}
6139
6140static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D,
6141 const ParsedAttr &AL) {
6142 if (auto *VD = dyn_cast<VarDecl>(D)) {
6143 assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically");
6144 if (!VD->hasLocalStorage()) {
6145 S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained)
6146 << 0;
6147 return;
6148 }
6149
6150 if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true))
6151 return;
6152
6153 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6154 return;
6155 }
6156
6157 // If D is a function-like declaration (method, block, or function), then we
6158 // make every parameter psuedo-strong.
6159 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); I != E; ++I) {
6160 auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I));
6161 QualType Ty = PVD->getType();
6162
6163 // If a user wrote a parameter with __strong explicitly, then assume they
6164 // want "real" strong semantics for that parameter. This works because if
6165 // the parameter was written with __strong, then the strong qualifier will
6166 // be non-local.
6167 if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() ==
6168 Qualifiers::OCL_Strong)
6169 continue;
6170
6171 tryMakeVariablePseudoStrong(S, PVD, /*DiagnoseFailure=*/false);
6172 }
6173 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
6174}
6175
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00006176//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006177// Top Level Sema Entry Points
6178//===----------------------------------------------------------------------===//
6179
Richard Smithf8a75c32013-08-29 00:47:48 +00006180/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
6181/// the attribute applies to decls. If the attribute is a type attribute, just
6182/// silently ignore it if a GNU attribute.
6183static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00006184 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00006185 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00006186 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00006187 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00006188
Richard Smithf8a75c32013-08-29 00:47:48 +00006189 // Ignore C++11 attributes on declarator chunks: they appertain to the type
6190 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006191 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00006192 return;
6193
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006194 // Unknown attributes are automatically warned on. Target-specific attributes
6195 // which do not apply to the current target architecture are treated as
6196 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006197 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006198 !AL.existsInTarget(S.Context.getTargetInfo())) {
Simon Pilgrimfc0ff612018-12-17 12:17:37 +00006199 S.Diag(AL.getLoc(),
6200 AL.isDeclspecAttribute()
6201 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
6202 : (unsigned)diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006203 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006204 return;
6205 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006206
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006207 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00006208 return;
6209
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006210 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00006211 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006212 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00006213 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006214 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00006215 break;
6216 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006217 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00006218 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006219 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006220 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006221 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006222 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006223 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006224 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006225 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006226 case ParsedAttr::AT_DLLExport:
6227 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006228 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006229 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006230 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006231 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006232 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006233 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006234 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006235 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006236 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006237 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006238 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006239 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006240 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006241 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006242 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006243 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006244 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006245 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006246 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006247 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006248 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006249 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006250 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006251 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006252 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006253 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006254 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006255 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006256 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006257 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006258 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006259 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006260 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006261 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006262 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006263 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006264 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006265 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006266 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006267 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006268 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006269 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006270 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006271 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006272 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006273 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006274 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006275 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006276 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006277 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006278 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006279 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006280 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006281 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006282 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006283 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006284 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006285 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006286 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006287 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006288 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006289 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006290 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006291 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006292 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006293 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006294 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006295 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006296 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006297 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006298 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006299 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006300 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006301 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006302 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006303 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006304 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006305 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006306 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006307 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006308 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006309 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006310 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006311 case ParsedAttr::AT_CPUDispatch:
6312 case ParsedAttr::AT_CPUSpecific:
6313 handleCPUSpecificAttr(S, D, AL);
6314 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006315 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006316 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006317 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006318 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006319 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006320 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006321 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006322 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006323 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006324 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006325 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006326 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006327 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006328 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006329 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006330 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006331 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006332 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006333 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006334 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006335 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006336 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006337 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006338 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006339 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006340 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006341 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006342 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006343 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006344 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006345 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006346 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006347 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006348 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006349 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006350 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006351 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006352 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006353 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006354 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006355 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006356 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006357 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006358 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006359 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006360 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006361 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006362 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006363 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006364 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006365 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006366 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006367 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006368 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006369 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006370 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006371 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006372 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006373 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006374 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006375 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006376 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006377 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006378 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006379 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006380 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006381 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006382 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006383 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006384 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006385 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006386 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006387 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006388 case ParsedAttr::AT_LifetimeBound:
6389 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6390 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006391 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006392 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006393 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006394 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006395 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006396 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006397 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006398 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006399 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006400 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006401 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006402 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006403 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006404 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006405 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006406 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006407 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6408 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006409 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006410 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006411 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006412 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006413 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006414 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006415 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006416 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006417 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006418 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006419 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006420 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006421 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006422 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006423 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006424 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006425 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006426 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006427 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006428 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006429 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006430 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006431 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006432 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006433 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006434 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006435 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006436 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006437 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006438 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006439 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006440 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006441 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006442 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006443 handleNoCfCheckAttr(S, D, AL);
6444 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006445 case ParsedAttr::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006446 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006447 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006448 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006449 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006450 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006451 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006452 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006453 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006454 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006455 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006456 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006457 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006458 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006459 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006460 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006461 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006462 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006463 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006464 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006465 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006466 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006467 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006468 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006469 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006470 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006471 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006472 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006473 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006474 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006475 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006476 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006477 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006478 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006479 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006480 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006481 case ParsedAttr::AT_ObjCRuntimeVisible:
6482 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6483 break;
6484 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006485 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006486 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006487 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006488 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6489 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006490 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006491 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006492 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6493 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006494 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006495 case ParsedAttr::AT_CFConsumed:
6496 case ParsedAttr::AT_NSConsumed:
George Karpenkov1657f362018-11-30 02:18:37 +00006497 case ParsedAttr::AT_OSConsumed:
6498 S.AddXConsumedAttr(D, AL.getRange(), AL.getAttributeSpellingListIndex(),
6499 parsedAttrToRetainOwnershipKind(AL),
6500 /*IsTemplateInstantiation=*/false);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006501 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006502 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006503 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006504 break;
George Karpenkovda2c77f2018-12-06 22:06:59 +00006505 case ParsedAttr::AT_OSConsumesThis:
6506 handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL);
6507 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006508 case ParsedAttr::AT_NSReturnsAutoreleased:
6509 case ParsedAttr::AT_NSReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006510 case ParsedAttr::AT_NSReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006511 case ParsedAttr::AT_CFReturnsNotRetained:
Erich Keanee891aa92018-07-13 15:07:47 +00006512 case ParsedAttr::AT_CFReturnsRetained:
George Karpenkov1657f362018-11-30 02:18:37 +00006513 case ParsedAttr::AT_OSReturnsNotRetained:
6514 case ParsedAttr::AT_OSReturnsRetained:
6515 handleXReturnsXRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006516 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006517 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006518 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006519 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006520 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006521 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006522 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006523 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006524 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006525 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006526 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006527 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006528 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006529 case ParsedAttr::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006530 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006531 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006532 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006533 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006534 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006535 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006536 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006537 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006538 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006539 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006540 break;
Zola Bridgescbac3ad2018-11-27 19:56:46 +00006541 case ParsedAttr::AT_SpeculativeLoadHardening:
6542 handleSimpleAttribute<SpeculativeLoadHardeningAttr>(S, D, AL);
6543 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006544 case ParsedAttr::AT_CodeSeg:
6545 handleCodeSegAttr(S, D, AL);
6546 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006547 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006548 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006549 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006550 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006551 handleMinVectorWidthAttr(S, D, AL);
6552 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006553 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006554 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006555 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006556 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006557 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006558 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006559 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006560 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006561 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006562 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006563 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006564 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006565 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006566 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006567 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006568 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006569 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006570 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006571 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006572 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006573 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006574 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006575 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006576 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006577 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006578 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6579 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006580 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006581 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00006582 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
6583 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006584 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006585 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006586 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006587 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006588 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006589 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006590 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006591 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006592 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006593 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006594 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006595 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006596 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006597 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006598 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006599 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006600 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006601 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006602 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006603 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006604 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006605 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006606 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006607 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006608 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006609 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006610 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006611 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006612 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006613 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006614 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006615 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006616 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006617 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006618 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006619 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006620 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006621 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006622 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006623 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006624 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006625 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006626 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006627 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006628 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006629 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006630 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006631 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006632 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006633 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006634 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006635 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006636 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006637 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006638 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006639 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006640 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006641 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006642 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006643 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006644 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006645 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006646 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006647 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006648 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006649 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006650 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006651 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006652 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006653 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006654 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00006655 // Interacts with -fstack-protector options.
6656 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
6657 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006658 case ParsedAttr::AT_StdCall:
6659 case ParsedAttr::AT_CDecl:
6660 case ParsedAttr::AT_FastCall:
6661 case ParsedAttr::AT_ThisCall:
6662 case ParsedAttr::AT_Pascal:
6663 case ParsedAttr::AT_RegCall:
6664 case ParsedAttr::AT_SwiftCall:
6665 case ParsedAttr::AT_VectorCall:
6666 case ParsedAttr::AT_MSABI:
6667 case ParsedAttr::AT_SysVABI:
6668 case ParsedAttr::AT_Pcs:
6669 case ParsedAttr::AT_IntelOclBicc:
6670 case ParsedAttr::AT_PreserveMost:
6671 case ParsedAttr::AT_PreserveAll:
Sander de Smalen44a22532018-11-26 16:38:37 +00006672 case ParsedAttr::AT_AArch64VectorPcs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006673 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006674 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006675 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006676 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006677 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006678 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006679 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006680 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006681 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006682 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006683 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006684 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006685 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006686 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006687 case ParsedAttr::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006688 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006689 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006690 case ParsedAttr::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006691 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006692 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006693 case ParsedAttr::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006694 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006695 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006696 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006697 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006698 break;
Louis Dionned2695792018-10-04 15:49:42 +00006699 case ParsedAttr::AT_ExcludeFromExplicitInstantiation:
6700 handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL);
6701 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006702 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006703 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006704 break;
John McCall8d32c052012-05-22 21:28:12 +00006705
6706 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006707 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006708 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006709 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006710 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006711 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006712 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006713 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006714 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006715 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006716 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006717 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00006718 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006719 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006720 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00006721 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006722 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006723 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00006724 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006725 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006726 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006727 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006728 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006729 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006730 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006731 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006732 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006733 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00006734
Erich Keanee891aa92018-07-13 15:07:47 +00006735 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006736 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006737 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006738
6739 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006740 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006741 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006742 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006743 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006744 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006745 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006746 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006747 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006748 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006749 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006750 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006751 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006752 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006753 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006754 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006755 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006756 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00006757 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006758 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006759 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00006760 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006761 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006762 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00006763 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006764 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006765 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006766 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006767 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006768 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006769 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006770 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006771 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006772 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006773 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006774 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006775 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006776 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006777 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006778 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006779 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006780 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006781 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006782 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006783 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006784 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006785 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006786 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006787 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006788
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006789 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006790 case ParsedAttr::AT_Capability:
6791 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006792 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006793 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006794 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006795 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006796 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006797
Erich Keanee891aa92018-07-13 15:07:47 +00006798 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006799 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006800 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006801 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006802 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006803 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006804 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006805 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006806 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006807 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006808 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006809 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006810
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00006811 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006812 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006813 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006814 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006815 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006816 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006817 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006818 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006819 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006820 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006821 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006822 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006823 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006824 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006825 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00006826 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006827 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006828 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006829 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006830 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006831 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006832 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006833 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006834 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006835 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006836
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006837 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006838 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006839 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006840 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006841 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006842 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006843 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006844 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006845 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006846 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006847 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006848 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00006849 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006850 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006851 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006852 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006853 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006854 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006855 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006856 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00006857
6858 // Move semantics attribute.
6859 case ParsedAttr::AT_Reinitializes:
6860 handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
6861 break;
Erik Pilkington5a559e62018-08-21 17:24:06 +00006862
6863 case ParsedAttr::AT_AlwaysDestroy:
6864 case ParsedAttr::AT_NoDestroy:
6865 handleDestroyAttr(S, D, AL);
6866 break;
JF Bastien14daa202018-12-18 05:12:21 +00006867
6868 case ParsedAttr::AT_Uninitialized:
6869 handleUninitializedAttr(S, D, AL);
6870 break;
Erik Pilkington1e368822019-01-04 18:33:06 +00006871
6872 case ParsedAttr::AT_ObjCExternallyRetained:
6873 handleObjCExternallyRetainedAttr(S, D, AL);
6874 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006875 }
6876}
6877
6878/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
6879/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00006880void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00006881 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00006882 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00006883 if (AttrList.empty())
6884 return;
6885
Erich Keanee891aa92018-07-13 15:07:47 +00006886 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00006887 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00006888
Joey Gouly2cd9db12013-12-13 16:15:28 +00006889 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00006890 // GCC accepts
6891 // static int a9 __attribute__((weakref));
6892 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00006893 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00006894 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
6895 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00006896 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00006897 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006898 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006899
Aaron Ballmanbe243a72014-12-04 22:45:31 +00006900 // FIXME: We should be able to handle this in TableGen as well. It would be
6901 // good to have a way to specify "these attributes must appear as a group",
6902 // for these. Additionally, it would be good to have a way to specify "these
6903 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00006904 if (!D->hasAttr<OpenCLKernelAttr>()) {
6905 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006906 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006907 // FIXME: This emits a different error message than
6908 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00006909 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006910 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006911 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006912 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006913 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006914 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006915 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006916 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006917 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006918 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
6919 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00006920 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
6921 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
6922 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6923 << A << ExpectedKernelFunction;
6924 D->setInvalidDecl();
6925 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
6926 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6927 << A << ExpectedKernelFunction;
6928 D->setInvalidDecl();
6929 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
6930 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6931 << A << ExpectedKernelFunction;
6932 D->setInvalidDecl();
6933 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
6934 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6935 << A << ExpectedKernelFunction;
6936 D->setInvalidDecl();
6937 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006938 }
6939 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006940}
6941
Hiroshi Inoue939d9322017-06-30 05:40:31 +00006942// Helper for delayed processing TransparentUnion attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006943void Sema::ProcessDeclAttributeDelayed(Decl *D,
6944 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006945 for (const ParsedAttr &AL : AttrList)
6946 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00006947 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00006948 break;
6949 }
6950}
6951
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006952// Annotation attributes are the only attributes allowed after an access
6953// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00006954bool Sema::ProcessAccessDeclAttributeList(
6955 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006956 for (const ParsedAttr &AL : AttrList) {
6957 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00006958 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006959 } else {
Erich Keanec480f302018-07-12 21:09:05 +00006960 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006961 return true;
6962 }
6963 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006964 return false;
6965}
6966
John McCall42856de2011-10-01 05:17:03 +00006967/// checkUnusedDeclAttributes - Check a list of attributes to see if it
6968/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00006969static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00006970 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00006971 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006972 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
6973 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00006974 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00006975 continue;
John McCall42856de2011-10-01 05:17:03 +00006976
Erich Keanee891aa92018-07-13 15:07:47 +00006977 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00006978 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006979 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006980 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00006981 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
6982 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006983 }
6984 }
6985}
6986
6987/// checkUnusedDeclAttributes - Given a declarator which is not being
6988/// used to build a declaration, complain about any decl attributes
6989/// which might be lying around on it.
6990void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00006991 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00006992 ::checkUnusedDeclAttributes(*this, D.getAttributes());
6993 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
6994 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
6995}
6996
Ryan Flynn7d470f32009-07-30 03:15:39 +00006997/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00006998/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00006999NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
7000 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00007001 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00007002 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007003 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00007004 FunctionDecl *NewFD;
7005 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00007006 // FIXME: Mangling?
7007 // FIXME: Is the qualifier info correct?
7008 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00007009 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
7010 Loc, Loc, DeclarationName(II),
7011 FD->getType(), FD->getTypeSourceInfo(),
7012 SC_None, false/*isInlineSpecified*/,
7013 FD->hasPrototype(),
7014 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007015 NewD = NewFD;
7016
7017 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00007018 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00007019
7020 // Fake up parameter variables; they are declared as if this were
7021 // a typedef.
7022 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007023 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00007024 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00007025 for (const auto &AI : FT->param_types()) {
7026 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00007027 Param->setScopeInfo(0, Params.size());
7028 Params.push_back(Param);
7029 }
David Blaikie9c70e042011-09-21 18:16:56 +00007030 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00007031 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007032 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00007033 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00007034 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00007035 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00007036 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007037 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00007038 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00007039 }
7040 return NewD;
7041}
7042
James Dennett634962f2012-06-14 21:40:34 +00007043/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00007044/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00007045void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00007046 if (W.getUsed()) return; // only do this once
7047 W.setUsed(true);
7048 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
7049 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00007050 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00007051 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
7052 W.getLocation()));
7053 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00007054 WeakTopLevelDecl.push_back(NewD);
7055 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
7056 // to insert Decl at TU scope, sorry.
7057 DeclContext *SavedContext = CurContext;
7058 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00007059 NewD->setDeclContext(CurContext);
7060 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00007061 PushOnScopeChains(NewD, S);
7062 CurContext = SavedContext;
7063 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00007064 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00007065 }
7066}
7067
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007068void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
7069 // It's valid to "forward-declare" #pragma weak, in which case we
7070 // have to do this.
7071 LoadExternalWeakUndeclaredIdentifiers();
7072 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00007073 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007074 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007075 if (VD->isExternC())
7076 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007077 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007078 if (FD->isExternC())
7079 ND = FD;
7080 if (ND) {
7081 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00007082 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00007083 if (I != WeakUndeclaredIdentifiers.end()) {
7084 WeakInfo W = I->second;
7085 DeclApplyPragmaWeak(S, ND, W);
7086 WeakUndeclaredIdentifiers[Id] = W;
7087 }
7088 }
7089 }
7090 }
7091}
7092
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007093/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
7094/// it, apply them to D. This is a bit tricky because PD can have attributes
7095/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00007096void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007097 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00007098 if (!PD.getDeclSpec().getAttributes().empty())
7099 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00007100
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007101 // Walk the declarator structure, applying decl attributes that were in a type
7102 // position to the decl itself. This handles cases like:
7103 // int *__attr__(x)** D;
7104 // when X is a decl attribute.
7105 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00007106 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
7107 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00007108
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007109 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00007110 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00007111
7112 // Apply additional attributes specified by '#pragma clang attribute'.
7113 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00007114}
John McCall28a6aea2009-11-04 02:18:39 +00007115
John McCall31168b02011-06-15 23:02:42 +00007116/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00007117/// If so, it'll still be annotated with an attribute that makes it
7118/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007119static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00007120 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00007121 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00007122 // Private ivars are always okay. Unfortunately, people don't
7123 // always properly make their ivars private, even in system headers.
7124 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007125 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
7126 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00007127 return false;
7128
John McCallc6af8c62015-10-28 05:03:19 +00007129 // Silently accept unsupported uses of __weak in both user and system
7130 // declarations when it's been disabled, for ease of integration with
7131 // -fno-objc-arc files. We do have to take some care against attempts
7132 // to define such things; for now, we've only done that for ivars
7133 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007134 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00007135 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
7136 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
7137 reason = UnavailableAttr::IR_ForbiddenWeak;
7138 return true;
7139 }
John McCallb61e14e2015-10-27 04:54:50 +00007140 }
7141
John McCallc6af8c62015-10-28 05:03:19 +00007142 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007143 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00007144 // Currently, all the failures dealt with this way are due to ARC
7145 // restrictions.
7146 reason = UnavailableAttr::IR_ARCForbiddenType;
7147 return true;
John McCallb61e14e2015-10-27 04:54:50 +00007148 }
7149
7150 return false;
John McCall31168b02011-06-15 23:02:42 +00007151}
7152
7153/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007154static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
7155 Decl *D) {
7156 auto Reason = UnavailableAttr::IR_None;
7157 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
7158 assert(Reason && "didn't set reason?");
7159 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00007160 return;
7161 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00007162 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007163 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00007164 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007165 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007166 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007167 DD.getForbiddenTypeDiagnostic() ==
7168 diag::err_arc_array_param_no_ownership) {
7169 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00007170 return;
7171 }
7172 }
John McCall31168b02011-06-15 23:02:42 +00007173
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007174 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
7175 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
7176 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00007177}
7178
Manman Ren45b1ab12016-05-06 19:57:16 +00007179static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
7180 const Decl *D) {
7181 // Check each AvailabilityAttr to find the one for this platform.
7182 for (const auto *A : D->attrs()) {
7183 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
7184 // FIXME: this is copied from CheckAvailability. We should try to
7185 // de-duplicate.
7186
7187 // Check if this is an App Extension "platform", and if so chop off
7188 // the suffix for matching with the actual platform.
7189 StringRef ActualPlatform = Avail->getPlatform()->getName();
7190 StringRef RealizedPlatform = ActualPlatform;
7191 if (Context.getLangOpts().AppExt) {
7192 size_t suffix = RealizedPlatform.rfind("_app_extension");
7193 if (suffix != StringRef::npos)
7194 RealizedPlatform = RealizedPlatform.slice(0, suffix);
7195 }
7196
7197 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
7198
7199 // Match the platform name.
7200 if (RealizedPlatform == TargetPlatform)
7201 return Avail;
7202 }
7203 }
7204 return nullptr;
7205}
7206
Erik Pilkington9f866a72017-07-18 20:32:07 +00007207/// The diagnostic we should emit for \c D, and the declaration that
7208/// originated it, or \c AR_Available.
7209///
7210/// \param D The declaration to check.
7211/// \param Message If non-null, this will be populated with the message from
7212/// the availability attribute that is selected.
Erik Pilkington42578572018-09-10 22:20:09 +00007213/// \param ClassReceiver If we're checking the the method of a class message
7214/// send, the class. Otherwise nullptr.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007215static std::pair<AvailabilityResult, const NamedDecl *>
Erik Pilkington42578572018-09-10 22:20:09 +00007216ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D,
7217 std::string *Message,
7218 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007219 AvailabilityResult Result = D->getAvailability(Message);
7220
7221 // For typedefs, if the typedef declaration appears available look
7222 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007223 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007224 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007225 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007226 D = TT->getDecl();
7227 Result = D->getAvailability(Message);
7228 continue;
7229 }
7230 }
7231 break;
7232 }
7233
7234 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007235 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007236 if (IDecl->getDefinition()) {
7237 D = IDecl->getDefinition();
7238 Result = D->getAvailability(Message);
7239 }
7240 }
7241
7242 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
7243 if (Result == AR_Available) {
7244 const DeclContext *DC = ECD->getDeclContext();
7245 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
7246 Result = TheEnumDecl->getAvailability(Message);
7247 D = TheEnumDecl;
7248 }
7249 }
7250
Erik Pilkington42578572018-09-10 22:20:09 +00007251 // For +new, infer availability from -init.
7252 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7253 if (S.NSAPIObj && ClassReceiver) {
7254 ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod(
7255 S.NSAPIObj->getInitSelector());
7256 if (Init && Result == AR_Available && MD->isClassMethod() &&
7257 MD->getSelector() == S.NSAPIObj->getNewSelector() &&
7258 MD->definedInNSObject(S.getASTContext())) {
7259 Result = Init->getAvailability(Message);
7260 D = Init;
7261 }
7262 }
7263 }
7264
Erik Pilkington9f866a72017-07-18 20:32:07 +00007265 return {Result, D};
7266}
7267
7268
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007269/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007270/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
7271/// in a deprecated context, but not the other way around.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007272static bool
7273ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
7274 VersionTuple DeclVersion, Decl *Ctx,
7275 const NamedDecl *OffendingDecl) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007276 assert(K != AR_Available && "Expected an unavailable declaration here!");
7277
7278 // Checks if we should emit the availability diagnostic in the context of C.
7279 auto CheckContext = [&](const Decl *C) {
7280 if (K == AR_NotYetIntroduced) {
7281 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
7282 if (AA->getIntroduced() >= DeclVersion)
7283 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007284 } else if (K == AR_Deprecated) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007285 if (C->isDeprecated())
7286 return true;
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007287 } else if (K == AR_Unavailable) {
7288 // It is perfectly fine to refer to an 'unavailable' Objective-C method
7289 // when it's actually defined and is referenced from within the
7290 // @implementation itself. In this context, we interpret unavailable as a
7291 // form of access control.
7292 if (const auto *MD = dyn_cast<ObjCMethodDecl>(OffendingDecl)) {
7293 if (const auto *Impl = dyn_cast<ObjCImplDecl>(C)) {
7294 if (MD->getClassInterface() == Impl->getClassInterface() &&
7295 MD->isDefined())
7296 return true;
7297 }
7298 }
7299 }
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007300
7301 if (C->isUnavailable())
7302 return true;
7303 return false;
7304 };
7305
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007306 do {
7307 if (CheckContext(Ctx))
7308 return false;
7309
7310 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00007311 // Unless it is "+load" method.
7312 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
7313 if (MethodD->isClassMethod() &&
7314 MethodD->getSelector().getAsString() == "load")
7315 return true;
7316
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007317 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007318 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
7319 if (CheckContext(Interface))
7320 return false;
7321 }
7322 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007323 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007324 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
7325 if (CheckContext(Interface))
7326 return false;
7327 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
7328
7329 return true;
7330}
7331
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007332static bool
7333shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
7334 const VersionTuple &DeploymentVersion,
7335 const VersionTuple &DeclVersion) {
7336 const auto &Triple = Context.getTargetInfo().getTriple();
7337 VersionTuple ForceAvailabilityFromVersion;
7338 switch (Triple.getOS()) {
7339 case llvm::Triple::IOS:
7340 case llvm::Triple::TvOS:
7341 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7342 break;
7343 case llvm::Triple::WatchOS:
7344 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7345 break;
7346 case llvm::Triple::Darwin:
7347 case llvm::Triple::MacOSX:
7348 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7349 break;
7350 default:
7351 // New targets should always warn about availability.
7352 return Triple.getVendor() == llvm::Triple::Apple;
7353 }
7354 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7355 DeclVersion >= ForceAvailabilityFromVersion;
7356}
7357
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007358static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7359 for (Decl *Ctx = OrigCtx; Ctx;
7360 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7361 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7362 return cast<NamedDecl>(Ctx);
7363 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7364 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7365 return Imp->getClassInterface();
7366 return CD;
7367 }
7368 }
7369
7370 return dyn_cast<NamedDecl>(OrigCtx);
7371}
7372
Alex Lorenz727c21e2017-07-26 13:58:02 +00007373namespace {
7374
7375struct AttributeInsertion {
7376 StringRef Prefix;
7377 SourceLocation Loc;
7378 StringRef Suffix;
7379
7380 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007381 return {" ", D->getEndLoc(), ""};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007382 }
7383 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7384 return {" ", Loc, ""};
7385 }
7386 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007387 return {"", D->getBeginLoc(), "\n"};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007388 }
7389};
7390
7391} // end anonymous namespace
7392
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007393/// Tries to parse a string as ObjC method name.
7394///
7395/// \param Name The string to parse. Expected to originate from availability
7396/// attribute argument.
7397/// \param SlotNames The vector that will be populated with slot names. In case
7398/// of unsuccessful parsing can contain invalid data.
7399/// \returns A number of method parameters if parsing was successful, None
7400/// otherwise.
7401static Optional<unsigned>
7402tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
7403 const LangOptions &LangOpts) {
7404 // Accept replacements starting with - or + as valid ObjC method names.
7405 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
7406 Name = Name.drop_front(1);
7407 if (Name.empty())
7408 return None;
7409 Name.split(SlotNames, ':');
7410 unsigned NumParams;
7411 if (Name.back() == ':') {
7412 // Remove an empty string at the end that doesn't represent any slot.
7413 SlotNames.pop_back();
7414 NumParams = SlotNames.size();
7415 } else {
7416 if (SlotNames.size() != 1)
7417 // Not a valid method name, just a colon-separated string.
7418 return None;
7419 NumParams = 0;
7420 }
7421 // Verify all slot names are valid.
7422 bool AllowDollar = LangOpts.DollarIdents;
7423 for (StringRef S : SlotNames) {
7424 if (S.empty())
7425 continue;
7426 if (!isValidIdentifier(S, AllowDollar))
7427 return None;
7428 }
7429 return NumParams;
7430}
7431
Alex Lorenz727c21e2017-07-26 13:58:02 +00007432/// Returns a source location in which it's appropriate to insert a new
7433/// attribute for the given declaration \D.
7434static Optional<AttributeInsertion>
7435createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7436 const LangOptions &LangOpts) {
7437 if (isa<ObjCPropertyDecl>(D))
7438 return AttributeInsertion::createInsertionAfter(D);
7439 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7440 if (MD->hasBody())
7441 return None;
7442 return AttributeInsertion::createInsertionAfter(D);
7443 }
7444 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7445 SourceLocation Loc =
7446 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7447 if (Loc.isInvalid())
7448 return None;
7449 // Insert after the 'struct'/whatever keyword.
7450 return AttributeInsertion::createInsertionAfter(Loc);
7451 }
7452 return AttributeInsertion::createInsertionBefore(D);
7453}
7454
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007455/// Actually emit an availability diagnostic for a reference to an unavailable
7456/// decl.
7457///
7458/// \param Ctx The context that the reference occurred in
7459/// \param ReferringDecl The exact declaration that was referenced.
7460/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007461/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007462/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7463/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7464/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007465static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007466 Decl *Ctx, const NamedDecl *ReferringDecl,
7467 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007468 StringRef Message,
7469 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007470 const ObjCInterfaceDecl *UnknownObjCClass,
7471 const ObjCPropertyDecl *ObjCProperty,
7472 bool ObjCPropertyAccess) {
7473 // Diagnostics for deprecated or unavailable.
7474 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007475 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007476 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007477
7478 // Matches 'diag::note_property_attribute' options.
7479 unsigned property_note_select;
7480
7481 // Matches diag::note_availability_specified_here.
7482 unsigned available_here_select_kind;
7483
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007484 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007485 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007486 DeclVersion = AA->getIntroduced();
7487
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007488 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx,
7489 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007490 return;
7491
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007492 SourceLocation Loc = Locs.front();
7493
Erik Pilkington8b352c42017-08-14 19:49:12 +00007494 // The declaration can have multiple availability attributes, we are looking
7495 // at one of them.
7496 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7497 if (A && A->isInherited()) {
7498 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7499 Redecl = Redecl->getPreviousDecl()) {
7500 const AvailabilityAttr *AForRedecl =
7501 getAttrForPlatform(S.Context, Redecl);
7502 if (AForRedecl && !AForRedecl->isInherited()) {
7503 // If D is a declaration with inherited attributes, the note should
7504 // point to the declaration with actual attributes.
7505 NoteLocation = Redecl->getLocation();
7506 break;
7507 }
7508 }
7509 }
7510
Aaron Ballmanfb237522014-10-15 15:37:51 +00007511 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007512 case AR_NotYetIntroduced: {
7513 // We would like to emit the diagnostic even if -Wunguarded-availability is
7514 // not specified for deployment targets >= to iOS 11 or equivalent or
7515 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7516 // later.
7517 const AvailabilityAttr *AA =
7518 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7519 VersionTuple Introduced = AA->getIntroduced();
7520
7521 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7522 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7523 Introduced);
7524 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7525 : diag::warn_unguarded_availability;
7526
7527 S.Diag(Loc, Warning)
7528 << OffendingDecl
7529 << AvailabilityAttr::getPrettyPlatformName(
7530 S.getASTContext().getTargetInfo().getPlatformName())
7531 << Introduced.getAsString();
7532
7533 S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here)
7534 << OffendingDecl << /* partial */ 3;
7535
7536 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007537 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007538 if (TD->getDeclName().isEmpty()) {
7539 S.Diag(TD->getLocation(),
7540 diag::note_decl_unguarded_availability_silence)
7541 << /*Anonymous*/ 1 << TD->getKindName();
7542 return;
7543 }
7544 auto FixitNoteDiag =
7545 S.Diag(Enclosing->getLocation(),
7546 diag::note_decl_unguarded_availability_silence)
7547 << /*Named*/ 0 << Enclosing;
7548 // Don't offer a fixit for declarations with availability attributes.
7549 if (Enclosing->hasAttr<AvailabilityAttr>())
7550 return;
7551 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7552 return;
7553 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7554 Enclosing, S.getSourceManager(), S.getLangOpts());
7555 if (!Insertion)
7556 return;
7557 std::string PlatformName =
7558 AvailabilityAttr::getPlatformNameSourceSpelling(
7559 S.getASTContext().getTargetInfo().getPlatformName())
7560 .lower();
7561 std::string Introduced =
7562 OffendingDecl->getVersionIntroduced().getAsString();
7563 FixitNoteDiag << FixItHint::CreateInsertion(
7564 Insertion->Loc,
7565 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7566 "(" + Introduced + "))" + Insertion->Suffix)
7567 .str());
7568 }
7569 return;
7570 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007571 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007572 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7573 : diag::warn_property_method_deprecated;
7574 diag_message = diag::warn_deprecated_message;
7575 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7576 property_note_select = /* deprecated */ 0;
7577 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007578 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7579 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007580 break;
7581
Erik Pilkington796a3e22016-08-05 22:59:03 +00007582 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007583 diag = !ObjCPropertyAccess ? diag::err_unavailable
7584 : diag::err_property_method_unavailable;
7585 diag_message = diag::err_unavailable_message;
7586 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7587 property_note_select = /* unavailable */ 1;
7588 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007589
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007590 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7591 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007592 // Most of these failures are due to extra restrictions in ARC;
7593 // reflect that in the primary diagnostic when applicable.
7594 auto flagARCError = [&] {
7595 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007596 S.getSourceManager().isInSystemHeader(
7597 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007598 diag = diag::err_unavailable_in_arc;
7599 };
7600
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007601 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007602 case UnavailableAttr::IR_None: break;
7603
7604 case UnavailableAttr::IR_ARCForbiddenType:
7605 flagARCError();
7606 diag_available_here = diag::note_arc_forbidden_type;
7607 break;
7608
7609 case UnavailableAttr::IR_ForbiddenWeak:
7610 if (S.getLangOpts().ObjCWeakRuntime)
7611 diag_available_here = diag::note_arc_weak_disabled;
7612 else
7613 diag_available_here = diag::note_arc_weak_no_runtime;
7614 break;
7615
7616 case UnavailableAttr::IR_ARCForbiddenConversion:
7617 flagARCError();
7618 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7619 break;
7620
7621 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7622 flagARCError();
7623 diag_available_here = diag::note_arc_init_returns_unrelated;
7624 break;
7625
7626 case UnavailableAttr::IR_ARCFieldWithOwnership:
7627 flagARCError();
7628 diag_available_here = diag::note_arc_field_with_ownership;
7629 break;
7630 }
7631 }
John McCallb61e14e2015-10-27 04:54:50 +00007632 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007633 break;
7634
Erik Pilkington796a3e22016-08-05 22:59:03 +00007635 case AR_Available:
7636 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007637 }
7638
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007639 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007640 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007641 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007642 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7643 Replacement = AL->getReplacement();
7644 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7645 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007646
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007647 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00007648 if (!Replacement.empty())
7649 UseRange =
7650 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007651 if (UseRange.isValid()) {
7652 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
7653 Selector Sel = MethodDecl->getSelector();
7654 SmallVector<StringRef, 12> SelectorSlotNames;
7655 Optional<unsigned> NumParams = tryParseObjCMethodName(
7656 Replacement, SelectorSlotNames, S.getLangOpts());
7657 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
7658 assert(SelectorSlotNames.size() == Locs.size());
7659 for (unsigned I = 0; I < Locs.size(); ++I) {
7660 if (!Sel.getNameForSlot(I).empty()) {
7661 CharSourceRange NameRange = CharSourceRange::getCharRange(
7662 Locs[I], S.getLocForEndOfToken(Locs[I]));
7663 FixIts.push_back(FixItHint::CreateReplacement(
7664 NameRange, SelectorSlotNames[I]));
7665 } else
7666 FixIts.push_back(
7667 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
7668 }
7669 } else
7670 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7671 } else
7672 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7673 }
Manman Renc7890fe2016-03-16 18:50:49 +00007674 }
7675
Aaron Ballmanfb237522014-10-15 15:37:51 +00007676 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007677 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007678 if (ObjCProperty)
7679 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7680 << ObjCProperty->getDeclName() << property_note_select;
7681 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007682 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007683 if (ObjCProperty)
7684 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7685 << ObjCProperty->getDeclName() << property_note_select;
7686 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007687 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007688 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7689 }
7690
Erik Pilkington8b352c42017-08-14 19:49:12 +00007691 S.Diag(NoteLocation, diag_available_here)
7692 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007693}
7694
7695static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
7696 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007697 assert(DD.Kind == DelayedDiagnostic::Availability &&
7698 "Expected an availability diagnostic here");
7699
Aaron Ballmanfb237522014-10-15 15:37:51 +00007700 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00007701 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007702 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007703 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
7704 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
7705 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00007706}
7707
John McCall2ec85372012-05-07 06:16:41 +00007708void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7709 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007710 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007711 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007712
John McCall2ec85372012-05-07 06:16:41 +00007713 // When delaying diagnostics to run in the context of a parsed
7714 // declaration, we only want to actually emit anything if parsing
7715 // succeeds.
7716 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007717
John McCall2ec85372012-05-07 06:16:41 +00007718 // We emit all the active diagnostics in this pool or any of its
7719 // parents. In general, we'll get one pool for the decl spec
7720 // and a child pool for each declarator; in a decl group like:
7721 // deprecated_typedef foo, *bar, baz();
7722 // only the declarator pops will be passed decls. This is correct;
7723 // we really do need to consider delayed diagnostics from the decl spec
7724 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007725 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007726 do {
Richard Smith5c9b3b72018-09-25 22:12:44 +00007727 bool AnyAccessFailures = false;
John McCall6347b682012-05-07 06:16:58 +00007728 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007729 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7730 // This const_cast is a bit lame. Really, Triggered should be mutable.
7731 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007732 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007733 continue;
7734
John McCallc1465822011-02-14 07:13:47 +00007735 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007736 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007737 // Don't bother giving deprecation/unavailable diagnostics if
7738 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007739 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00007740 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00007741 break;
7742
7743 case DelayedDiagnostic::Access:
Richard Smith5c9b3b72018-09-25 22:12:44 +00007744 // Only produce one access control diagnostic for a structured binding
7745 // declaration: we don't need to tell the user that all the fields are
7746 // inaccessible one at a time.
7747 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
7748 continue;
John McCall2ec85372012-05-07 06:16:41 +00007749 HandleDelayedAccessCheck(diag, decl);
Richard Smith5c9b3b72018-09-25 22:12:44 +00007750 if (diag.Triggered)
7751 AnyAccessFailures = true;
John McCall86121512010-01-27 03:50:35 +00007752 break;
John McCall31168b02011-06-15 23:02:42 +00007753
7754 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007755 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007756 break;
John McCall86121512010-01-27 03:50:35 +00007757 }
7758 }
John McCall2ec85372012-05-07 06:16:41 +00007759 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007760}
7761
John McCall6347b682012-05-07 06:16:58 +00007762/// Given a set of delayed diagnostics, re-emit them as if they had
7763/// been delayed in the current context instead of in the given pool.
7764/// Essentially, this just moves them to the current pool.
7765void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7766 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7767 assert(curPool && "re-emitting in undelayed context not supported");
7768 curPool->steal(pool);
7769}
7770
Erik Pilkington9f866a72017-07-18 20:32:07 +00007771static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
7772 const NamedDecl *ReferringDecl,
7773 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007774 StringRef Message,
7775 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007776 const ObjCInterfaceDecl *UnknownObjCClass,
7777 const ObjCPropertyDecl *ObjCProperty,
7778 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00007779 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007780 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
7781 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007782 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007783 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007784 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00007785 return;
7786 }
7787
Erik Pilkington9f866a72017-07-18 20:32:07 +00007788 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
7789 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007790 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007791 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00007792}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00007793
Erik Pilkington5cd57172016-08-16 17:44:11 +00007794namespace {
7795
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007796/// Returns true if the given statement can be a body-like child of \p Parent.
7797bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
7798 switch (Parent->getStmtClass()) {
7799 case Stmt::IfStmtClass:
7800 return cast<IfStmt>(Parent)->getThen() == S ||
7801 cast<IfStmt>(Parent)->getElse() == S;
7802 case Stmt::WhileStmtClass:
7803 return cast<WhileStmt>(Parent)->getBody() == S;
7804 case Stmt::DoStmtClass:
7805 return cast<DoStmt>(Parent)->getBody() == S;
7806 case Stmt::ForStmtClass:
7807 return cast<ForStmt>(Parent)->getBody() == S;
7808 case Stmt::CXXForRangeStmtClass:
7809 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
7810 case Stmt::ObjCForCollectionStmtClass:
7811 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
7812 case Stmt::CaseStmtClass:
7813 case Stmt::DefaultStmtClass:
7814 return cast<SwitchCase>(Parent)->getSubStmt() == S;
7815 default:
7816 return false;
7817 }
7818}
7819
7820class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
7821 const Stmt *Target;
7822
7823public:
7824 bool VisitStmt(Stmt *S) { return S != Target; }
7825
7826 /// Returns true if the given statement is present in the given declaration.
7827 static bool isContained(const Stmt *Target, const Decl *D) {
7828 StmtUSEFinder Visitor;
7829 Visitor.Target = Target;
7830 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
7831 }
7832};
7833
7834/// Traverses the AST and finds the last statement that used a given
7835/// declaration.
7836class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
7837 const Decl *D;
7838
7839public:
7840 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7841 if (DRE->getDecl() == D)
7842 return false;
7843 return true;
7844 }
7845
7846 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
7847 const CompoundStmt *Scope) {
7848 LastDeclUSEFinder Visitor;
7849 Visitor.D = D;
7850 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
7851 const Stmt *S = *I;
7852 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
7853 return S;
7854 }
7855 return nullptr;
7856 }
7857};
7858
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007859/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00007860///
7861/// This is done with a traversal of the AST of a function that makes reference
7862/// to a partially available declaration. Whenever we encounter an \c if of the
7863/// form: \c if(@available(...)), we use the version from the condition to visit
7864/// the then statement.
7865class DiagnoseUnguardedAvailability
7866 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
7867 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
7868
7869 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007870 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007871
7872 /// Stack of potentially nested 'if (@available(...))'s.
7873 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007874 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007875
Erik Pilkington42578572018-09-10 22:20:09 +00007876 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range,
7877 ObjCInterfaceDecl *ClassReceiver = nullptr);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007878
7879public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007880 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
7881 : SemaRef(SemaRef), Ctx(Ctx) {
7882 AvailabilityStack.push_back(
7883 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007884 }
7885
Alex Lorenz6f911122017-05-16 13:58:53 +00007886 bool TraverseDecl(Decl *D) {
7887 // Avoid visiting nested functions to prevent duplicate warnings.
7888 if (!D || isa<FunctionDecl>(D))
7889 return true;
7890 return Base::TraverseDecl(D);
7891 }
7892
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007893 bool TraverseStmt(Stmt *S) {
7894 if (!S)
7895 return true;
7896 StmtStack.push_back(S);
7897 bool Result = Base::TraverseStmt(S);
7898 StmtStack.pop_back();
7899 return Result;
7900 }
7901
Erik Pilkington5cd57172016-08-16 17:44:11 +00007902 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
7903
7904 bool TraverseIfStmt(IfStmt *If);
7905
Alex Lorenz6f911122017-05-16 13:58:53 +00007906 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
7907
Erik Pilkingtonba87c622017-08-18 20:20:56 +00007908 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
7909 // to any useful diagnostics.
7910 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
7911
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007912 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
7913 if (PRE->isClassReceiver())
7914 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
7915 return true;
7916 }
7917
Erik Pilkington5cd57172016-08-16 17:44:11 +00007918 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
Erik Pilkington42578572018-09-10 22:20:09 +00007919 if (ObjCMethodDecl *D = Msg->getMethodDecl()) {
7920 ObjCInterfaceDecl *ID = nullptr;
7921 QualType ReceiverTy = Msg->getClassReceiver();
7922 if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType())
7923 ID = ReceiverTy->getAsObjCInterfaceType()->getInterface();
7924
Erik Pilkington5cd57172016-08-16 17:44:11 +00007925 DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00007926 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID);
7927 }
Erik Pilkington5cd57172016-08-16 17:44:11 +00007928 return true;
7929 }
7930
7931 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7932 DiagnoseDeclAvailability(DRE->getDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007933 SourceRange(DRE->getBeginLoc(), DRE->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00007934 return true;
7935 }
7936
7937 bool VisitMemberExpr(MemberExpr *ME) {
7938 DiagnoseDeclAvailability(ME->getMemberDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007939 SourceRange(ME->getBeginLoc(), ME->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00007940 return true;
7941 }
7942
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007943 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007944 SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use)
Erik Pilkingtonfa983902018-10-30 20:31:30 +00007945 << (!SemaRef.getLangOpts().ObjC);
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007946 return true;
7947 }
7948
Erik Pilkington5cd57172016-08-16 17:44:11 +00007949 bool VisitTypeLoc(TypeLoc Ty);
7950};
7951
7952void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
Erik Pilkington42578572018-09-10 22:20:09 +00007953 NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007954 AvailabilityResult Result;
7955 const NamedDecl *OffendingDecl;
7956 std::tie(Result, OffendingDecl) =
Erik Pilkington42578572018-09-10 22:20:09 +00007957 ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007958 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007959 // All other diagnostic kinds have already been handled in
7960 // DiagnoseAvailabilityOfDecl.
7961 if (Result != AR_NotYetIntroduced)
7962 return;
7963
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007964 const AvailabilityAttr *AA =
7965 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007966 VersionTuple Introduced = AA->getIntroduced();
7967
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007968 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007969 return;
7970
7971 // If the context of this function is less available than D, we should not
7972 // emit a diagnostic.
Alex Lorenz4e3c0bd2019-01-09 22:31:37 +00007973 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx,
7974 OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007975 return;
7976
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007977 // We would like to emit the diagnostic even if -Wunguarded-availability is
7978 // not specified for deployment targets >= to iOS 11 or equivalent or
7979 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7980 // later.
7981 unsigned DiagKind =
7982 shouldDiagnoseAvailabilityByDefault(
7983 SemaRef.Context,
7984 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
7985 ? diag::warn_unguarded_availability_new
7986 : diag::warn_unguarded_availability;
7987
7988 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington5cd57172016-08-16 17:44:11 +00007989 << Range << D
7990 << AvailabilityAttr::getPrettyPlatformName(
7991 SemaRef.getASTContext().getTargetInfo().getPlatformName())
7992 << Introduced.getAsString();
7993
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007994 SemaRef.Diag(OffendingDecl->getLocation(),
7995 diag::note_availability_specified_here)
7996 << OffendingDecl << /* partial */ 3;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007997
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007998 auto FixitDiag =
7999 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
8000 << Range << D
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008001 << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0
8002 : /*__builtin_available*/ 1);
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008003
8004 // Find the statement which should be enclosed in the if @available check.
8005 if (StmtStack.empty())
8006 return;
8007 const Stmt *StmtOfUse = StmtStack.back();
8008 const CompoundStmt *Scope = nullptr;
8009 for (const Stmt *S : llvm::reverse(StmtStack)) {
8010 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
8011 Scope = CS;
8012 break;
8013 }
8014 if (isBodyLikeChildStmt(StmtOfUse, S)) {
8015 // The declaration won't be seen outside of the statement, so we don't
8016 // have to wrap the uses of any declared variables in if (@available).
8017 // Therefore we can avoid setting Scope here.
8018 break;
8019 }
8020 StmtOfUse = S;
8021 }
8022 const Stmt *LastStmtOfUse = nullptr;
8023 if (isa<DeclStmt>(StmtOfUse) && Scope) {
8024 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
8025 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
8026 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
8027 break;
8028 }
8029 }
8030 }
8031
8032 const SourceManager &SM = SemaRef.getSourceManager();
8033 SourceLocation IfInsertionLoc =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008034 SM.getExpansionLoc(StmtOfUse->getBeginLoc());
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008035 SourceLocation StmtEndLoc =
8036 SM.getExpansionRange(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008037 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc())
Richard Smithb5f81712018-04-30 05:25:48 +00008038 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008039 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
8040 return;
8041
8042 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
8043 const char *ExtraIndentation = " ";
8044 std::string FixItString;
8045 llvm::raw_string_ostream FixItOS(FixItString);
Erik Pilkingtonfa983902018-10-30 20:31:30 +00008046 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available"
8047 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00008048 << "("
8049 << AvailabilityAttr::getPlatformNameSourceSpelling(
8050 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00008051 << " " << Introduced.getAsString() << ", *)) {\n"
8052 << Indentation << ExtraIndentation;
8053 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
8054 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
8055 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
8056 /*SkipTrailingWhitespaceAndNewLine=*/false);
8057 if (ElseInsertionLoc.isInvalid())
8058 ElseInsertionLoc =
8059 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
8060 FixItOS.str().clear();
8061 FixItOS << "\n"
8062 << Indentation << "} else {\n"
8063 << Indentation << ExtraIndentation
8064 << "// Fallback on earlier versions\n"
8065 << Indentation << "}";
8066 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008067 }
8068}
8069
8070bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
8071 const Type *TyPtr = Ty.getTypePtr();
8072 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
8073
Erik Pilkington6ac77a62017-05-22 15:41:12 +00008074 if (Range.isInvalid())
8075 return true;
8076
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008077 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008078 TagDecl *TD = TT->getDecl();
8079 DiagnoseDeclAvailability(TD, Range);
8080
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008081 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00008082 TypedefNameDecl *D = TD->getDecl();
8083 DiagnoseDeclAvailability(D, Range);
8084
8085 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
8086 if (NamedDecl *D = ObjCO->getInterface())
8087 DiagnoseDeclAvailability(D, Range);
8088 }
8089
8090 return true;
8091}
8092
8093bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
8094 VersionTuple CondVersion;
8095 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
8096 CondVersion = E->getVersion();
8097
8098 // If we're using the '*' case here or if this check is redundant, then we
8099 // use the enclosing version to check both branches.
8100 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00008101 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00008102 } else {
8103 // This isn't an availability checking 'if', we can just continue.
8104 return Base::TraverseIfStmt(If);
8105 }
8106
8107 AvailabilityStack.push_back(CondVersion);
8108 bool ShouldContinue = TraverseStmt(If->getThen());
8109 AvailabilityStack.pop_back();
8110
8111 return ShouldContinue && TraverseStmt(If->getElse());
8112}
8113
8114} // end anonymous namespace
8115
8116void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
8117 Stmt *Body = nullptr;
8118
8119 if (auto *FD = D->getAsFunction()) {
8120 // FIXME: We only examine the pattern decl for availability violations now,
8121 // but we should also examine instantiated templates.
8122 if (FD->isTemplateInstantiation())
8123 return;
8124
8125 Body = FD->getBody();
8126 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
8127 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00008128 else if (auto *BD = dyn_cast<BlockDecl>(D))
8129 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00008130
8131 assert(Body && "Need a body here!");
8132
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00008133 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00008134}
Erik Pilkington9f866a72017-07-18 20:32:07 +00008135
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008136void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
8137 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008138 const ObjCInterfaceDecl *UnknownObjCClass,
8139 bool ObjCPropertyAccess,
Erik Pilkington42578572018-09-10 22:20:09 +00008140 bool AvoidPartialAvailabilityChecks,
8141 ObjCInterfaceDecl *ClassReceiver) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008142 std::string Message;
8143 AvailabilityResult Result;
8144 const NamedDecl* OffendingDecl;
8145 // See if this declaration is unavailable, deprecated, or partial.
Erik Pilkington42578572018-09-10 22:20:09 +00008146 std::tie(Result, OffendingDecl) =
8147 ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver);
Erik Pilkington9f866a72017-07-18 20:32:07 +00008148 if (Result == AR_Available)
8149 return;
8150
8151 if (Result == AR_NotYetIntroduced) {
8152 if (AvoidPartialAvailabilityChecks)
8153 return;
8154
8155 // We need to know the @available context in the current function to
8156 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
8157 // when we're done parsing the current function.
8158 if (getCurFunctionOrMethodDecl()) {
8159 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
8160 return;
8161 } else if (getCurBlock() || getCurLambda()) {
8162 getCurFunction()->HasPotentialAvailabilityViolations = true;
8163 return;
8164 }
8165 }
8166
8167 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00008168 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00008169 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
8170 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
8171 if (PDeclResult == Result)
8172 ObjCPDecl = PD;
8173 }
8174 }
8175
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00008176 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00008177 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
8178}