blob: db5df4db80222cd9fa966217f5b7d19d409d4950 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
David Majnemer929025d2016-01-26 19:30:26 +000014#include "clang/AST/ASTConsumer.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000016#include "clang/AST/ASTMutationListener.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000022#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000023#include "clang/AST/Mangle.h"
Erik Pilkington5cd57172016-08-16 17:44:11 +000024#include "clang/AST/RecursiveASTVisitor.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000025#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000026#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000028#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000029#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000030#include "clang/Sema/DelayedDiagnostic.h"
Artem Belevichbcec9da2016-06-06 22:54:57 +000031#include "clang/Sema/Initialization.h"
John McCallf1e8b342011-09-29 07:17:38 +000032#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000033#include "clang/Sema/Scope.h"
Reid Kleckner04f9bca2018-03-07 22:48:35 +000034#include "clang/Sema/ScopeInfo.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000035#include "clang/Sema/SemaInternal.h"
George Burgess IV177399e2017-01-09 04:12:14 +000036#include "llvm/ADT/STLExtras.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000037#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000038#include "llvm/Support/MathExtras.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000039
Chris Lattner2c6fcf52008-06-26 18:38:35 +000040using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000041using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000042
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000043namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000044 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000045 C,
46 Cpp,
47 ObjC
48 };
Eugene Zelenko1ced5092016-02-12 22:53:10 +000049} // end namespace AttributeLangSupport
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000050
Chris Lattner58418ff2008-06-29 00:16:31 +000051//===----------------------------------------------------------------------===//
52// Helper functions
53//===----------------------------------------------------------------------===//
54
Ted Kremenek527042b2009-08-14 20:49:40 +000055/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000056/// type (function or function-typed variable) or an Objective-C
57/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000058static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000059 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000060}
Eugene Zelenko1ced5092016-02-12 22:53:10 +000061
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000062/// Return true if the given decl has function type (function or
David Majnemer06864812015-04-07 06:01:53 +000063/// function-typed variable) or an Objective-C method or a block.
64static bool isFunctionOrMethodOrBlock(const Decl *D) {
65 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
66}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000067
John McCall3882ace2011-01-05 12:14:39 +000068/// Return true if the given decl has a declarator that should have
69/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000070static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000071 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000072 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
73 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000074}
75
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000076/// hasFunctionProto - Return true if the given decl has a argument
77/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000078/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000080 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000081 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000082 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000083}
84
Alp Toker601b22c2014-01-21 23:35:24 +000085/// getFunctionOrMethodNumParams - Return number of function or method
86/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000087/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000088static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000089 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000090 return cast<FunctionProtoType>(FnTy)->getNumParams();
Aaron Ballmana70c6b52018-02-15 16:20:20 +000091 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000092 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000094}
95
Alp Toker601b22c2014-01-21 23:35:24 +000096static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000097 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000098 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Aaron Ballmana70c6b52018-02-15 16:20:20 +000099 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000100 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000101
Alp Toker03376dc2014-07-07 09:02:20 +0000102 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000103}
104
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000105static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
106 if (const auto *FD = dyn_cast<FunctionDecl>(D))
107 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000108 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000109 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000110 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000111 return BD->getParamDecl(Idx)->getSourceRange();
112 return SourceRange();
113}
114
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000116 if (const FunctionType *FnTy = D->getFunctionType())
George Burgess IV00f70bd2018-03-01 05:43:23 +0000117 return FnTy->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000118 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000119}
120
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000121static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
122 if (const auto *FD = dyn_cast<FunctionDecl>(D))
123 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000124 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000125 return MD->getReturnTypeSourceRange();
126 return SourceRange();
127}
128
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000129static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000130 if (const FunctionType *FnTy = D->getFunctionType())
131 return cast<FunctionProtoType>(FnTy)->isVariadic();
132 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballmane13d0092014-08-01 17:02:34 +0000133 return BD->isVariadic();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000134 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000135}
136
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137static bool isInstanceMethod(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000138 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000139 return MethodDecl->isInstance();
140 return false;
141}
142
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000143static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000144 const auto *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000145 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000146 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000147
John McCall96fa4842010-05-17 21:00:27 +0000148 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
149 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000150 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000151
John McCall96fa4842010-05-17 21:00:27 +0000152 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000153
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000154 // FIXME: Should we walk the chain of classes?
155 return ClsName == &Ctx.Idents.get("NSString") ||
156 ClsName == &Ctx.Idents.get("NSMutableString");
157}
158
Daniel Dunbar980c6692008-09-26 03:32:58 +0000159static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000160 const auto *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000161 if (!PT)
162 return false;
163
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000164 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000165 if (!RT)
166 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167
Daniel Dunbar980c6692008-09-26 03:32:58 +0000168 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000169 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000170 return false;
171
172 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
173}
174
Erich Keanee891aa92018-07-13 15:07:47 +0000175static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
Richard Smithb87c4652013-10-31 21:23:20 +0000176 // FIXME: Include the type in the argument list.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000177 return AL.getNumArgs() + AL.hasParsedType();
Richard Smithb87c4652013-10-31 21:23:20 +0000178}
179
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000180template <typename Compare>
Erich Keanee891aa92018-07-13 15:07:47 +0000181static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000182 unsigned Num, unsigned Diag,
183 Compare Comp) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000184 if (Comp(getNumAttributeArgs(AL), Num)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000185 S.Diag(AL.getLoc(), Diag) << AL << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000186 return false;
187 }
188
189 return true;
190}
191
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000192/// Check if the attribute has exactly as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000193/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000194static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000195 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000196 diag::err_attribute_wrong_number_arguments,
197 std::not_equal_to<unsigned>());
198}
199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000200/// Check if the attribute has at least as many args as Num. May
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000201/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000202static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL,
Richard Smithb87c4652013-10-31 21:23:20 +0000203 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000204 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000205 diag::err_attribute_too_few_arguments,
206 std::less<unsigned>());
207}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000208
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000209/// Check if the attribute has at most as many args as Num. May
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000210/// output an error.
Erich Keanee891aa92018-07-13 15:07:47 +0000211static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL,
212 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000213 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000214 diag::err_attribute_too_many_arguments,
215 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000216}
217
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000218/// A helper function to provide Attribute Location for the Attr types
Erich Keanee891aa92018-07-13 15:07:47 +0000219/// AND the ParsedAttr.
Erich Keane623efd82017-03-30 21:48:55 +0000220template <typename AttrInfo>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000221static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value,
Erich Keane623efd82017-03-30 21:48:55 +0000222 SourceLocation>::type
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000223getAttrLoc(const AttrInfo &AL) {
224 return AL.getLocation();
Erich Keane623efd82017-03-30 21:48:55 +0000225}
Erich Keanee891aa92018-07-13 15:07:47 +0000226static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); }
Erich Keane623efd82017-03-30 21:48:55 +0000227
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000228/// If Expr is a valid integer constant, get the value of the integer
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000229/// expression and return success or failure. May output an error.
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000230///
231/// Negative argument is implicitly converted to unsigned, unless
232/// \p StrictlyUnsigned is true.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000233template <typename AttrInfo>
234static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000235 uint32_t &Val, unsigned Idx = UINT_MAX,
236 bool StrictlyUnsigned = false) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000237 llvm::APSInt I(32);
238 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
239 !Expr->isIntegerConstantExpr(I, S.Context)) {
240 if (Idx != UINT_MAX)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000241 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000242 << AI << Idx << AANT_ArgumentIntegerConstant
243 << Expr->getSourceRange();
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000244 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000245 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000246 << AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange();
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000247 return false;
248 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000249
250 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000251 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
252 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000253 return false;
254 }
255
Andrew Savonichevd353e6d2018-09-06 11:54:09 +0000256 if (StrictlyUnsigned && I.isSigned() && I.isNegative()) {
257 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_negative) << AI;
258 return false;
259 }
260
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000261 Val = (uint32_t)I.getZExtValue();
262 return true;
263}
264
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000265/// Wrapper around checkUInt32Argument, with an extra check to be sure
George Burgess IVe3763372016-12-22 02:50:20 +0000266/// that the result will fit into a regular (signed) int. All args have the same
267/// purpose as they do in checkUInt32Argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000268template <typename AttrInfo>
269static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000270 int &Val, unsigned Idx = UINT_MAX) {
George Burgess IVe3763372016-12-22 02:50:20 +0000271 uint32_t UVal;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000272 if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
George Burgess IVe3763372016-12-22 02:50:20 +0000273 return false;
274
George Burgess IVa8049572016-12-22 19:00:31 +0000275 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
George Burgess IVe3763372016-12-22 02:50:20 +0000276 llvm::APSInt I(32); // for toString
277 I = UVal;
278 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
279 << I.toString(10, false) << 32 << /* Unsigned */ 0;
280 return false;
281 }
282
283 Val = UVal;
284 return true;
285}
286
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000287/// Diagnose mutually exclusive attributes when present on a given
Aaron Ballmanfb763042013-12-02 18:05:46 +0000288/// declaration. Returns true if diagnosed.
289template <typename AttrTy>
Erich Keane44bacdf2018-08-09 13:21:32 +0000290static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000291 if (const auto *A = D->getAttr<AttrTy>()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000292 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
293 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
294 return true;
295 }
296 return false;
297}
298
299template <typename AttrTy>
300static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
301 if (const auto *A = D->getAttr<AttrTy>()) {
302 S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000303 << A;
304 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000305 return true;
306 }
307 return false;
308}
309
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000310/// Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000311/// instance method D. May output an error.
312///
313/// \returns true if IdxExpr is a valid index.
Erich Keane623efd82017-03-30 21:48:55 +0000314template <typename AttrInfo>
315static bool checkFunctionOrMethodParameterIndex(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000316 Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
Joel E. Denny81508102018-03-13 14:51:22 +0000317 const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
David Majnemer06864812015-04-07 06:01:53 +0000318 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000319
320 // In C++ the implicit 'this' function parameter also counts.
321 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000322 bool HP = hasFunctionProto(D);
323 bool HasImplicitThisParam = isInstanceMethod(D);
324 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000325 unsigned NumParams =
326 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000327
328 llvm::APSInt IdxInt;
329 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
330 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000331 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000332 << &AI << AttrArgNum << AANT_ArgumentIntegerConstant
333 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000334 return false;
335 }
336
Joel E. Denny81508102018-03-13 14:51:22 +0000337 unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX);
338 if (IdxSource < 1 || (!IV && IdxSource > NumParams)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000339 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +0000340 << &AI << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000341 return false;
342 }
Joel E. Denny81508102018-03-13 14:51:22 +0000343 if (HasImplicitThisParam && !CanIndexImplicitThis) {
344 if (IdxSource == 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000345 S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
346 << &AI << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000347 return false;
348 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000349 }
350
Joel E. Denny81508102018-03-13 14:51:22 +0000351 Idx = ParamIdx(IdxSource, D);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000352 return true;
353}
354
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000355/// Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000356/// If not emit an error and return false. If the argument is an identifier it
357/// will emit an error with a fixit hint and treat it as if it was a string
358/// literal.
Erich Keanee891aa92018-07-13 15:07:47 +0000359bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
360 StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000361 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000362 // Look for identifiers. If we have one emit a hint to fix it to a literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000363 if (AL.isArgIdent(ArgNum)) {
364 IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000365 Diag(Loc->Loc, diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000366 << AL << AANT_ArgumentString
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000367 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000368 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000369 Str = Loc->Ident->getName();
370 if (ArgLocation)
371 *ArgLocation = Loc->Loc;
372 return true;
373 }
374
375 // Now check for an actual string literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000376 Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
377 const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000378 if (ArgLocation)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000379 *ArgLocation = ArgExpr->getBeginLoc();
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000380
381 if (!Literal || !Literal->isAscii()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000382 Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000383 << AL << AANT_ArgumentString;
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000384 return false;
385 }
386
387 Str = Literal->getString();
388 return true;
389}
390
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000391/// Applies the given attribute to the Decl without performing any
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000392/// additional semantic checking.
393template <typename AttrType>
Erich Keanee891aa92018-07-13 15:07:47 +0000394static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000395 D->addAttr(::new (S.Context) AttrType(AL.getRange(), S.Context,
396 AL.getAttributeSpellingListIndex()));
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000397}
398
Justin Lebar3eaaf862016-01-13 01:07:35 +0000399template <typename AttrType>
400static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000401 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000402 handleSimpleAttribute<AttrType>(S, D, AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000403}
404
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000405/// Applies the given attribute to the Decl so long as the Decl doesn't
Justin Lebar3eaaf862016-01-13 01:07:35 +0000406/// already have one of the given incompatible attributes.
407template <typename AttrType, typename IncompatibleAttrType,
408 typename... IncompatibleAttrTypes>
409static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000410 const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000411 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
Justin Lebar3eaaf862016-01-13 01:07:35 +0000412 return;
413 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000414 AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000415}
416
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000417/// Check if the passed-in expression is of type int or bool.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000418static bool isIntOrBool(Expr *Exp) {
419 QualType QT = Exp->getType();
420 return QT->isBooleanType() || QT->isIntegerType();
421}
422
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000423
424// Check to see if the type is a smart pointer of some kind. We assume
425// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000426static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000427 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
428 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000429 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000430 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000431
Richard Smithcf4bdde2015-02-21 02:45:19 +0000432 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
433 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000434 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000435 return false;
436
437 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000438}
439
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000440/// Check if passed in Decl is a pointer type.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000441/// Note that this function may produce an error message.
442/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000443static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000444 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000445 const auto *VD = cast<ValueDecl>(D);
446 QualType QT = VD->getType();
Aaron Ballman553e6812013-12-26 14:54:11 +0000447 if (QT->isAnyPointerType())
448 return true;
449
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000450 if (const auto *RT = QT->getAs<RecordType>()) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000451 // If it's an incomplete type, it could be a smart pointer; skip it.
452 // (We don't want to force template instantiation if we can avoid it,
453 // since that would alter the order in which templates are instantiated.)
454 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000455 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000456
Aaron Ballman553e6812013-12-26 14:54:11 +0000457 if (threadSafetyCheckIsSmartPointer(S, RT))
458 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000459 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000460
Erich Keane44bacdf2018-08-09 13:21:32 +0000461 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000462 return false;
463}
464
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000465/// Checks that the passed in QualType either is of RecordType or points
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000466/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000467static const RecordType *getRecordType(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000468 if (const auto *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000469 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000470
471 // Now check if we point to record type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000472 if (const auto *PT = QT->getAs<PointerType>())
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000473 return PT->getPointeeType()->getAs<RecordType>();
474
Craig Topperc3ec1492014-05-26 06:22:03 +0000475 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000476}
477
Josh Gao55afa752017-08-11 07:54:35 +0000478static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000479 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000480
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000481 if (!RT)
482 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000483
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000484 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000485 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000486 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000487
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000488 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000489 // FIXME -- Check the type that the smart pointer points to.
490 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000491 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000492
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000493 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000494 RecordDecl *RD = RT->getDecl();
Josh Gao55afa752017-08-11 07:54:35 +0000495 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000496 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000497
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000498 // Else check if any base classes have a capability.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000499 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000500 CXXBasePaths BPaths(false, false);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000501 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) {
502 const auto *Type = BS->getType()->getAs<RecordType>();
Josh Gao55afa752017-08-11 07:54:35 +0000503 return Type->getDecl()->hasAttr<CapabilityAttr>();
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000504 }, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000505 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000506 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000507 return false;
508}
509
Josh Gao55afa752017-08-11 07:54:35 +0000510static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000511 const auto *TD = Ty->getAs<TypedefType>();
512 if (!TD)
513 return false;
514
515 TypedefNameDecl *TN = TD->getDecl();
516 if (!TN)
517 return false;
518
Josh Gao55afa752017-08-11 07:54:35 +0000519 return TN->hasAttr<CapabilityAttr>();
Aaron Ballman76050722014-04-04 15:13:57 +0000520}
521
Josh Gaob40c1772017-08-08 19:44:35 +0000522static bool typeHasCapability(Sema &S, QualType Ty) {
Josh Gao55afa752017-08-11 07:54:35 +0000523 if (checkTypedefTypeForCapability(Ty))
524 return true;
Josh Gaob40c1772017-08-08 19:44:35 +0000525
Josh Gao55afa752017-08-11 07:54:35 +0000526 if (checkRecordTypeForCapability(S, Ty))
527 return true;
528
529 return false;
Josh Gaob40c1772017-08-08 19:44:35 +0000530}
531
Aaron Ballman76050722014-04-04 15:13:57 +0000532static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
533 // Capability expressions are simple expressions involving the boolean logic
534 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
535 // a DeclRefExpr is found, its type should be checked to determine whether it
536 // is a capability or not.
537
Yi Kong2d58d192017-12-14 22:24:45 +0000538 if (const auto *E = dyn_cast<CastExpr>(Ex))
Aaron Ballman76050722014-04-04 15:13:57 +0000539 return isCapabilityExpr(S, E->getSubExpr());
540 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
541 return isCapabilityExpr(S, E->getSubExpr());
542 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
Yi Kong2d58d192017-12-14 22:24:45 +0000543 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
544 E->getOpcode() == UO_Deref)
Aaron Ballman76050722014-04-04 15:13:57 +0000545 return isCapabilityExpr(S, E->getSubExpr());
546 return false;
547 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
548 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
549 return isCapabilityExpr(S, E->getLHS()) &&
550 isCapabilityExpr(S, E->getRHS());
551 return false;
552 }
553
Yi Kong2d58d192017-12-14 22:24:45 +0000554 return typeHasCapability(S, Ex->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000555}
556
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000557/// Checks that all attribute arguments, starting from Sidx, resolve to
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000558/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000559/// \param Sidx The attribute argument index to start checking with.
560/// \param ParamIdxOk Whether an argument can be indexing into a function
561/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000562static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000563 const ParsedAttr &AL,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000564 SmallVectorImpl<Expr *> &Args,
565 int Sidx = 0,
566 bool ParamIdxOk = false) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000567 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
568 Expr *ArgExp = AL.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000569
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000570 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000571 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000572 Args.push_back(ArgExp);
573 continue;
574 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000575
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000576 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000577 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000578 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000579 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000580 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000581 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000582 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000583 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000584
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000585 // We allow constant strings to be used as a placeholder for expressions
586 // that are not valid C++ syntax, but warn that they are ignored.
Erich Keane44bacdf2018-08-09 13:21:32 +0000587 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000588 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000589 continue;
590 }
591
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000592 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000593
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000594 // A pointer to member expression of the form &MyClass::mu is treated
595 // specially -- we need to look at the type of the member.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000596 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000597 if (UOp->getOpcode() == UO_AddrOf)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000598 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000599 if (DRE->getDecl()->isCXXInstanceMember())
600 ArgTy = DRE->getDecl()->getType();
601
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000602 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000603 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000604
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000605 // Now check if we index into a record type function param.
Josh Gao55afa752017-08-11 07:54:35 +0000606 if(!RT && ParamIdxOk) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000607 const auto *FD = dyn_cast<FunctionDecl>(D);
608 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
Josh Gao55afa752017-08-11 07:54:35 +0000609 if(FD && IL) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000610 unsigned int NumParams = FD->getNumParams();
611 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000612 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
613 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000614 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
615 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
Erich Keane44bacdf2018-08-09 13:21:32 +0000616 << AL << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000617 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000618 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000619 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000620 }
621 }
622
Aaron Ballman76050722014-04-04 15:13:57 +0000623 // If the type does not have a capability, see if the components of the
624 // expression have capabilities. This allows for writing C code where the
625 // capability may be on the type, and the expression is a capability
626 // boolean logic expression. Eg) requires_capability(A || B && !C)
627 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000628 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Erich Keane44bacdf2018-08-09 13:21:32 +0000629 << AL << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000630
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000631 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000632 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000633}
634
Chris Lattner58418ff2008-06-29 00:16:31 +0000635//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000636// Attribute Implementations
637//===----------------------------------------------------------------------===//
638
Erich Keanee891aa92018-07-13 15:07:47 +0000639static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000640 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000641 return;
642
Michael Han99315932013-01-24 16:46:58 +0000643 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000644 PtGuardedVarAttr(AL.getRange(), S.Context,
645 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000646}
647
Erich Keanee891aa92018-07-13 15:07:47 +0000648static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000649 Expr *&Arg) {
650 SmallVector<Expr *, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000651 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000652 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000653 unsigned Size = Args.size();
654 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000655 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000656
Michael Han3be3b442012-07-23 18:48:41 +0000657 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000658
Michael Han3be3b442012-07-23 18:48:41 +0000659 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000660}
661
Erich Keanee891aa92018-07-13 15:07:47 +0000662static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000663 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000664 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000665 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000666
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000667 D->addAttr(::new (S.Context) GuardedByAttr(
668 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000669}
670
Erich Keanee891aa92018-07-13 15:07:47 +0000671static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000672 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000673 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000674 return;
675
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000676 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000677 return;
678
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000679 D->addAttr(::new (S.Context) PtGuardedByAttr(
680 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000681}
682
Erich Keanee891aa92018-07-13 15:07:47 +0000683static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000684 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000685 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000686 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000687
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000688 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000689 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000690 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000691 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000692 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000693 }
694
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000695 // Check that all arguments are lockable objects.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000696 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000697 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000698 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000699
Michael Han3be3b442012-07-23 18:48:41 +0000700 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000701}
702
Erich Keanee891aa92018-07-13 15:07:47 +0000703static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000704 SmallVector<Expr *, 1> Args;
705 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000706 return;
707
708 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000709 D->addAttr(::new (S.Context) AcquiredAfterAttr(
710 AL.getRange(), S.Context, StartArg, Args.size(),
711 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000712}
713
Erich Keanee891aa92018-07-13 15:07:47 +0000714static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000715 SmallVector<Expr *, 1> Args;
716 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000717 return;
718
719 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000720 D->addAttr(::new (S.Context) AcquiredBeforeAttr(
721 AL.getRange(), S.Context, StartArg, Args.size(),
722 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000723}
724
Erich Keanee891aa92018-07-13 15:07:47 +0000725static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000726 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000727 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000728 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000729 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000730
Michael Han3be3b442012-07-23 18:48:41 +0000731 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000732}
733
Erich Keanee891aa92018-07-13 15:07:47 +0000734static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000735 SmallVector<Expr *, 1> Args;
736 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000737 return;
738
739 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000740 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000741 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000742 AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size,
743 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000744}
745
746static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000747 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000748 SmallVector<Expr *, 1> Args;
749 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000750 return;
751
752 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000753 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000754 D->addAttr(::new (S.Context) AssertExclusiveLockAttr(
755 AL.getRange(), S.Context, StartArg, Size,
756 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000757}
758
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000759/// Checks to be sure that the given parameter number is in bounds, and
Aaron Ballman836684a2018-02-25 20:40:06 +0000760/// is an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000761/// false.
762///
Aaron Ballman836684a2018-02-25 20:40:06 +0000763/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000764template <typename AttrInfo>
765static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Joel E. Denny81508102018-03-13 14:51:22 +0000766 const AttrInfo &AI, unsigned AttrArgNo) {
Aaron Ballman836684a2018-02-25 20:40:06 +0000767 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
768 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
Joel E. Denny81508102018-03-13 14:51:22 +0000769 ParamIdx Idx;
Aaron Ballman836684a2018-02-25 20:40:06 +0000770 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000771 Idx))
772 return false;
773
Joel E. Denny81508102018-03-13 14:51:22 +0000774 const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +0000775 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000776 SourceLocation SrcLoc = AttrArg->getBeginLoc();
Erich Keane623efd82017-03-30 21:48:55 +0000777 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +0000778 << AI << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000779 return false;
780 }
781 return true;
782}
783
Erich Keanee891aa92018-07-13 15:07:47 +0000784static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000785 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
786 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000787 return;
788
789 const auto *FD = cast<FunctionDecl>(D);
790 if (!FD->getReturnType()->isPointerType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000791 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
George Burgess IVe3763372016-12-22 02:50:20 +0000792 return;
793 }
794
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000795 const Expr *SizeExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +0000796 int SizeArgNoVal;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000797 // Parameter indices are 1-indexed, hence Index=1
Joel E. Denny81508102018-03-13 14:51:22 +0000798 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Index=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000799 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000800 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0))
George Burgess IVe3763372016-12-22 02:50:20 +0000801 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000802 ParamIdx SizeArgNo(SizeArgNoVal, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000803
Joel E. Denny81508102018-03-13 14:51:22 +0000804 ParamIdx NumberArgNo;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000805 if (AL.getNumArgs() == 2) {
806 const Expr *NumberExpr = AL.getArgAsExpr(1);
Joel E. Denny81508102018-03-13 14:51:22 +0000807 int Val;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000808 // Parameter indices are 1-based, hence Index=2
Joel E. Denny81508102018-03-13 14:51:22 +0000809 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Index=*/2))
George Burgess IVe3763372016-12-22 02:50:20 +0000810 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000811 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000812 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000813 NumberArgNo = ParamIdx(Val, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000814 }
815
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000816 D->addAttr(::new (S.Context)
817 AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo,
818 AL.getAttributeSpellingListIndex()));
George Burgess IVe3763372016-12-22 02:50:20 +0000819}
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000820
Erich Keanee891aa92018-07-13 15:07:47 +0000821static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000822 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000823 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000824 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000825
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000826 if (!isIntOrBool(AL.getArgAsExpr(0))) {
827 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +0000828 << AL << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000829 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000830 }
831
832 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000833 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000834
Michael Han3be3b442012-07-23 18:48:41 +0000835 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000836}
837
Michael Hana9171bc2012-08-03 17:40:43 +0000838static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000839 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000840 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000841 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000842 return;
843
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000844 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
845 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(),
846 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000847}
848
Michael Hana9171bc2012-08-03 17:40:43 +0000849static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000850 const ParsedAttr &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000851 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000852 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000853 return;
854
Nico Weber462fd1e2015-01-07 23:50:05 +0000855 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000856 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(),
857 Args.size(), AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000858}
859
Erich Keanee891aa92018-07-13 15:07:47 +0000860static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000861 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000862 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000863 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000864 unsigned Size = Args.size();
865 if (Size == 0)
866 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000867
Michael Han99315932013-01-24 16:46:58 +0000868 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000869 LockReturnedAttr(AL.getRange(), S.Context, Args[0],
870 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000871}
872
Erich Keanee891aa92018-07-13 15:07:47 +0000873static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000874 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000875 return;
876
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000877 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000878 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000879 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000880 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000881 if (Size == 0)
882 return;
883 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000884
Michael Han99315932013-01-24 16:46:58 +0000885 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000886 LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size,
887 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000888}
889
Erich Keanee891aa92018-07-13 15:07:47 +0000890static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
George Burgess IV177399e2017-01-09 04:12:14 +0000891 Expr *&Cond, StringRef &Msg) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000892 Cond = AL.getArgAsExpr(0);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000893 if (!Cond->isTypeDependent()) {
894 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
895 if (Converted.isInvalid())
George Burgess IV177399e2017-01-09 04:12:14 +0000896 return false;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000897 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000898 }
899
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000900 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000901 return false;
902
903 if (Msg.empty())
904 Msg = "<no message provided>";
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000905
906 SmallVector<PartialDiagnosticAt, 8> Diags;
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000907 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000908 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
909 Diags)) {
Erich Keane44bacdf2018-08-09 13:21:32 +0000910 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
George Burgess IV0d546532016-11-10 21:47:12 +0000911 for (const PartialDiagnosticAt &PDiag : Diags)
912 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +0000913 return false;
914 }
915 return true;
916}
917
Erich Keanee891aa92018-07-13 15:07:47 +0000918static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000919 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +0000920
921 Expr *Cond;
922 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000923 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000924 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000925 EnableIfAttr(AL.getRange(), S.Context, Cond, Msg,
926 AL.getAttributeSpellingListIndex()));
George Burgess IV177399e2017-01-09 04:12:14 +0000927}
928
929namespace {
930/// Determines if a given Expr references any of the given function's
931/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
932class ArgumentDependenceChecker
933 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
934#ifndef NDEBUG
935 const CXXRecordDecl *ClassType;
936#endif
937 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
938 bool Result;
939
940public:
941 ArgumentDependenceChecker(const FunctionDecl *FD) {
942#ifndef NDEBUG
943 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
944 ClassType = MD->getParent();
945 else
946 ClassType = nullptr;
947#endif
948 Parms.insert(FD->param_begin(), FD->param_end());
949 }
950
951 bool referencesArgs(Expr *E) {
952 Result = false;
953 TraverseStmt(E);
954 return Result;
955 }
956
957 bool VisitCXXThisExpr(CXXThisExpr *E) {
958 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
959 "`this` doesn't refer to the enclosing class?");
960 Result = true;
961 return false;
962 }
963
964 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
965 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
966 if (Parms.count(PVD)) {
967 Result = true;
968 return false;
969 }
970 return true;
971 }
972};
973}
974
Erich Keanee891aa92018-07-13 15:07:47 +0000975static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000976 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +0000977
978 Expr *Cond;
979 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000980 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000981 return;
982
983 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000984 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +0000985 return;
986
987 DiagnoseIfAttr::DiagnosticType DiagType;
988 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000989 S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
George Burgess IV177399e2017-01-09 04:12:14 +0000990 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000991 return;
992 }
993
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000994 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +0000995 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000996 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +0000997 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000998 AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent,
999 cast<NamedDecl>(D), AL.getAttributeSpellingListIndex()));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001000}
1001
Erich Keanee891aa92018-07-13 15:07:47 +00001002static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001003 if (D->hasAttr<PassObjectSizeAttr>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001004 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001005 return;
1006 }
1007
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001008 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001009 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001010 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001011 return;
1012
1013 // pass_object_size's argument is passed in as the second argument of
1014 // __builtin_object_size. So, it has the same constraints as that second
1015 // argument; namely, it must be in the range [0, 3].
1016 if (Type > 3) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001017 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00001018 << AL << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001019 return;
1020 }
1021
1022 // pass_object_size is only supported on constant pointer parameters; as a
1023 // kindness to users, we allow the parameter to be non-const for declarations.
1024 // At this point, we have no clue if `D` belongs to a function declaration or
1025 // definition, so we defer the constness check until later.
1026 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001027 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001028 return;
1029 }
1030
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001031 D->addAttr(::new (S.Context) PassObjectSizeAttr(
1032 AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex()));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001033}
1034
Erich Keanee891aa92018-07-13 15:07:47 +00001035static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001036 ConsumableAttr::ConsumedState DefaultState;
1037
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001038 if (AL.isArgIdent(0)) {
1039 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001040 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1041 DefaultState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001042 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1043 << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001044 return;
1045 }
David Blaikie16f76d22013-09-06 01:28:43 +00001046 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001047 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001048 << AL << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001049 return;
1050 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001051
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001052 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001053 ConsumableAttr(AL.getRange(), S.Context, DefaultState,
1054 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001055}
1056
1057static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Erich Keanee891aa92018-07-13 15:07:47 +00001058 const ParsedAttr &AL) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001059 ASTContext &CurrContext = S.getASTContext();
1060 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001061
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001062 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1063 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001064 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001065 RD->getNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001066
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001067 return false;
1068 }
1069 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001070
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001071 return true;
1072}
1073
Erich Keanee891aa92018-07-13 15:07:47 +00001074static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001075 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001076 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001077
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001078 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001079 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001080
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001081 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001082 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001083 CallableWhenAttr::ConsumedState CallableState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001084
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001085 StringRef StateString;
1086 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001087 if (AL.isArgIdent(ArgIndex)) {
1088 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001089 StateString = Ident->Ident->getName();
1090 Loc = Ident->Loc;
1091 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001092 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001093 return;
1094 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001095
1096 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001097 CallableState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001098 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001099 return;
1100 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001101
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001102 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001103 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001104
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001105 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001106 CallableWhenAttr(AL.getRange(), S.Context, States.data(),
1107 States.size(), AL.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001108}
1109
Erich Keanee891aa92018-07-13 15:07:47 +00001110static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001111 ParamTypestateAttr::ConsumedState ParamState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001112
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001113 if (AL.isArgIdent(0)) {
1114 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001115 StringRef StateString = Ident->Ident->getName();
1116
1117 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1118 ParamState)) {
1119 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00001120 << AL << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001121 return;
1122 }
1123 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001124 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1125 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001126 return;
1127 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001128
DeLesley Hutchins69391772013-10-17 23:23:53 +00001129 // FIXME: This check is currently being done in the analysis. It can be
1130 // enabled here only after the parser propagates attributes at
1131 // template specialization definition, not declaration.
1132 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1133 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1134 //
1135 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001136 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001137 // ReturnType.getAsString();
1138 // return;
1139 //}
Fangrui Song6907ce22018-07-30 19:24:48 +00001140
DeLesley Hutchins69391772013-10-17 23:23:53 +00001141 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001142 ParamTypestateAttr(AL.getRange(), S.Context, ParamState,
1143 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001144}
1145
Erich Keanee891aa92018-07-13 15:07:47 +00001146static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001147 ReturnTypestateAttr::ConsumedState ReturnState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001148
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001149 if (AL.isArgIdent(0)) {
1150 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001151 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1152 ReturnState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001153 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1154 << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001155 return;
1156 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001157 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001158 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1159 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001160 return;
1161 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001162
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001163 // FIXME: This check is currently being done in the analysis. It can be
1164 // enabled here only after the parser propagates attributes at
1165 // template specialization definition, not declaration.
1166 //QualType ReturnType;
1167 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001168 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1169 // ReturnType = Param->getType();
1170 //
1171 //} else if (const CXXConstructorDecl *Constructor =
1172 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001173 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001174 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001175 //} else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001176 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001177 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1178 //}
1179 //
1180 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1181 //
1182 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1183 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1184 // ReturnType.getAsString();
1185 // return;
1186 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001187
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001188 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001189 ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState,
1190 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001191}
1192
Erich Keanee891aa92018-07-13 15:07:47 +00001193static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001194 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001195 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001196
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001197 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001198 if (AL.isArgIdent(0)) {
1199 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001200 StringRef Param = Ident->Ident->getName();
1201 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001202 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1203 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001204 return;
1205 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001206 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001207 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1208 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001209 return;
1210 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001211
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001212 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001213 SetTypestateAttr(AL.getRange(), S.Context, NewState,
1214 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001215}
1216
Erich Keanee891aa92018-07-13 15:07:47 +00001217static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001218 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001219 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001220
1221 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001222 if (AL.isArgIdent(0)) {
1223 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001224 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001225 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001226 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1227 << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001228 return;
1229 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001230 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001231 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1232 << AL << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001233 return;
1234 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001235
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001236 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001237 TestTypestateAttr(AL.getRange(), S.Context, TestState,
1238 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001239}
1240
Erich Keanee891aa92018-07-13 15:07:47 +00001241static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001242 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001243 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001244}
1245
Erich Keanee891aa92018-07-13 15:07:47 +00001246static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001247 if (auto *TD = dyn_cast<TagDecl>(D))
1248 TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context,
1249 AL.getAttributeSpellingListIndex()));
1250 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001251 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1252 !FD->getType()->isIncompleteType() &&
1253 FD->isBitField() &&
1254 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001255
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001256 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1257 if (BitfieldByteAligned)
1258 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001259 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001260 << AL << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001261 else
1262 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001263 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001264 } else {
1265 // Report warning about changed offset in the newer compiler versions.
1266 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001267 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001268
1269 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001270 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001271 }
1272
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001273 } else
Erich Keane44bacdf2018-08-09 13:21:32 +00001274 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001275}
1276
Erich Keanee891aa92018-07-13 15:07:47 +00001277static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001278 // The IBOutlet/IBOutletCollection attributes only apply to instance
1279 // variables or properties of Objective-C classes. The outlet must also
1280 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001281 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001282 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001283 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001284 << AL << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001285 return false;
1286 }
1287 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001288 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001289 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001290 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001291 << AL << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001292 return false;
1293 }
1294 }
1295 else {
Erich Keane44bacdf2018-08-09 13:21:32 +00001296 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001297 return false;
1298 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001299
Ted Kremenek7fd17232011-09-29 07:02:25 +00001300 return true;
1301}
1302
Erich Keanee891aa92018-07-13 15:07:47 +00001303static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001304 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001305 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001306
Michael Han99315932013-01-24 16:46:58 +00001307 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001308 IBOutletAttr(AL.getRange(), S.Context,
1309 AL.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001310}
1311
Erich Keanee891aa92018-07-13 15:07:47 +00001312static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001313
1314 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001315 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001316 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001317 return;
1318 }
1319
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001320 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001321 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001322
Richard Smithb1f9a282013-10-31 01:56:18 +00001323 ParsedType PT;
1324
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001325 if (AL.hasParsedType())
1326 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001327 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001328 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001329 S.getScopeForContext(D->getDeclContext()->getParent()));
1330 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001331 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001332 return;
1333 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001334 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001335
Craig Topperc3ec1492014-05-26 06:22:03 +00001336 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001337 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1338 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001339 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001340
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001341 // Diagnose use of non-object type in iboutletcollection attribute.
1342 // FIXME. Gnu attribute extension ignores use of builtin types in
1343 // attributes. So, __attribute__((iboutletcollection(char))) will be
1344 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001345 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001346 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001347 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1348 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001349 return;
1350 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001351
Michael Han99315932013-01-24 16:46:58 +00001352 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001353 IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc,
1354 AL.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001355}
1356
Hal Finkelee90a222014-09-26 05:04:30 +00001357bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1358 if (RefOkay) {
1359 if (T->isReferenceType())
1360 return true;
1361 } else {
1362 T = T.getNonReferenceType();
1363 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001364
Hal Finkelee90a222014-09-26 05:04:30 +00001365 // The nonnull attribute, and other similar attributes, can be applied to a
1366 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001367 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001368 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1369 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001370 for (const auto *I : UD->fields()) {
1371 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001372 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1373 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001374 }
1375 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001376 }
1377
1378 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001379}
1380
Erich Keanee891aa92018-07-13 15:07:47 +00001381static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001382 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001383 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001384 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001385 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001386 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001387 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001388 << AL << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001389 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001390 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001391 << AL << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001392 return false;
1393 }
1394 return true;
1395}
1396
Erich Keanee891aa92018-07-13 15:07:47 +00001397static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001398 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001399 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1400 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001401 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001402 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001403 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001404
1405 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001406 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1407 !attrNonNullArgCheck(
1408 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1409 Ex->getSourceRange(),
1410 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001411 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001412
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001413 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001414 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001415
1416 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001417 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1418 // check if the attribute came from a macro expansion or a template
1419 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001420 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001421 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001422 bool AnyPointers = isFunctionOrMethodVariadic(D);
1423 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1424 I != E && !AnyPointers; ++I) {
1425 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001426 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001427 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001428 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001429
Richard Smith588bd9b2014-08-27 04:59:42 +00001430 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001431 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001432 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001433
Joel E. Denny81508102018-03-13 14:51:22 +00001434 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001435 unsigned Size = NonNullArgs.size();
1436 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001437 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001438 NonNullAttr(AL.getRange(), S.Context, Start, Size,
1439 AL.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001440}
1441
Jordan Rosec9399072014-02-11 17:27:59 +00001442static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00001443 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001444 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001445 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001446 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001447 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001448 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001449 << D->getSourceRange();
1450 }
1451 return;
1452 }
1453
1454 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001455 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001456 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001457 return;
1458
1459 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001460 NonNullAttr(AL.getRange(), S.Context, nullptr, 0,
1461 AL.getAttributeSpellingListIndex()));
Jordan Rosec9399072014-02-11 17:27:59 +00001462}
1463
Erich Keanee891aa92018-07-13 15:07:47 +00001464static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001465 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001466 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001467 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001468 /* isReturnValue */ true))
1469 return;
1470
1471 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001472 ReturnsNonNullAttr(AL.getRange(), S.Context,
1473 AL.getAttributeSpellingListIndex()));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001474}
1475
Erich Keanee891aa92018-07-13 15:07:47 +00001476static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001477 if (D->isInvalidDecl())
1478 return;
1479
1480 // noescape only applies to pointer types.
1481 QualType T = cast<ParmVarDecl>(D)->getType();
1482 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001483 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001484 << AL << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001485 return;
1486 }
1487
1488 D->addAttr(::new (S.Context) NoEscapeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001489 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001490}
1491
Erich Keanee891aa92018-07-13 15:07:47 +00001492static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001493 Expr *E = AL.getArgAsExpr(0),
1494 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1495 S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE,
1496 AL.getAttributeSpellingListIndex());
Hal Finkelee90a222014-09-26 05:04:30 +00001497}
1498
Erich Keanee891aa92018-07-13 15:07:47 +00001499static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001500 S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0),
1501 AL.getAttributeSpellingListIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001502}
1503
Hal Finkelee90a222014-09-26 05:04:30 +00001504void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1505 Expr *OE, unsigned SpellingListIndex) {
1506 QualType ResultType = getFunctionOrMethodResultType(D);
1507 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1508
1509 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1510 SourceLocation AttrLoc = AttrRange.getBegin();
1511
1512 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1513 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1514 << &TmpAttr << AttrRange << SR;
1515 return;
1516 }
1517
1518 if (!E->isValueDependent()) {
1519 llvm::APSInt I(64);
1520 if (!E->isIntegerConstantExpr(I, Context)) {
1521 if (OE)
1522 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1523 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1524 << E->getSourceRange();
1525 else
1526 Diag(AttrLoc, diag::err_attribute_argument_type)
1527 << &TmpAttr << AANT_ArgumentIntegerConstant
1528 << E->getSourceRange();
1529 return;
1530 }
1531
1532 if (!I.isPowerOf2()) {
1533 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1534 << E->getSourceRange();
1535 return;
1536 }
1537 }
1538
1539 if (OE) {
1540 if (!OE->isValueDependent()) {
1541 llvm::APSInt I(64);
1542 if (!OE->isIntegerConstantExpr(I, Context)) {
1543 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1544 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1545 << OE->getSourceRange();
1546 return;
1547 }
1548 }
1549 }
1550
1551 D->addAttr(::new (Context)
1552 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1553}
1554
Erich Keane623efd82017-03-30 21:48:55 +00001555void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr,
1556 unsigned SpellingListIndex) {
1557 QualType ResultType = getFunctionOrMethodResultType(D);
1558
Joel E. Denny81508102018-03-13 14:51:22 +00001559 AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex);
Erich Keane623efd82017-03-30 21:48:55 +00001560 SourceLocation AttrLoc = AttrRange.getBegin();
1561
1562 if (!ResultType->isDependentType() &&
1563 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1564 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1565 << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D);
1566 return;
1567 }
1568
Joel E. Denny81508102018-03-13 14:51:22 +00001569 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001570 const auto *FuncDecl = cast<FunctionDecl>(D);
1571 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Joel E. Denny81508102018-03-13 14:51:22 +00001572 /*AttrArgNo=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001573 return;
1574
Joel E. Denny81508102018-03-13 14:51:22 +00001575 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001576 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001577 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001578 << &TmpAttr
1579 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001580 return;
1581 }
1582
Joel E. Denny81508102018-03-13 14:51:22 +00001583 D->addAttr(::new (Context)
1584 AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex));
Erich Keane623efd82017-03-30 21:48:55 +00001585}
1586
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001587/// Normalize the attribute, __foo__ becomes foo.
1588/// Returns true if normalization was applied.
1589static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001590 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1591 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001592 AttrName = AttrName.drop_front(2).drop_back(2);
1593 return true;
1594 }
1595 return false;
1596}
1597
Erich Keanee891aa92018-07-13 15:07:47 +00001598static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001599 // This attribute must be applied to a function declaration. The first
1600 // argument to the attribute must be an identifier, the name of the resource,
1601 // for example: malloc. The following arguments must be argument indexes, the
1602 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001603 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001604 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001605 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001606
Aaron Ballman00e99962013-08-31 01:11:41 +00001607 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001608 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001609 << AL << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001610 return;
1611 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001612
Richard Smith852e9ce2013-11-27 01:46:48 +00001613 // Figure out our Kind.
1614 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001615 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001616 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001617
Richard Smith852e9ce2013-11-27 01:46:48 +00001618 // Check arguments.
1619 switch (K) {
1620 case OwnershipAttr::Takes:
1621 case OwnershipAttr::Holds:
1622 if (AL.getNumArgs() < 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001623 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001624 return;
1625 }
1626 break;
1627 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001628 if (AL.getNumArgs() > 2) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001629 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001630 return;
1631 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001632 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001633 }
1634
Richard Smith852e9ce2013-11-27 01:46:48 +00001635 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001636
Richard Smith852e9ce2013-11-27 01:46:48 +00001637 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001638 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001639 Module = &S.PP.getIdentifierTable().get(ModuleName);
1640 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001641
Joel E. Denny81508102018-03-13 14:51:22 +00001642 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001643 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1644 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001645 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001646 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001647 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001648
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001649 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001650 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001651 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001652 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001653 case OwnershipAttr::Takes:
1654 case OwnershipAttr::Holds:
1655 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1656 Err = 0;
1657 break;
1658 case OwnershipAttr::Returns:
1659 if (!T->isIntegerType())
1660 Err = 1;
1661 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001662 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001663 if (-1 != Err) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001664 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1665 << Ex->getSourceRange();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001666 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001667 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001668
1669 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001670 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001671 // Cannot have two ownership attributes of different kinds for the same
1672 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001673 if (I->getOwnKind() != K && I->args_end() !=
1674 std::find(I->args_begin(), I->args_end(), Idx)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001675 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001676 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001677 } else if (K == OwnershipAttr::Returns &&
1678 I->getOwnKind() == OwnershipAttr::Returns) {
1679 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001680 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001681 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1682 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001683 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001684 if (I->args_size())
1685 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001686 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001687 return;
1688 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001689 }
1690 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001691 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001692 }
1693
Joel E. Denny81508102018-03-13 14:51:22 +00001694 ParamIdx *Start = OwnershipArgs.data();
1695 unsigned Size = OwnershipArgs.size();
1696 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001697 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001698 OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size,
1699 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001700}
1701
Erich Keanee891aa92018-07-13 15:07:47 +00001702static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001703 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001704 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001705 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001706 return;
1707 }
1708
1709 // gcc rejects
1710 // class c {
1711 // static int a __attribute__((weakref ("v2")));
1712 // static int b() __attribute__((weakref ("f3")));
1713 // };
1714 // and ignores the attributes of
1715 // void f(void) {
1716 // static int a __attribute__((weakref ("v2")));
1717 // }
1718 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001719 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001720 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001721 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1722 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001723 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001724 }
1725
1726 // The GCC manual says
1727 //
1728 // At present, a declaration to which `weakref' is attached can only
1729 // be `static'.
1730 //
1731 // It also says
1732 //
1733 // Without a TARGET,
1734 // given as an argument to `weakref' or to `alias', `weakref' is
1735 // equivalent to `weak'.
1736 //
1737 // gcc 4.4.1 will accept
1738 // int a7 __attribute__((weakref));
1739 // as
1740 // int a7 __attribute__((weak));
1741 // This looks like a bug in gcc. We reject that for now. We should revisit
1742 // it if this behaviour is actually used.
1743
Rafael Espindolac18086a2010-02-23 22:00:30 +00001744 // GCC rejects
1745 // static ((alias ("y"), weakref)).
1746 // Should we? How to check that weakref is before or after alias?
1747
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001748 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1749 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1750 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001751 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001752 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001753 // GCC will accept anything as the argument of weakref. Should we
1754 // check for an existing decl?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001755 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1756 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001757
Michael Han99315932013-01-24 16:46:58 +00001758 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001759 WeakRefAttr(AL.getRange(), S.Context,
1760 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001761}
1762
Erich Keanee891aa92018-07-13 15:07:47 +00001763static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001764 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001765 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001766 return;
1767
1768 // Aliases should be on declarations, not definitions.
1769 const auto *FD = cast<FunctionDecl>(D);
1770 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001771 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001772 return;
1773 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001774
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001775 D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str,
1776 AL.getAttributeSpellingListIndex()));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001777}
1778
Erich Keanee891aa92018-07-13 15:07:47 +00001779static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001780 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001781 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001782 return;
1783
Douglas Gregore8bbc122011-09-02 00:18:52 +00001784 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001785 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001786 return;
1787 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001788 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001789 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001790 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001791
David Majnemer2dc81462015-01-19 09:00:28 +00001792 // Aliases should be on declarations, not definitions.
1793 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1794 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001795 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001796 return;
1797 }
1798 } else {
1799 const auto *VD = cast<VarDecl>(D);
1800 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001801 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001802 return;
1803 }
1804 }
1805
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001806 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001807
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001808 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1809 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001810}
1811
Erich Keanee891aa92018-07-13 15:07:47 +00001812static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001813 StringRef Model;
1814 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001815 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001816 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001817 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001818
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001819 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001820 if (Model != "global-dynamic" && Model != "local-dynamic"
1821 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001822 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001823 return;
1824 }
1825
Michael Han99315932013-01-24 16:46:58 +00001826 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001827 TLSModelAttr(AL.getRange(), S.Context, Model,
1828 AL.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001829}
1830
Erich Keanee891aa92018-07-13 15:07:47 +00001831static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001832 QualType ResultType = getFunctionOrMethodResultType(D);
1833 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1834 D->addAttr(::new (S.Context) RestrictAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001835 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
David Majnemer631a90b2015-02-04 07:23:21 +00001836 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001837 }
1838
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001839 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
Erich Keane44bacdf2018-08-09 13:21:32 +00001840 << AL << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001841}
1842
Erich Keane3efe0022018-07-20 14:13:28 +00001843static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1844 FunctionDecl *FD = cast<FunctionDecl>(D);
Erich Keane659c8712018-09-10 14:31:56 +00001845
1846 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1847 if (MD->getParent()->isLambda()) {
1848 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
1849 return;
1850 }
1851 }
1852
Erich Keane3efe0022018-07-20 14:13:28 +00001853 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1854 return;
1855
1856 SmallVector<IdentifierInfo *, 8> CPUs;
1857 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
1858 if (!AL.isArgIdent(ArgNo)) {
1859 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001860 << AL << AANT_ArgumentIdentifier;
Erich Keane3efe0022018-07-20 14:13:28 +00001861 return;
1862 }
1863
1864 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1865 StringRef CPUName = CPUArg->Ident->getName().trim();
1866
1867 if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1868 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1869 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1870 return;
1871 }
1872
1873 const TargetInfo &Target = S.Context.getTargetInfo();
1874 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1875 return Target.CPUSpecificManglingCharacter(CPUName) ==
1876 Target.CPUSpecificManglingCharacter(Cur->getName());
1877 })) {
1878 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1879 return;
1880 }
1881 CPUs.push_back(CPUArg->Ident);
1882 }
1883
1884 FD->setIsMultiVersion(true);
1885 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
1886 D->addAttr(::new (S.Context) CPUSpecificAttr(
1887 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
1888 AL.getAttributeSpellingListIndex()));
1889 else
1890 D->addAttr(::new (S.Context) CPUDispatchAttr(
1891 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
1892 AL.getAttributeSpellingListIndex()));
1893}
1894
Erich Keanee891aa92018-07-13 15:07:47 +00001895static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001896 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001897 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00001898 << AL << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001899 return;
1900 }
1901
Erich Keane44bacdf2018-08-09 13:21:32 +00001902 if (CommonAttr *CA = S.mergeCommonAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001903 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001904}
1905
Erich Keanee891aa92018-07-13 15:07:47 +00001906static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001907 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
Charles Davis0e379112016-08-08 21:19:08 +00001908 return;
1909
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001910 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001911 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
1912 const auto &Arch = Triple.getArch();
1913 if (Arch != llvm::Triple::x86 &&
1914 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001915 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
Erich Keane44bacdf2018-08-09 13:21:32 +00001916 << AL << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001917 return;
1918 }
1919 }
1920
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001921 D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context,
1922 AL.getAttributeSpellingListIndex()));
Charles Davis0e379112016-08-08 21:19:08 +00001923}
1924
Erich Keanee891aa92018-07-13 15:07:47 +00001925static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001926 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001927
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001928 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001929 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00001930 << Attrs << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001931 return;
1932 }
1933
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001934 D->addAttr(::new (S.Context) NoReturnAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00001935 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001936}
1937
Erich Keanee891aa92018-07-13 15:07:47 +00001938static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00001939 if (!S.getLangOpts().CFProtectionBranch)
1940 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
1941 else
1942 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00001943}
1944
Erich Keanee891aa92018-07-13 15:07:47 +00001945bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001946 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
1947 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00001948 return true;
1949 }
1950
1951 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001952}
1953
Erich Keanee891aa92018-07-13 15:07:47 +00001954bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001955 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001956 if (!AL.existsInTarget(Context.getTargetInfo())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001957 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001958 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001959 return true;
1960 }
1961
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001962 return false;
1963}
1964
Erich Keanee891aa92018-07-13 15:07:47 +00001965static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1966
Ted Kremenek5295ce82010-08-19 00:51:58 +00001967 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1968 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001969 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001970 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001971 if (!VD || (!VD->getType()->isBlockPointerType() &&
1972 !VD->getType()->isFunctionPointerType())) {
Erich Keane44bacdf2018-08-09 13:21:32 +00001973 S.Diag(AL.getLoc(), AL.isCXX11Attribute()
1974 ? diag::err_attribute_wrong_decl_type
1975 : diag::warn_attribute_wrong_decl_type)
1976 << AL << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001977 return;
1978 }
1979 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001980
Michael Han99315932013-01-24 16:46:58 +00001981 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001982 AnalyzerNoReturnAttr(AL.getRange(), S.Context,
1983 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001984}
1985
John Thompsoncdb847ba2010-08-09 21:53:52 +00001986// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00001987static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1988 /*
1989 Returning a Vector Class in Registers
1990
1991 According to the PPU ABI specifications, a class with a single member of
1992 vector type is returned in memory when used as the return value of a
1993 function.
1994 This results in inefficient code when implementing vector classes. To return
1995 the value in a single vector register, add the vecreturn attribute to the
1996 class definition. This attribute is also applicable to struct types.
1997
1998 Example:
1999
2000 struct Vector
2001 {
2002 __vector float xyzw;
2003 } __attribute__((vecreturn));
2004
2005 Vector Add(Vector lhs, Vector rhs)
2006 {
2007 Vector result;
2008 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2009 return result; // This will be returned in a register
2010 }
2011 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002012 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002013 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002014 return;
2015 }
2016
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002017 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002018 int count = 0;
2019
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002020 if (!isa<CXXRecordDecl>(R)) {
2021 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002022 return;
2023 }
2024
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002025 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2026 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002027 return;
2028 }
2029
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002030 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002031 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002032 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002033 return;
2034 }
2035 count++;
2036 }
2037
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002038 D->addAttr(::new (S.Context) VecReturnAttr(
2039 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002040}
2041
Richard Smithe233fbf2013-01-28 22:42:45 +00002042static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002043 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002044 if (isa<ParmVarDecl>(D)) {
2045 // [[carries_dependency]] can only be applied to a parameter if it is a
2046 // parameter of a function declaration or lambda.
2047 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002048 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002049 diag::err_carries_dependency_param_not_function_decl);
2050 return;
2051 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002052 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002053
2054 D->addAttr(::new (S.Context) CarriesDependencyAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002055 AL.getRange(), S.Context,
2056 AL.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002057}
2058
Erich Keanee891aa92018-07-13 15:07:47 +00002059static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002060 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002061
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002062 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002063 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002064 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Erich Keane44bacdf2018-08-09 13:21:32 +00002065 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002066
2067 D->addAttr(::new (S.Context) UnusedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002068 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002069}
2070
Erich Keanee891aa92018-07-13 15:07:47 +00002071static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002072 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002073 if (AL.getNumArgs() &&
2074 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002075 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002076
Michael Han99315932013-01-24 16:46:58 +00002077 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002078 ConstructorAttr(AL.getRange(), S.Context, priority,
2079 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002080}
2081
Erich Keanee891aa92018-07-13 15:07:47 +00002082static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002083 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002084 if (AL.getNumArgs() &&
2085 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002086 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002087
Michael Han99315932013-01-24 16:46:58 +00002088 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002089 DestructorAttr(AL.getRange(), S.Context, priority,
2090 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002091}
2092
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002093template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002094static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002095 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002096 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002097 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002098 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002099
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002100 D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str,
2101 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002102}
2103
Ted Kremenek438f8db2014-02-22 01:06:05 +00002104static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002105 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002106 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002107 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
Erich Keane44bacdf2018-08-09 13:21:32 +00002108 << AL << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002109 return;
2110 }
2111
Ted Kremenek28eace62013-11-23 01:01:34 +00002112 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002113 ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context,
2114 AL.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00002115}
2116
Jordy Rose740b0c22012-05-08 03:27:22 +00002117static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2118 IdentifierInfo *Platform,
2119 VersionTuple Introduced,
2120 VersionTuple Deprecated,
2121 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002122 StringRef PlatformName
2123 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2124 if (PlatformName.empty())
2125 PlatformName = Platform->getName();
2126
2127 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2128 // of these steps are needed).
2129 if (!Introduced.empty() && !Deprecated.empty() &&
2130 !(Introduced <= Deprecated)) {
2131 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2132 << 1 << PlatformName << Deprecated.getAsString()
2133 << 0 << Introduced.getAsString();
2134 return true;
2135 }
2136
2137 if (!Introduced.empty() && !Obsoleted.empty() &&
2138 !(Introduced <= Obsoleted)) {
2139 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2140 << 2 << PlatformName << Obsoleted.getAsString()
2141 << 0 << Introduced.getAsString();
2142 return true;
2143 }
2144
2145 if (!Deprecated.empty() && !Obsoleted.empty() &&
2146 !(Deprecated <= Obsoleted)) {
2147 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2148 << 2 << PlatformName << Obsoleted.getAsString()
2149 << 1 << Deprecated.getAsString();
2150 return true;
2151 }
2152
2153 return false;
2154}
2155
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002156/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002157///
2158/// If either version tuple is empty, then they are assumed to match. If
2159/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2160static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2161 bool BeforeIsOkay) {
2162 if (X.empty() || Y.empty())
2163 return true;
2164
2165 if (X == Y)
2166 return true;
2167
2168 if (BeforeIsOkay && X < Y)
2169 return true;
2170
2171 return false;
2172}
2173
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002174AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002175 IdentifierInfo *Platform,
Manman Ren719a8642016-05-06 21:04:01 +00002176 bool Implicit,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002177 VersionTuple Introduced,
2178 VersionTuple Deprecated,
2179 VersionTuple Obsoleted,
2180 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002181 StringRef Message,
Manman Rend8039df2016-02-22 04:47:24 +00002182 bool IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002183 StringRef Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002184 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00002185 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002186 VersionTuple MergedIntroduced = Introduced;
2187 VersionTuple MergedDeprecated = Deprecated;
2188 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002189 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002190 bool OverrideOrImpl = false;
2191 switch (AMK) {
2192 case AMK_None:
2193 case AMK_Redeclaration:
2194 OverrideOrImpl = false;
2195 break;
2196
2197 case AMK_Override:
2198 case AMK_ProtocolImplementation:
2199 OverrideOrImpl = true;
2200 break;
2201 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002202
Rafael Espindolac67f2232012-05-10 02:50:16 +00002203 if (D->hasAttrs()) {
2204 AttrVec &Attrs = D->getAttrs();
2205 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002206 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002207 if (!OldAA) {
2208 ++i;
2209 continue;
2210 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002211
Rafael Espindolac67f2232012-05-10 02:50:16 +00002212 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2213 if (OldPlatform != Platform) {
2214 ++i;
2215 continue;
2216 }
2217
Tim Northover7a73cc72015-10-30 16:30:49 +00002218 // If there is an existing availability attribute for this platform that
2219 // is explicit and the new one is implicit use the explicit one and
2220 // discard the new implicit attribute.
Manman Ren719a8642016-05-06 21:04:01 +00002221 if (!OldAA->isImplicit() && Implicit) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002222 return nullptr;
2223 }
2224
2225 // If there is an existing attribute for this platform that is implicit
2226 // and the new attribute is explicit then erase the old one and
2227 // continue processing the attributes.
Manman Ren719a8642016-05-06 21:04:01 +00002228 if (!Implicit && OldAA->isImplicit()) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002229 Attrs.erase(Attrs.begin() + i);
2230 --e;
2231 continue;
2232 }
2233
Rafael Espindolac67f2232012-05-10 02:50:16 +00002234 FoundAny = true;
2235 VersionTuple OldIntroduced = OldAA->getIntroduced();
2236 VersionTuple OldDeprecated = OldAA->getDeprecated();
2237 VersionTuple OldObsoleted = OldAA->getObsoleted();
2238 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002239
Douglas Gregord2a713e2015-09-30 21:27:42 +00002240 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2241 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2242 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002243 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002244 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2245 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002246 int Which = -1;
2247 VersionTuple FirstVersion;
2248 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002249 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002250 Which = 0;
2251 FirstVersion = OldIntroduced;
2252 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002253 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002254 Which = 1;
2255 FirstVersion = Deprecated;
2256 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002257 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002258 Which = 2;
2259 FirstVersion = Obsoleted;
2260 SecondVersion = OldObsoleted;
2261 }
2262
2263 if (Which == -1) {
2264 Diag(OldAA->getLocation(),
2265 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002266 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2267 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002268 } else {
2269 Diag(OldAA->getLocation(),
2270 diag::warn_mismatched_availability_override)
2271 << Which
2272 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002273 << FirstVersion.getAsString() << SecondVersion.getAsString()
2274 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002275 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002276 if (AMK == AMK_Override)
2277 Diag(Range.getBegin(), diag::note_overridden_method);
2278 else
2279 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002280 } else {
2281 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2282 Diag(Range.getBegin(), diag::note_previous_attribute);
2283 }
2284
Rafael Espindolac67f2232012-05-10 02:50:16 +00002285 Attrs.erase(Attrs.begin() + i);
2286 --e;
2287 continue;
2288 }
2289
2290 VersionTuple MergedIntroduced2 = MergedIntroduced;
2291 VersionTuple MergedDeprecated2 = MergedDeprecated;
2292 VersionTuple MergedObsoleted2 = MergedObsoleted;
2293
2294 if (MergedIntroduced2.empty())
2295 MergedIntroduced2 = OldIntroduced;
2296 if (MergedDeprecated2.empty())
2297 MergedDeprecated2 = OldDeprecated;
2298 if (MergedObsoleted2.empty())
2299 MergedObsoleted2 = OldObsoleted;
2300
2301 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2302 MergedIntroduced2, MergedDeprecated2,
2303 MergedObsoleted2)) {
2304 Attrs.erase(Attrs.begin() + i);
2305 --e;
2306 continue;
2307 }
2308
2309 MergedIntroduced = MergedIntroduced2;
2310 MergedDeprecated = MergedDeprecated2;
2311 MergedObsoleted = MergedObsoleted2;
2312 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002313 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002314 }
2315
2316 if (FoundAny &&
2317 MergedIntroduced == Introduced &&
2318 MergedDeprecated == Deprecated &&
2319 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002320 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002321
Douglas Gregord2a713e2015-09-30 21:27:42 +00002322 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002323 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002324 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002325 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002326 !OverrideOrImpl) {
Manman Ren719a8642016-05-06 21:04:01 +00002327 auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002328 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002329 Obsoleted, IsUnavailable, Message,
Manman Ren75bc6762016-03-21 17:30:55 +00002330 IsStrict, Replacement,
2331 AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002332 Avail->setImplicit(Implicit);
2333 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002334 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002335 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002336}
2337
Erich Keanee891aa92018-07-13 15:07:47 +00002338static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002339 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002340 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002341 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2342 unsigned Index = AL.getAttributeSpellingListIndex();
Fangrui Song6907ce22018-07-30 19:24:48 +00002343
Aaron Ballman00e99962013-08-31 01:11:41 +00002344 IdentifierInfo *II = Platform->Ident;
2345 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2346 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2347 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002348
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002349 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002350 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002351 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002352
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002353 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2354 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2355 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2356 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2357 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002358 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002359 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002360 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002361 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002362 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002363 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002364
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002365 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II,
Manman Ren719a8642016-05-06 21:04:01 +00002366 false/*Implicit*/,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002367 Introduced.Version,
2368 Deprecated.Version,
2369 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002370 IsUnavailable, Str,
Manman Ren75bc6762016-03-21 17:30:55 +00002371 IsStrict, Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002372 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002373 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002374 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002375 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002376
2377 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2378 // matches before the start of the watchOS platform.
2379 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2380 IdentifierInfo *NewII = nullptr;
2381 if (II->getName() == "ios")
2382 NewII = &S.Context.Idents.get("watchos");
2383 else if (II->getName() == "ios_app_extension")
2384 NewII = &S.Context.Idents.get("watchos_app_extension");
2385
2386 if (NewII) {
2387 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2388 if (Version.empty())
2389 return Version;
2390 auto Major = Version.getMajor();
2391 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2392 if (NewMajor >= 2) {
2393 if (Version.getMinor().hasValue()) {
2394 if (Version.getSubminor().hasValue())
2395 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2396 Version.getSubminor().getValue());
2397 else
2398 return VersionTuple(NewMajor, Version.getMinor().getValue());
2399 }
2400 }
2401
2402 return VersionTuple(2, 0);
2403 };
2404
2405 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2406 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2407 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2408
2409 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002410 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002411 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002412 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002413 NewIntroduced,
2414 NewDeprecated,
2415 NewObsoleted,
2416 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002417 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002418 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002419 Sema::AMK_None,
2420 Index);
2421 if (NewAttr)
2422 D->addAttr(NewAttr);
2423 }
2424 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2425 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2426 // matches before the start of the tvOS platform.
2427 IdentifierInfo *NewII = nullptr;
2428 if (II->getName() == "ios")
2429 NewII = &S.Context.Idents.get("tvos");
2430 else if (II->getName() == "ios_app_extension")
2431 NewII = &S.Context.Idents.get("tvos_app_extension");
2432
2433 if (NewII) {
2434 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002435 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002436 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002437 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002438 Introduced.Version,
2439 Deprecated.Version,
2440 Obsoleted.Version,
2441 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002442 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002443 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002444 Sema::AMK_None,
2445 Index);
2446 if (NewAttr)
2447 D->addAttr(NewAttr);
2448 }
2449 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002450}
2451
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002452static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002453 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002454 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002455 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002456 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002457 "Invalid number of arguments in an external_source_symbol attribute");
2458
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002459 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002460 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002461 Language = SE->getString();
2462 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002463 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002464 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002465 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002466
2467 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002468 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2469 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002470}
2471
John McCalld041a9b2013-02-20 01:54:26 +00002472template <class T>
2473static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2474 typename T::VisibilityType value,
2475 unsigned attrSpellingListIndex) {
2476 T *existingAttr = D->getAttr<T>();
2477 if (existingAttr) {
2478 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2479 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002480 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002481 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2482 S.Diag(range.getBegin(), diag::note_previous_attribute);
2483 D->dropAttr<T>();
2484 }
2485 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2486}
2487
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002488VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002489 VisibilityAttr::VisibilityType Vis,
2490 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002491 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2492 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002493}
2494
John McCalld041a9b2013-02-20 01:54:26 +00002495TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2496 TypeVisibilityAttr::VisibilityType Vis,
2497 unsigned AttrSpellingListIndex) {
2498 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2499 AttrSpellingListIndex);
2500}
2501
Erich Keanee891aa92018-07-13 15:07:47 +00002502static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002503 bool isTypeVisibility) {
2504 // Visibility attributes don't mean anything on a typedef.
2505 if (isa<TypedefNameDecl>(D)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002506 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
John McCalld041a9b2013-02-20 01:54:26 +00002507 return;
2508 }
2509
2510 // 'type_visibility' can only go on a type or namespace.
2511 if (isTypeVisibility &&
2512 !(isa<TagDecl>(D) ||
2513 isa<ObjCInterfaceDecl>(D) ||
2514 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002515 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002516 << AL << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002517 return;
2518 }
2519
Benjamin Kramer70370212013-09-09 15:08:57 +00002520 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002521 StringRef TypeStr;
2522 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002523 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002524 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002525
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002526 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002527 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002528 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2529 << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002530 return;
2531 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002532
Aaron Ballman682ee422013-09-11 19:47:58 +00002533 // Complain about attempts to use protected visibility on targets
2534 // (like Darwin) that don't support it.
2535 if (type == VisibilityAttr::Protected &&
2536 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002537 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002538 type = VisibilityAttr::Default;
2539 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002540
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002541 unsigned Index = AL.getAttributeSpellingListIndex();
2542 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002543 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002544 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002545 (TypeVisibilityAttr::VisibilityType) type,
2546 Index);
2547 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002548 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002549 }
2550 if (newAttr)
2551 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002552}
2553
Erich Keanee891aa92018-07-13 15:07:47 +00002554static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002555 const auto *M = cast<ObjCMethodDecl>(D);
2556 if (!AL.isArgIdent(0)) {
2557 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002558 << AL << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002559 return;
2560 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002561
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002562 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002563 ObjCMethodFamilyAttr::FamilyKind F;
2564 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002565 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002566 return;
2567 }
2568
Alp Toker314cc812014-01-25 16:55:45 +00002569 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002570 !M->getReturnType()->isObjCObjectPointerType()) {
2571 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2572 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002573 // Ignore the attribute.
2574 return;
2575 }
2576
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002577 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2578 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002579}
2580
Erich Keanee891aa92018-07-13 15:07:47 +00002581static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002582 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002583 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002584 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002585 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2586 return;
2587 }
2588 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002589 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002590 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002591 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002592 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2593 return;
2594 }
2595 }
2596 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002597 // It is okay to include this attribute on properties, e.g.:
2598 //
2599 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2600 //
2601 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002602 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002603 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002604 }
Michael Han99315932013-01-24 16:46:58 +00002605 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002606 ObjCNSObjectAttr(AL.getRange(), S.Context,
2607 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002608}
2609
Erich Keanee891aa92018-07-13 15:07:47 +00002610static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002611 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002612 QualType T = TD->getUnderlyingType();
2613 if (!T->isObjCObjectPointerType()) {
2614 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2615 return;
2616 }
2617 } else {
2618 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2619 return;
2620 }
2621 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002622 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2623 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002624}
2625
Erich Keanee891aa92018-07-13 15:07:47 +00002626static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002627 if (!AL.isArgIdent(0)) {
2628 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002629 << AL << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002630 return;
2631 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002632
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002633 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002634 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002635 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002636 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002637 return;
2638 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002639
Michael Han99315932013-01-24 16:46:58 +00002640 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002641 BlocksAttr(AL.getRange(), S.Context, type,
2642 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002643}
2644
Erich Keanee891aa92018-07-13 15:07:47 +00002645static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002646 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002647 if (AL.getNumArgs() > 0) {
2648 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002649 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002650 if (E->isTypeDependent() || E->isValueDependent() ||
2651 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002652 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002653 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002654 return;
2655 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002656
John McCallb46f2872011-09-09 07:56:05 +00002657 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002658 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002659 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002660 return;
2661 }
John McCallb46f2872011-09-09 07:56:05 +00002662
2663 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002664 }
2665
Aaron Ballman18a78382013-11-21 00:28:23 +00002666 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002667 if (AL.getNumArgs() > 1) {
2668 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002669 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002670 if (E->isTypeDependent() || E->isValueDependent() ||
2671 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002672 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002673 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002674 return;
2675 }
2676 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002677
John McCallb46f2872011-09-09 07:56:05 +00002678 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002679 // FIXME: This error message could be improved, it would be nice
2680 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002681 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002682 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002683 return;
2684 }
2685 }
2686
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002687 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002688 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002689 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002690 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002691 return;
2692 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002693
Chris Lattner9363e312009-03-17 23:03:47 +00002694 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002695 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002696 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002697 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002698 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002699 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002700 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002701 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002702 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002703 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002704 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002705 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002706 return;
2707 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002708 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002709 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002710 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002711 const FunctionType *FT = Ty->isFunctionPointerType()
2712 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002713 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002714 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002715 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002716 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002717 return;
2718 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002719 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002720 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002721 << AL << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002722 return;
2723 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002724 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002725 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002726 << AL << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002727 return;
2728 }
Michael Han99315932013-01-24 16:46:58 +00002729 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002730 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2731 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002732}
2733
Erich Keanee891aa92018-07-13 15:07:47 +00002734static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002735 if (D->getFunctionType() &&
2736 D->getFunctionType()->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002737 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002738 return;
2739 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002740 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002741 if (MD->getReturnType()->isVoidType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002742 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002743 return;
2744 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002745
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002746 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002747 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002748 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2749 !AL.getScopeName())
Erich Keane44bacdf2018-08-09 13:21:32 +00002750 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
Aaron Ballmane7964782016-03-07 22:44:55 +00002751
Fangrui Song6907ce22018-07-30 19:24:48 +00002752 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002753 WarnUnusedResultAttr(AL.getRange(), S.Context,
2754 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002755}
2756
Erich Keanee891aa92018-07-13 15:07:47 +00002757static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002758 // weak_import only applies to variable & function declarations.
2759 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002760 if (!D->canBeWeakImported(isDef)) {
2761 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002762 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002763 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002764 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002765 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002766 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002767 // Nothing to warn about here.
2768 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002769 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00002770 << AL << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002771
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002772 return;
2773 }
2774
Michael Han99315932013-01-24 16:46:58 +00002775 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002776 WeakImportAttr(AL.getRange(), S.Context,
2777 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002778}
2779
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002780// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002781template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002782static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002783 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002784 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002785 const Expr *E = AL.getArgAsExpr(i);
Andrew Savonichevd353e6d2018-09-06 11:54:09 +00002786 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2787 /*StrictlyUnsigned=*/true))
Nate Begemanf2758702009-06-26 06:32:41 +00002788 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002789 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002790 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002791 << AL << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002792 return;
2793 }
2794 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002795
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002796 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2797 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2798 Existing->getYDim() == WGSize[1] &&
2799 Existing->getZDim() == WGSize[2]))
Erich Keane44bacdf2018-08-09 13:21:32 +00002800 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002801
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002802 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002803 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002804 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002805}
2806
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002807// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002808static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002809 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002810 const Expr *E = AL.getArgAsExpr(0);
2811 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002812 return;
2813 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002814 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
Erich Keane44bacdf2018-08-09 13:21:32 +00002815 << AL << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002816 return;
2817 }
2818
2819 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2820 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2821 if (Existing && Existing->getSubGroupSize() != SGSize)
Erich Keane44bacdf2018-08-09 13:21:32 +00002822 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002823
2824 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002825 AL.getRange(), S.Context, SGSize,
2826 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002827}
2828
Erich Keanee891aa92018-07-13 15:07:47 +00002829static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002830 if (!AL.hasParsedType()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002831 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002832 return;
2833 }
2834
Craig Topperc3ec1492014-05-26 06:22:03 +00002835 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002836 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002837 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002838
2839 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2840 (ParmType->isBooleanType() ||
2841 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002842 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002843 << ParmType;
2844 return;
2845 }
2846
Aaron Ballmana9e05402013-12-02 22:16:55 +00002847 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002848 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00002849 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Joey Goulyaba589c2013-03-08 09:42:32 +00002850 return;
2851 }
2852 }
2853
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002854 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002855 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002856 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002857}
2858
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002859SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002860 StringRef Name,
2861 unsigned AttrSpellingListIndex) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002862 // Explicit or partial specializations do not inherit
2863 // the section attribute from the primary template.
2864 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2865 if (AttrSpellingListIndex == SectionAttr::Declspec_allocate &&
2866 FD->isFunctionTemplateSpecialization())
2867 return nullptr;
2868 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002869 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2870 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002871 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002872 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2873 << 1 /*section*/;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002874 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002875 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002876 }
Michael Han99315932013-01-24 16:46:58 +00002877 return ::new (Context) SectionAttr(Range, Context, Name,
2878 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002879}
2880
Reid Kleckner2a133222015-03-04 23:39:17 +00002881bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2882 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2883 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002884 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002885 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002886 return false;
2887 }
2888 return true;
2889}
2890
Erich Keanee891aa92018-07-13 15:07:47 +00002891static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002892 // Make sure that there is a string literal as the sections's single
2893 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002894 StringRef Str;
2895 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002896 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002897 return;
Mike Stump11289f42009-09-09 15:08:12 +00002898
Reid Kleckner2a133222015-03-04 23:39:17 +00002899 if (!S.checkSectionName(LiteralLoc, Str))
2900 return;
2901
Chris Lattner30ba6742009-08-10 19:03:04 +00002902 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002903 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002904 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002905 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002906 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002907 return;
2908 }
Mike Stump11289f42009-09-09 15:08:12 +00002909
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002910 unsigned Index = AL.getAttributeSpellingListIndex();
2911 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002912 if (NewAttr)
2913 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002914}
2915
Erich Keane7963e8b2018-07-18 20:04:48 +00002916static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) {
2917 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
2918 if (!Error.empty()) {
2919 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
2920 << 0 /*'code-seg'*/;
2921 return false;
2922 }
2923 return true;
2924}
2925
2926CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range,
2927 StringRef Name,
2928 unsigned AttrSpellingListIndex) {
2929 // Explicit or partial specializations do not inherit
2930 // the code_seg attribute from the primary template.
2931 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2932 if (FD->isFunctionTemplateSpecialization())
2933 return nullptr;
2934 }
2935 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
2936 if (ExistingAttr->getName() == Name)
2937 return nullptr;
2938 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2939 << 0 /*codeseg*/;
2940 Diag(Range.getBegin(), diag::note_previous_attribute);
2941 return nullptr;
2942 }
2943 return ::new (Context) CodeSegAttr(Range, Context, Name,
2944 AttrSpellingListIndex);
2945}
2946
2947static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2948 StringRef Str;
2949 SourceLocation LiteralLoc;
2950 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
2951 return;
2952 if (!checkCodeSegName(S, LiteralLoc, Str))
2953 return;
2954 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
2955 if (!ExistingAttr->isImplicit()) {
2956 S.Diag(AL.getLoc(),
2957 ExistingAttr->getName() == Str
2958 ? diag::warn_duplicate_codeseg_attribute
2959 : diag::err_conflicting_codeseg_attribute);
2960 return;
2961 }
2962 D->dropAttr<CodeSegAttr>();
2963 }
2964 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str,
2965 AL.getAttributeSpellingListIndex()))
2966 D->addAttr(CSA);
2967}
2968
Erich Keane57e15cd2017-07-19 22:06:33 +00002969// Check for things we'd like to warn about. Multiversioning issues are
2970// handled later in the process, once we know how many exist.
2971bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2972 enum FirstParam { Unsupported, Duplicate };
2973 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00002974 for (auto Str : {"tune=", "fpmath="})
2975 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00002976 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2977 << Unsupported << None << Str;
2978
2979 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
2980
2981 if (!ParsedAttrs.Architecture.empty() &&
2982 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
2983 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2984 << Unsupported << Architecture << ParsedAttrs.Architecture;
2985
2986 if (ParsedAttrs.DuplicateArchitecture)
2987 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2988 << Duplicate << None << "arch=";
2989
2990 for (const auto &Feature : ParsedAttrs.Features) {
2991 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
2992 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
2993 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2994 << Unsupported << None << CurFeature;
2995 }
2996
Erich Keane29636aa2018-02-16 17:31:59 +00002997 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002998}
2999
Erich Keanee891aa92018-07-13 15:07:47 +00003000static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003001 StringRef Str;
3002 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003003 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003004 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003005 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003006
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003007 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00003008 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003009 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00003010 D->addAttr(NewAttr);
3011}
3012
Erich Keanee891aa92018-07-13 15:07:47 +00003013static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003014 Expr *E = AL.getArgAsExpr(0);
3015 uint32_t VecWidth;
3016 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3017 AL.setInvalid();
3018 return;
3019 }
3020
3021 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3022 if (Existing && Existing->getVectorWidth() != VecWidth) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003023 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
Craig Topper74c10e32018-07-09 19:00:16 +00003024 return;
3025 }
3026
3027 D->addAttr(::new (S.Context)
3028 MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth,
3029 AL.getAttributeSpellingListIndex()));
3030}
3031
Erich Keanee891aa92018-07-13 15:07:47 +00003032static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003033 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003034 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003035 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003036 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003037
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003038 // gcc only allows for simple identifiers. Since we support more than gcc, we
3039 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003040 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003041 if (DRE->hasQualifier())
3042 S.Diag(Loc, diag::warn_cleanup_ext);
3043 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3044 NI = DRE->getNameInfo();
3045 if (!FD) {
3046 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3047 << NI.getName();
3048 return;
3049 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003050 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003051 if (ULE->hasExplicitTemplateArgs())
3052 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003053 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3054 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003055 if (!FD) {
3056 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3057 << NI.getName();
3058 if (ULE->getType() == S.Context.OverloadTy)
3059 S.NoteAllOverloadCandidates(ULE);
3060 return;
3061 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003062 } else {
3063 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003064 return;
3065 }
3066
Anders Carlssond277d792009-01-31 01:16:18 +00003067 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003068 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3069 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003070 return;
3071 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003072
Anders Carlsson723f55d2009-02-07 23:16:50 +00003073 // We're currently more strict than GCC about what function types we accept.
3074 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003075 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003076 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003077 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3078 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003079 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3080 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003081 return;
3082 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003083
Michael Han99315932013-01-24 16:46:58 +00003084 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003085 CleanupAttr(AL.getRange(), S.Context, FD,
3086 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003087}
3088
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003089static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003090 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003091 if (!AL.isArgIdent(0)) {
3092 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003093 << AL << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003094 return;
3095 }
3096
3097 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003098 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003099 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3100 ExtensibilityKind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003101 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003102 return;
3103 }
3104
3105 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003106 AL.getRange(), S.Context, ExtensibilityKind,
3107 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003108}
3109
Mike Stumpd3bb5572009-07-24 19:02:52 +00003110/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003111/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003112static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003113 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003114 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003115 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003116 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003117
Eric Christopherb64963e2015-08-13 21:34:35 +00003118 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003119 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003120
Eric Christopherb64963e2015-08-13 21:34:35 +00003121 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3122 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003123 !isCFStringType(Ty, S.Context) &&
3124 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003125 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003126 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003127 << "a string type" << IdxExpr->getSourceRange()
3128 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003129 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003130 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003131 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003132 if (!isNSStringType(Ty, S.Context) &&
3133 !isCFStringType(Ty, S.Context) &&
3134 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003135 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003136 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003137 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003138 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003139 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003140 }
3141
Joel E. Denny81508102018-03-13 14:51:22 +00003142 D->addAttr(::new (S.Context) FormatArgAttr(
3143 AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003144}
3145
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003146enum FormatAttrKind {
3147 CFStringFormat,
3148 NSStringFormat,
3149 StrftimeFormat,
3150 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003151 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003152 InvalidFormat
3153};
3154
3155/// getFormatAttrKind - Map from format attribute names to supported format
3156/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003157static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003158 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003159 // Check for formats that get handled specially.
3160 .Case("NSString", NSStringFormat)
3161 .Case("CFString", CFStringFormat)
3162 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003163
Mehdi Amini06d367c2016-10-24 20:39:34 +00003164 // Otherwise, check for supported formats.
3165 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3166 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3167 .Case("kprintf", SupportedFormat) // OpenBSD.
3168 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3169 .Case("os_trace", SupportedFormat)
3170 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003171
Mehdi Amini06d367c2016-10-24 20:39:34 +00003172 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3173 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003174}
3175
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003176/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003177/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003178static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003179 if (!S.getLangOpts().CPlusPlus) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003180 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003181 return;
3182 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003183
Aaron Ballman4a611152013-11-27 16:34:09 +00003184 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003185 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3186 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003187 return;
3188 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003189 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003190 if (S.Context.getAsArrayType(T))
3191 T = S.Context.getBaseElementType(T);
3192 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003193 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3194 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003195 return;
3196 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003197
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003198 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003199 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003200 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3201 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003202 return;
3203 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003204
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003205 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003206 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
Erich Keane44bacdf2018-08-09 13:21:32 +00003207 << E->getSourceRange() << AL << 101 << 65535;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003208 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003209 return;
3210 }
Michael Han99315932013-01-24 16:46:58 +00003211 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003212 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3213 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003214}
3215
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003216FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3217 IdentifierInfo *Format, int FormatIdx,
3218 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003219 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003220 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003221 for (auto *F : D->specific_attrs<FormatAttr>()) {
3222 if (F->getType() == Format &&
3223 F->getFormatIdx() == FormatIdx &&
3224 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003225 // If we don't have a valid location for this attribute, adopt the
3226 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003227 if (F->getLocation().isInvalid())
3228 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003229 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003230 }
3231 }
3232
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003233 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3234 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003235}
3236
Mike Stumpd3bb5572009-07-24 19:02:52 +00003237/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003238/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003239static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003240 if (!AL.isArgIdent(0)) {
3241 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00003242 << AL << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003243 return;
3244 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003245
Chandler Carruth743682b2010-11-16 08:35:43 +00003246 // In C++ the implicit 'this' function parameter also counts, and they are
3247 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003248 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003249 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003250
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003251 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003252 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003253
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003254 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003255 // If we've modified the string name, we need a new identifier for it.
3256 II = &S.Context.Idents.get(Format);
3257 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003258
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003259 // Check for supported formats.
3260 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003261
Chris Lattner12161d32010-03-22 21:08:50 +00003262 if (Kind == IgnoredFormat)
3263 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003264
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003265 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003266 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00003267 << AL << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003268 return;
3269 }
3270
3271 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003272 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003273 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003274 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003275 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003276
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003277 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003278 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003279 << AL << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003280 return;
3281 }
3282
3283 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003284 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003285
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003286 if (HasImplicitThisParam) {
3287 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003288 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003289 diag::err_format_attribute_implicit_this_format_string)
3290 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003291 return;
3292 }
3293 ArgIdx--;
3294 }
Mike Stump11289f42009-09-09 15:08:12 +00003295
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003296 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003297 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003298
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003299 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003300 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003301 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003302 << "a CFString" << IdxExpr->getSourceRange()
3303 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003304 return;
3305 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003306 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003307 // FIXME: do we need to check if the type is NSString*? What are the
3308 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003309 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003310 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003311 << "an NSString" << IdxExpr->getSourceRange()
3312 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003313 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003314 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003315 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003316 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003317 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003318 << "a string type" << IdxExpr->getSourceRange()
3319 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003320 return;
3321 }
3322
3323 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003324 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003325 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003326 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003327 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003328
3329 // check if the function is variadic if the 3rd argument non-zero
3330 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003331 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003332 ++NumArgs; // +1 for ...
3333 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003334 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003335 return;
3336 }
3337 }
3338
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003339 // strftime requires FirstArg to be 0 because it doesn't read from any
3340 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003341 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003342 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003343 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003344 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003345 return;
3346 }
3347 // if 0 it disables parameter checking (to use with e.g. va_list)
3348 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003349 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00003350 << AL << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003351 return;
3352 }
3353
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003354 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003355 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003356 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003357 if (NewAttr)
3358 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003359}
3360
Erich Keanee891aa92018-07-13 15:07:47 +00003361static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003362 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003363 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003364 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003365 if (TD && TD->getUnderlyingType()->isUnionType())
3366 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3367 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003368 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003369
3370 if (!RD || !RD->isUnion()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003371 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3372 << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003373 return;
3374 }
3375
John McCallf937c022011-10-07 06:10:15 +00003376 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003377 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003378 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003379 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003380 return;
3381 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003382
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003383 RecordDecl::field_iterator Field = RD->field_begin(),
3384 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003385 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003386 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003387 return;
3388 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003389
David Blaikie40ed2972012-06-06 20:45:41 +00003390 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003391 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003392 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003393 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003394 diag::warn_transparent_union_attribute_floating)
3395 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003396 return;
3397 }
3398
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003399 if (FirstType->isIncompleteType())
3400 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003401 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3402 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3403 for (; Field != FieldEnd; ++Field) {
3404 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003405 if (FieldType->isIncompleteType())
3406 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003407 // FIXME: this isn't fully correct; we also need to test whether the
3408 // members of the union would all have the same calling convention as the
3409 // first member of the union. Checking just the size and alignment isn't
3410 // sufficient (consider structs passed on the stack instead of in registers
3411 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003412 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003413 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003414 // Warn if we drop the attribute.
3415 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003416 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003417 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003418 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003419 diag::warn_transparent_union_attribute_field_size_align)
3420 << isSize << Field->getDeclName() << FieldBits;
3421 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003422 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003423 diag::note_transparent_union_first_field_size_align)
3424 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003425 return;
3426 }
3427 }
3428
Michael Han99315932013-01-24 16:46:58 +00003429 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003430 TransparentUnionAttr(AL.getRange(), S.Context,
3431 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003432}
3433
Erich Keanee891aa92018-07-13 15:07:47 +00003434static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003435 // Make sure that there is a string literal as the annotation's single
3436 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003437 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003438 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003439 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003440
3441 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003442 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3443 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003444 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003445 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003446
Michael Han99315932013-01-24 16:46:58 +00003447 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003448 AnnotateAttr(AL.getRange(), S.Context, Str,
3449 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003450}
3451
Erich Keanee891aa92018-07-13 15:07:47 +00003452static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003453 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3454 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003455}
3456
3457void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3458 unsigned SpellingListIndex) {
3459 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3460 SourceLocation AttrLoc = AttrRange.getBegin();
3461
3462 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003463 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003464 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003465 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003466 T = VD->getType();
3467 else
3468 llvm_unreachable("Unknown decl type for align_value");
3469
3470 if (!T->isDependentType() && !T->isAnyPointerType() &&
3471 !T->isReferenceType() && !T->isMemberPointerType()) {
3472 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3473 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3474 return;
3475 }
3476
3477 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003478 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003479 ExprResult ICE
3480 = VerifyIntegerConstantExpression(E, &Alignment,
3481 diag::err_align_value_attribute_argument_not_int,
3482 /*AllowFold*/ false);
3483 if (ICE.isInvalid())
3484 return;
3485
3486 if (!Alignment.isPowerOf2()) {
3487 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3488 << E->getSourceRange();
3489 return;
3490 }
3491
3492 D->addAttr(::new (Context)
3493 AlignValueAttr(AttrRange, Context, ICE.get(),
3494 SpellingListIndex));
3495 return;
3496 }
3497
3498 // Save dependent expressions in the AST to be instantiated.
3499 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003500}
3501
Erich Keanee891aa92018-07-13 15:07:47 +00003502static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003503 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003504 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003505 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003506 return;
3507 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003508
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003509 if (AL.getNumArgs() == 0) {
3510 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3511 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003512 return;
3513 }
3514
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003515 Expr *E = AL.getArgAsExpr(0);
3516 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3517 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003518 diag::err_pack_expansion_without_parameter_packs);
3519 return;
3520 }
3521
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003522 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003523 return;
3524
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003525 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3526 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003527}
3528
3529void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003530 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003531 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3532 SourceLocation AttrLoc = AttrRange.getBegin();
3533
Richard Smith1dba27c2013-01-29 09:02:09 +00003534 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003535 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003536 // C++11 [dcl.align]p1:
3537 // An alignment-specifier may be applied to a variable or to a class
3538 // data member, but it shall not be applied to a bit-field, a function
3539 // parameter, the formal parameter of a catch clause, or a variable
3540 // declared with the register storage class specifier. An
3541 // alignment-specifier may also be applied to the declaration of a class
3542 // or enumeration type.
3543 // C11 6.7.5/2:
3544 // An alignment attribute shall not be specified in a declaration of
3545 // a typedef, or a bit-field, or a function, or a parameter, or an
3546 // object declared with the register storage-class specifier.
3547 int DiagKind = -1;
3548 if (isa<ParmVarDecl>(D)) {
3549 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003550 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003551 if (VD->getStorageClass() == SC_Register)
3552 DiagKind = 1;
3553 if (VD->isExceptionVariable())
3554 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003555 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003556 if (FD->isBitField())
3557 DiagKind = 3;
3558 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003559 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003560 << (TmpAttr.isC11() ? ExpectedVariableOrField
3561 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003562 return;
3563 }
3564 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003565 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003566 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003567 return;
3568 }
3569 }
3570
Richard Smith90ae9672018-06-20 23:36:55 +00003571 if (E->isValueDependent()) {
3572 // We can't support a dependent alignment on a non-dependent type,
3573 // because we have no way to model that a type is "alignment-dependent"
3574 // but not dependent in any other way.
3575 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3576 if (!TND->getUnderlyingType()->isDependentType()) {
3577 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3578 << E->getSourceRange();
3579 return;
3580 }
3581 }
3582
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003583 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003584 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3585 AA->setPackExpansion(IsPackExpansion);
3586 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003587 return;
3588 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003589
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003590 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003591 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003592 ExprResult ICE
3593 = VerifyIntegerConstantExpression(E, &Alignment,
3594 diag::err_aligned_attribute_argument_not_int,
3595 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003596 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003597 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003598
David Majnemer0be6bd02015-07-26 09:02:21 +00003599 uint64_t AlignVal = Alignment.getZExtValue();
3600
Richard Smith848e1f12013-02-01 08:12:08 +00003601 // C++11 [dcl.align]p2:
3602 // -- if the constant expression evaluates to zero, the alignment
3603 // specifier shall have no effect
3604 // C11 6.7.5p6:
3605 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003606 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003607 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003608 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3609 << E->getSourceRange();
3610 return;
3611 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003612 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003613
David Majnemerabecae72014-02-12 20:36:10 +00003614 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003615 unsigned MaxValidAlignment =
3616 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3617 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003618 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003619 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3620 << E->getSourceRange();
3621 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003622 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003623
David Majnemer0be6bd02015-07-26 09:02:21 +00003624 if (Context.getTargetInfo().isTLSSupported()) {
3625 unsigned MaxTLSAlign =
3626 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3627 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003628 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003629 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3630 VD->getTLSKind() != VarDecl::TLS_None) {
3631 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3632 << (unsigned)AlignVal << VD << MaxTLSAlign;
3633 return;
3634 }
3635 }
3636
Richard Smith44c247f2013-02-22 08:32:16 +00003637 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003638 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003639 AA->setPackExpansion(IsPackExpansion);
3640 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003641}
3642
Michael Hanaf02bbe2013-02-01 01:19:17 +00003643void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003644 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003645 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003646 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003647 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3648 SpellingListIndex);
3649 AA->setPackExpansion(IsPackExpansion);
3650 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003651}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003652
Richard Smith848e1f12013-02-01 08:12:08 +00003653void Sema::CheckAlignasUnderalignment(Decl *D) {
3654 assert(D->hasAttrs() && "no attributes on decl");
3655
David Majnemer475b25e2015-01-21 10:54:38 +00003656 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003657 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003658 UnderlyingTy = DiagTy = VD->getType();
3659 } else {
3660 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003661 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003662 UnderlyingTy = ED->getIntegerType();
3663 }
3664 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003665 return;
3666
3667 // C++11 [dcl.align]p5, C11 6.7.5/4:
3668 // The combined effect of all alignment attributes in a declaration shall
3669 // not specify an alignment that is less strict than the alignment that
3670 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003671 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003672 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003673 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003674 if (I->isAlignmentDependent())
3675 return;
3676 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003677 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003678 Align = std::max(Align, I->getAlignment(Context));
3679 }
3680
3681 if (AlignasAttr && Align) {
3682 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003683 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003684 if (NaturalAlign > RequestedAlign)
3685 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003686 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003687 }
3688}
3689
David Majnemer2c4e00a2014-01-29 22:07:36 +00003690bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003691 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003692 MSInheritanceAttr::Spelling SemanticSpelling) {
3693 assert(RD->hasDefinition() && "RD has no definition!");
3694
David Majnemer98c9ee22014-02-07 00:43:07 +00003695 // We may not have seen base specifiers or any virtual methods yet. We will
3696 // have to wait until the record is defined to catch any mismatches.
3697 if (!RD->getDefinition()->isCompleteDefinition())
3698 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003699
David Majnemer98c9ee22014-02-07 00:43:07 +00003700 // The unspecified model never matches what a definition could need.
3701 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3702 return false;
3703
David Majnemer4bb09802014-02-10 19:50:15 +00003704 if (BestCase) {
3705 if (RD->calculateInheritanceModel() == SemanticSpelling)
3706 return false;
3707 } else {
3708 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3709 return false;
3710 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003711
3712 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3713 << 0 /*definition*/;
3714 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3715 << RD->getNameAsString();
3716 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003717}
3718
Alexey Bataevf278eb12015-11-19 10:13:11 +00003719/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3720/// attribute.
3721static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3722 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003723 IntegerMode = true;
3724 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003725 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003726 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003727 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003728 case 'Q':
3729 DestWidth = 8;
3730 break;
3731 case 'H':
3732 DestWidth = 16;
3733 break;
3734 case 'S':
3735 DestWidth = 32;
3736 break;
3737 case 'D':
3738 DestWidth = 64;
3739 break;
3740 case 'X':
3741 DestWidth = 96;
3742 break;
3743 case 'T':
3744 DestWidth = 128;
3745 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003746 }
3747 if (Str[1] == 'F') {
3748 IntegerMode = false;
3749 } else if (Str[1] == 'C') {
3750 IntegerMode = false;
3751 ComplexMode = true;
3752 } else if (Str[1] != 'I') {
3753 DestWidth = 0;
3754 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003755 break;
3756 case 4:
3757 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3758 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003759 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003760 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003761 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003762 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003763 break;
3764 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003765 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003766 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003767 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003768 case 11:
3769 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003770 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003771 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003772 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003773}
3774
3775/// handleModeAttr - This attribute modifies the width of a decl with primitive
3776/// type.
3777///
3778/// Despite what would be logical, the mode attribute is a decl attribute, not a
3779/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3780/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00003781static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003782 // This attribute isn't documented, but glibc uses it. It changes
3783 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003784 if (!AL.isArgIdent(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003785 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
3786 << AL << AANT_ArgumentIdentifier;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003787 return;
3788 }
3789
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003790 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003791
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003792 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003793}
3794
3795void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3796 unsigned SpellingListIndex, bool InInstantiation) {
3797 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003798 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003799 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003800
3801 unsigned DestWidth = 0;
3802 bool IntegerMode = true;
3803 bool ComplexMode = false;
3804 llvm::APInt VectorSize(64, 0);
3805 if (Str.size() >= 4 && Str[0] == 'V') {
3806 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3807 size_t StrSize = Str.size();
3808 size_t VectorStringLength = 0;
3809 while ((VectorStringLength + 1) < StrSize &&
3810 isdigit(Str[VectorStringLength + 1]))
3811 ++VectorStringLength;
3812 if (VectorStringLength &&
3813 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3814 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003815 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003816 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003817 // Avoid duplicate warning from template instantiation.
3818 if (!InInstantiation)
3819 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003820 } else {
3821 VectorSize = 0;
3822 }
3823 }
3824
3825 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003826 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3827
3828 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3829 // and friends, at least with glibc.
3830 // FIXME: Make sure floating-point mappings are accurate
3831 // FIXME: Support XF and TF types
3832 if (!DestWidth) {
3833 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3834 return;
3835 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003836
3837 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003838 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003839 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003840 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003841 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3842 // Try to get type from enum declaration, default to int.
3843 OldTy = ED->getIntegerType();
3844 if (OldTy.isNull())
3845 OldTy = Context.IntTy;
3846 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003847 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003848
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003849 if (OldTy->isDependentType()) {
3850 D->addAttr(::new (Context)
3851 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3852 return;
3853 }
3854
Alexey Bataev326057d2015-06-19 07:46:21 +00003855 // Base type can also be a vector type (see PR17453).
3856 // Distinguish between base type and base element type.
3857 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003858 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00003859 OldElemTy = VT->getElementType();
3860
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003861 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3862 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3863 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3864 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3865 VectorSize.getBoolValue()) {
3866 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3867 return;
3868 }
3869 bool IntegralOrAnyEnumType =
3870 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3871
3872 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3873 !IntegralOrAnyEnumType)
3874 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003875 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003876 if (!IntegralOrAnyEnumType)
3877 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003878 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003879 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003880 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003881 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003882 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003883 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003884 }
3885
Alexey Bataev326057d2015-06-19 07:46:21 +00003886 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003887
3888 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003889 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3890 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003891 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003892 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003893
Alexey Bataev326057d2015-06-19 07:46:21 +00003894 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003895 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003896 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003897 }
3898
Eli Friedman4735374e2009-03-03 06:41:03 +00003899 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003900 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003901 }
3902
3903 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003904 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003905 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3906 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003907 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003908 // Complex machine mode does not support base vector types.
3909 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003910 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003911 return;
3912 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003913 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00003914 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003915 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003916 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003917 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00003918 }
3919
3920 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003921 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003922 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003923 }
3924
3925 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003926 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003927 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003928 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003929 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003930 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003931 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003932
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003933 D->addAttr(::new (Context)
3934 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003935}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003936
Erich Keanee891aa92018-07-13 15:07:47 +00003937static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Han99315932013-01-24 16:46:58 +00003938 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003939 NoDebugAttr(AL.getRange(), S.Context,
3940 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003941}
3942
Paul Robinson30e41fb2014-12-15 18:57:28 +00003943AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003944 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003945 unsigned AttrSpellingListIndex) {
3946 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003947 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003948 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3949 return nullptr;
3950 }
3951
3952 if (D->hasAttr<AlwaysInlineAttr>())
3953 return nullptr;
3954
3955 return ::new (Context) AlwaysInlineAttr(Range, Context,
3956 AttrSpellingListIndex);
3957}
3958
Erich Keane44bacdf2018-08-09 13:21:32 +00003959CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) {
3960 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003961 return nullptr;
3962
Erich Keane44bacdf2018-08-09 13:21:32 +00003963 return ::new (Context)
3964 CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003965}
3966
Erich Keane44bacdf2018-08-09 13:21:32 +00003967CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) {
3968 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL))
3969 return nullptr;
3970
3971 return ::new (Context)
3972 CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex());
3973}
3974
3975InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
3976 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003977 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003978 // Attribute applies to Var but not any subclass of it (like ParmVar,
3979 // ImplicitParm or VarTemplateSpecialization).
3980 if (VD->getKind() != Decl::Var) {
Erich Keane44bacdf2018-08-09 13:21:32 +00003981 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3982 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3983 : ExpectedVariableOrFunction);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003984 return nullptr;
3985 }
3986 // Attribute does not apply to non-static local variables.
3987 if (VD->hasLocalStorage()) {
3988 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3989 return nullptr;
3990 }
3991 }
3992
Erich Keane44bacdf2018-08-09 13:21:32 +00003993 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
3994 return nullptr;
3995
3996 return ::new (Context) InternalLinkageAttr(
3997 AL.getRange(), Context, AL.getAttributeSpellingListIndex());
3998}
3999InternalLinkageAttr *
4000Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4001 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4002 // Attribute applies to Var but not any subclass of it (like ParmVar,
4003 // ImplicitParm or VarTemplateSpecialization).
4004 if (VD->getKind() != Decl::Var) {
4005 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4006 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4007 : ExpectedVariableOrFunction);
4008 return nullptr;
4009 }
4010 // Attribute does not apply to non-static local variables.
4011 if (VD->hasLocalStorage()) {
4012 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4013 return nullptr;
4014 }
4015 }
4016
4017 if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004018 return nullptr;
4019
4020 return ::new (Context)
Erich Keane44bacdf2018-08-09 13:21:32 +00004021 InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex());
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004022}
4023
Paul Robinson30e41fb2014-12-15 18:57:28 +00004024MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
4025 unsigned AttrSpellingListIndex) {
4026 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4027 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
4028 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4029 return nullptr;
4030 }
4031
4032 if (D->hasAttr<MinSizeAttr>())
4033 return nullptr;
4034
4035 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
4036}
4037
4038OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
4039 unsigned AttrSpellingListIndex) {
4040 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4041 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4042 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4043 D->dropAttr<AlwaysInlineAttr>();
4044 }
4045 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4046 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4047 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4048 D->dropAttr<MinSizeAttr>();
4049 }
4050
4051 if (D->hasAttr<OptimizeNoneAttr>())
4052 return nullptr;
4053
4054 return ::new (Context) OptimizeNoneAttr(Range, Context,
4055 AttrSpellingListIndex);
4056}
4057
Erich Keanee891aa92018-07-13 15:07:47 +00004058static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004059 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL))
Akira Hatanakac8667622015-11-06 23:56:15 +00004060 return;
4061
Paul Robinson080b1f32015-01-13 18:34:56 +00004062 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004063 D, AL.getRange(), AL.getName(),
4064 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004065 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004066}
4067
Erich Keanee891aa92018-07-13 15:07:47 +00004068static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004069 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004070 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004071 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004072}
4073
Erich Keanee891aa92018-07-13 15:07:47 +00004074static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004075 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004076 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004077 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004078}
4079
Erich Keanee891aa92018-07-13 15:07:47 +00004080static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004081 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004082 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004083 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004084 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004085 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004086 return;
4087 }
4088 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004089 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004090}
4091
Erich Keanee891aa92018-07-13 15:07:47 +00004092static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004093 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL))
Justin Lebar10411012016-09-30 23:57:30 +00004094 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004095 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004096 // extern __shared__ is only allowed on arrays with no length (e.g.
4097 // "int x[]").
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004098 if (!S.getLangOpts().CUDARelocatableDeviceCode && VD->hasExternalStorage() &&
4099 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004100 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004101 return;
4102 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004103 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004104 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004105 << S.CurrentCUDATarget())
4106 return;
Justin Lebar10411012016-09-30 23:57:30 +00004107 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004108 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004109}
4110
Erich Keanee891aa92018-07-13 15:07:47 +00004111static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004112 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) ||
4113 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004114 return;
4115 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004116 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004117 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004118 SourceRange RTRange = FD->getReturnTypeSourceRange();
4119 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004120 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004121 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4122 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004123 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004124 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004125 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4126 if (Method->isInstance()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004127 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
Justin Lebarc66a1062016-01-20 00:26:57 +00004128 << Method;
4129 return;
4130 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004131 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
Justin Lebarc66a1062016-01-20 00:26:57 +00004132 }
4133 // Only warn for "inline" when compiling for host, to cut down on noise.
4134 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004135 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004136
Aaron Ballman3aff6332013-12-02 19:30:36 +00004137 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004138 CUDAGlobalAttr(AL.getRange(), S.Context,
4139 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004140}
4141
Erich Keanee891aa92018-07-13 15:07:47 +00004142static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004143 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004144 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004145 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004146 return;
4147 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004148
Michael Han99315932013-01-24 16:46:58 +00004149 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004150 GNUInlineAttr(AL.getRange(), S.Context,
4151 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004152}
4153
Erich Keanee891aa92018-07-13 15:07:47 +00004154static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004155 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004156
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004157 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004158 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4159 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004160 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004161 return;
4162
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004163 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004164 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004165 << AL << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004166 return;
4167 }
4168
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004169 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004170 case ParsedAttr::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004171 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004172 FastCallAttr(AL.getRange(), S.Context,
4173 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004174 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004175 case ParsedAttr::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004176 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004177 StdCallAttr(AL.getRange(), S.Context,
4178 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004179 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004180 case ParsedAttr::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004181 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004182 ThisCallAttr(AL.getRange(), S.Context,
4183 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004184 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004185 case ParsedAttr::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004186 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004187 CDeclAttr(AL.getRange(), S.Context,
4188 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004189 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004190 case ParsedAttr::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004191 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004192 PascalAttr(AL.getRange(), S.Context,
4193 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004194 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004195 case ParsedAttr::AT_SwiftCall:
John McCall477f2bb2016-03-03 06:39:32 +00004196 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004197 SwiftCallAttr(AL.getRange(), S.Context,
4198 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004199 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004200 case ParsedAttr::AT_VectorCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004201 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004202 VectorCallAttr(AL.getRange(), S.Context,
4203 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004204 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004205 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004206 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004207 MSABIAttr(AL.getRange(), S.Context,
4208 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004209 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004210 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004211 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004212 SysVABIAttr(AL.getRange(), S.Context,
4213 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004214 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004215 case ParsedAttr::AT_RegCall:
Erich Keane757d3172016-11-02 18:29:35 +00004216 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004217 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004218 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004219 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004220 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004221 switch (CC) {
4222 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004223 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004224 break;
4225 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004226 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004227 break;
4228 default:
4229 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004230 }
4231
Michael Han99315932013-01-24 16:46:58 +00004232 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004233 PcsAttr(AL.getRange(), S.Context, PCS,
4234 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004235 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004236 }
Erich Keanee891aa92018-07-13 15:07:47 +00004237 case ParsedAttr::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004238 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004239 IntelOclBiccAttr(AL.getRange(), S.Context,
4240 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004241 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004242 case ParsedAttr::AT_PreserveMost:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004243 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004244 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004245 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004246 case ParsedAttr::AT_PreserveAll:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004247 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004248 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004249 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004250 default:
4251 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004252 }
4253}
4254
Erich Keanee891aa92018-07-13 15:07:47 +00004255static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004256 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004257 return;
4258
4259 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004260 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004261 StringRef RuleName;
4262
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004263 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004264 return;
4265
4266 // FIXME: Warn if the rule name is unknown. This is tricky because only
4267 // clang-tidy knows about available rules.
4268 DiagnosticIdentifiers.push_back(RuleName);
4269 }
4270 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004271 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4272 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004273}
4274
Erich Keanee891aa92018-07-13 15:07:47 +00004275bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004276 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004277 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004278 return true;
4279
Erich Keaneb11ebc52017-09-27 03:20:13 +00004280 if (Attrs.hasProcessingCache()) {
4281 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004282 return false;
4283 }
4284
Erich Keanee891aa92018-07-13 15:07:47 +00004285 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004286 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4287 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004288 return true;
4289 }
4290
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004291 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004292 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004293 case ParsedAttr::AT_CDecl:
4294 CC = CC_C;
4295 break;
4296 case ParsedAttr::AT_FastCall:
4297 CC = CC_X86FastCall;
4298 break;
4299 case ParsedAttr::AT_StdCall:
4300 CC = CC_X86StdCall;
4301 break;
4302 case ParsedAttr::AT_ThisCall:
4303 CC = CC_X86ThisCall;
4304 break;
4305 case ParsedAttr::AT_Pascal:
4306 CC = CC_X86Pascal;
4307 break;
4308 case ParsedAttr::AT_SwiftCall:
4309 CC = CC_Swift;
4310 break;
4311 case ParsedAttr::AT_VectorCall:
4312 CC = CC_X86VectorCall;
4313 break;
4314 case ParsedAttr::AT_RegCall:
4315 CC = CC_X86RegCall;
4316 break;
4317 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004318 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004319 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004320 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004321 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004322 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4323 CC_C;
4324 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004325 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004326 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004327 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4328 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004329 return true;
4330 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004331 if (StrRef == "aapcs") {
4332 CC = CC_AAPCS;
4333 break;
4334 } else if (StrRef == "aapcs-vfp") {
4335 CC = CC_AAPCS_VFP;
4336 break;
4337 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004338
Erich Keaneb11ebc52017-09-27 03:20:13 +00004339 Attrs.setInvalid();
4340 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004341 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004342 }
Erich Keanee891aa92018-07-13 15:07:47 +00004343 case ParsedAttr::AT_IntelOclBicc:
4344 CC = CC_IntelOclBicc;
4345 break;
4346 case ParsedAttr::AT_PreserveMost:
4347 CC = CC_PreserveMost;
4348 break;
4349 case ParsedAttr::AT_PreserveAll:
4350 CC = CC_PreserveAll;
4351 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004352 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004353 }
4354
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004355 const TargetInfo &TI = Context.getTargetInfo();
4356 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004357 if (A != TargetInfo::CCCR_OK) {
4358 if (A == TargetInfo::CCCR_Warning)
Erich Keane44bacdf2018-08-09 13:21:32 +00004359 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs;
Aaron Ballman02df2e02012-12-09 17:45:41 +00004360
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004361 // This convention is not valid for the target. Use the default function or
4362 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004363 bool IsCXXMethod = false, IsVariadic = false;
4364 if (FD) {
4365 IsCXXMethod = FD->isCXXInstanceMember();
4366 IsVariadic = FD->isVariadic();
4367 }
4368 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004369 }
4370
Erich Keaneb11ebc52017-09-27 03:20:13 +00004371 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004372 return false;
4373}
4374
John McCall477f2bb2016-03-03 06:39:32 +00004375/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004376static bool isValidSwiftContextType(QualType Ty) {
4377 if (!Ty->hasPointerRepresentation())
4378 return Ty->isDependentType();
4379 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004380}
4381
4382/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004383static bool isValidSwiftIndirectResultType(QualType Ty) {
4384 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4385 Ty = PtrType->getPointeeType();
4386 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4387 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004388 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004389 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004390 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004391 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004392}
4393
4394/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004395static bool isValidSwiftErrorResultType(QualType Ty) {
4396 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4397 Ty = PtrType->getPointeeType();
4398 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4399 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004400 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004401 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004402 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004403 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004404 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004405 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004406}
4407
Erich Keanee891aa92018-07-13 15:07:47 +00004408static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004409 ParameterABI Abi) {
4410 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4411 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004412}
4413
4414void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4415 unsigned spellingIndex) {
4416
4417 QualType type = cast<ParmVarDecl>(D)->getType();
4418
4419 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4420 if (existingAttr->getABI() != abi) {
4421 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4422 << getParameterABISpelling(abi) << existingAttr;
4423 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4424 return;
4425 }
4426 }
4427
4428 switch (abi) {
4429 case ParameterABI::Ordinary:
4430 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4431
4432 case ParameterABI::SwiftContext:
4433 if (!isValidSwiftContextType(type)) {
4434 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4435 << getParameterABISpelling(abi)
4436 << /*pointer to pointer */ 0 << type;
4437 }
4438 D->addAttr(::new (Context)
4439 SwiftContextAttr(range, Context, spellingIndex));
4440 return;
4441
4442 case ParameterABI::SwiftErrorResult:
4443 if (!isValidSwiftErrorResultType(type)) {
4444 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4445 << getParameterABISpelling(abi)
4446 << /*pointer to pointer */ 1 << type;
4447 }
4448 D->addAttr(::new (Context)
4449 SwiftErrorResultAttr(range, Context, spellingIndex));
4450 return;
4451
4452 case ParameterABI::SwiftIndirectResult:
4453 if (!isValidSwiftIndirectResultType(type)) {
4454 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4455 << getParameterABISpelling(abi)
4456 << /*pointer*/ 0 << type;
4457 }
4458 D->addAttr(::new (Context)
4459 SwiftIndirectResultAttr(range, Context, spellingIndex));
4460 return;
4461 }
4462 llvm_unreachable("bad parameter ABI attribute");
4463}
4464
John McCall3882ace2011-01-05 12:14:39 +00004465/// Checks a regparm attribute, returning true if it is ill-formed and
4466/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004467bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004468 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004469 return true;
4470
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004471 if (!checkAttributeNumArgs(*this, AL, 1)) {
4472 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004473 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004474 }
Eli Friedman7044b762009-03-27 21:06:47 +00004475
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004476 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004477 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4478 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4479 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004480 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004481 }
4482
Douglas Gregore8bbc122011-09-02 00:18:52 +00004483 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004484 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004485 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004486 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004487 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004488 }
4489
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004490 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004491 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004492 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004493 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004494 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004495 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004496 }
4497
John McCall3882ace2011-01-05 12:14:39 +00004498 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004499}
4500
Artem Belevichbcec9da2016-06-06 22:54:57 +00004501// Checks whether an argument of launch_bounds attribute is
4502// acceptable, performs implicit conversion to Rvalue, and returns
4503// non-nullptr Expr result on success. Otherwise, it returns nullptr
4504// and may output an error.
4505static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004506 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004507 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004508 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004509 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004510
4511 // Accept template arguments for now as they depend on something else.
4512 // We'll get to check them when they eventually get instantiated.
4513 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004514 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004515
4516 llvm::APSInt I(64);
4517 if (!E->isIntegerConstantExpr(I, S.Context)) {
4518 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004519 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004520 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004521 }
4522 // Make sure we can fit it in 32 bits.
4523 if (!I.isIntN(32)) {
4524 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4525 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004526 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004527 }
4528 if (I < 0)
4529 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004530 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004531
Artem Belevichbcec9da2016-06-06 22:54:57 +00004532 // We may need to perform implicit conversion of the argument.
4533 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4534 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4535 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4536 assert(!ValArg.isInvalid() &&
4537 "Unexpected PerformCopyInitialization() failure.");
4538
4539 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004540}
4541
4542void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4543 Expr *MinBlocks, unsigned SpellingListIndex) {
4544 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4545 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004546 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4547 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004548 return;
4549
Artem Belevichbcec9da2016-06-06 22:54:57 +00004550 if (MinBlocks) {
4551 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4552 if (MinBlocks == nullptr)
4553 return;
4554 }
Artem Belevich7093e402015-04-21 22:55:54 +00004555
4556 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4557 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4558}
4559
Erich Keanee891aa92018-07-13 15:07:47 +00004560static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004561 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4562 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004563 return;
4564
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004565 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4566 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4567 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004568}
4569
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004570static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004571 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004572 if (!AL.isArgIdent(0)) {
4573 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004574 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004575 return;
4576 }
Joel E. Denny81508102018-03-13 14:51:22 +00004577
4578 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004579 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004580 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004581 return;
4582
Joel E. Denny81508102018-03-13 14:51:22 +00004583 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004584 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004585 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004586 return;
4587
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004588 bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004589 if (IsPointer) {
4590 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004591 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4592 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4593 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Erich Keane44bacdf2018-08-09 13:21:32 +00004594 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004595 }
4596
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004597 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
4598 AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx,
4599 TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004600}
4601
4602static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004603 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004604 if (!AL.isArgIdent(0)) {
4605 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004606 << AL << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004607 return;
4608 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004609
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004610 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004611 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004612
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004613 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004614 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004615 << AL << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004616 return;
4617 }
4618
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004619 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004620 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004621 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004622 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004623
Michael Han99315932013-01-24 16:46:58 +00004624 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004625 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004626 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004627 AL.getLayoutCompatible(),
4628 AL.getMustBeNull(),
4629 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004630}
4631
Erich Keanee891aa92018-07-13 15:07:47 +00004632static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004633 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004634
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004635 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004636 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004637 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004638 return;
4639
Joel E. Denny81508102018-03-13 14:51:22 +00004640 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
4641 D->addAttr(::new (S.Context) XRayLogArgsAttr(
4642 AL.getRange(), S.Context, ArgCount.getSourceIndex(),
4643 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004644}
4645
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004646//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004647// Checker-specific attribute handlers.
4648//===----------------------------------------------------------------------===//
4649
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004650static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4651 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004652}
4653
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004654static bool isValidSubjectOfNSAttribute(Sema &S, QualType QT) {
4655 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
4656 S.Context.isObjCNSObjectType(QT);
John McCalled433932011-01-25 03:31:58 +00004657}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004658
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004659static bool isValidSubjectOfCFAttribute(Sema &S, QualType QT) {
4660 return QT->isDependentType() || QT->isPointerType() ||
4661 isValidSubjectOfNSAttribute(S, QT);
John McCalled433932011-01-25 03:31:58 +00004662}
4663
Erich Keanee891aa92018-07-13 15:07:47 +00004664static void handleNSConsumedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004665 S.AddNSConsumedAttr(AL.getRange(), D, AL.getAttributeSpellingListIndex(),
Erich Keanee891aa92018-07-13 15:07:47 +00004666 AL.getKind() == ParsedAttr::AT_NSConsumed,
John McCall3b5a8f52016-03-03 00:10:03 +00004667 /*template instantiation*/ false);
4668}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004669
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004670void Sema::AddNSConsumedAttr(SourceRange AttrRange, Decl *D,
4671 unsigned SpellingIndex, bool IsNSConsumed,
4672 bool IsTemplateInstantiation) {
4673 const auto *Param = cast<ParmVarDecl>(D);
4674 bool TypeOK;
John McCall3b5a8f52016-03-03 00:10:03 +00004675
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004676 if (IsNSConsumed)
4677 TypeOK = isValidSubjectOfNSAttribute(*this, Param->getType());
4678 else
4679 TypeOK = isValidSubjectOfCFAttribute(*this, Param->getType());
John McCalled433932011-01-25 03:31:58 +00004680
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004681 if (!TypeOK) {
John McCall3b5a8f52016-03-03 00:10:03 +00004682 // These attributes are normally just advisory, but in ARC, ns_consumed
4683 // is significant. Allow non-dependent code to contain inappropriate
4684 // attributes even in ARC, but require template instantiations to be
4685 // set up correctly.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004686 Diag(D->getBeginLoc(), (IsTemplateInstantiation && IsNSConsumed &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004687 getLangOpts().ObjCAutoRefCount
4688 ? diag::err_ns_attribute_wrong_parameter_type
4689 : diag::warn_ns_attribute_wrong_parameter_type))
4690 << AttrRange << (IsNSConsumed ? "ns_consumed" : "cf_consumed")
4691 << (IsNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00004692 return;
4693 }
4694
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004695 if (IsNSConsumed)
4696 D->addAttr(::new (Context)
4697 NSConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004698 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004699 D->addAttr(::new (Context)
4700 CFConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004701}
4702
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004703bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4704 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004705 return false;
4706
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004707 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4708 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004709 return true;
4710}
4711
Chandler Carruthedc2c642011-07-02 00:01:44 +00004712static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004713 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004714 QualType ReturnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004715
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004716 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
4717 ReturnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004718 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Erich Keanee891aa92018-07-13 15:07:47 +00004719 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004720 return; // ignore: was handled as a type attribute
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004721 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D))
4722 ReturnType = PD->getType();
4723 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
4724 ReturnType = FD->getReturnType();
4725 else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
4726 ReturnType = Param->getType()->getPointeeType();
4727 if (ReturnType.isNull()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004728 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004729 << AL << /*pointer-to-CF*/ 2 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004730 return;
4731 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004732 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00004733 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004734 } else {
4735 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004736 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004737 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004738 case ParsedAttr::AT_NSReturnsRetained:
4739 case ParsedAttr::AT_NSReturnsAutoreleased:
4740 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004741 ExpectedDeclKind = ExpectedFunctionOrMethod;
4742 break;
4743
Erich Keanee891aa92018-07-13 15:07:47 +00004744 case ParsedAttr::AT_CFReturnsRetained:
4745 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004746 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4747 break;
4748 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004749 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004750 << AL.getRange() << AL << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004751 return;
4752 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004753
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004754 bool TypeOK;
4755 bool Cf;
4756 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004757 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004758 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004759 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
4760 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004761 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004762
4763 case ParsedAttr::AT_NSReturnsAutoreleased:
4764 case ParsedAttr::AT_NSReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004765 TypeOK = isValidSubjectOfNSAttribute(S, ReturnType);
4766 Cf = false;
John McCalled433932011-01-25 03:31:58 +00004767 break;
4768
Erich Keanee891aa92018-07-13 15:07:47 +00004769 case ParsedAttr::AT_CFReturnsRetained:
4770 case ParsedAttr::AT_CFReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004771 TypeOK = isValidSubjectOfCFAttribute(S, ReturnType);
4772 Cf = true;
John McCalled433932011-01-25 03:31:58 +00004773 break;
4774 }
4775
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004776 if (!TypeOK) {
4777 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00004778 return;
4779
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004780 if (isa<ParmVarDecl>(D)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004781 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004782 << AL << /*pointer-to-CF*/ 2 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004783 } else {
4784 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4785 enum : unsigned {
4786 Function,
4787 Method,
4788 Property
4789 } SubjectKind = Function;
4790 if (isa<ObjCMethodDecl>(D))
4791 SubjectKind = Method;
4792 else if (isa<ObjCPropertyDecl>(D))
4793 SubjectKind = Property;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004794 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004795 << AL << SubjectKind << Cf << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004796 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004797 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004798 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004799
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004800 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004801 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004802 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004803 case ParsedAttr::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004804 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004805 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004806 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004807 case ParsedAttr::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004808 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004809 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004810 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004811 case ParsedAttr::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004812 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004813 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004814 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004815 case ParsedAttr::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004816 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004817 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004818 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004819 case ParsedAttr::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004820 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004821 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004822 return;
4823 };
4824}
4825
John McCallcf166702011-07-22 08:53:00 +00004826static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004827 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004828 const int EP_ObjCMethod = 1;
4829 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00004830
Erich Keaneb11ebc52017-09-27 03:20:13 +00004831 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004832 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004833 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004834 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004835 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004836 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004837
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004838 if (!resultType->isReferenceType() &&
4839 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004840 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004841 << SourceRange(loc) << Attrs
4842 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
4843 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004844
4845 // Drop the attribute.
4846 return;
4847 }
4848
Nico Weber462fd1e2015-01-07 23:50:05 +00004849 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00004850 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004851}
4852
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004853static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004854 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004855 const auto *Method = cast<ObjCMethodDecl>(D);
4856
4857 const DeclContext *DC = Method->getDeclContext();
4858 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004859 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00004860 << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004861 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4862 return;
4863 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004864 if (Method->getMethodFamily() == OMF_dealloc) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004865 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
Erich Keane44bacdf2018-08-09 13:21:32 +00004866 << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004867 return;
4868 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004869
4870 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
4871 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004872}
4873
Erich Keanee891aa92018-07-13 15:07:47 +00004874static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004875 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004876
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004877 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004878 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004879 return;
4880 }
John McCall28592582015-02-01 22:34:06 +00004881
4882 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004883 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00004884 if (!Parm->Ident->isStr("id")) {
Erich Keane44bacdf2018-08-09 13:21:32 +00004885 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL;
John McCall28592582015-02-01 22:34:06 +00004886 return;
4887 }
4888
4889 // Only allow 'cv void *'.
4890 QualType T = TD->getUnderlyingType();
4891 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004892 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00004893 return;
4894 }
4895 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004896
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004897 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004898 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
4899 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004900}
4901
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004902static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004903 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004904 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00004905
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004906 if (!Parm) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004907 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004908 return;
4909 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004910
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004911 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004912 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
4913 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004914}
4915
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004916static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004917 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004918 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004919 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004920 if (!RelatedClass) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004921 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004922 return;
4923 }
4924 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004925 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004926 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004927 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004928 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004929 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004930 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004931 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004932}
4933
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004934static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004935 const ParsedAttr &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004936 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004937 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004938 IFace = CatDecl->getClassInterface();
4939 else
4940 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004941
4942 if (!IFace)
4943 return;
4944
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004945 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004946 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004947 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
4948 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004949}
4950
Erich Keanee891aa92018-07-13 15:07:47 +00004951static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004952 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004953 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004954 return;
4955 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004956 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004957 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004958 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004959}
4960
Nico Webera6916892016-06-10 18:53:04 +00004961// When a user wants to use objc_boxable with a union or struct
4962// but they don't have access to the declaration (legacy/third-party code)
4963// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00004964// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00004965static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00004966 bool notify = false;
4967
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004968 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00004969 if (RD && RD->getDefinition()) {
4970 RD = RD->getDefinition();
4971 notify = true;
4972 }
4973
4974 if (RD) {
4975 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004976 ObjCBoxableAttr(AL.getRange(), S.Context,
4977 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00004978 RD->addAttr(BoxableAttr);
4979 if (notify) {
4980 // we need to notify ASTReader/ASTWriter about
4981 // modification of existing declaration
4982 if (ASTMutationListener *L = S.getASTMutationListener())
4983 L->AddedAttributeToRecord(BoxableAttr, RD);
4984 }
4985 }
4986}
4987
Erich Keanee891aa92018-07-13 15:07:47 +00004988static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004989 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004990
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004991 S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00004992 << AL.getRange() << AL << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004993}
4994
Chandler Carruthedc2c642011-07-02 00:01:44 +00004995static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004996 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004997 const auto *VD = cast<ValueDecl>(D);
4998 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00004999
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005000 if (!QT->isDependentType() &&
5001 !QT->isObjCLifetimeType()) {
5002 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5003 << QT;
John McCall31168b02011-06-15 23:02:42 +00005004 return;
5005 }
5006
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005007 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005008
5009 // If we have no lifetime yet, check the lifetime we're presumably
5010 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005011 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5012 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005013
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005014 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005015 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005016 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005017 "didn't infer lifetime for non-dependent type?");
5018 break;
5019
5020 case Qualifiers::OCL_Weak: // meaningful
5021 case Qualifiers::OCL_Strong: // meaningful
5022 break;
5023
5024 case Qualifiers::OCL_ExplicitNone:
5025 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005026 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5027 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005028 break;
5029 }
5030
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005031 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005032 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
5033 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00005034}
5035
Francois Picheta83957a2010-12-19 06:50:37 +00005036//===----------------------------------------------------------------------===//
5037// Microsoft specific attribute handlers.
5038//===----------------------------------------------------------------------===//
5039
Nico Weber88f5ed92016-09-13 18:55:26 +00005040UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
5041 unsigned AttrSpellingListIndex, StringRef Uuid) {
5042 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005043 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005044 return nullptr;
5045 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5046 Diag(Range.getBegin(), diag::note_previous_uuid);
5047 D->dropAttr<UuidAttr>();
5048 }
5049
5050 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5051}
5052
Erich Keanee891aa92018-07-13 15:07:47 +00005053static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005054 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005055 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005056 << AL << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005057 return;
5058 }
5059
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005060 StringRef StrRef;
5061 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005062 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005063 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005064
David Majnemer89085342013-08-09 08:56:20 +00005065 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5066 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005067 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5068 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005069
Reid Kleckner140c4a72013-05-17 14:04:52 +00005070 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005071 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005072 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005073 return;
5074 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005075
David Majnemer89085342013-08-09 08:56:20 +00005076 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005077 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005078 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005079 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005080 return;
5081 }
David Majnemer89085342013-08-09 08:56:20 +00005082 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005083 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005084 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005085 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005086 }
Francois Picheta83957a2010-12-19 06:50:37 +00005087
Nico Weber469891e2017-05-05 17:05:56 +00005088 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5089 // the only thing in the [] list, the [] too), and add an insertion of
5090 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5091 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005092 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005093 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005094 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5095 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005096
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005097 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5098 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005099 if (UA)
5100 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005101}
5102
Erich Keanee891aa92018-07-13 15:07:47 +00005103static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005104 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005105 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
Erich Keane44bacdf2018-08-09 13:21:32 +00005106 << AL << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005107 return;
5108 }
5109 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005110 D, AL.getRange(), /*BestCase=*/true,
5111 AL.getAttributeSpellingListIndex(),
5112 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005113 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005114 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005115 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5116 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005117}
5118
Erich Keanee891aa92018-07-13 15:07:47 +00005119static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005120 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005121 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005122 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005123 return;
5124 }
5125 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005126 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005127 return;
5128 }
5129 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005130 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005131 return;
5132 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005133 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5134 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005135}
5136
Erich Keanee891aa92018-07-13 15:07:47 +00005137static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005138 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005139 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005140 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005141 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005142 return;
5143 Tags.push_back(Tag);
5144 }
5145
5146 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5147 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005148 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005149 return;
5150 }
5151 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005152 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005153 return;
5154 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005155 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005156 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005157 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005158 return;
5159
5160 // Store tags sorted and without duplicates.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00005161 llvm::sort(Tags.begin(), Tags.end());
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005162 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5163
5164 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005165 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5166 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005167}
5168
Erich Keanee891aa92018-07-13 15:07:47 +00005169static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005170 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005171 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005172 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005173 return;
5174 }
5175
5176 StringRef Str;
5177 SourceLocation ArgLoc;
5178
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005179 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005180 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005181 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005182 return;
5183
5184 ARMInterruptAttr::InterruptType Kind;
5185 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005186 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5187 << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005188 return;
5189 }
5190
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005191 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005192 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005193 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005194}
5195
Erich Keanee891aa92018-07-13 15:07:47 +00005196static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005197 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005198 return;
5199
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005200 if (!AL.isArgExpr(0)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005201 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5202 << AL << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005203 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005204 }
5205
5206 // FIXME: Check for decl - it should be void ()(void).
5207
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005208 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005209 llvm::APSInt NumParams(32);
5210 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005211 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005212 << AL << AANT_ArgumentIntegerConstant
5213 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005214 return;
5215 }
5216
5217 unsigned Num = NumParams.getLimitedValue(255);
5218 if ((Num & 1) || Num > 30) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005219 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005220 << AL << (int)NumParams.getSExtValue()
5221 << NumParamsExpr->getSourceRange();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005222 return;
5223 }
5224
Aaron Ballman36a53502014-01-16 13:03:14 +00005225 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005226 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5227 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005228 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005229}
5230
Erich Keanee891aa92018-07-13 15:07:47 +00005231static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005232 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005233 if (AL.getNumArgs() > 1) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005234 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005235 return;
5236 }
5237
5238 StringRef Str;
5239 SourceLocation ArgLoc;
5240
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005241 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005242 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005243 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005244 return;
5245
5246 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5247 // a) Must be a function.
5248 // b) Must have no parameters.
5249 // c) Must have the 'void' return type.
5250 // d) Cannot have the 'mips16' attribute, as that instruction set
5251 // lacks the 'eret' instruction.
5252 // e) The attribute itself must either have no argument or one of the
5253 // valid interrupt types, see [MipsInterruptDocs].
5254
5255 if (!isFunctionOrMethod(D)) {
5256 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5257 << "'interrupt'" << ExpectedFunctionOrMethod;
5258 return;
5259 }
5260
5261 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5262 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5263 << 0;
5264 return;
5265 }
5266
5267 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5268 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5269 << 1;
5270 return;
5271 }
5272
Erich Keane44bacdf2018-08-09 13:21:32 +00005273 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005274 return;
5275
5276 MipsInterruptAttr::InterruptType Kind;
5277 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005278 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
Erich Keane44bacdf2018-08-09 13:21:32 +00005279 << AL << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005280 return;
5281 }
5282
5283 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005284 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005285}
5286
Erich Keanee891aa92018-07-13 15:07:47 +00005287static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005288 // Semantic checks for a function with the 'interrupt' attribute.
5289 // a) Must be a function.
5290 // b) Must have the 'void' return type.
5291 // c) Must take 1 or 2 arguments.
5292 // d) The 1st argument must be a pointer.
5293 // e) The 2nd argument (if any) must be an unsigned integer.
5294 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5295 CXXMethodDecl::isStaticOverloadedOperator(
5296 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005297 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005298 << AL << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005299 return;
5300 }
5301 // Interrupt handler must have void return type.
5302 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5303 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5304 diag::err_anyx86_interrupt_attribute)
5305 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5306 ? 0
5307 : 1)
5308 << 0;
5309 return;
5310 }
5311 // Interrupt handler must have 1 or 2 parameters.
5312 unsigned NumParams = getFunctionOrMethodNumParams(D);
5313 if (NumParams < 1 || NumParams > 2) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005314 S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute)
Alexey Bataevd51e9932016-01-15 04:06:31 +00005315 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5316 ? 0
5317 : 1)
5318 << 1;
5319 return;
5320 }
5321 // The first argument must be a pointer.
5322 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5323 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5324 diag::err_anyx86_interrupt_attribute)
5325 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5326 ? 0
5327 : 1)
5328 << 2;
5329 return;
5330 }
5331 // The second argument, if present, must be an unsigned integer.
5332 unsigned TypeSize =
5333 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5334 ? 64
5335 : 32;
5336 if (NumParams == 2 &&
5337 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5338 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5339 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5340 diag::err_anyx86_interrupt_attribute)
5341 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5342 ? 0
5343 : 1)
5344 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5345 return;
5346 }
5347 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005348 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005349 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5350}
5351
Erich Keanee891aa92018-07-13 15:07:47 +00005352static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005353 if (!isFunctionOrMethod(D)) {
5354 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5355 << "'interrupt'" << ExpectedFunction;
5356 return;
5357 }
5358
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005359 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005360 return;
5361
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005362 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005363}
5364
Erich Keanee891aa92018-07-13 15:07:47 +00005365static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005366 if (!isFunctionOrMethod(D)) {
5367 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5368 << "'signal'" << ExpectedFunction;
5369 return;
5370 }
5371
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005372 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005373 return;
5374
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005375 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005376}
5377
Ana Pazos1eee1b72018-07-26 17:37:45 +00005378
5379static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5380 const ParsedAttr &AL) {
5381 // Warn about repeated attributes.
5382 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5383 S.Diag(AL.getRange().getBegin(),
5384 diag::warn_riscv_repeated_interrupt_attribute);
5385 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5386 return;
5387 }
5388
5389 // Check the attribute argument. Argument is optional.
5390 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5391 return;
5392
5393 StringRef Str;
5394 SourceLocation ArgLoc;
5395
5396 // 'machine'is the default interrupt mode.
5397 if (AL.getNumArgs() == 0)
5398 Str = "machine";
5399 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5400 return;
5401
5402 // Semantic checks for a function with the 'interrupt' attribute:
5403 // - Must be a function.
5404 // - Must have no parameters.
5405 // - Must have the 'void' return type.
5406 // - The attribute itself must either have no argument or one of the
5407 // valid interrupt types, see [RISCVInterruptDocs].
5408
5409 if (D->getFunctionType() == nullptr) {
5410 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5411 << "'interrupt'" << ExpectedFunction;
5412 return;
5413 }
5414
5415 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5416 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 0;
5417 return;
5418 }
5419
5420 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5421 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 1;
5422 return;
5423 }
5424
5425 RISCVInterruptAttr::InterruptType Kind;
5426 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005427 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
5428 << ArgLoc;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005429 return;
5430 }
5431
5432 D->addAttr(::new (S.Context) RISCVInterruptAttr(
5433 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
5434}
5435
Erich Keanee891aa92018-07-13 15:07:47 +00005436static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005437 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005438 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5439 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005440 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005441 break;
5442 case llvm::Triple::mipsel:
5443 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005444 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005445 break;
5446 case llvm::Triple::x86:
5447 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005448 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005449 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005450 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005451 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005452 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005453 case llvm::Triple::riscv32:
5454 case llvm::Triple::riscv64:
5455 handleRISCVInterruptAttr(S, D, AL);
5456 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005457 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005458 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005459 break;
5460 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005461}
5462
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005463static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005464 const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005465 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005466 Expr *MinExpr = AL.getArgAsExpr(0);
5467 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005468 return;
5469
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005470 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005471 Expr *MaxExpr = AL.getArgAsExpr(1);
5472 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005473 return;
5474
5475 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005476 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005477 return;
5478 }
5479 if (Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005480 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005481 return;
5482 }
5483
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005484 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005485 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5486 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005487}
5488
Erich Keanee891aa92018-07-13 15:07:47 +00005489static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005490 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005491 Expr *MinExpr = AL.getArgAsExpr(0);
5492 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005493 return;
5494
5495 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005496 if (AL.getNumArgs() == 2) {
5497 Expr *MaxExpr = AL.getArgAsExpr(1);
5498 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005499 return;
5500 }
5501
5502 if (Min == 0 && Max != 0) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005503 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005504 return;
5505 }
5506 if (Max != 0 && Min > Max) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005507 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005508 return;
5509 }
5510
5511 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005512 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5513 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005514}
5515
Erich Keanee891aa92018-07-13 15:07:47 +00005516static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005517 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005518 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5519 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005520 return;
5521
5522 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005523 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5524 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005525}
5526
Erich Keanee891aa92018-07-13 15:07:47 +00005527static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005528 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005529 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5530 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005531 return;
5532
5533 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005534 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5535 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005536}
5537
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005538static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005539 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005540 // If we try to apply it to a function pointer, don't warn, but don't
5541 // do anything, either. It doesn't matter anyway, because there's nothing
5542 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005543 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005544 if (VD && VD->getType()->isFunctionPointerType())
5545 return;
5546 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005547 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005548 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5549 TD->getUnderlyingType()->isFunctionType()))
5550 return;
5551 // Attribute can only be applied to function types.
5552 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005553 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005554 << AL << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005555 return;
5556 }
5557
Aaron Ballman36a53502014-01-16 13:03:14 +00005558 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005559 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5560 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005561}
5562
Erich Keanee891aa92018-07-13 15:07:47 +00005563static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005564 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005565 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5566 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005567 return;
5568
5569 // TODO: Investigate what happens with the next major version of MSVC.
5570 if (Version != LangOptions::MSVC2015) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005571 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
Erich Keane44bacdf2018-08-09 13:21:32 +00005572 << AL << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005573 return;
5574 }
5575
5576 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005577 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5578 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005579}
5580
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005581DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5582 unsigned AttrSpellingListIndex) {
5583 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005584 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005585 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005586 }
5587
5588 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005589 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005590
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005591 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005592}
5593
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005594DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5595 unsigned AttrSpellingListIndex) {
5596 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005597 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005598 D->dropAttr<DLLImportAttr>();
5599 }
5600
5601 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005602 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005603
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005604 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005605}
5606
Erich Keanee891aa92018-07-13 15:07:47 +00005607static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005608 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5609 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005610 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
Hans Wennborg5e645282014-06-24 23:57:13 +00005611 return;
5612 }
5613
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005614 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00005615 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005616 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5617 // MinGW doesn't allow dllimport on inline functions.
5618 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
Erich Keane44bacdf2018-08-09 13:21:32 +00005619 << A;
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005620 return;
5621 }
5622 }
5623
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005624 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005625 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5626 MD->getParent()->isLambda()) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005627 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
Hans Wennborg5869ec42015-09-15 21:05:30 +00005628 return;
5629 }
5630 }
5631
Hans Wennborge82f19c2014-06-24 23:57:05 +00005632 unsigned Index = A.getAttributeSpellingListIndex();
Erich Keanee891aa92018-07-13 15:07:47 +00005633 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Hans Wennborge82f19c2014-06-24 23:57:05 +00005634 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5635 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005636 if (NewAttr)
5637 D->addAttr(NewAttr);
5638}
5639
David Majnemer2c4e00a2014-01-29 22:07:36 +00005640MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00005641Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00005642 unsigned AttrSpellingListIndex,
5643 MSInheritanceAttr::Spelling SemanticSpelling) {
5644 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
5645 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00005646 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005647 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
5648 << 1 /*previous declaration*/;
5649 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
5650 D->dropAttr<MSInheritanceAttr>();
5651 }
5652
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005653 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005654 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00005655 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
5656 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005657 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005658 }
5659 } else {
5660 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
5661 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5662 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005663 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005664 }
5665 if (RD->getDescribedClassTemplate()) {
5666 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5667 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005668 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005669 }
5670 }
5671
5672 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00005673 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005674}
5675
Erich Keanee891aa92018-07-13 15:07:47 +00005676static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005677 // The capability attributes take a single string parameter for the name of
5678 // the capability they represent. The lockable attribute does not take any
5679 // parameters. However, semantically, both attributes represent the same
5680 // concept, and so they use the same semantic attribute. Eventually, the
5681 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00005682 //
Alp Toker958027b2014-07-14 19:42:55 +00005683 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00005684 // literal will be considered a "mutex."
5685 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005686 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00005687 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005688 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005689 return;
5690
Aaron Ballman6c810072014-03-05 21:47:13 +00005691 // Currently, there are only two names allowed for a capability: role and
5692 // mutex (case insensitive). Diagnose other capability names.
5693 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
5694 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
5695
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005696 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
5697 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005698}
5699
Erich Keanee891aa92018-07-13 15:07:47 +00005700static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00005701 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005702 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00005703 return;
5704
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005705 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00005706 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005707 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005708}
5709
5710static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005711 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005712 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005713 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005714 return;
5715
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005716 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005717 S.Context,
5718 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005719 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005720}
5721
5722static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005723 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005724 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005725 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005726 return;
5727
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005728 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005729 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005730 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005731 Args.data(),
5732 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005733 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005734}
5735
5736static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005737 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005738 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005739 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005740 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005741
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005742 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005743 AL.getRange(), S.Context, Args.data(), Args.size(),
5744 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005745}
5746
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005747static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005748 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005749 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005750 return;
5751
5752 // check that all arguments are lockable objects
5753 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005754 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005755 if (Args.empty())
5756 return;
5757
5758 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005759 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
5760 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005761
5762 D->addAttr(RCA);
5763}
5764
Erich Keanee891aa92018-07-13 15:07:47 +00005765static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005766 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005767 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005768 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00005769 // Do not want to attach the attribute to the namespace because that will
5770 // cause confusing diagnostic reports for uses of declarations within the
5771 // namespace.
5772 return;
5773 }
5774 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005775
Manman Renc7890fe2016-03-16 18:50:49 +00005776 // Handle the cases where the attribute has a text message.
5777 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005778 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
5779 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00005780 return;
5781
5782 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005783 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
5784 checkAttributeAtMostNumArgs(S, AL, 1);
5785 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
5786 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
Manman Renc7890fe2016-03-16 18:50:49 +00005787 return;
5788
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005789 if (!S.getLangOpts().CPlusPlus14)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005790 if (AL.isCXX11Attribute() &&
5791 !(AL.hasScope() && AL.getScopeName()->isStr("gnu")))
Erich Keane44bacdf2018-08-09 13:21:32 +00005792 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005793
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005794 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005795 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
5796 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005797}
5798
5799static bool isGlobalVar(const Decl *D) {
5800 if (const auto *S = dyn_cast<VarDecl>(D))
5801 return S->hasGlobalStorage();
5802 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00005803}
5804
Erich Keanee891aa92018-07-13 15:07:47 +00005805static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005806 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00005807 return;
5808
Benjamin Kramer1b582012016-02-13 18:11:49 +00005809 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00005810
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005811 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00005812 StringRef SanitizerName;
5813 SourceLocation LiteralLoc;
5814
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005815 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00005816 return;
5817
5818 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
5819 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005820 else if (isGlobalVar(D) && SanitizerName != "address")
5821 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005822 << AL << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00005823 Sanitizers.push_back(SanitizerName);
5824 }
5825
5826 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005827 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
5828 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005829}
5830
5831static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005832 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005833 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00005834 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005835 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
5836 .Case("no_address_safety_analysis", "address")
5837 .Case("no_sanitize_address", "address")
5838 .Case("no_sanitize_thread", "thread")
5839 .Case("no_sanitize_memory", "memory");
5840 if (isGlobalVar(D) && SanitizerName != "address")
5841 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Erich Keane44bacdf2018-08-09 13:21:32 +00005842 << AL << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00005843 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005844 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
5845 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005846}
5847
Erich Keanee891aa92018-07-13 15:07:47 +00005848static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Erich Keane44bacdf2018-08-09 13:21:32 +00005849 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005850 D->addAttr(Internal);
5851}
5852
Erich Keanee891aa92018-07-13 15:07:47 +00005853static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005854 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005855 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
Erich Keane44bacdf2018-08-09 13:21:32 +00005856 << AL << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005857 else
Erich Keane44bacdf2018-08-09 13:21:32 +00005858 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
5859 << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005860}
5861
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005862/// Handles semantic checking for features that are common to all attributes,
5863/// such as checking whether a parameter was properly specified, or the correct
5864/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005865static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005866 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005867 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00005868 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005869 //
5870 // We also bail on unknown and ignored attributes because those are handled
5871 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00005872 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005873 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005874 // Check whether the attribute requires specific language extensions to be
5875 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005876 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00005877 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00005878 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005879 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00005880 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005881 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00005882 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005883
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005884 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005885 // If there are no optional arguments, then checking for the argument count
5886 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005887 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005888 return true;
5889 } else {
5890 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005891 if (AL.getMinArgs() &&
5892 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005893 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005894 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
5895 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005896 return true;
5897 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005898
Oren Ben Simhon220671a2018-03-17 13:31:35 +00005899 if (S.CheckAttrTarget(AL))
5900 return true;
5901
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005902 return false;
5903}
5904
Erich Keanee891aa92018-07-13 15:07:47 +00005905static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005906 if (D->isInvalidDecl())
5907 return;
5908
5909 // Check if there is only one access qualifier.
5910 if (D->hasAttr<OpenCLAccessAttr>()) {
Andrew Savonichev05a15af2018-09-06 15:10:26 +00005911 if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() ==
5912 AL.getSemanticSpelling()) {
5913 S.Diag(AL.getLoc(), diag::warn_duplicate_declspec)
5914 << AL.getName()->getName() << AL.getRange();
5915 } else {
5916 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
5917 << D->getSourceRange();
5918 D->setInvalidDecl(true);
5919 return;
5920 }
Xiuli Pan11e13f62016-02-26 03:13:03 +00005921 }
5922
5923 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
5924 // image object can be read and written.
5925 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
5926 // object. Using the read_write (or __read_write) qualifier with the pipe
5927 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005928 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005929 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005930 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005931 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005932 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
Erich Keane44bacdf2018-08-09 13:21:32 +00005933 << AL << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00005934 D->setInvalidDecl(true);
5935 return;
5936 }
5937 }
5938 }
5939
5940 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005941 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00005942}
5943
Erik Pilkington5a559e62018-08-21 17:24:06 +00005944static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Erik Pilkington63e7ab12018-08-21 17:50:10 +00005945 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00005946 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
5947 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
5948 return;
5949 }
5950
Erik Pilkington63e7ab12018-08-21 17:50:10 +00005951 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
Erik Pilkington5a559e62018-08-21 17:24:06 +00005952 handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A);
Erik Pilkington63e7ab12018-08-21 17:50:10 +00005953 else
Erik Pilkington5a559e62018-08-21 17:24:06 +00005954 handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A);
Erik Pilkington5a559e62018-08-21 17:24:06 +00005955}
5956
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005957//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005958// Top Level Sema Entry Points
5959//===----------------------------------------------------------------------===//
5960
Richard Smithf8a75c32013-08-29 00:47:48 +00005961/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5962/// the attribute applies to decls. If the attribute is a type attribute, just
5963/// silently ignore it if a GNU attribute.
5964static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005965 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00005966 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00005967 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005968 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005969
Richard Smithf8a75c32013-08-29 00:47:48 +00005970 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5971 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005972 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00005973 return;
5974
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005975 // Unknown attributes are automatically warned on. Target-specific attributes
5976 // which do not apply to the current target architecture are treated as
5977 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00005978 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005979 !AL.existsInTarget(S.Context.getTargetInfo())) {
5980 S.Diag(AL.getLoc(), AL.isDeclspecAttribute()
Erich Keanec480f302018-07-12 21:09:05 +00005981 ? diag::warn_unhandled_ms_attribute_ignored
5982 : diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00005983 << AL;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005984 return;
5985 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005986
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005987 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005988 return;
5989
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005990 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005991 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005992 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00005993 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005994 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00005995 break;
5996 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005997 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
Erich Keane44bacdf2018-08-09 13:21:32 +00005998 << AL << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005999 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006000 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006001 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006002 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006003 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006004 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006005 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006006 case ParsedAttr::AT_DLLExport:
6007 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006008 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006009 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006010 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006011 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006012 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006013 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006014 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006015 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006016 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006017 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006018 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006019 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006020 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006021 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006022 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006023 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006024 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006025 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006026 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006027 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006028 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006029 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006030 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006031 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006032 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006033 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006034 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006035 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006036 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006037 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006038 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006039 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006040 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006041 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006042 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006043 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006044 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006045 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006046 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006047 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006048 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006049 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006050 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006051 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006052 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006053 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006054 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006055 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006056 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006057 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006058 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006059 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006060 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006061 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006062 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006063 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006064 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006065 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006066 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006067 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006068 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006069 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006070 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006071 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006072 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006073 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006074 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006075 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006076 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006077 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006078 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006079 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006080 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006081 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006082 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006083 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006084 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006085 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006086 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006087 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006088 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006089 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006090 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006091 case ParsedAttr::AT_CPUDispatch:
6092 case ParsedAttr::AT_CPUSpecific:
6093 handleCPUSpecificAttr(S, D, AL);
6094 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006095 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006096 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006097 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006098 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006099 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006100 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006101 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006102 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006103 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006104 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006105 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006106 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006107 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006108 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006109 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006110 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006111 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006112 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006113 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006114 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006115 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006116 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006117 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006118 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006119 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006120 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006121 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006122 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006123 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006124 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006125 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006126 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006127 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006128 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006129 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006130 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006131 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006132 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006133 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006134 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006135 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006136 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006137 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006138 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006139 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006140 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006141 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006142 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006143 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006144 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006145 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006146 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006147 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006148 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006149 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006150 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006151 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006152 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006153 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006154 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006155 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006156 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006157 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006158 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006159 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006160 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006161 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006162 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006163 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006164 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006165 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006166 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006167 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006168 case ParsedAttr::AT_LifetimeBound:
6169 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6170 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006171 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006172 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006173 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006174 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006175 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006176 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006177 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006178 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006179 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006180 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006181 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006182 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006183 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006184 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006185 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006186 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006187 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6188 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006189 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006190 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006191 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006192 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006193 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006194 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006195 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006196 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006197 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006198 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006199 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006200 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006201 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006202 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006203 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006204 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006205 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006206 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006207 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006208 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006209 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006210 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006211 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006212 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006213 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006214 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006215 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006216 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006217 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006218 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006219 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006220 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006221 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006222 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006223 handleNoCfCheckAttr(S, D, AL);
6224 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006225 case ParsedAttr::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006226 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006227 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006228 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006229 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006230 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006231 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006232 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006233 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006234 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006235 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006236 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006237 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006238 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006239 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006240 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006241 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006242 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006243 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006244 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006245 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006246 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006247 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006248 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006249 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006250 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006251 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006252 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006253 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006254 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006255 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006256 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006257 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006258 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006259 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006260 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006261 case ParsedAttr::AT_ObjCRuntimeVisible:
6262 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6263 break;
6264 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006265 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006266 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006267 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006268 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6269 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006270 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006271 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006272 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6273 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006274 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006275 case ParsedAttr::AT_CFConsumed:
6276 case ParsedAttr::AT_NSConsumed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006277 handleNSConsumedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006278 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006279 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006280 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006281 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006282 case ParsedAttr::AT_NSReturnsAutoreleased:
6283 case ParsedAttr::AT_NSReturnsNotRetained:
6284 case ParsedAttr::AT_CFReturnsNotRetained:
6285 case ParsedAttr::AT_NSReturnsRetained:
6286 case ParsedAttr::AT_CFReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006287 handleNSReturnsRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006288 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006289 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006290 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006291 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006292 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006293 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006294 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006295 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006296 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006297 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006298 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006299 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006300 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006301 case ParsedAttr::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006302 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006303 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006304 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006305 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006306 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006307 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006308 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006309 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006310 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006311 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006312 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006313 case ParsedAttr::AT_CodeSeg:
6314 handleCodeSegAttr(S, D, AL);
6315 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006316 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006317 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006318 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006319 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006320 handleMinVectorWidthAttr(S, D, AL);
6321 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006322 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006323 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006324 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006325 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006326 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006327 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006328 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006329 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006330 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006331 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006332 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006333 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006334 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006335 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006336 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006337 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006338 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006339 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006340 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006341 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006342 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006343 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006344 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006345 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006346 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006347 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6348 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006349 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006350 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00006351 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
6352 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006353 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006354 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006355 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006356 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006357 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006358 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006359 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006360 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006361 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006362 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006363 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006364 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006365 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006366 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006367 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006368 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006369 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006370 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006371 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006372 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006373 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006374 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006375 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006376 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006377 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006378 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006379 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006380 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006381 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006382 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006383 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006384 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006385 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006386 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006387 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006388 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006389 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006390 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006391 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006392 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006393 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006394 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006395 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006396 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006397 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006398 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006399 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006400 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006401 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006402 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006403 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006404 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006405 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006406 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006407 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006408 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006409 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006410 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006411 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006412 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006413 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006414 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006415 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006416 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006417 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006418 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006419 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006420 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006421 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006422 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006423 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00006424 // Interacts with -fstack-protector options.
6425 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
6426 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006427 case ParsedAttr::AT_StdCall:
6428 case ParsedAttr::AT_CDecl:
6429 case ParsedAttr::AT_FastCall:
6430 case ParsedAttr::AT_ThisCall:
6431 case ParsedAttr::AT_Pascal:
6432 case ParsedAttr::AT_RegCall:
6433 case ParsedAttr::AT_SwiftCall:
6434 case ParsedAttr::AT_VectorCall:
6435 case ParsedAttr::AT_MSABI:
6436 case ParsedAttr::AT_SysVABI:
6437 case ParsedAttr::AT_Pcs:
6438 case ParsedAttr::AT_IntelOclBicc:
6439 case ParsedAttr::AT_PreserveMost:
6440 case ParsedAttr::AT_PreserveAll:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006441 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006442 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006443 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006444 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006445 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006446 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006447 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006448 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006449 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006450 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006451 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006452 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006453 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006454 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006455 case ParsedAttr::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006456 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006457 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006458 case ParsedAttr::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006459 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006460 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006461 case ParsedAttr::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006462 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006463 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006464 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006465 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006466 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006467 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006468 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006469 break;
John McCall8d32c052012-05-22 21:28:12 +00006470
6471 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006472 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006473 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006474 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006475 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006476 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006477 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006478 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006479 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006480 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006481 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006482 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00006483 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006484 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006485 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00006486 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006487 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006488 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00006489 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006490 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006491 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006492 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006493 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006494 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006495 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006496 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006497 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006498 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00006499
Erich Keanee891aa92018-07-13 15:07:47 +00006500 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006501 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006502 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006503
6504 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006505 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006506 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006507 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006508 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006509 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006510 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006511 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006512 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006513 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006514 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006515 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006516 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006517 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006518 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006519 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006520 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006521 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00006522 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006523 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006524 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00006525 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006526 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006527 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00006528 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006529 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006530 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006531 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006532 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006533 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006534 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006535 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006536 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006537 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006538 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006539 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006540 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006541 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006542 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006543 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006544 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006545 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006546 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006547 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006548 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006549 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006550 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006551 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006552 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006553
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006554 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006555 case ParsedAttr::AT_Capability:
6556 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006557 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006558 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006559 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006560 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006561 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006562
Erich Keanee891aa92018-07-13 15:07:47 +00006563 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006564 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006565 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006566 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006567 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006568 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006569 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006570 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006571 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006572 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006573 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006574 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006575
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00006576 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006577 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006578 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006579 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006580 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006581 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006582 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006583 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006584 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006585 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006586 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006587 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006588 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006589 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006590 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00006591 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006592 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006593 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006594 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006595 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006596 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006597 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006598 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006599 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006600 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006601
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006602 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006603 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006604 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006605 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006606 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006607 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006608 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006609 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006610 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006611 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006612 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006613 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00006614 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006615 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006616 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006617 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006618 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006619 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006620 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006621 break;
Martin Bohme4e1293b2018-08-13 14:11:03 +00006622
6623 // Move semantics attribute.
6624 case ParsedAttr::AT_Reinitializes:
6625 handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
6626 break;
Erik Pilkington5a559e62018-08-21 17:24:06 +00006627
6628 case ParsedAttr::AT_AlwaysDestroy:
6629 case ParsedAttr::AT_NoDestroy:
6630 handleDestroyAttr(S, D, AL);
6631 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006632 }
6633}
6634
6635/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
6636/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00006637void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00006638 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00006639 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00006640 if (AttrList.empty())
6641 return;
6642
Erich Keanee891aa92018-07-13 15:07:47 +00006643 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00006644 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00006645
Joey Gouly2cd9db12013-12-13 16:15:28 +00006646 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00006647 // GCC accepts
6648 // static int a9 __attribute__((weakref));
6649 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00006650 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00006651 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
6652 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00006653 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00006654 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006655 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006656
Aaron Ballmanbe243a72014-12-04 22:45:31 +00006657 // FIXME: We should be able to handle this in TableGen as well. It would be
6658 // good to have a way to specify "these attributes must appear as a group",
6659 // for these. Additionally, it would be good to have a way to specify "these
6660 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00006661 if (!D->hasAttr<OpenCLKernelAttr>()) {
6662 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006663 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006664 // FIXME: This emits a different error message than
6665 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00006666 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006667 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006668 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006669 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006670 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006671 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006672 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006673 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006674 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006675 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
6676 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00006677 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
6678 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
6679 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6680 << A << ExpectedKernelFunction;
6681 D->setInvalidDecl();
6682 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
6683 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6684 << A << ExpectedKernelFunction;
6685 D->setInvalidDecl();
6686 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
6687 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6688 << A << ExpectedKernelFunction;
6689 D->setInvalidDecl();
6690 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
6691 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6692 << A << ExpectedKernelFunction;
6693 D->setInvalidDecl();
6694 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006695 }
6696 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006697}
6698
Hiroshi Inoue939d9322017-06-30 05:40:31 +00006699// Helper for delayed processing TransparentUnion attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006700void Sema::ProcessDeclAttributeDelayed(Decl *D,
6701 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006702 for (const ParsedAttr &AL : AttrList)
6703 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00006704 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00006705 break;
6706 }
6707}
6708
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006709// Annotation attributes are the only attributes allowed after an access
6710// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00006711bool Sema::ProcessAccessDeclAttributeList(
6712 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006713 for (const ParsedAttr &AL : AttrList) {
6714 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00006715 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006716 } else {
Erich Keanec480f302018-07-12 21:09:05 +00006717 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006718 return true;
6719 }
6720 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006721 return false;
6722}
6723
John McCall42856de2011-10-01 05:17:03 +00006724/// checkUnusedDeclAttributes - Check a list of attributes to see if it
6725/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00006726static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00006727 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00006728 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006729 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
6730 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00006731 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00006732 continue;
John McCall42856de2011-10-01 05:17:03 +00006733
Erich Keanee891aa92018-07-13 15:07:47 +00006734 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00006735 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
Erich Keane44bacdf2018-08-09 13:21:32 +00006736 << AL << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006737 } else {
Erich Keane44bacdf2018-08-09 13:21:32 +00006738 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
6739 << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006740 }
6741 }
6742}
6743
6744/// checkUnusedDeclAttributes - Given a declarator which is not being
6745/// used to build a declaration, complain about any decl attributes
6746/// which might be lying around on it.
6747void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00006748 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00006749 ::checkUnusedDeclAttributes(*this, D.getAttributes());
6750 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
6751 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
6752}
6753
Ryan Flynn7d470f32009-07-30 03:15:39 +00006754/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00006755/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00006756NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
6757 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00006758 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00006759 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006760 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00006761 FunctionDecl *NewFD;
6762 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00006763 // FIXME: Mangling?
6764 // FIXME: Is the qualifier info correct?
6765 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00006766 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
6767 Loc, Loc, DeclarationName(II),
6768 FD->getType(), FD->getTypeSourceInfo(),
6769 SC_None, false/*isInlineSpecified*/,
6770 FD->hasPrototype(),
6771 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006772 NewD = NewFD;
6773
6774 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00006775 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00006776
6777 // Fake up parameter variables; they are declared as if this were
6778 // a typedef.
6779 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006780 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00006781 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00006782 for (const auto &AI : FT->param_types()) {
6783 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006784 Param->setScopeInfo(0, Params.size());
6785 Params.push_back(Param);
6786 }
David Blaikie9c70e042011-09-21 18:16:56 +00006787 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00006788 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006789 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00006790 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00006791 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00006792 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00006793 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006794 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00006795 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00006796 }
6797 return NewD;
6798}
6799
James Dennett634962f2012-06-14 21:40:34 +00006800/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00006801/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00006802void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00006803 if (W.getUsed()) return; // only do this once
6804 W.setUsed(true);
6805 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
6806 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00006807 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00006808 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
6809 W.getLocation()));
6810 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00006811 WeakTopLevelDecl.push_back(NewD);
6812 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
6813 // to insert Decl at TU scope, sorry.
6814 DeclContext *SavedContext = CurContext;
6815 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00006816 NewD->setDeclContext(CurContext);
6817 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00006818 PushOnScopeChains(NewD, S);
6819 CurContext = SavedContext;
6820 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00006821 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00006822 }
6823}
6824
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006825void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
6826 // It's valid to "forward-declare" #pragma weak, in which case we
6827 // have to do this.
6828 LoadExternalWeakUndeclaredIdentifiers();
6829 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006830 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006831 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006832 if (VD->isExternC())
6833 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006834 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006835 if (FD->isExternC())
6836 ND = FD;
6837 if (ND) {
6838 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00006839 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006840 if (I != WeakUndeclaredIdentifiers.end()) {
6841 WeakInfo W = I->second;
6842 DeclApplyPragmaWeak(S, ND, W);
6843 WeakUndeclaredIdentifiers[Id] = W;
6844 }
6845 }
6846 }
6847 }
6848}
6849
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006850/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
6851/// it, apply them to D. This is a bit tricky because PD can have attributes
6852/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00006853void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006854 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00006855 if (!PD.getDeclSpec().getAttributes().empty())
6856 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00006857
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006858 // Walk the declarator structure, applying decl attributes that were in a type
6859 // position to the decl itself. This handles cases like:
6860 // int *__attr__(x)** D;
6861 // when X is a decl attribute.
6862 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00006863 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
6864 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006865
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006866 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00006867 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00006868
6869 // Apply additional attributes specified by '#pragma clang attribute'.
6870 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006871}
John McCall28a6aea2009-11-04 02:18:39 +00006872
John McCall31168b02011-06-15 23:02:42 +00006873/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00006874/// If so, it'll still be annotated with an attribute that makes it
6875/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006876static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00006877 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00006878 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00006879 // Private ivars are always okay. Unfortunately, people don't
6880 // always properly make their ivars private, even in system headers.
6881 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006882 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
6883 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00006884 return false;
6885
John McCallc6af8c62015-10-28 05:03:19 +00006886 // Silently accept unsupported uses of __weak in both user and system
6887 // declarations when it's been disabled, for ease of integration with
6888 // -fno-objc-arc files. We do have to take some care against attempts
6889 // to define such things; for now, we've only done that for ivars
6890 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006891 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00006892 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
6893 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
6894 reason = UnavailableAttr::IR_ForbiddenWeak;
6895 return true;
6896 }
John McCallb61e14e2015-10-27 04:54:50 +00006897 }
6898
John McCallc6af8c62015-10-28 05:03:19 +00006899 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006900 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00006901 // Currently, all the failures dealt with this way are due to ARC
6902 // restrictions.
6903 reason = UnavailableAttr::IR_ARCForbiddenType;
6904 return true;
John McCallb61e14e2015-10-27 04:54:50 +00006905 }
6906
6907 return false;
John McCall31168b02011-06-15 23:02:42 +00006908}
6909
6910/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006911static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
6912 Decl *D) {
6913 auto Reason = UnavailableAttr::IR_None;
6914 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
6915 assert(Reason && "didn't set reason?");
6916 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00006917 return;
6918 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00006919 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006920 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00006921 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006922 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006923 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006924 DD.getForbiddenTypeDiagnostic() ==
6925 diag::err_arc_array_param_no_ownership) {
6926 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006927 return;
6928 }
6929 }
John McCall31168b02011-06-15 23:02:42 +00006930
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006931 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
6932 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
6933 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00006934}
6935
Manman Ren45b1ab12016-05-06 19:57:16 +00006936static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
6937 const Decl *D) {
6938 // Check each AvailabilityAttr to find the one for this platform.
6939 for (const auto *A : D->attrs()) {
6940 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
6941 // FIXME: this is copied from CheckAvailability. We should try to
6942 // de-duplicate.
6943
6944 // Check if this is an App Extension "platform", and if so chop off
6945 // the suffix for matching with the actual platform.
6946 StringRef ActualPlatform = Avail->getPlatform()->getName();
6947 StringRef RealizedPlatform = ActualPlatform;
6948 if (Context.getLangOpts().AppExt) {
6949 size_t suffix = RealizedPlatform.rfind("_app_extension");
6950 if (suffix != StringRef::npos)
6951 RealizedPlatform = RealizedPlatform.slice(0, suffix);
6952 }
6953
6954 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
6955
6956 // Match the platform name.
6957 if (RealizedPlatform == TargetPlatform)
6958 return Avail;
6959 }
6960 }
6961 return nullptr;
6962}
6963
Erik Pilkington9f866a72017-07-18 20:32:07 +00006964/// The diagnostic we should emit for \c D, and the declaration that
6965/// originated it, or \c AR_Available.
6966///
6967/// \param D The declaration to check.
6968/// \param Message If non-null, this will be populated with the message from
6969/// the availability attribute that is selected.
6970static std::pair<AvailabilityResult, const NamedDecl *>
6971ShouldDiagnoseAvailabilityOfDecl(const NamedDecl *D, std::string *Message) {
6972 AvailabilityResult Result = D->getAvailability(Message);
6973
6974 // For typedefs, if the typedef declaration appears available look
6975 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006976 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006977 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006978 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006979 D = TT->getDecl();
6980 Result = D->getAvailability(Message);
6981 continue;
6982 }
6983 }
6984 break;
6985 }
6986
6987 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006988 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006989 if (IDecl->getDefinition()) {
6990 D = IDecl->getDefinition();
6991 Result = D->getAvailability(Message);
6992 }
6993 }
6994
6995 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
6996 if (Result == AR_Available) {
6997 const DeclContext *DC = ECD->getDeclContext();
6998 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
6999 Result = TheEnumDecl->getAvailability(Message);
7000 D = TheEnumDecl;
7001 }
7002 }
7003
7004 return {Result, D};
7005}
7006
7007
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007008/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007009/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
7010/// in a deprecated context, but not the other way around.
7011static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
7012 VersionTuple DeclVersion,
7013 Decl *Ctx) {
7014 assert(K != AR_Available && "Expected an unavailable declaration here!");
7015
7016 // Checks if we should emit the availability diagnostic in the context of C.
7017 auto CheckContext = [&](const Decl *C) {
7018 if (K == AR_NotYetIntroduced) {
7019 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
7020 if (AA->getIntroduced() >= DeclVersion)
7021 return true;
7022 } else if (K == AR_Deprecated)
7023 if (C->isDeprecated())
7024 return true;
7025
7026 if (C->isUnavailable())
7027 return true;
7028 return false;
7029 };
7030
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007031 do {
7032 if (CheckContext(Ctx))
7033 return false;
7034
7035 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00007036 // Unless it is "+load" method.
7037 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
7038 if (MethodD->isClassMethod() &&
7039 MethodD->getSelector().getAsString() == "load")
7040 return true;
7041
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007042 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007043 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
7044 if (CheckContext(Interface))
7045 return false;
7046 }
7047 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007048 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007049 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
7050 if (CheckContext(Interface))
7051 return false;
7052 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
7053
7054 return true;
7055}
7056
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007057static bool
7058shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
7059 const VersionTuple &DeploymentVersion,
7060 const VersionTuple &DeclVersion) {
7061 const auto &Triple = Context.getTargetInfo().getTriple();
7062 VersionTuple ForceAvailabilityFromVersion;
7063 switch (Triple.getOS()) {
7064 case llvm::Triple::IOS:
7065 case llvm::Triple::TvOS:
7066 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7067 break;
7068 case llvm::Triple::WatchOS:
7069 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7070 break;
7071 case llvm::Triple::Darwin:
7072 case llvm::Triple::MacOSX:
7073 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7074 break;
7075 default:
7076 // New targets should always warn about availability.
7077 return Triple.getVendor() == llvm::Triple::Apple;
7078 }
7079 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7080 DeclVersion >= ForceAvailabilityFromVersion;
7081}
7082
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007083static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7084 for (Decl *Ctx = OrigCtx; Ctx;
7085 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7086 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7087 return cast<NamedDecl>(Ctx);
7088 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7089 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7090 return Imp->getClassInterface();
7091 return CD;
7092 }
7093 }
7094
7095 return dyn_cast<NamedDecl>(OrigCtx);
7096}
7097
Alex Lorenz727c21e2017-07-26 13:58:02 +00007098namespace {
7099
7100struct AttributeInsertion {
7101 StringRef Prefix;
7102 SourceLocation Loc;
7103 StringRef Suffix;
7104
7105 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007106 return {" ", D->getEndLoc(), ""};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007107 }
7108 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7109 return {" ", Loc, ""};
7110 }
7111 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007112 return {"", D->getBeginLoc(), "\n"};
Alex Lorenz727c21e2017-07-26 13:58:02 +00007113 }
7114};
7115
7116} // end anonymous namespace
7117
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007118/// Tries to parse a string as ObjC method name.
7119///
7120/// \param Name The string to parse. Expected to originate from availability
7121/// attribute argument.
7122/// \param SlotNames The vector that will be populated with slot names. In case
7123/// of unsuccessful parsing can contain invalid data.
7124/// \returns A number of method parameters if parsing was successful, None
7125/// otherwise.
7126static Optional<unsigned>
7127tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
7128 const LangOptions &LangOpts) {
7129 // Accept replacements starting with - or + as valid ObjC method names.
7130 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
7131 Name = Name.drop_front(1);
7132 if (Name.empty())
7133 return None;
7134 Name.split(SlotNames, ':');
7135 unsigned NumParams;
7136 if (Name.back() == ':') {
7137 // Remove an empty string at the end that doesn't represent any slot.
7138 SlotNames.pop_back();
7139 NumParams = SlotNames.size();
7140 } else {
7141 if (SlotNames.size() != 1)
7142 // Not a valid method name, just a colon-separated string.
7143 return None;
7144 NumParams = 0;
7145 }
7146 // Verify all slot names are valid.
7147 bool AllowDollar = LangOpts.DollarIdents;
7148 for (StringRef S : SlotNames) {
7149 if (S.empty())
7150 continue;
7151 if (!isValidIdentifier(S, AllowDollar))
7152 return None;
7153 }
7154 return NumParams;
7155}
7156
Alex Lorenz727c21e2017-07-26 13:58:02 +00007157/// Returns a source location in which it's appropriate to insert a new
7158/// attribute for the given declaration \D.
7159static Optional<AttributeInsertion>
7160createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7161 const LangOptions &LangOpts) {
7162 if (isa<ObjCPropertyDecl>(D))
7163 return AttributeInsertion::createInsertionAfter(D);
7164 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7165 if (MD->hasBody())
7166 return None;
7167 return AttributeInsertion::createInsertionAfter(D);
7168 }
7169 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7170 SourceLocation Loc =
7171 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7172 if (Loc.isInvalid())
7173 return None;
7174 // Insert after the 'struct'/whatever keyword.
7175 return AttributeInsertion::createInsertionAfter(Loc);
7176 }
7177 return AttributeInsertion::createInsertionBefore(D);
7178}
7179
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007180/// Actually emit an availability diagnostic for a reference to an unavailable
7181/// decl.
7182///
7183/// \param Ctx The context that the reference occurred in
7184/// \param ReferringDecl The exact declaration that was referenced.
7185/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007186/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007187/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7188/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7189/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007190static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007191 Decl *Ctx, const NamedDecl *ReferringDecl,
7192 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007193 StringRef Message,
7194 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007195 const ObjCInterfaceDecl *UnknownObjCClass,
7196 const ObjCPropertyDecl *ObjCProperty,
7197 bool ObjCPropertyAccess) {
7198 // Diagnostics for deprecated or unavailable.
7199 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007200 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007201 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007202
7203 // Matches 'diag::note_property_attribute' options.
7204 unsigned property_note_select;
7205
7206 // Matches diag::note_availability_specified_here.
7207 unsigned available_here_select_kind;
7208
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007209 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007210 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007211 DeclVersion = AA->getIntroduced();
7212
7213 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx))
7214 return;
7215
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007216 SourceLocation Loc = Locs.front();
7217
Erik Pilkington8b352c42017-08-14 19:49:12 +00007218 // The declaration can have multiple availability attributes, we are looking
7219 // at one of them.
7220 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7221 if (A && A->isInherited()) {
7222 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7223 Redecl = Redecl->getPreviousDecl()) {
7224 const AvailabilityAttr *AForRedecl =
7225 getAttrForPlatform(S.Context, Redecl);
7226 if (AForRedecl && !AForRedecl->isInherited()) {
7227 // If D is a declaration with inherited attributes, the note should
7228 // point to the declaration with actual attributes.
7229 NoteLocation = Redecl->getLocation();
7230 break;
7231 }
7232 }
7233 }
7234
Aaron Ballmanfb237522014-10-15 15:37:51 +00007235 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007236 case AR_NotYetIntroduced: {
7237 // We would like to emit the diagnostic even if -Wunguarded-availability is
7238 // not specified for deployment targets >= to iOS 11 or equivalent or
7239 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7240 // later.
7241 const AvailabilityAttr *AA =
7242 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7243 VersionTuple Introduced = AA->getIntroduced();
7244
7245 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7246 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7247 Introduced);
7248 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7249 : diag::warn_unguarded_availability;
7250
7251 S.Diag(Loc, Warning)
7252 << OffendingDecl
7253 << AvailabilityAttr::getPrettyPlatformName(
7254 S.getASTContext().getTargetInfo().getPlatformName())
7255 << Introduced.getAsString();
7256
7257 S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here)
7258 << OffendingDecl << /* partial */ 3;
7259
7260 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007261 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007262 if (TD->getDeclName().isEmpty()) {
7263 S.Diag(TD->getLocation(),
7264 diag::note_decl_unguarded_availability_silence)
7265 << /*Anonymous*/ 1 << TD->getKindName();
7266 return;
7267 }
7268 auto FixitNoteDiag =
7269 S.Diag(Enclosing->getLocation(),
7270 diag::note_decl_unguarded_availability_silence)
7271 << /*Named*/ 0 << Enclosing;
7272 // Don't offer a fixit for declarations with availability attributes.
7273 if (Enclosing->hasAttr<AvailabilityAttr>())
7274 return;
7275 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7276 return;
7277 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7278 Enclosing, S.getSourceManager(), S.getLangOpts());
7279 if (!Insertion)
7280 return;
7281 std::string PlatformName =
7282 AvailabilityAttr::getPlatformNameSourceSpelling(
7283 S.getASTContext().getTargetInfo().getPlatformName())
7284 .lower();
7285 std::string Introduced =
7286 OffendingDecl->getVersionIntroduced().getAsString();
7287 FixitNoteDiag << FixItHint::CreateInsertion(
7288 Insertion->Loc,
7289 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7290 "(" + Introduced + "))" + Insertion->Suffix)
7291 .str());
7292 }
7293 return;
7294 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007295 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007296 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7297 : diag::warn_property_method_deprecated;
7298 diag_message = diag::warn_deprecated_message;
7299 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7300 property_note_select = /* deprecated */ 0;
7301 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007302 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7303 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007304 break;
7305
Erik Pilkington796a3e22016-08-05 22:59:03 +00007306 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007307 diag = !ObjCPropertyAccess ? diag::err_unavailable
7308 : diag::err_property_method_unavailable;
7309 diag_message = diag::err_unavailable_message;
7310 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7311 property_note_select = /* unavailable */ 1;
7312 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007313
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007314 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7315 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007316 // Most of these failures are due to extra restrictions in ARC;
7317 // reflect that in the primary diagnostic when applicable.
7318 auto flagARCError = [&] {
7319 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007320 S.getSourceManager().isInSystemHeader(
7321 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007322 diag = diag::err_unavailable_in_arc;
7323 };
7324
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007325 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007326 case UnavailableAttr::IR_None: break;
7327
7328 case UnavailableAttr::IR_ARCForbiddenType:
7329 flagARCError();
7330 diag_available_here = diag::note_arc_forbidden_type;
7331 break;
7332
7333 case UnavailableAttr::IR_ForbiddenWeak:
7334 if (S.getLangOpts().ObjCWeakRuntime)
7335 diag_available_here = diag::note_arc_weak_disabled;
7336 else
7337 diag_available_here = diag::note_arc_weak_no_runtime;
7338 break;
7339
7340 case UnavailableAttr::IR_ARCForbiddenConversion:
7341 flagARCError();
7342 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7343 break;
7344
7345 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7346 flagARCError();
7347 diag_available_here = diag::note_arc_init_returns_unrelated;
7348 break;
7349
7350 case UnavailableAttr::IR_ARCFieldWithOwnership:
7351 flagARCError();
7352 diag_available_here = diag::note_arc_field_with_ownership;
7353 break;
7354 }
7355 }
John McCallb61e14e2015-10-27 04:54:50 +00007356 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007357 break;
7358
Erik Pilkington796a3e22016-08-05 22:59:03 +00007359 case AR_Available:
7360 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007361 }
7362
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007363 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007364 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007365 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007366 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7367 Replacement = AL->getReplacement();
7368 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7369 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007370
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007371 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00007372 if (!Replacement.empty())
7373 UseRange =
7374 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007375 if (UseRange.isValid()) {
7376 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
7377 Selector Sel = MethodDecl->getSelector();
7378 SmallVector<StringRef, 12> SelectorSlotNames;
7379 Optional<unsigned> NumParams = tryParseObjCMethodName(
7380 Replacement, SelectorSlotNames, S.getLangOpts());
7381 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
7382 assert(SelectorSlotNames.size() == Locs.size());
7383 for (unsigned I = 0; I < Locs.size(); ++I) {
7384 if (!Sel.getNameForSlot(I).empty()) {
7385 CharSourceRange NameRange = CharSourceRange::getCharRange(
7386 Locs[I], S.getLocForEndOfToken(Locs[I]));
7387 FixIts.push_back(FixItHint::CreateReplacement(
7388 NameRange, SelectorSlotNames[I]));
7389 } else
7390 FixIts.push_back(
7391 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
7392 }
7393 } else
7394 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7395 } else
7396 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7397 }
Manman Renc7890fe2016-03-16 18:50:49 +00007398 }
7399
Aaron Ballmanfb237522014-10-15 15:37:51 +00007400 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007401 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007402 if (ObjCProperty)
7403 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7404 << ObjCProperty->getDeclName() << property_note_select;
7405 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007406 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007407 if (ObjCProperty)
7408 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7409 << ObjCProperty->getDeclName() << property_note_select;
7410 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007411 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007412 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7413 }
7414
Erik Pilkington8b352c42017-08-14 19:49:12 +00007415 S.Diag(NoteLocation, diag_available_here)
7416 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007417}
7418
7419static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
7420 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007421 assert(DD.Kind == DelayedDiagnostic::Availability &&
7422 "Expected an availability diagnostic here");
7423
Aaron Ballmanfb237522014-10-15 15:37:51 +00007424 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00007425 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007426 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007427 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
7428 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
7429 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00007430}
7431
John McCall2ec85372012-05-07 06:16:41 +00007432void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7433 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007434 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007435 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007436
John McCall2ec85372012-05-07 06:16:41 +00007437 // When delaying diagnostics to run in the context of a parsed
7438 // declaration, we only want to actually emit anything if parsing
7439 // succeeds.
7440 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007441
John McCall2ec85372012-05-07 06:16:41 +00007442 // We emit all the active diagnostics in this pool or any of its
7443 // parents. In general, we'll get one pool for the decl spec
7444 // and a child pool for each declarator; in a decl group like:
7445 // deprecated_typedef foo, *bar, baz();
7446 // only the declarator pops will be passed decls. This is correct;
7447 // we really do need to consider delayed diagnostics from the decl spec
7448 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007449 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007450 do {
John McCall6347b682012-05-07 06:16:58 +00007451 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007452 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7453 // This const_cast is a bit lame. Really, Triggered should be mutable.
7454 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007455 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007456 continue;
7457
John McCallc1465822011-02-14 07:13:47 +00007458 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007459 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007460 // Don't bother giving deprecation/unavailable diagnostics if
7461 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007462 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00007463 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00007464 break;
7465
7466 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00007467 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00007468 break;
John McCall31168b02011-06-15 23:02:42 +00007469
7470 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007471 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007472 break;
John McCall86121512010-01-27 03:50:35 +00007473 }
7474 }
John McCall2ec85372012-05-07 06:16:41 +00007475 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007476}
7477
John McCall6347b682012-05-07 06:16:58 +00007478/// Given a set of delayed diagnostics, re-emit them as if they had
7479/// been delayed in the current context instead of in the given pool.
7480/// Essentially, this just moves them to the current pool.
7481void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7482 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7483 assert(curPool && "re-emitting in undelayed context not supported");
7484 curPool->steal(pool);
7485}
7486
Erik Pilkington9f866a72017-07-18 20:32:07 +00007487static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
7488 const NamedDecl *ReferringDecl,
7489 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007490 StringRef Message,
7491 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007492 const ObjCInterfaceDecl *UnknownObjCClass,
7493 const ObjCPropertyDecl *ObjCProperty,
7494 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00007495 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007496 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
7497 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007498 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007499 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007500 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00007501 return;
7502 }
7503
Erik Pilkington9f866a72017-07-18 20:32:07 +00007504 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
7505 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007506 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007507 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00007508}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00007509
Erik Pilkington5cd57172016-08-16 17:44:11 +00007510namespace {
7511
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007512/// Returns true if the given statement can be a body-like child of \p Parent.
7513bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
7514 switch (Parent->getStmtClass()) {
7515 case Stmt::IfStmtClass:
7516 return cast<IfStmt>(Parent)->getThen() == S ||
7517 cast<IfStmt>(Parent)->getElse() == S;
7518 case Stmt::WhileStmtClass:
7519 return cast<WhileStmt>(Parent)->getBody() == S;
7520 case Stmt::DoStmtClass:
7521 return cast<DoStmt>(Parent)->getBody() == S;
7522 case Stmt::ForStmtClass:
7523 return cast<ForStmt>(Parent)->getBody() == S;
7524 case Stmt::CXXForRangeStmtClass:
7525 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
7526 case Stmt::ObjCForCollectionStmtClass:
7527 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
7528 case Stmt::CaseStmtClass:
7529 case Stmt::DefaultStmtClass:
7530 return cast<SwitchCase>(Parent)->getSubStmt() == S;
7531 default:
7532 return false;
7533 }
7534}
7535
7536class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
7537 const Stmt *Target;
7538
7539public:
7540 bool VisitStmt(Stmt *S) { return S != Target; }
7541
7542 /// Returns true if the given statement is present in the given declaration.
7543 static bool isContained(const Stmt *Target, const Decl *D) {
7544 StmtUSEFinder Visitor;
7545 Visitor.Target = Target;
7546 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
7547 }
7548};
7549
7550/// Traverses the AST and finds the last statement that used a given
7551/// declaration.
7552class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
7553 const Decl *D;
7554
7555public:
7556 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7557 if (DRE->getDecl() == D)
7558 return false;
7559 return true;
7560 }
7561
7562 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
7563 const CompoundStmt *Scope) {
7564 LastDeclUSEFinder Visitor;
7565 Visitor.D = D;
7566 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
7567 const Stmt *S = *I;
7568 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
7569 return S;
7570 }
7571 return nullptr;
7572 }
7573};
7574
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007575/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00007576///
7577/// This is done with a traversal of the AST of a function that makes reference
7578/// to a partially available declaration. Whenever we encounter an \c if of the
7579/// form: \c if(@available(...)), we use the version from the condition to visit
7580/// the then statement.
7581class DiagnoseUnguardedAvailability
7582 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
7583 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
7584
7585 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007586 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007587
7588 /// Stack of potentially nested 'if (@available(...))'s.
7589 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007590 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007591
7592 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range);
7593
7594public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007595 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
7596 : SemaRef(SemaRef), Ctx(Ctx) {
7597 AvailabilityStack.push_back(
7598 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007599 }
7600
Alex Lorenz6f911122017-05-16 13:58:53 +00007601 bool TraverseDecl(Decl *D) {
7602 // Avoid visiting nested functions to prevent duplicate warnings.
7603 if (!D || isa<FunctionDecl>(D))
7604 return true;
7605 return Base::TraverseDecl(D);
7606 }
7607
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007608 bool TraverseStmt(Stmt *S) {
7609 if (!S)
7610 return true;
7611 StmtStack.push_back(S);
7612 bool Result = Base::TraverseStmt(S);
7613 StmtStack.pop_back();
7614 return Result;
7615 }
7616
Erik Pilkington5cd57172016-08-16 17:44:11 +00007617 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
7618
7619 bool TraverseIfStmt(IfStmt *If);
7620
Alex Lorenz6f911122017-05-16 13:58:53 +00007621 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
7622
Erik Pilkingtonba87c622017-08-18 20:20:56 +00007623 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
7624 // to any useful diagnostics.
7625 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
7626
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007627 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
7628 if (PRE->isClassReceiver())
7629 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
7630 return true;
7631 }
7632
Erik Pilkington5cd57172016-08-16 17:44:11 +00007633 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
7634 if (ObjCMethodDecl *D = Msg->getMethodDecl())
7635 DiagnoseDeclAvailability(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007636 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00007637 return true;
7638 }
7639
7640 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7641 DiagnoseDeclAvailability(DRE->getDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007642 SourceRange(DRE->getBeginLoc(), DRE->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00007643 return true;
7644 }
7645
7646 bool VisitMemberExpr(MemberExpr *ME) {
7647 DiagnoseDeclAvailability(ME->getMemberDecl(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007648 SourceRange(ME->getBeginLoc(), ME->getEndLoc()));
Erik Pilkington5cd57172016-08-16 17:44:11 +00007649 return true;
7650 }
7651
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007652 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007653 SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use)
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007654 << (!SemaRef.getLangOpts().ObjC1);
7655 return true;
7656 }
7657
Erik Pilkington5cd57172016-08-16 17:44:11 +00007658 bool VisitTypeLoc(TypeLoc Ty);
7659};
7660
7661void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
7662 NamedDecl *D, SourceRange Range) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007663 AvailabilityResult Result;
7664 const NamedDecl *OffendingDecl;
7665 std::tie(Result, OffendingDecl) =
Erik Pilkington9f866a72017-07-18 20:32:07 +00007666 ShouldDiagnoseAvailabilityOfDecl(D, nullptr);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007667 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007668 // All other diagnostic kinds have already been handled in
7669 // DiagnoseAvailabilityOfDecl.
7670 if (Result != AR_NotYetIntroduced)
7671 return;
7672
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007673 const AvailabilityAttr *AA =
7674 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007675 VersionTuple Introduced = AA->getIntroduced();
7676
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007677 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007678 return;
7679
7680 // If the context of this function is less available than D, we should not
7681 // emit a diagnostic.
7682 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx))
7683 return;
7684
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007685 // We would like to emit the diagnostic even if -Wunguarded-availability is
7686 // not specified for deployment targets >= to iOS 11 or equivalent or
7687 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7688 // later.
7689 unsigned DiagKind =
7690 shouldDiagnoseAvailabilityByDefault(
7691 SemaRef.Context,
7692 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
7693 ? diag::warn_unguarded_availability_new
7694 : diag::warn_unguarded_availability;
7695
7696 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington5cd57172016-08-16 17:44:11 +00007697 << Range << D
7698 << AvailabilityAttr::getPrettyPlatformName(
7699 SemaRef.getASTContext().getTargetInfo().getPlatformName())
7700 << Introduced.getAsString();
7701
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007702 SemaRef.Diag(OffendingDecl->getLocation(),
7703 diag::note_availability_specified_here)
7704 << OffendingDecl << /* partial */ 3;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007705
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007706 auto FixitDiag =
7707 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
7708 << Range << D
7709 << (SemaRef.getLangOpts().ObjC1 ? /*@available*/ 0
7710 : /*__builtin_available*/ 1);
7711
7712 // Find the statement which should be enclosed in the if @available check.
7713 if (StmtStack.empty())
7714 return;
7715 const Stmt *StmtOfUse = StmtStack.back();
7716 const CompoundStmt *Scope = nullptr;
7717 for (const Stmt *S : llvm::reverse(StmtStack)) {
7718 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
7719 Scope = CS;
7720 break;
7721 }
7722 if (isBodyLikeChildStmt(StmtOfUse, S)) {
7723 // The declaration won't be seen outside of the statement, so we don't
7724 // have to wrap the uses of any declared variables in if (@available).
7725 // Therefore we can avoid setting Scope here.
7726 break;
7727 }
7728 StmtOfUse = S;
7729 }
7730 const Stmt *LastStmtOfUse = nullptr;
7731 if (isa<DeclStmt>(StmtOfUse) && Scope) {
7732 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
7733 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
7734 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
7735 break;
7736 }
7737 }
7738 }
7739
7740 const SourceManager &SM = SemaRef.getSourceManager();
7741 SourceLocation IfInsertionLoc =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007742 SM.getExpansionLoc(StmtOfUse->getBeginLoc());
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007743 SourceLocation StmtEndLoc =
7744 SM.getExpansionRange(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007745 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc())
Richard Smithb5f81712018-04-30 05:25:48 +00007746 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007747 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
7748 return;
7749
7750 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
7751 const char *ExtraIndentation = " ";
7752 std::string FixItString;
7753 llvm::raw_string_ostream FixItOS(FixItString);
7754 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC1 ? "@available"
7755 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00007756 << "("
7757 << AvailabilityAttr::getPlatformNameSourceSpelling(
7758 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007759 << " " << Introduced.getAsString() << ", *)) {\n"
7760 << Indentation << ExtraIndentation;
7761 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
7762 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
7763 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
7764 /*SkipTrailingWhitespaceAndNewLine=*/false);
7765 if (ElseInsertionLoc.isInvalid())
7766 ElseInsertionLoc =
7767 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
7768 FixItOS.str().clear();
7769 FixItOS << "\n"
7770 << Indentation << "} else {\n"
7771 << Indentation << ExtraIndentation
7772 << "// Fallback on earlier versions\n"
7773 << Indentation << "}";
7774 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007775 }
7776}
7777
7778bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
7779 const Type *TyPtr = Ty.getTypePtr();
7780 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
7781
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007782 if (Range.isInvalid())
7783 return true;
7784
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007785 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007786 TagDecl *TD = TT->getDecl();
7787 DiagnoseDeclAvailability(TD, Range);
7788
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007789 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007790 TypedefNameDecl *D = TD->getDecl();
7791 DiagnoseDeclAvailability(D, Range);
7792
7793 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
7794 if (NamedDecl *D = ObjCO->getInterface())
7795 DiagnoseDeclAvailability(D, Range);
7796 }
7797
7798 return true;
7799}
7800
7801bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
7802 VersionTuple CondVersion;
7803 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
7804 CondVersion = E->getVersion();
7805
7806 // If we're using the '*' case here or if this check is redundant, then we
7807 // use the enclosing version to check both branches.
7808 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00007809 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007810 } else {
7811 // This isn't an availability checking 'if', we can just continue.
7812 return Base::TraverseIfStmt(If);
7813 }
7814
7815 AvailabilityStack.push_back(CondVersion);
7816 bool ShouldContinue = TraverseStmt(If->getThen());
7817 AvailabilityStack.pop_back();
7818
7819 return ShouldContinue && TraverseStmt(If->getElse());
7820}
7821
7822} // end anonymous namespace
7823
7824void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
7825 Stmt *Body = nullptr;
7826
7827 if (auto *FD = D->getAsFunction()) {
7828 // FIXME: We only examine the pattern decl for availability violations now,
7829 // but we should also examine instantiated templates.
7830 if (FD->isTemplateInstantiation())
7831 return;
7832
7833 Body = FD->getBody();
7834 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
7835 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00007836 else if (auto *BD = dyn_cast<BlockDecl>(D))
7837 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00007838
7839 assert(Body && "Need a body here!");
7840
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007841 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007842}
Erik Pilkington9f866a72017-07-18 20:32:07 +00007843
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007844void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
7845 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007846 const ObjCInterfaceDecl *UnknownObjCClass,
7847 bool ObjCPropertyAccess,
7848 bool AvoidPartialAvailabilityChecks) {
7849 std::string Message;
7850 AvailabilityResult Result;
7851 const NamedDecl* OffendingDecl;
7852 // See if this declaration is unavailable, deprecated, or partial.
7853 std::tie(Result, OffendingDecl) = ShouldDiagnoseAvailabilityOfDecl(D, &Message);
7854 if (Result == AR_Available)
7855 return;
7856
7857 if (Result == AR_NotYetIntroduced) {
7858 if (AvoidPartialAvailabilityChecks)
7859 return;
7860
7861 // We need to know the @available context in the current function to
7862 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
7863 // when we're done parsing the current function.
7864 if (getCurFunctionOrMethodDecl()) {
7865 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
7866 return;
7867 } else if (getCurBlock() || getCurLambda()) {
7868 getCurFunction()->HasPotentialAvailabilityViolations = true;
7869 return;
7870 }
7871 }
7872
7873 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007874 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007875 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
7876 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
7877 if (PDeclResult == Result)
7878 ObjCPDecl = PD;
7879 }
7880 }
7881
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007882 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007883 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
7884}